ROOT  6.06/09
Reference Guide
PyMethodBase.cxx
Go to the documentation of this file.
1 // @(#)root/tmva/pymva $Id$
2 // Authors: Omar Zapata, Lorenzo Moneta, Sergei Gleyzer 2015
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : PyMethodBase *
8  * *
9  * Description: *
10  * Virtual base class for all MVA method based on python *
11  * *
12  **********************************************************************************/
13 
14 #include<TMVA/PyMethodBase.h>
15 #include<TApplication.h>
16 
17 #pragma GCC diagnostic ignored "-Wunused-parameter"
18 #pragma GCC diagnostic ignored "-Wunused-function"
19 #include <Python.h>
20 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
21 #include <numpy/arrayobject.h>
22 
23 #include <fstream>
24 
25 using namespace TMVA;
26 
28 
29 PyObject *PyMethodBase::fModuleBuiltin = NULL;
30 PyObject *PyMethodBase::fEval = NULL;
31 PyObject *PyMethodBase::fOpen = NULL;
32 
33 PyObject *PyMethodBase::fModulePickle = NULL;
34 PyObject *PyMethodBase::fPickleDumps = NULL;
35 PyObject *PyMethodBase::fPickleLoads = NULL;
36 
37 PyObject *PyMethodBase::fMain = NULL;
38 PyObject *PyMethodBase::fGlobalNS = NULL;
39 PyObject *PyMethodBase::fLocalNS = NULL;
40 
41 
42 
43 //_______________________________________________________________________
45  Types::EMVA methodType,
46  const TString &methodTitle,
47  DataSetInfo &dsi,
48  const TString &theOption ,
49  TDirectory *theBaseDir): MethodBase(jobName, methodType, methodTitle, dsi, theOption, theBaseDir),
50  fClassifier(NULL)
51 {
52  if (!PyIsInitialized()) {
53  PyInitialize();
54  }
55 }
56 
57 //_______________________________________________________________________
59  DataSetInfo &dsi,
60  const TString &weightFile,
61  TDirectory *theBaseDir): MethodBase(methodType, dsi, weightFile, theBaseDir),
62  fClassifier(NULL)
63 {
64  if (!PyIsInitialized()) {
65  PyInitialize();
66  }
67 }
68 
69 //_______________________________________________________________________
71 {
72 }
73 
74 //_______________________________________________________________________
76 {
78  PyObject *pycode = Py_BuildValue("(sOO)", code.Data(), fGlobalNS, fLocalNS);
79  PyObject *result = PyObject_CallObject(fEval, pycode);
80  Py_DECREF(pycode);
81  return result;
82 }
83 
84 //_______________________________________________________________________
86 {
88  if (!PyIsInitialized()) {
89  Py_Initialize();
90  _import_array();
91  }
92 
93  fMain = PyImport_AddModule("__main__");
94  if (!fMain) {
95  Log << kFATAL << "Can't import __main__" << Endl;
96  Log << Endl;
97  }
98 
99  fGlobalNS = PyModule_GetDict(fMain);
100  if (!fGlobalNS) {
101  Log << kFATAL << "Can't init global namespace" << Endl;
102  Log << Endl;
103  }
104 
105  fLocalNS = PyDict_New();
106  if (!fMain) {
107  Log << kFATAL << "Can't init local namespace" << Endl;
108  Log << Endl;
109  }
110 
111  #if PY_MAJOR_VERSION < 3
112  //preparing objects for eval
113  PyObject *bName = PyUnicode_FromString("__builtin__");
114  // Import the file as a Python module.
115  fModuleBuiltin = PyImport_Import(bName);
116  if (!fModuleBuiltin) {
117  Log << kFATAL << "Can't import __builtin__" << Endl;
118  Log << Endl;
119  }
120  #else
121  //preparing objects for eval
122  PyObject *bName = PyUnicode_FromString("builtins");
123  // Import the file as a Python module.
124  fModuleBuiltin = PyImport_Import(bName);
125  if (!fModuleBuiltin) {
126  Log << kFATAL << "Can't import builtins" << Endl;
127  Log << Endl;
128  }
129  #endif
130 
131  PyObject *mDict = PyModule_GetDict(fModuleBuiltin);
132  fEval = PyDict_GetItemString(mDict, "eval");
133  fOpen = PyDict_GetItemString(mDict, "open");
134 
135  Py_DECREF(bName);
136  Py_DECREF(mDict);
137  //preparing objects for pickle
138  PyObject *pName = PyUnicode_FromString("pickle");
139  // Import the file as a Python module.
140  fModulePickle = PyImport_Import(pName);
141  if (!fModulePickle) {
142  Log << kFATAL << "Can't import pickle" << Endl;
143  Log << Endl;
144  }
145  PyObject *pDict = PyModule_GetDict(fModulePickle);
146  fPickleDumps = PyDict_GetItemString(pDict, "dump");
147  fPickleLoads = PyDict_GetItemString(pDict, "load");
148 
149  Py_DECREF(pName);
150  Py_DECREF(pDict);
151 
152 
153 }
154 
155 //_______________________________________________________________________
157 {
158  Py_Finalize();
159  if (fEval) Py_DECREF(fEval);
160  if (fModuleBuiltin) Py_DECREF(fModuleBuiltin);
161  if (fPickleDumps) Py_DECREF(fPickleDumps);
162  if (fPickleLoads) Py_DECREF(fPickleLoads);
163  if(fMain) Py_DECREF(fMain);//objects fGlobalNS and fLocalNS will be free here
164 }
166 {
167  #if PY_MAJOR_VERSION < 3
168  Py_SetProgramName(const_cast<char*>(name.Data()));
169  #else
170  Py_SetProgramName((wchar_t *)name.Data());
171  #endif
172 }
173 //_______________________________________________________________________
175 {
176  return Py_GetProgramName();
177 }
178 //_______________________________________________________________________
180 {
181  if (!Py_IsInitialized()) return kFALSE;
182  if (!fEval) return kFALSE;
183  if (!fModuleBuiltin) return kFALSE;
184  if (!fPickleDumps) return kFALSE;
185  if (!fPickleLoads) return kFALSE;
186  return kTRUE;
187 }
188 
190 {
191  if(!PyIsInitialized()) PyInitialize();
192  PyObject *file_arg = Py_BuildValue("(ss)", path.Data(),"wb");
193  PyObject *file = PyObject_CallObject(fOpen,file_arg);
194  PyObject *model_arg = Py_BuildValue("(OO)", obj,file);
195  PyObject *model_data = PyObject_CallObject(fPickleDumps , model_arg);
196 
197  Py_DECREF(file_arg);
198  Py_DECREF(file);
199  Py_DECREF(model_arg);
200  Py_DECREF(model_data);
201 }
202 
204 {
205  PyObject *file_arg = Py_BuildValue("(ss)", path.Data(),"rb");
206  PyObject *file = PyObject_CallObject(fOpen,file_arg);
207 
208  PyObject *model_arg = Py_BuildValue("(O)", file);
209  *obj = PyObject_CallObject(fPickleLoads , model_arg);
210 
211  Py_DECREF(file_arg);
212  Py_DECREF(file);
213  Py_DECREF(model_arg);
214 }
215 
216 
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
static PyObject * fModulePickle
Definition: PyMethodBase.h:125
Basic string class.
Definition: TString.h:137
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:121
const Bool_t kFALSE
Definition: Rtypes.h:92
static void Serialize(TString file, PyObject *classifier)
static int PyIsInitialized()
static void PyInitialize()
const char * Data() const
Definition: TString.h:349
static PyObject * Eval(TString code)
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="", TDirectory *theBaseDir=0)
static PyObject * fEval
Definition: PyMethodBase.h:122
static void PyFinalize()
static TString Py_GetProgramName()
static void PySetProgramName(TString name)
static PyObject * fOpen
Definition: PyMethodBase.h:123
static PyObject * fLocalNS
Definition: PyMethodBase.h:131
#define ClassImp(name)
Definition: Rtypes.h:279
Describe directory structure in memory.
Definition: TDirectory.h:41
MsgLogger & Log() const
Definition: Configurable.h:130
#define name(a, b)
Definition: linkTestLib0.cpp:5
Abstract ClassifierFactory template that handles arbitrary types.
static PyObject * fPickleLoads
Definition: PyMethodBase.h:127
static PyObject * fPickleDumps
Definition: PyMethodBase.h:126
virtual ~PyMethodBase()
#define NULL
Definition: Rtypes.h:82
double result[121]
static PyObject * fMain
Definition: PyMethodBase.h:129
static void UnSerialize(TString file, PyObject **obj)
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
static PyObject * fGlobalNS
Definition: PyMethodBase.h:130
_object PyObject
Definition: TPyArg.h:22