Belle II Software development
PythonExpert Class Reference

Expert for the Python MVA method. More...

#include <Python.h>

Inheritance diagram for PythonExpert:
Expert

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 378 of file Python.cc.

379 {
381 }
static PythonInitializerSingleton & GetInstance()
Return static instance of PythonInitializerSingleton.
Definition: Python.cc:141

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 422 of file Python.cc.

423 {
424
425 uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
426 uint64_t numberOfEvents = test_data.getNumberOfEvents();
427
428 auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
429 npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
430
431 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
432 test_data.loadEvent(iEvent);
434 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
435 X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
436 } else {
437 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
438 X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
439 }
440 }
441
442 std::vector<float> probabilities(test_data.getNumberOfEvents(), std::numeric_limits<float>::quiet_NaN());
443
444 try {
445 auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
446 auto result = m_framework.attr("apply")(m_state, ndarray_X);
447 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
448 // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
449 // to a PyObject but do not inherit from it!
450 probabilities[iEvent] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR1(reinterpret_cast<PyArrayObject*>(result.ptr()),
451 iEvent)));
452 }
453 } catch (...) {
454 PyErr_Print();
455 PyErr_Clear();
456 B2ERROR("Failed calling applying PythonExpert");
457 throw std::runtime_error("Failed calling applying PythonExpert");
458 }
459
460 return probabilities;
461 }
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 463 of file Python.cc.

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

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