Logo ROOT   6.12/07
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 // needed by NPY_API_VERSION
47 #include "numpy/numpyconfig.h"
48 #if (NPY_API_VERSION >= 0x00000007 )
49 struct tagPyArrayObject;
50 typedef tagPyArrayObject PyArrayObject;
51 #else
52 struct PyArrayObject;
53 #endif
54 
55 namespace TMVA {
56 
57  class Ranking;
58  class PDF;
59  class TSpline1;
60  class MethodCuts;
61  class MethodBoost;
62  class DataSetInfo;
63 
64  class PyMethodBase : public MethodBase {
65 
66  friend class Factory;
67  public:
68 
69  // default constructur
70  PyMethodBase(const TString &jobName,
71  Types::EMVA methodType,
72  const TString &methodTitle,
73  DataSetInfo &dsi,
74  const TString &theOption = "");
75 
76  // constructor used for Testing + Application of the MVA, only (no training),
77  // using given weight file
78  PyMethodBase(Types::EMVA methodType,
79  DataSetInfo &dsi,
80  const TString &weightFile);
81 
82  // default destructur
83  virtual ~PyMethodBase();
84  //basic python related function
85  static void PyInitialize();
86  static int PyIsInitialized();
87  static void PyFinalize();
88  static void PySetProgramName(TString name);
89  static TString Py_GetProgramName();
90 
91  PyObject *Eval(TString code); // required to parse booking options from string to pyobjects
92  static void Serialize(TString file,PyObject *classifier);
93  static Int_t UnSerialize(TString file,PyObject** obj);
94 
95  virtual void Train() = 0;
96  // options treatment
97  virtual void Init() = 0;
98  virtual void DeclareOptions() = 0;
99  virtual void ProcessOptions() = 0;
100  // create ranking
101  virtual const Ranking *CreateRanking() = 0;
102 
103  virtual Double_t GetMvaValue(Double_t *errLower = 0, Double_t *errUpper = 0) = 0;
104 
105  Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) = 0;
106  protected:
107  // the actual "weights"
108  virtual void AddWeightsXMLTo(void *parent) const = 0;
109  virtual void ReadWeightsFromXML(void *wghtnode) = 0;
110  virtual void ReadWeightsFromStream(std::istream &) = 0; // backward compatibility
111  virtual void ReadWeightsFromStream(TFile &) {} // backward compatibility
112 
113  virtual void ReadModelFromFile() = 0;
114 
115  // signal/background classification response for all current set of data
116  virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false) = 0;
117 
118  protected:
119  PyObject *fModule; // Module to load
120  PyObject *fClassifier; // Classifier object
121 
122  PyArrayObject *fTrainData;
123  PyArrayObject *fTrainDataWeights; // array of weights
124  PyArrayObject *fTrainDataClasses; // array with sig/bgk class
125 
126  PyObject *fPyReturn; // python return data
127 
128  protected:
129  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
130 
131  private:
133  static PyObject *fEval; // eval funtion from python
134  static PyObject *fOpen; // open function for files
135 
136  protected:
137  static PyObject *fModulePickle; // Module for model persistence
138  static PyObject *fPickleDumps; // Function to dumps PyObject information into string
139  static PyObject *fPickleLoads; // Function to load PyObject information from string
140 
141  static PyObject *fMain; // module __main__ to get namespace local and global
142  static PyObject *fGlobalNS; // global namesapace
143  PyObject *fLocalNS; // local namesapace
144 
145  ClassDef(PyMethodBase, 0) // Virtual base class for all TMVA method
146 
147  };
148 
149 } // namespace TMVA
150 
151 #endif
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
long long Long64_t
Definition: RtypesCore.h:69
PyObject * fClassifier
Definition: PyMethodBase.h:120
PyObject * fPyReturn
Definition: PyMethodBase.h:126
static PyObject * fModulePickle
Definition: PyMethodBase.h:137
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:46
EAnalysisType
Definition: Types.h:125
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
virtual void Init()=0
Basic string class.
Definition: TString.h:125
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
Ranking for variables in method (implementation)
Definition: Ranking.h:48
int Int_t
Definition: RtypesCore.h:41
virtual void Train()=0
bool Bool_t
Definition: RtypesCore.h:59
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
PyArrayObject * fTrainDataClasses
Definition: PyMethodBase.h:124
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:320
virtual void ReadModelFromFile()=0
virtual void ReadWeightsFromStream(TFile &)
Definition: PyMethodBase.h:111
static PyObject * fEval
Definition: PyMethodBase.h:133
PyObject * Eval(TString code)
Evaluate Python code.
Class that contains all the data information.
Definition: DataSetInfo.h:60
PyArrayObject * fTrainDataWeights
Definition: PyMethodBase.h:123
virtual void ReadWeightsFromStream(std::istream &)=0
PyObject * fModule
Definition: PyMethodBase.h:119
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:42
PyArrayObject * fTrainData
Definition: PyMethodBase.h:122
This is the main MVA steering class.
Definition: Factory.h:81
virtual void AddWeightsXMLTo(void *parent) const =0
static PyObject * fOpen
Definition: PyMethodBase.h:134
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:610
virtual void ReadWeightsFromXML(void *wghtnode)=0
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:132
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:34
virtual Double_t GetMvaValue(Double_t *errLower=0, Double_t *errUpper=0)=0
int type
Definition: TGX11.cxx:120
virtual void ProcessOptions()=0
Abstract ClassifierFactory template that handles arbitrary types.
static PyObject * fPickleLoads
Definition: PyMethodBase.h:139
static PyObject * fPickleDumps
Definition: PyMethodBase.h:138
Definition: file.py:1
virtual ~PyMethodBase()
PyObject * fLocalNS
Definition: PyMethodBase.h:143
A Graph is a graphics 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 object has a header with a name and a title.
Definition: TTree.h:70
static PyObject * fMain
Definition: PyMethodBase.h:141
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:142
_object PyObject
Definition: TPyArg.h:20