16 #pragma GCC diagnostic ignored "-Wunused-parameter" 17 #pragma GCC diagnostic ignored "-Wunused-function" 27 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 28 #include <numpy/arrayobject.h> 50 PyGILState_STATE m_GILState;
52 PyGILRAII():m_GILState(PyGILState_Ensure()){}
53 ~PyGILRAII(){PyGILState_Release(m_GILState);}
61 const TString &theOption ):
MethodBase(jobName, methodType, methodTitle, dsi, theOption),
101 if (!pyIsInitialized) {
105 PyGILRAII thePyGILRAII;
107 if (!pyIsInitialized) {
111 fMain = PyImport_AddModule(
"__main__");
113 Log <<
kFATAL <<
"Can't import __main__" <<
Endl;
119 Log <<
kFATAL <<
"Can't init global namespace" <<
Endl;
125 Log <<
kFATAL <<
"Can't init local namespace" <<
Endl;
129 #if PY_MAJOR_VERSION < 3 131 PyObject *bName = PyUnicode_FromString(
"__builtin__");
135 Log <<
kFATAL <<
"Can't import __builtin__" <<
Endl;
140 PyObject *bName = PyUnicode_FromString(
"builtins");
144 Log <<
kFATAL <<
"Can't import builtins" <<
Endl;
150 fEval = PyDict_GetItemString(mDict,
"eval");
151 fOpen = PyDict_GetItemString(mDict,
"open");
156 PyObject *pName = PyUnicode_FromString(
"pickle");
160 Log <<
kFATAL <<
"Can't import pickle" <<
Endl;
185 #if PY_MAJOR_VERSION < 3 186 Py_SetProgramName(const_cast<char*>(name.
Data()));
188 Py_SetProgramName((
wchar_t *)name.
Data());
192 size_t mystrlen(
const char* s) {
return strlen(s); }
193 size_t mystrlen(
const wchar_t* s) {
return wcslen(s); }
199 return std::string(progName, progName +
mystrlen(progName));
204 if (!Py_IsInitialized())
return kFALSE;
215 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"wb");
217 PyObject *model_arg = Py_BuildValue(
"(OO)", obj,file);
222 Py_DECREF(model_arg);
223 Py_DECREF(model_data);
228 PyObject *file_arg = Py_BuildValue(
"(ss)", path.
Data(),
"rb");
231 PyObject *model_arg = Py_BuildValue(
"(O)", file);
236 Py_DECREF(model_arg);
248 if (firstEvt > lastEvt || lastEvt > nEvents) lastEvt =
nEvents;
249 if (firstEvt < 0) firstEvt = 0;
250 std::vector<Double_t> values(lastEvt-firstEvt);
252 nEvents = values.size();
259 PyArrayObject *pEvent= (PyArrayObject *)PyArray_FromDims(2, dims, NPY_FLOAT);
260 float *pValue = (
float *)(PyArray_DATA(pEvent));
278 for (
UInt_t i = 0; i < nvars; i++) {
279 pValue[ievt * nvars + i] = e->
GetValue(i);
286 PyArrayObject *
result = (PyArrayObject *)PyObject_CallMethod(
fClassifier, const_cast<char *>(
"predict_proba"),
const_cast<char *
>(
"(O)"), pEvent);
287 double *proba = (
double *)(PyArray_DATA(result));
291 std::vector<double> mvaValues(nEvents);
292 for (
int i = 0; i <
nEvents; ++i)
293 mvaValues[i] = proba[2*i];
PyMethodBase(const TString &jobName, Types::EMVA methodType, const TString &methodTitle, DataSetInfo &dsi, const TString &theOption="")
MsgLogger & Endl(MsgLogger &ml)
static PyObject * fModulePickle
const char * GetName() const
static void Serialize(TString file, PyObject *classifier)
TString GetElapsedTime(Bool_t Scientific=kTRUE)
static int PyIsInitialized()
static void PyInitialize()
const TString & GetMethodName() const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
const char * Data() const
virtual void ReadModelFromFile()=0
static PyObject * Eval(TString code)
void SetCurrentEvent(Long64_t ievt) 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
UInt_t GetNVariables() const
accessor to the number of variables
static TString Py_GetProgramName()
static void PySetProgramName(TString name)
char * Form(const char *fmt,...)
Types::ETreeType GetCurrentType() const
size_t mystrlen(const char *s)
const Event * GetEvent() const
static PyObject * fLocalNS
static PyObject * fModuleBuiltin
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
DataSetInfo & DataInfo() const
Abstract ClassifierFactory template that handles arbitrary types.
static PyObject * fPickleLoads
static PyObject * fPickleDumps
static void UnSerialize(TString file, PyObject **obj)
static PyObject * fGlobalNS