17 #pragma GCC diagnostic ignored "-Wunused-parameter" 18 #pragma GCC diagnostic ignored "-Wunused-function" 28 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 29 #include <numpy/arrayobject.h> 51 PyGILState_STATE m_GILState;
53 PyGILRAII():m_GILState(PyGILState_Ensure()){}
54 ~PyGILRAII(){PyGILState_Release(m_GILState);}
60 const TString &methodTitle,
62 const TString &theOption ):
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
73 const TString &weightFile):
MethodBase(methodType, dsi, weightFile),
102 if (!pyIsInitialized) {
106 PyGILRAII thePyGILRAII;
108 if (!pyIsInitialized) {
112 fMain = PyImport_AddModule(
"__main__");
114 Log <<
kFATAL <<
"Can't import __main__" <<
Endl;
120 Log <<
kFATAL <<
"Can't init global namespace" <<
Endl;
126 Log <<
kFATAL <<
"Can't init local namespace" <<
Endl;
130 #if PY_MAJOR_VERSION < 3 132 PyObject *bName = PyUnicode_FromString(
"__builtin__");
136 Log <<
kFATAL <<
"Can't import __builtin__" <<
Endl;
141 PyObject *bName = PyUnicode_FromString(
"builtins");
145 Log <<
kFATAL <<
"Can't import builtins" <<
Endl;
151 fEval = PyDict_GetItemString(mDict,
"eval");
152 fOpen = PyDict_GetItemString(mDict,
"open");
157 PyObject *pName = PyUnicode_FromString(
"pickle");
161 Log <<
kFATAL <<
"Can't import pickle" <<
Endl;
186 #if PY_MAJOR_VERSION < 3 187 Py_SetProgramName(const_cast<char*>(name.Data()));
189 Py_SetProgramName((
wchar_t *)name.Data());
193 size_t mystrlen(
const char* s) {
return strlen(s); }
194 size_t mystrlen(
const wchar_t* s) {
return wcslen(s); }
200 return std::string(progName, progName +
mystrlen(progName));
205 if (!Py_IsInitialized())
return kFALSE;
216 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"wb");
218 PyObject *model_arg = Py_BuildValue(
"(OO)", obj,file);
223 Py_DECREF(model_arg);
224 Py_DECREF(model_data);
229 PyObject *file_arg = Py_BuildValue(
"(ss)", path.Data(),
"rb");
232 PyObject *model_arg = Py_BuildValue(
"(O)", file);
237 Py_DECREF(model_arg);
249 if (firstEvt > lastEvt || lastEvt > nEvents) lastEvt =
nEvents;
250 if (firstEvt < 0) firstEvt = 0;
251 std::vector<Double_t> values(lastEvt-firstEvt);
253 nEvents = values.size();
260 PyArrayObject *pEvent= (PyArrayObject *)PyArray_FromDims(2, dims, NPY_FLOAT);
261 float *pValue = (
float *)(PyArray_DATA(pEvent));
279 for (
UInt_t i = 0; i < nvars; i++) {
280 pValue[ievt * nvars + i] = e->
GetValue(i);
287 PyArrayObject *
result = (PyArrayObject *)PyObject_CallMethod(
fClassifier, const_cast<char *>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
288 double *proba = (
double *)(PyArray_DATA(result));
292 std::vector<double> mvaValues(nEvents);
293 for (
int i = 0; i <
nEvents; ++i)
294 mvaValues[i] = proba[2*i];
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
void SetCurrentEvent(Long64_t ievt) const
MsgLogger & Endl(MsgLogger &ml)
static PyObject * fModulePickle
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
static void Serialize(TString file, PyObject *classifier)
TString GetElapsedTime(Bool_t Scientific=kTRUE)
static int PyIsInitialized()
static void PyInitialize()
void PyRunString(TString code, TString errorMessage="Failed to run python code", int start=Py_single_input)
virtual void ReadModelFromFile()=0
Types::ETreeType GetCurrentType() const
static PyObject * Eval(TString code)
DataSetInfo & DataInfo() const
virtual std::vector< Double_t > GetMvaValues(Long64_t firstEvt=0, Long64_t lastEvt=-1, Bool_t logProgress=false)
get all the MVA values for the events of the current Data type
static TString Py_GetProgramName()
const char * GetName() const
static void PySetProgramName(TString name)
char * Form(const char *fmt,...)
const TString & GetMethodName() const
UInt_t GetNVariables() const
accessor to the number of variables
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
size_t mystrlen(const char *s)
static PyObject * fLocalNS
static PyObject * fModuleBuiltin
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Abstract ClassifierFactory template that handles arbitrary types.
static PyObject * fPickleLoads
static PyObject * fPickleDumps
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
static void UnSerialize(TString file, PyObject **obj)
const Event * GetEvent() const
static PyObject * fGlobalNS