26#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
27#include <numpy/arrayobject.h>
44 TString python_version =
gSystem->GetFromPipe(
"root-config --python-version");
45 if (python_version.
IsNull()) {
46 TMVA::gTools().
Log() << kFATAL <<
"Can't find a valid Python version used to build ROOT" <<
Endl;
53 if (python_version[0] ==
'2' || python_version[0] ==
'3')
56 if (python_version[0] ==
'2')
58 else if (python_version[0] ==
'3')
61 TMVA::gTools().
Log() << kFATAL <<
"Invalid Python version used to build ROOT : " << python_version <<
Endl;
85 :
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
95 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
113 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
157 if (!pyIsInitialized) {
162 if (!pyIsInitialized) {
167 fMain = PyImport_AddModule(
"__main__");
169 Log << kFATAL <<
"Can't import __main__" <<
Endl;
176 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
181 #if PY_MAJOR_VERSION < 3
183 PyObject *bName = PyUnicode_FromString(
"__builtin__");
188 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
193 PyObject *bName = PyUnicode_FromString(
"builtins");
197 Log << kFATAL <<
"Can't import builtins" <<
Endl;
204 fEval = PyDict_GetItemString(mDict,
"eval");
205 fOpen = PyDict_GetItemString(mDict,
"open");
214 PyObject *pName = PyUnicode_FromString(
"pickle");
219 Log << kFATAL <<
"Can't import pickle" <<
Endl;
254 if (!Py_IsInitialized())
return kFALSE;
275 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
277 PyObject *model_arg = Py_BuildValue(
"(OO)", obj,file);
282 Py_DECREF(model_arg);
283 Py_DECREF(model_data);
296 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
301 PyObject *model_arg = Py_BuildValue(
"(O)", file);
307 Py_DECREF(model_arg);
328 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
329 Log() << kWARNING <<
"Python error message:" <<
Endl;
331 Log() << kFATAL << errorMessage <<
Endl;
349 std::cout<<
"\nPython error message:\n";
351 throw std::runtime_error(
"\nFailed to run python code: "+code);
362 PyObject* encodedString = PyUnicode_AsUTF8String(
string);
375 std::vector<size_t>tupleVec;
376 for(
Py_ssize_t tupleIter=0;tupleIter<PyTuple_Size(tupleObject);++tupleIter){
377 auto itemObj = PyTuple_GetItem(tupleObject,tupleIter);
378 if (itemObj == Py_None)
379 tupleVec.push_back(0);
381 tupleVec.push_back((
size_t)PyLong_AsLong(itemObj));
394 std::vector<size_t>listVec;
395 for(
Py_ssize_t listIter=0; listIter<PyList_Size(listObject);++listIter){
396 listVec.push_back((
size_t)PyLong_AsLong(PyList_GetItem(listObject,listIter)));
411 return PyDict_GetItemWithError(dict, PyUnicode_FromString(key));
int Int_t
Signed integer 4 bytes (int).
Class that contains all the data information.
PyGILState_STATE m_GILState
MethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
standard constructor
ostringstream derivative to redirect and format output
static std::vector< size_t > GetDataFromTuple(PyObject *tupleObject)
Utility function which retrieves and returns the values of the Tuple object as a vector of size_t.
static int PyIsInitialized()
Check Python interpreter initialization status.
static std::vector< size_t > GetDataFromList(PyObject *listObject)
Utility function which retrieves and returns the values of the List object as a vector of size_t.
static PyObject * fPickleDumps
PyObject * Eval(TString code)
Evaluate Python code.
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 const char * PyStringAsString(PyObject *string)
Returns const char* from Python string in PyObject.
static PyObject * fPickleLoads
static PyObject * fGlobalNS
static PyObject * fModulePickle
static PyObject * fModuleBuiltin
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
static PyObject * GetValueFromDict(PyObject *dict, const char *key)
Utility function which checks if a given key is present in a Python dictionary object and returns the...
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=256)
Execute Python code from string.
const char * Data() const
TString & ReplaceAll(const TString &s1, const TString &s2)
create variable transformations
TString Python_Executable()
Function to find current Python executable used by ROOT If "Python3" is installed,...
MsgLogger & Endl(MsgLogger &ml)