Belle II Software light-2406-ragdoll
PythonExpert Class Reference

Expert for the Python MVA method. More...

#include <Python.h>

Inheritance diagram for PythonExpert:
Collaboration diagram for PythonExpert:

Public Member Functions

 PythonExpert ()
 Constructs a new Python Expert.
 
virtual void load (Weightfile &weightfile) override
 Load the expert from a Weightfile.
 
virtual std::vector< float > apply (Dataset &test_data) const override
 Apply this expert onto a dataset.
 
virtual std::vector< std::vector< float > > applyMulticlass (Dataset &test_data) const override
 Apply this expert onto a dataset for multiclass problem.
 

Protected Attributes

PythonOptions m_specific_options
 Method specific options.
 
boost::python::object m_framework
 Framework module.
 
boost::python::object m_state
 current state object of method
 
std::vector< float > m_means
 Means of all features for normalization.
 
std::vector< float > m_stds
 Stds of all features for normalization.
 
GeneralOptions m_general_options
 General options loaded from the weightfile.
 

Detailed Description

Expert for the Python MVA method.

Definition at line 113 of file Python.h.

Constructor & Destructor Documentation

◆ PythonExpert()

Constructs a new Python Expert.

Definition at line 381 of file Python.cc.

382 {
384 }
static PythonInitializerSingleton & GetInstance()
Return static instance of PythonInitializerSingleton.
Definition: Python.cc:144

Member Function Documentation

◆ apply()

std::vector< float > apply ( Dataset test_data) const
overridevirtual

Apply this expert onto a dataset.

Parameters
test_datadataset

Implements Expert.

Definition at line 425 of file Python.cc.

426 {
427
428 uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
429 uint64_t numberOfEvents = test_data.getNumberOfEvents();
430
431 auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
432 npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
433
434 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
435 test_data.loadEvent(iEvent);
437 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
438 X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
439 } else {
440 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
441 X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
442 }
443 }
444
445 std::vector<float> probabilities(test_data.getNumberOfEvents(), std::numeric_limits<float>::quiet_NaN());
446
447 try {
448 auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
449 auto result = m_framework.attr("apply")(m_state, ndarray_X);
450 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
451 // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
452 // to a PyObject but do not inherit from it!
453 probabilities[iEvent] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR1(reinterpret_cast<PyArrayObject*>(result.ptr()),
454 iEvent)));
455 }
456 } catch (...) {
457 PyErr_Print();
458 PyErr_Clear();
459 B2ERROR("Failed calling applying PythonExpert");
460 throw std::runtime_error("Failed calling applying PythonExpert");
461 }
462
463 return probabilities;
464 }
boost::python::object m_state
current state object of method
Definition: Python.h:142
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:144
boost::python::object m_framework
Framework module.
Definition: Python.h:141
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:140
std::vector< float > m_means
Means of all features for normalization.
Definition: Python.h:143
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:83

◆ applyMulticlass()

std::vector< std::vector< float > > applyMulticlass ( Dataset test_data) const
overridevirtual

Apply this expert onto a dataset for multiclass problem.

Parameters
test_datadataset

Reimplemented from Expert.

Definition at line 466 of file Python.cc.

467 {
468
469 uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
470 uint64_t numberOfEvents = test_data.getNumberOfEvents();
471
472 auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
473 npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
474
475 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
476 test_data.loadEvent(iEvent);
478 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
479 X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
480 } else {
481 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
482 X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
483 }
484 }
485
486 unsigned int nClasses = m_general_options.m_nClasses;
487 std::vector<std::vector<float>> probabilities(test_data.getNumberOfEvents(), std::vector<float>(nClasses,
488 std::numeric_limits<float>::quiet_NaN()));
489
490 try {
491 auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
492 auto result = m_framework.attr("apply")(m_state, ndarray_X);
493 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
494 // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
495 // to a PyObject but do not inherit from it!
496 for (uint64_t iClass = 0; iClass < nClasses; ++iClass) {
497 probabilities[iEvent][iClass] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR2(reinterpret_cast<PyArrayObject*>
498 (result.ptr()),
499 iEvent, iClass)));
500 }
501 }
502 } catch (...) {
503 PyErr_Print();
504 PyErr_Clear();
505 B2ERROR("Failed calling applying PythonExpert");
506 throw std::runtime_error("Failed calling applying PythonExpert");
507 }
508
509 return probabilities;
510 }
GeneralOptions m_general_options
General options loaded from the weightfile.
Definition: Expert.h:70
unsigned int m_nClasses
Number of classes in a classification problem.
Definition: Options.h:89

◆ load()

void load ( Weightfile weightfile)
overridevirtual

Load the expert from a Weightfile.

Parameters
weightfilecontaining all information necessary to build the expert

Implements Expert.

Definition at line 387 of file Python.cc.

388 {
389
390 std::string custom_weightfile = weightfile.generateFileName();
391 weightfile.getFile("Python_Weightfile", custom_weightfile);
392 weightfile.getOptions(m_general_options);
393 weightfile.getOptions(m_specific_options);
394
396 m_means = weightfile.getVector<float>("Python_Means");
397 m_stds = weightfile.getVector<float>("Python_Stds");
398 }
399
400 try {
401 auto pickle = boost::python::import("pickle");
402 auto builtins = boost::python::import("builtins");
403 m_framework = boost::python::import((std::string("basf2_mva_python_interface.") + m_specific_options.m_framework).c_str());
404
405 if (weightfile.containsElement("Python_Steeringfile")) {
406 std::string custom_steeringfile = weightfile.generateFileName();
407 weightfile.getFile("Python_Steeringfile", custom_steeringfile);
408 auto steeringfile = builtins.attr("open")(custom_steeringfile.c_str(), "rb");
409 auto source_code = pickle.attr("load")(steeringfile);
410 builtins.attr("exec")(boost::python::object(source_code), boost::python::object(m_framework.attr("__dict__")));
411 }
412
413 auto file = builtins.attr("open")(custom_weightfile.c_str(), "rb");
414 auto unpickled_fit_object = pickle.attr("load")(file);
415 m_state = m_framework.attr("load")(unpickled_fit_object);
416 } catch (...) {
417 PyErr_Print();
418 PyErr_Clear();
419 B2ERROR("Failed calling load in PythonExpert");
420 throw std::runtime_error("Failed calling load in PythonExpert");
421 }
422
423 }
std::string m_framework
framework to use e.g.
Definition: Python.h:77

Member Data Documentation

◆ m_framework

boost::python::object m_framework
protected

Framework module.

Definition at line 141 of file Python.h.

◆ m_general_options

GeneralOptions m_general_options
protectedinherited

General options loaded from the weightfile.

Definition at line 70 of file Expert.h.

◆ m_means

std::vector<float> m_means
protected

Means of all features for normalization.

Definition at line 143 of file Python.h.

◆ m_specific_options

PythonOptions m_specific_options
protected

Method specific options.

Definition at line 140 of file Python.h.

◆ m_state

boost::python::object m_state
protected

current state object of method

Definition at line 142 of file Python.h.

◆ m_stds

std::vector<float> m_stds
protected

Stds of all features for normalization.

Definition at line 144 of file Python.h.


The documentation for this class was generated from the following files: