Belle II Software  release-05-02-19
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. More...
 
virtual std::vector< float > apply (Dataset &test_data) const override
 Apply this expert onto a dataset. More...
 
virtual std::vector< float > apply (Dataset &test_data, const unsigned int classID) const
 Apply this expert onto a dataset. More...
 

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 115 of file Python.h.

Member Function Documentation

◆ apply() [1/2]

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

Apply this expert onto a dataset.

Parameters
test_datadataset

Implements Expert.

Definition at line 412 of file Python.cc.

413  {
414 
415  uint64_t numberOfFeatures = test_data.getNumberOfFeatures();
416  uint64_t numberOfEvents = test_data.getNumberOfEvents();
417 
418  auto X = std::unique_ptr<float[]>(new float[numberOfEvents * numberOfFeatures]);
419  npy_intp dimensions_X[2] = {static_cast<npy_intp>(numberOfEvents), static_cast<npy_intp>(numberOfFeatures)};
420 
421  for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
422  test_data.loadEvent(iEvent);
424  for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
425  X[iEvent * numberOfFeatures + iFeature] = (test_data.m_input[iFeature] - m_means[iFeature]) / m_stds[iFeature];
426  } else {
427  for (uint64_t iFeature = 0; iFeature < numberOfFeatures; ++iFeature)
428  X[iEvent * numberOfFeatures + iFeature] = test_data.m_input[iFeature];
429  }
430  }
431 
432  std::vector<float> probabilities(test_data.getNumberOfEvents());
433 
434  try {
435  auto ndarray_X = boost::python::handle<>(PyArray_SimpleNewFromData(2, dimensions_X, NPY_FLOAT32, X.get()));
436  auto result = m_framework.attr("apply")(m_state, ndarray_X);
437  for (uint64_t iEvent = 0; iEvent < numberOfEvents; ++iEvent) {
438  // We have to do some nasty casting here, because the Python C-Api uses structs which are binary compatible
439  // to a PyObject but do not inherit from it!
440  probabilities[iEvent] = static_cast<float>(*static_cast<float*>(PyArray_GETPTR1(reinterpret_cast<PyArrayObject*>(result.ptr()),
441  iEvent)));
442  }
443  } catch (...) {
444  PyErr_Print();
445  PyErr_Clear();
446  B2ERROR("Failed calling applying PythonExpert");
447  throw std::runtime_error("Failed calling applying PythonExpert");
448  }
449 
450  return probabilities;
451  }

◆ apply() [2/2]

virtual std::vector<float> apply ( Dataset test_data,
const unsigned int  classID 
) const
inlinevirtualinherited

Apply this expert onto a dataset.

Multi-class mode signature. Not pure virtual, since not all derived classes need to re-implement this.

Parameters
test_datadataset
classIDclass identifier.

Reimplemented in TMVAExpertMulticlass.

Definition at line 59 of file Expert.h.

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


The documentation for this class was generated from the following files:
Belle2::MVA::PythonOptions::m_normalize
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:85
Belle2::MVA::PythonExpert::m_framework
boost::python::object m_framework
Framework module.
Definition: Python.h:137
Belle2::MVA::PythonExpert::m_state
boost::python::object m_state
current state object of method
Definition: Python.h:138
Belle2::MVA::PythonExpert::m_stds
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:140
Belle2::MVA::PythonExpert::m_specific_options
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:136
Belle2::MVA::PythonExpert::m_means
std::vector< float > m_means
Means of all features for normalization.
Definition: Python.h:139