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