Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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:
119  static PyObject *fModuleBuiltin;
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