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
TMVA::PyMethodBase::UnSerialize
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
Definition: PyMethodBase.cxx:289
TMVA::PyMethodBase::ReadWeightsFromStream
virtual void ReadWeightsFromStream(std::istream &)=0
TMVA::PyMethodBase::Eval
PyObject * Eval(TString code)
Evaluate Python code.
Definition: PyMethodBase.cxx:111
TMVA::PyMethodBase
Definition: PyMethodBase.h:55
PyObject
_object PyObject
Definition: PyMethodBase.h:41
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:110
Long64_t
long long Long64_t
Definition: RtypesCore.h:71
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:78
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
TMVA::PyMethodBase::fPickleLoads
static PyObject * fPickleLoads
Definition: PyMethodBase.h:126
TMVA::PyMethodBase::ReadWeightsFromXML
virtual void ReadWeightsFromXML(void *wghtnode)=0
TSpline
Base class for spline implementation containing the Draw/Paint methods.
Definition: TSpline.h:22
TMVA::PyMethodBase::fModuleBuiltin
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:119
MethodBase.h
TMVA::PyMethodBase::AddWeightsXMLTo
virtual void AddWeightsXMLTo(void *parent) const =0
TMVA::PyMethodBase::fOpen
static PyObject * fOpen
Definition: PyMethodBase.h:121
TMVA::PyMethodBase::fMain
static PyObject * fMain
Definition: PyMethodBase.h:128
TString
Basic string class.
Definition: TString.h:131
TString.h
bool
TMVA::PyMethodBase::ProcessOptions
virtual void ProcessOptions()=0
Py_single_input
#define Py_single_input
Definition: PyMethodBase.h:43
TMVA::PyMethodBase::fPickleDumps
static PyObject * fPickleDumps
Definition: PyMethodBase.h:125
TMVA::PyMethodBase::fLocalNS
PyObject * fLocalNS
Definition: PyMethodBase.h:130
TMVA::PyMethodBase::DeclareOptions
virtual void DeclareOptions()=0
TMVA::DataSetInfo
Class that contains all the data information.
Definition: DataSetInfo.h:60
TMVA::PyMethodBase::fClassifier
PyObject * fClassifier
Definition: PyMethodBase.h:111
TMVA::PyMethodBase::fPyReturn
PyObject * fPyReturn
Definition: PyMethodBase.h:113
TMVA::Types::EAnalysisType
EAnalysisType
Definition: Types.h:127
TMVA::PyMethodBase::~PyMethodBase
virtual ~PyMethodBase()
Definition: PyMethodBase.cxx:98
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:124
TMVA::Factory
This is the main MVA steering class.
Definition: Factory.h:81
TMVA::PyMethodBase::PyMethodBase
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
Definition: PyMethodBase.cxx:62
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:320
TMVA::PyMethodBase::fGlobalNS
static PyObject * fGlobalNS
Definition: PyMethodBase.h:129
TFile
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
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:57
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:571
TMVA::PyMethodBase::ReadWeightsFromStream
virtual void ReadWeightsFromStream(TFile &)
Definition: PyMethodBase.h:102
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:322
TMVA::PyMethodBase::fEval
static PyObject * fEval
Definition: PyMethodBase.h:120
name
char name[80]
Definition: TGX11.cxx:109
TMVA::PyMethodBase::PyFinalize
static void PyFinalize()
Definition: PyMethodBase.cxx:199
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:237
TMVA::PyMethodBase::Serialize
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
Definition: PyMethodBase.cxx:267
TMVA::PyMethodBase::PyIsInitialized
static int PyIsInitialized()
Check Python interpreter initialization status.
Definition: PyMethodBase.cxx:248
TMVA::PyMethodBase::Init
virtual void Init()=0
type
int type
Definition: TGX11.cxx:120
TMVA::PyMethodBase::PySetProgramName
static void PySetProgramName(TString name)
Set program name for Python interpeter.
Definition: PyMethodBase.cxx:214
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:128
TMVA
create variable transformations
Definition: GeneticMinimizer.h:21
int