26#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
27#include <numpy/arrayobject.h>
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),
97 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
117 Log() << kFATAL <<
"Can't init local namespace" <<
Endl;
127 <<
"PyMVA is deprecated and will be removed in ROOT 6.44. Please use the underlying Python "
128 "machine-learning packages directly, or export your model to ONNX and evaluate it with SOFIE "
129 "(see the RSofieReader class)."
173 if (!pyIsInitialized) {
178 if (!pyIsInitialized) {
183 fMain = PyImport_AddModule(
"__main__");
185 Log << kFATAL <<
"Can't import __main__" <<
Endl;
192 Log << kFATAL <<
"Can't init global namespace" <<
Endl;
197 #if PY_MAJOR_VERSION < 3
199 PyObject *bName = PyUnicode_FromString(
"__builtin__");
204 Log << kFATAL <<
"Can't import __builtin__" <<
Endl;
209 PyObject *bName = PyUnicode_FromString(
"builtins");
213 Log << kFATAL <<
"Can't import builtins" <<
Endl;
220 fEval = PyDict_GetItemString(mDict,
"eval");
221 fOpen = PyDict_GetItemString(mDict,
"open");
230 PyObject *pName = PyUnicode_FromString(
"pickle");
235 Log << kFATAL <<
"Can't import pickle" <<
Endl;
270 if (!Py_IsInitialized())
return kFALSE;
291 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
293 PyObject *model_arg = Py_BuildValue(
"(OO)", obj,file);
298 Py_DECREF(model_arg);
299 Py_DECREF(model_data);
312 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
317 PyObject *model_arg = Py_BuildValue(
"(O)", file);
323 Py_DECREF(model_arg);
344 Log() << kWARNING <<
"Failed to run python code: " << code <<
Endl;
345 Log() << kWARNING <<
"Python error message:" <<
Endl;
347 Log() << kFATAL << errorMessage <<
Endl;
365 std::cout<<
"\nPython error message:\n";
367 throw std::runtime_error(
"\nFailed to run python code: "+code);
378 PyObject* encodedString = PyUnicode_AsUTF8String(
string);
379 const char* cstring = PyBytes_AsString(encodedString);
391 std::vector<size_t>tupleVec;
392 for(Py_ssize_t tupleIter=0;tupleIter<PyTuple_Size(tupleObject);++tupleIter){
393 auto itemObj = PyTuple_GetItem(tupleObject,tupleIter);
394 if (itemObj == Py_None)
395 tupleVec.push_back(0);
397 tupleVec.push_back((
size_t)PyLong_AsLong(itemObj));
410 std::vector<size_t>listVec;
411 for(Py_ssize_t listIter=0; listIter<PyList_Size(listObject);++listIter){
412 listVec.push_back((
size_t)PyLong_AsLong(PyList_GetItem(listObject,listIter)));
427 return PyDict_GetItemWithError(dict, PyUnicode_FromString(key));
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
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 void PyInitialize()
Initialize Python interpreter.
static void Serialize(TString file, PyObject *classifier)
Serialize Python object.
void PrintDeprecationWarning()
Warn that PyMVA is deprecated and scheduled for removal in ROOT 6.44.
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)
virtual TString GetFromPipe(const char *command, Int_t *ret=nullptr, Bool_t redirectStderr=kFALSE)
Execute command and return output in TString.
create variable transformations
TString Python_Executable()
Function to find current Python executable used by ROOT If "Python3" is installed,...
MsgLogger & Endl(MsgLogger &ml)