Logo ROOT   6.08/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
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/scikit-learn //
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 
56 namespace TMVA {
57 
58  class Ranking;
59  class PDF;
60  class TSpline1;
61  class MethodCuts;
62  class MethodBoost;
63  class DataSetInfo;
64 
65  class PyMethodBase : public MethodBase {
66 
67  friend class Factory;
68  public:
69 
70  // default constructur
71  PyMethodBase(const TString &jobName,
72  Types::EMVA methodType,
73  const TString &methodTitle,
74  DataSetInfo &dsi,
75  const TString &theOption = "");
76 
77  // constructor used for Testing + Application of the MVA, only (no training),
78  // using given weight file
79  PyMethodBase(Types::EMVA methodType,
80  DataSetInfo &dsi,
81  const TString &weightFile);
82 
83  // default destructur
84  virtual ~PyMethodBase();
85  //basic python related function
86  static void PyInitialize();
87  static int PyIsInitialized();
88  static void PyFinalize();
89  static void PySetProgramName(TString name);
90  static TString Py_GetProgramName();
91 
92  static PyObject *Eval(TString code); // required to parse booking options from string to pyobjects
93  static void Serialize(TString file,PyObject *classifier);
94  static void UnSerialize(TString file,PyObject** obj);
95 
96  virtual void Train() = 0;
97  // options treatment
98  virtual void Init() = 0;
99  virtual void DeclareOptions() = 0;
100  virtual void ProcessOptions() = 0;
101  // create ranking
102  virtual const Ranking *CreateRanking() = 0;
103 
104  virtual Double_t GetMvaValue(Double_t *errLower = 0, Double_t *errUpper = 0) = 0;
105 
106  Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) = 0;
107  protected:
108  // the actual "weights"
109  virtual void AddWeightsXMLTo(void *parent) const = 0;
110  virtual void ReadWeightsFromXML(void *wghtnode) = 0;
111  virtual void ReadWeightsFromStream(std::istream &) = 0; // backward compatibility
112  virtual void ReadWeightsFromStream(TFile &) {} // backward compatibility
113 
114  virtual void ReadModelFromFile() = 0;
115 
116  // signal/background classification response for all current set of data
117  virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false);
118 
119  protected:
120  PyObject *fModule; // Module to load
121  PyObject *fClassifier; // Classifier object
122 
123  PyArrayObject *fTrainData;
124  PyArrayObject *fTrainDataWeights; // array of weights
125  PyArrayObject *fTrainDataClasses; // array with sig/bgk class
126 
127  PyObject *fPyReturn; // python return data
128 
129  protected:
130  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
131 
132  private:
134  static PyObject *fEval; // eval funtion from python
135  static PyObject *fOpen; // open function for files
136 
137  protected:
138  static PyObject *fModulePickle; // Module for model persistence
139  static PyObject *fPickleDumps; // Function to dumps PyObject information into string
140  static PyObject *fPickleLoads; // Function to load PyObject information from string
141 
142  static PyObject *fMain; // module __main__ to get namesapace local and global
143  static PyObject *fGlobalNS; // global namesapace
144  static PyObject *fLocalNS; // local namesapace
145 
146  ClassDef(PyMethodBase, 0) // Virtual base class for all TMVA method
147 
148  };
149 } // namespace TMVA
150 
151 #endif
152 
153 
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:121
PyObject * fPyReturn
Definition: PyMethodBase.h:127
static PyObject * fModulePickle
Definition: PyMethodBase.h:138
virtual void DeclareOptions()=0
Base class for spline implementation containing the Draw/Paint methods //.
Definition: TSpline.h:22
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
EAnalysisType
Definition: Types.h:129
virtual void Init()=0
Basic string class.
Definition: TString.h:137
tomato 1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:575
virtual void Train()=0
bool Bool_t
Definition: RtypesCore.h:59
static void Serialize(TString file, PyObject *classifier)
PyArrayObject * fTrainDataClasses
Definition: PyMethodBase.h:125
static int PyIsInitialized()
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)=0
static void PyInitialize()
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void ReadModelFromFile()=0
virtual void ReadWeightsFromStream(TFile &)
Definition: PyMethodBase.h:112
static PyObject * fEval
Definition: PyMethodBase.h:134
static PyObject * Eval(TString code)
PyArrayObject * fTrainDataWeights
Definition: PyMethodBase.h:124
virtual std::vector< Double_t > GetMvaValues(Long64_t firstEvt=0, Long64_t lastEvt=-1, Bool_t logProgress=false)
get all the MVA values for the events of the current Data type
virtual void ReadWeightsFromStream(std::istream &)=0
PyObject * fModule
Definition: PyMethodBase.h:120
static void PyFinalize()
static TString Py_GetProgramName()
_object PyObject
Definition: PyMethodBase.h:41
virtual const Ranking * CreateRanking()=0
static void PySetProgramName(TString name)
unsigned int UInt_t
Definition: RtypesCore.h:42
PyArrayObject * fTrainData
Definition: PyMethodBase.h:123
virtual void AddWeightsXMLTo(void *parent) const =0
static PyObject * fOpen
Definition: PyMethodBase.h:135
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:618
virtual void ReadWeightsFromXML(void *wghtnode)=0
static PyObject * fLocalNS
Definition: PyMethodBase.h:144
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:133
double Double_t
Definition: RtypesCore.h:55
Describe directory structure in memory.
Definition: TDirectory.h:44
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:140
static PyObject * fPickleDumps
Definition: PyMethodBase.h:139
Definition: file.py:1
virtual ~PyMethodBase()
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
#define Py_single_input
Definition: PyMethodBase.h:43
A TTree object has a header with a name and a title.
Definition: TTree.h:98
static PyObject * fMain
Definition: PyMethodBase.h:142
static void UnSerialize(TString file, PyObject **obj)
char name[80]
Definition: TGX11.cxx:109
static PyObject * fGlobalNS
Definition: PyMethodBase.h:143
_object PyObject
Definition: TPyArg.h:22