ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
24 using namespace TMVA;
25 
27 
28 PyObject *PyMethodBase::fModuleBuiltin = NULL;
29 PyObject *PyMethodBase::fEval = NULL;
30 PyObject *PyMethodBase::fModulePickle = NULL;
31 PyObject *PyMethodBase::fPickleDumps = NULL;
32 PyObject *PyMethodBase::fPickleLoads = NULL;
33 
34 //_______________________________________________________________________
36  Types::EMVA methodType,
37  const TString &methodTitle,
38  DataSetInfo &dsi,
39  const TString &theOption ,
40  TDirectory *theBaseDir): MethodBase(jobName, methodType, methodTitle, dsi, theOption, theBaseDir),
41  fClassifier(NULL)
42 {
43  if (!PyIsInitialized()) {
44  PyInitialize();
45  }
46 }
47 
48 //_______________________________________________________________________
50  DataSetInfo &dsi,
51  const TString &weightFile,
52  TDirectory *theBaseDir): MethodBase(methodType, dsi, weightFile, theBaseDir),
53  fClassifier(NULL)
54 {
55  if (!PyIsInitialized()) {
56  PyInitialize();
57  }
58 }
59 
60 //_______________________________________________________________________
62 {
63  if (PyIsInitialized()) {
64 // PyFinalize();
65  }
66 }
67 
68 //_______________________________________________________________________
70 {
71  PyObject *main = PyImport_AddModule("__main__");
72  PyObject *global = PyModule_GetDict(main);
73  PyObject *local = PyDict_New();
74 
75  PyObject *pycode = Py_BuildValue("(sOO)", code.Data(), global, local);
76  PyObject *result = PyObject_CallObject(fEval, pycode);
77 // Py_DECREF(main);
78 // Py_DECREF(global);
79 // Py_DECREF(local);
80  Py_DECREF(pycode);
81  return result;
82 }
83 
84 //_______________________________________________________________________
86 {
88  if (!PyIsInitialized()) {
89  Py_Initialize();
90  _import_array();
91  import_array();
92  }
93  //preparing objects for eval
94  PyObject *bName = PyString_FromString("__builtin__");
95  // Import the file as a Python module.
96  fModuleBuiltin = PyImport_Import(bName);
97  if (!fModuleBuiltin) {
98  Log << kFATAL << "Can't import __builtin__" << Endl;
99  Log << Endl;
100  }
101  PyObject *mDict = PyModule_GetDict(fModuleBuiltin);
102  fEval = PyDict_GetItemString(mDict, "eval");
103 
104  Py_DECREF(bName);
105  Py_DECREF(mDict);
106  //preparing objects for pickle
107  PyObject *pName = PyString_FromString("pickle");
108  // Import the file as a Python module.
109  fModulePickle = PyImport_Import(pName);
110  if (!fModulePickle) {
111  Log << kFATAL << "Can't import pickle" << Endl;
112  Log << Endl;
113  }
114  PyObject *pDict = PyModule_GetDict(fModulePickle);
115  fPickleDumps = PyDict_GetItemString(pDict, "dumps");
116  fPickleLoads = PyDict_GetItemString(pDict, "loads");
117 
118  Py_DECREF(pName);
119  Py_DECREF(pDict);
120 
121 
122 }
123 
124 //_______________________________________________________________________
126 {
127  Py_Finalize();
128  if (fEval) delete fEval;
129  if (fModuleBuiltin) delete fModuleBuiltin;
130  if (fPickleDumps) delete fPickleDumps;
131  if (fPickleLoads) delete fPickleLoads;
132 
133 }
135 {
136  Py_SetProgramName(const_cast<char *>(name.Data()));
137 }
138 //_______________________________________________________________________
140 {
141  return Py_GetProgramName();
142 }
143 //_______________________________________________________________________
145 {
146  if (!Py_IsInitialized()) return kFALSE;
147  if (!fEval) return kFALSE;
148  if (!fModuleBuiltin) return kFALSE;
149  if (!fPickleDumps) return kFALSE;
150  if (!fPickleLoads) return kFALSE;
151  return kTRUE;
152 }
153 
154 
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
static PyObject * fModulePickle
Definition: PyMethodBase.h:122
Basic string class.
Definition: TString.h:137
static PyObject * fModuleBuiltin
Definition: PyMethodBase.h:119
const Bool_t kFALSE
Definition: Rtypes.h:92
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:120
static void PyFinalize()
static TString Py_GetProgramName()
tuple main
Definition: hsum.py:20
static void PySetProgramName(TString name)
#define ClassImp(name)
Definition: Rtypes.h:279
Describe directory structure in memory.
Definition: TDirectory.h:44
MsgLogger & Log() const
Definition: Configurable.h:130
#define name(a, b)
Definition: linkTestLib0.cpp:5
static PyObject * fPickleLoads
Definition: PyMethodBase.h:124
static PyObject * fPickleDumps
Definition: PyMethodBase.h:123
virtual ~PyMethodBase()
#define NULL
Definition: Rtypes.h:82
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
_object PyObject
Definition: TPyArg.h:22