25#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
26#include <numpy/arrayobject.h>
36 PyGILState_STATE m_GILState;
39 PyGILRAII() : m_GILState(PyGILState_Ensure()) {}
40 ~PyGILRAII() { PyGILState_Release(m_GILState); }
49PyObject *PyMethodBase::fModuleBuiltin = NULL;
53PyObject *PyMethodBase::fModulePickle = NULL;
54PyObject *PyMethodBase::fPickleDumps = NULL;
55PyObject *PyMethodBase::fPickleLoads = NULL;
58PyObject *PyMethodBase::fGlobalNS = NULL;
64 :
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
74 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
92 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
133 if (!pyIsInitialized) {
137 TMVA::Internal::PyGILRAII raii;
138 if (!pyIsInitialized) {
142 fMain = PyImport_AddModule(
"__main__");
144 Log << kFATAL <<
"Can't import __main__" <<
Endl;
150 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
154 #if PY_MAJOR_VERSION < 3
156 PyObject *bName = PyUnicode_FromString(
"__builtin__");
160 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
165 PyObject *bName = PyUnicode_FromString(
"builtins");
169 Log << kFATAL <<
"Can't import builtins" <<
Endl;
175 fEval = PyDict_GetItemString(mDict,
"eval");
176 fOpen = PyDict_GetItemString(mDict,
"open");
181 PyObject *pName = PyUnicode_FromString(
"pickle");
185 Log << kFATAL <<
"Can't import pickle" <<
Endl;
216 #if PY_MAJOR_VERSION < 3
217 Py_SetProgramName(
const_cast<char*
>(
name.Data()));
219 Py_SetProgramName((
wchar_t *)
name.Data());
240 return std::string(progName, progName +
mystrlen(progName));
250 if (!Py_IsInitialized())
return kFALSE;
271 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
278 Py_DECREF(model_arg);
279 Py_DECREF(model_data);
292 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
303 Py_DECREF(model_arg);
323 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
324 Log() << kWARNING <<
"Python error message:" <<
Endl;
326 Log() << kFATAL << errorMessage <<
Endl;
size_t mystrlen(const char *s)
Class that contains all the data information.
Virtual base Class for all MVA method.
ostringstream derivative to redirect and format output
static int PyIsInitialized()
Check Python interpreter initialization status.
static PyObject * fPickleDumps
PyObject * Eval(TString code)
Evaluate Python code.
static TString Py_GetProgramName()
Get program name from Python interpreter.
static void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
static Int_t UnSerialize(TString file, PyObject **obj)
Unserialize Python object.
static PyObject * fPickleLoads
static void PySetProgramName(TString name)
Set program name for Python interpeter.
static PyObject * fGlobalNS
static PyObject * fModulePickle
static PyObject * fModuleBuiltin
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
Execute Python code from string.
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
const char * Data() const
static constexpr double s
create variable transformations
MsgLogger & Endl(MsgLogger &ml)