Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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#include "TMVA/MethodBase.h"
20#include "TMVA/Types.h"
21
22#include "Rtypes.h"
23#include "TString.h"
24#include <vector>
25
26class TFile;
27class TGraph;
28class TTree;
29class TDirectory;
30class TSpline;
31class TH1F;
32class TH1D;
33
34#ifndef PyObject_HEAD
35struct _object;
36typedef _object PyObject;
37#define Py_single_input 256
38#endif
39
40namespace TMVA {
41
42 class Ranking;
43 class PDF;
44 class TSpline1;
45 class MethodCuts;
46 class MethodBoost;
47 class DataSetInfo;
48
49 /// Function to find current Python executable
50 /// used by ROOT
51 /// If "Python3" is installed, return "python3"
53
54 /// Virtual base class for all TMVA method based on Python
55 class PyMethodBase : public MethodBase {
56
57 friend class Factory;
58 public:
59
60 // default constructur
63 const TString &methodTitle,
65 const TString &theOption = "");
66
67 // constructor used for Testing + Application of the MVA, only (no training),
68 // using given weight file
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
80 PyObject *Eval(TString code); // required to parse booking options from string to pyobjects
81 static void Serialize(TString file,PyObject *classifier);
82 static Int_t UnSerialize(TString file,PyObject** obj);
83
84 virtual void Train() = 0;
85 // options treatment
86 virtual void Init() = 0;
87 virtual void DeclareOptions() = 0;
88 virtual void ProcessOptions() = 0;
89 // create ranking
90 virtual const Ranking *CreateRanking() = 0;
91
92 virtual Double_t GetMvaValue(Double_t *errLower = nullptr, Double_t *errUpper = nullptr) = 0;
93
95 protected:
96 // the actual "weights"
97 virtual void AddWeightsXMLTo(void *parent) const = 0;
98 virtual void ReadWeightsFromXML(void *wghtnode) = 0;
99 virtual void ReadWeightsFromStream(std::istream &) = 0; // backward compatibility
100 virtual void ReadWeightsFromStream(TFile &) {} // backward compatibility
101
102 virtual void ReadModelFromFile() = 0;
103
104 // signal/background classification response for all current set of data
105 virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false) = 0;
106
107 protected:
108 PyObject *fModule; // Module to load
109 PyObject *fClassifier; // Classifier object
110
111 PyObject *fPyReturn; // python return data
112
113 protected:
114 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
115
116 private:
118 static PyObject *fEval; // eval funtion from python
119 static PyObject *fOpen; // open function for files
120
121 protected:
122 static PyObject *fModulePickle; // Module for model persistence
123 static PyObject *fPickleDumps; // Function to dumps PyObject information into string
124 static PyObject *fPickleLoads; // Function to load PyObject information from string
125
126 static PyObject *fMain; // module __main__ to get namespace local and global
127 static PyObject *fGlobalNS; // global namesapace
128 PyObject *fLocalNS; // local namesapace
129
130 public:
131 static void PyRunString(TString code, PyObject *globalNS, PyObject* localNS); // Overloaded static Python utlity function for running Python code
132 static const char* PyStringAsString(PyObject *string); // Python Utility function for converting a Python String object to const char*
133 static std::vector<size_t> GetDataFromTuple(PyObject *tupleObject); // Function casts Python Tuple object into vector of size_t
134 static std::vector<size_t> GetDataFromList(PyObject *listObject); // Function casts Python List object into vector of size_t
135 static PyObject* GetValueFromDict(PyObject* dict, const char* key); // Function to check for a key in dict and return the associated value if present
136 ClassDef(PyMethodBase, 0) // Virtual base class for all TMVA method
137
138 };
139
140} // namespace TMVA
141
142#endif
_object PyObject
#define Py_single_input
long long Long64_t
Definition RtypesCore.h:69
#define ClassDef(name, id)
Definition Rtypes.h:342
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Describe directory structure in memory.
Definition TDirectory.h:45
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
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:698
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:650
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 base class for all TMVA method based on Python.
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 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()
virtual Double_t GetMvaValue(Double_t *errLower=nullptr, Double_t *errUpper=nullptr)=0
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.
static PyObject * fPickleLoads
virtual void ReadWeightsFromStream(TFile &)
virtual void ProcessOptions()=0
static PyObject * fGlobalNS
static PyObject * fModulePickle
static PyObject * fModuleBuiltin
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
static PyObject * fEval
static PyObject * GetValueFromDict(PyObject *dict, const char *key)
Utility function which checks if a given key is present in a Python dictionary object and returns the...
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=256)
Execute Python code from string.
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:139
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 "Python3" is installed,...