Logo ROOT  
Reference Guide
PyMethodBase.h
Go to the documentation of this file.
1 // @(#)root/tmva/pymva $Id$
2 // Authors: Omar Zapata, Lorenzo Moneta, Sergei Gleyzer 2015, Stefan Wunsch 2017
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : PyMethodBase *
8  * Web : http://oproject.org *
9  * *
10  * Description: *
11  * Virtual base class for all MVA method based on Python *
12  * *
13  **********************************************************************************/
14 
15 #ifndef ROOT_TMVA_PyMethodBase
16 #define ROOT_TMVA_PyMethodBase
17 
18 ////////////////////////////////////////////////////////////////////////////////
19 // //
20 // PyMethodBase //
21 // //
22 // Virtual base class for all TMVA method based on Python //
23 // //
24 ////////////////////////////////////////////////////////////////////////////////
25 
26 #include "TMVA/MethodBase.h"
27 #include "TMVA/Types.h"
28 
29 #include "Rtypes.h"
30 #include "TString.h"
31 
32 class TFile;
33 class TGraph;
34 class TTree;
35 class TDirectory;
36 class TSpline;
37 class TH1F;
38 class TH1D;
39 
40 #ifndef PyObject_HEAD
41 struct _object;
42 typedef _object PyObject;
43 #define Py_single_input 256
44 #endif
45 
46 namespace TMVA {
47 
48  class Ranking;
49  class PDF;
50  class TSpline1;
51  class MethodCuts;
52  class MethodBoost;
53  class DataSetInfo;
54 
55  class PyMethodBase : public MethodBase {
56 
57  friend class Factory;
58  public:
59 
60  // default constructur
61  PyMethodBase(const TString &jobName,
62  Types::EMVA methodType,
63  const TString &methodTitle,
64  DataSetInfo &dsi,
65  const TString &theOption = "");
66 
67  // constructor used for Testing + Application of the MVA, only (no training),
68  // using given weight file
69  PyMethodBase(Types::EMVA methodType,
70  DataSetInfo &dsi,
71  const TString &weightFile);
72 
73  // default destructur
74  virtual ~PyMethodBase();
75  //basic python related function
76  static void PyInitialize();
77  static int PyIsInitialized();
78  static void PyFinalize();
79  static void PySetProgramName(TString name);
80  static TString Py_GetProgramName();
81 
82  PyObject *Eval(TString code); // required to parse booking options from string to pyobjects
83  static void Serialize(TString file,PyObject *classifier);
84  static Int_t UnSerialize(TString file,PyObject** obj);
85 
86  virtual void Train() = 0;
87  // options treatment
88  virtual void Init() = 0;
89  virtual void DeclareOptions() = 0;
90  virtual void ProcessOptions() = 0;
91  // create ranking
92  virtual const Ranking *CreateRanking() = 0;
93 
94  virtual Double_t GetMvaValue(Double_t *errLower = 0, Double_t *errUpper = 0) = 0;
95 
96  Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) = 0;
97  protected:
98  // the actual "weights"
99  virtual void AddWeightsXMLTo(void *parent) const = 0;
100  virtual void ReadWeightsFromXML(void *wghtnode) = 0;
101  virtual void ReadWeightsFromStream(std::istream &) = 0; // backward compatibility
102  virtual void ReadWeightsFromStream(TFile &) {} // backward compatibility
103 
104  virtual void ReadModelFromFile() = 0;
105 
106  // signal/background classification response for all current set of data
107  virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false) = 0;
108 
109  protected:
110  PyObject *fModule; // Module to load
111  PyObject *fClassifier; // Classifier object
112 
113  PyObject *fPyReturn; // python return data
114 
115  protected:
116  void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input); // runs python code from string in local namespace with error handling
117 
118  private:
120  static PyObject *fEval; // eval funtion from python
121  static PyObject *fOpen; // open function for files
122 
123  protected:
124  static PyObject *fModulePickle; // Module for model persistence
125  static PyObject *fPickleDumps; // Function to dumps PyObject information into string
126  static PyObject *fPickleLoads; // Function to load PyObject information from string
127 
128  static PyObject *fMain; // module __main__ to get namespace local and global
129  static PyObject *fGlobalNS; // global namesapace
130  PyObject *fLocalNS; // local namesapace
131 
132  ClassDef(PyMethodBase, 0) // Virtual base class for all TMVA method
133 
134  };
135 
136 } // namespace TMVA
137 
138 #endif
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
long long Long64_t
Definition: RtypesCore.h:71
PyObject * fClassifier
Definition: PyMethodBase.h:111
PyObject * fPyReturn
Definition: PyMethodBase.h:113
static PyObject * fModulePickle
Definition: PyMethodBase.h:124
virtual void DeclareOptions()=0
Base class for spline implementation containing the Draw/Paint methods.
Definition: TSpline.h:20
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:53
EAnalysisType
Definition: Types.h:127
Virtual base Class for all MVA method.
Definition: MethodBase.h:111
virtual void Init()=0
Basic string class.
Definition: TString.h:131
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
Ranking for variables in method (implementation)
Definition: Ranking.h:48
virtual void Train()=0
bool Bool_t
Definition: RtypesCore.h:61
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
static int PyIsInitialized()
Check Python interpreter initialization status.
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)=0
static void PyInitialize()
Initialize Python interpreter.
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
#define ClassDef(name, id)
Definition: Rtypes.h:322
virtual void ReadModelFromFile()=0
virtual void ReadWeightsFromStream(TFile &)
Definition: PyMethodBase.h:102
static PyObject * fEval
Definition: PyMethodBase.h:120
PyObject * Eval(TString code)
Evaluate Python code.
Class that contains all the data information.
Definition: DataSetInfo.h:60
virtual void ReadWeightsFromStream(std::istream &)=0
PyObject * fModule
Definition: PyMethodBase.h:110
static void PyFinalize()
static TString Py_GetProgramName()
Get program name from Python interpreter.
_object PyObject
Definition: PyMethodBase.h:41
virtual const Ranking * CreateRanking()=0
static void PySetProgramName(TString name)
Set program name for Python interpeter.
unsigned int UInt_t
Definition: RtypesCore.h:44
This is the main MVA steering class.
Definition: Factory.h:81
virtual void AddWeightsXMLTo(void *parent) const =0
static PyObject * fOpen
Definition: PyMethodBase.h:121
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
virtual void ReadWeightsFromXML(void *wghtnode)=0
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:119
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
double Double_t
Definition: RtypesCore.h:57
Describe directory structure in memory.
Definition: TDirectory.h:40
virtual Double_t GetMvaValue(Double_t *errLower=0, Double_t *errUpper=0)=0
int type
Definition: TGX11.cxx:120
virtual void ProcessOptions()=0
create variable transformations
static PyObject * fPickleLoads
Definition: PyMethodBase.h:126
static PyObject * fPickleDumps
Definition: PyMethodBase.h:125
Definition: file.py:1
virtual ~PyMethodBase()
PyObject * fLocalNS
Definition: PyMethodBase.h:130
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
#define Py_single_input
Definition: PyMethodBase.h:43
A TTree represents a columnar dataset.
Definition: TTree.h:78
static PyObject * fMain
Definition: PyMethodBase.h:128
virtual std::vector< Double_t > GetMvaValues(Long64_t firstEvt=0, Long64_t lastEvt=-1, Bool_t logProgress=false)=0
get all the MVA values for the events of the current Data type
char name[80]
Definition: TGX11.cxx:109
static PyObject * fGlobalNS
Definition: PyMethodBase.h:129