26#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
27#include <numpy/arrayobject.h>
47 if (python_version.
IsNull()) {
48 TMVA::gTools().
Log() << kFATAL <<
"Can't find a valid Python version used to build ROOT" <<
Endl;
51 if(python_version[0] ==
'2')
53 else if (python_version[0] ==
'3')
56 TMVA::gTools().
Log() << kFATAL <<
"Invalid Python version used to build ROOT : " << python_version <<
Endl;
81 :
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
91 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
109 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
151 if (!pyIsInitialized) {
156 if (!pyIsInitialized) {
161 fMain = PyImport_AddModule(
"__main__");
163 Log << kFATAL <<
"Can't import __main__" <<
Endl;
170 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
175 #if PY_MAJOR_VERSION < 3
177 PyObject *bName = PyUnicode_FromString(
"__builtin__");
182 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
187 PyObject *bName = PyUnicode_FromString(
"builtins");
191 Log << kFATAL <<
"Can't import builtins" <<
Endl;
198 fEval = PyDict_GetItemString(mDict,
"eval");
199 fOpen = PyDict_GetItemString(mDict,
"open");
208 PyObject *pName = PyUnicode_FromString(
"pickle");
213 Log << kFATAL <<
"Can't import pickle" <<
Endl;
248 #if PY_MAJOR_VERSION < 3
249 Py_SetProgramName(
const_cast<char*
>(
name.Data()));
251 Py_SetProgramName((
wchar_t *)
name.Data());
258size_t mystrlen(
const char* s) {
return strlen(s); }
262size_t mystrlen(
const wchar_t* s) {
return wcslen(s); }
272 return std::string(progName, progName +
mystrlen(progName));
282 if (!Py_IsInitialized())
return kFALSE;
303 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
310 Py_DECREF(model_arg);
311 Py_DECREF(model_data);
324 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
335 Py_DECREF(model_arg);
355 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
356 Log() << kWARNING <<
"Python error message:" <<
Endl;
358 Log() << kFATAL << errorMessage <<
Endl;
376 std::cout<<
"\nPython error message:\n";
378 throw std::runtime_error(
"\nFailed to run python code: "+code);
389 PyObject* encodedString = PyUnicode_AsUTF8String(
string);
402 std::vector<size_t>tupleVec;
403 for(
Py_ssize_t tupleIter=0;tupleIter<PyTuple_Size(tupleObject);++tupleIter){
404 auto itemObj = PyTuple_GetItem(tupleObject,tupleIter);
405 if (itemObj == Py_None)
406 tupleVec.push_back(0);
408 tupleVec.push_back((
size_t)PyLong_AsLong(itemObj));
421 std::vector<size_t>listVec;
422 for(
Py_ssize_t listIter=0; listIter<PyList_Size(listObject);++listIter){
423 listVec.push_back((
size_t)PyLong_AsLong(PyList_GetItem(listObject,listIter)));
size_t mystrlen(const char *s)
R__EXTERN TSystem * gSystem
Class that contains all the data information.
PyGILState_STATE m_GILState
Virtual base Class for all MVA method.
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 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 const char * PyStringAsString(PyObject *string)
Returns const char* from Python string in PyObject.
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
virtual TString GetFromPipe(const char *command)
Execute command and return output in TString.
create variable transformations
TString Python_Executable()
Function to find current Python executable used by ROOT If Python2 is installed return "python" Inste...
MsgLogger & Endl(MsgLogger &ml)