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