Logo ROOT   6.16/01
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
32class TFile;
33class TGraph;
34class TTree;
35class TDirectory;
36class TSpline;
37class TH1F;
38class TH1D;
39
40#ifndef PyObject_HEAD
41struct _object;
42typedef _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 )
49struct tagPyArrayObject;
50typedef tagPyArrayObject PyArrayObject;
51#else
52struct PyArrayObject;
53#endif
54
55namespace 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);
90
91 PyObject *Eval(TString code); // required to parse booking options from string to pyobjects
92 static void Serialize(TString file,PyObject *classifier);
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
_object PyObject
Definition: PyMethodBase.h:41
#define Py_single_input
Definition: PyMethodBase.h:43
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
long long Long64_t
Definition: RtypesCore.h:69
#define ClassDef(name, id)
Definition: Rtypes.h:324
int type
Definition: TGX11.cxx:120
_object PyObject
Definition: TPyArg.h:20
Describe directory structure in memory.
Definition: TDirectory.h:34
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
Class that contains all the data information.
Definition: DataSetInfo.h:60
This is the main MVA steering class.
Definition: Factory.h:81
Virtual base Class for all MVA method.
Definition: MethodBase.h:109
virtual void Train()=0
PyObject * fPyReturn
Definition: PyMethodBase.h:126
virtual void AddWeightsXMLTo(void *parent) const =0
static int PyIsInitialized()
Check Python interpreter initialization status.
static PyObject * fOpen
Definition: PyMethodBase.h:134
virtual void DeclareOptions()=0
virtual void ReadWeightsFromStream(std::istream &)=0
PyArrayObject * fTrainData
Definition: PyMethodBase.h:122
virtual const Ranking * CreateRanking()=0
virtual void ReadModelFromFile()=0
static PyObject * fPickleDumps
Definition: PyMethodBase.h:138
virtual void Init()=0
PyObject * Eval(TString code)
Evaluate Python code.
PyObject * fModule
Definition: PyMethodBase.h:119
static TString Py_GetProgramName()
Get program name from Python interpreter.
static PyObject * fMain
Definition: PyMethodBase.h:141
virtual void ReadWeightsFromXML(void *wghtnode)=0
static void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
PyArrayObject * fTrainDataWeights
Definition: PyMethodBase.h:123
static void PyFinalize()
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets)=0
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
PyObject * fClassifier
Definition: PyMethodBase.h:120
virtual Double_t GetMvaValue(Double_t *errLower=0, Double_t *errUpper=0)=0
static PyObject * fPickleLoads
Definition: PyMethodBase.h:139
static void PySetProgramName(TString name)
Set program name for Python interpeter.
virtual ~PyMethodBase()
PyArrayObject * fTrainDataClasses
Definition: PyMethodBase.h:124
virtual void ReadWeightsFromStream(TFile &)
Definition: PyMethodBase.h:111
virtual void ProcessOptions()=0
static PyObject * fGlobalNS
Definition: PyMethodBase.h:142
static PyObject * fModulePickle
Definition: PyMethodBase.h:137
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:132
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
static PyObject * fEval
Definition: PyMethodBase.h:133
PyObject * fLocalNS
Definition: PyMethodBase.h:143
Ranking for variables in method (implementation)
Definition: Ranking.h:48
EAnalysisType
Definition: Types.h:127
Base class for spline implementation containing the Draw/Paint methods.
Definition: TSpline.h:22
Basic string class.
Definition: TString.h:131
A TTree object has a header with a name and a title.
Definition: TTree.h:71
Abstract ClassifierFactory template that handles arbitrary types.
Definition: file.py:1