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_unique_mva_module
 python module containing the mva methods
 
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 400 of file Python.cc.

401 {
402 PythonInitializerSingleton::GetInstance();
403 }

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

460 {
461
462 uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
463 uint64_t numberOfEvents = test_data.getNumberOfEvents();
464
465 auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
466 npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
467
468 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
469 test_data.loadEvent(iEvent);
470 if (m_specific_options.m_normalize) {
471 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
472 X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
473 } else {
474 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
475 X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
476 }
477 }
478
479 std::vector<float> probabilities(test_data.getNumberOfEvents(), std::numeric_limits<float>::quiet_NaN());
480
481 try {
482 auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
483 auto result = m_unique_mva_module.attr("apply")(m_state, ndarray_X);
484 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
485 // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
486 // to a PyObject but do not inherit from it!
487 probabilities[iEvent] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR1(reinterpret_cast<PyArrayObject*>(result.ptr()),
488 iEvent)));
489 }
490 } catch (...) {
491 PyErr_Print();
492 PyErr_Clear();
493 B2ERROR("Failed calling applying PythonExpert");
494 throw std::runtime_error("Failed calling applying PythonExpert");
495 }
496
497 return probabilities;
498 }

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

501 {
502
503 uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
504 uint64_t numberOfEvents = test_data.getNumberOfEvents();
505
506 auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
507 npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
508
509 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
510 test_data.loadEvent(iEvent);
511 if (m_specific_options.m_normalize) {
512 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
513 X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
514 } else {
515 for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
516 X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
517 }
518 }
519
520 unsigned int nClasses = m_general_options.m_nClasses;
521 std::vector<std::vector<float>> probabilities(test_data.getNumberOfEvents(), std::vector<float>(nClasses,
522 std::numeric_limits<float>::quiet_NaN()));
523
524 try {
525 auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
526 auto result = m_unique_mva_module.attr("apply")(m_state, ndarray_X);
527 for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
528 // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
529 // to a PyObject but do not inherit from it!
530 for (uint64_t iClass = 0; iClass < nClasses; ++iClass) {
531 probabilities[iEvent][iClass] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR2(reinterpret_cast<PyArrayObject*>
532 (result.ptr()),
533 iEvent, iClass)));
534 }
535 }
536 } catch (...) {
537 PyErr_Print();
538 PyErr_Clear();
539 B2ERROR("Failed calling applying PythonExpert");
540 throw std::runtime_error("Failed calling applying PythonExpert");
541 }
542
543 return probabilities;
544 }

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

407 {
408
409 std::string custom_weightfile = weightfile.generateFileName();
410 weightfile.getFile("Python_Weightfile", custom_weightfile);
411 weightfile.getOptions(m_general_options);
412 weightfile.getOptions(m_specific_options);
413
414 if (m_specific_options.m_normalize) {
415 m_means = weightfile.getVector<float>("Python_Means");
416 m_stds = weightfile.getVector<float>("Python_Stds");
417 }
418
419 try {
420 auto pickle = boost::python::import("pickle");
421 auto builtins = boost::python::import("builtins");
422
423 // Create a new empty module with a unique name.
424 // This way we dont end up with multiple mvas trying to implement
425 // the same apply method with the last one being used by all.
426 boost::uuids::random_generator uuid_gen;
427 std::string unique_mva_module_name = "custom_framework_" + boost::uuids::to_string(uuid_gen());
428 boost::python::object type = boost::python::import("types");
429 m_unique_mva_module = type.attr("ModuleType")(unique_mva_module_name.c_str());
430
431 // Find the framework file. Then execute it in the scope of the new module
432 auto framework = boost::python::import((std::string("basf2_mva_python_interface.") + m_specific_options.m_framework).c_str());
433 auto framework_file = framework.attr("__file__");
434 auto framework_file_source_code = loadPythonFileAsString(boost::python::extract<std::string>(boost::python::object(
435 framework_file)));
436 builtins.attr("exec")(framework_file_source_code.c_str(), boost::python::object(m_unique_mva_module.attr("__dict__")));
437
438 // Overwrite framework with user-defined code from the steering file if defined
439 if (weightfile.containsElement("Python_Steeringfile")) {
440 std::string custom_steeringfile = weightfile.generateFileName();
441 weightfile.getFile("Python_Steeringfile", custom_steeringfile);
442 auto steeringfile = builtins.attr("open")(custom_steeringfile.c_str(), "rb");
443 auto source_code = pickle.attr("load")(steeringfile);
444 builtins.attr("exec")(boost::python::object(source_code), boost::python::object(m_unique_mva_module.attr("__dict__")));
445 }
446
447 auto file = builtins.attr("open")(custom_weightfile.c_str(), "rb");
448 auto unpickled_fit_object = pickle.attr("load")(file);
449 m_state = m_unique_mva_module.attr("load")(unpickled_fit_object);
450 } catch (...) {
451 PyErr_Print();
452 PyErr_Clear();
453 B2ERROR("Failed calling load in PythonExpert");
454 throw std::runtime_error("Failed calling load in PythonExpert");
455 }
456
457 }

Member Data Documentation

◆ 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.

◆ m_unique_mva_module

boost::python::object m_unique_mva_module
protected

python module containing the mva methods

Definition at line 141 of file Python.h.


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