25 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 26 #include <numpy/arrayobject.h> 49 PyGILState_STATE m_GILState;
51 PyGILRAII():m_GILState(PyGILState_Ensure()){}
52 ~PyGILRAII(){PyGILState_Release(m_GILState);}
59 const TString &methodTitle,
61 const TString &theOption ):
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
71 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
79 const TString &weightFile):
MethodBase(methodType, dsi, weightFile),
89 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
130 if (!pyIsInitialized) {
134 PyGILRAII thePyGILRAII;
136 if (!pyIsInitialized) {
140 fMain = PyImport_AddModule(
"__main__");
142 Log << kFATAL <<
"Can't import __main__" <<
Endl;
148 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
152 #if PY_MAJOR_VERSION < 3 154 PyObject *bName = PyUnicode_FromString(
"__builtin__");
158 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
163 PyObject *bName = PyUnicode_FromString(
"builtins");
167 Log << kFATAL <<
"Can't import builtins" <<
Endl;
173 fEval = PyDict_GetItemString(mDict,
"eval");
174 fOpen = PyDict_GetItemString(mDict,
"open");
179 PyObject *pName = PyUnicode_FromString(
"pickle");
183 Log << kFATAL <<
"Can't import pickle" <<
Endl;
214 #if PY_MAJOR_VERSION < 3 215 Py_SetProgramName(const_cast<char*>(name.Data()));
217 Py_SetProgramName((
wchar_t *)name.Data());
238 return std::string(progName, progName +
mystrlen(progName));
248 if (!Py_IsInitialized())
return kFALSE;
269 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"wb");
271 PyObject *model_arg = Py_BuildValue(
"(OO)", obj,file);
276 Py_DECREF(model_arg);
277 Py_DECREF(model_data);
290 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"rb");
295 PyObject *model_arg = Py_BuildValue(
"(O)", file);
301 Py_DECREF(model_arg);
321 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
322 Log() << kWARNING <<
"Python error message:" <<
Endl;
324 Log() << kFATAL << errorMessage <<
Endl;
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
MsgLogger & Endl(MsgLogger &ml)
static PyObject * fModulePickle
Virtual base Class for all MVA method.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
static int PyIsInitialized()
Check Python interpreter initialization status.
static void PyInitialize()
Initialize Python interpreter.
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
PyObject * Eval(TString code)
Evaluate Python code.
Class that contains all the data information.
static TString Py_GetProgramName()
Get program name from Python interpreter.
static void PySetProgramName(TString name)
Set program name for Python interpeter.
size_t mystrlen(const char *s)
static PyObject * fModuleBuiltin
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
static constexpr double s
ostringstream derivative to redirect and format output
Abstract ClassifierFactory template that handles arbitrary types.
static PyObject * fPickleLoads
static PyObject * fPickleDumps
static PyObject * fGlobalNS