8#include <tracking/trackFindingCDC/mva/PyEstimator.h>
9#include <framework/utilities/FileSystem.h>
11#include <boost/python/import.hpp>
12#include <boost/python/extract.hpp>
13#include <boost/python/list.hpp>
14#include <boost/python/tuple.hpp>
16#include <framework/logging/Logger.h>
20using namespace TrackFindingCDC;
23 : m_pickleFileName(pickleFileName)
28 boost::python::object numpy = boost::python::import(
"numpy");
29 boost::python::list initValues;
30 initValues.append(0.0);
31 m_array = numpy.attr(
"array")(initValues);
36 }
catch (
const boost::python::error_already_set&) {
38 B2ERROR(
"Could not construct PyEstimator from " << pickleFileName);
44 size_t nVars = inputVariables.size();
47 for (
size_t iVar = 0; iVar < nVars; ++iVar) {
48 m_array[boost::python::make_tuple(0, iVar)] = inputVariables[iVar];
57 nVars += floatTuple->size();
63 for (
size_t iTuple = 0; iTuple < floatTuple->size(); ++iTuple) {
64 m_array[boost::python::make_tuple(0, iVar)] = floatTuple->get(iTuple);
73 boost::python::object predictions;
78 boost::python::object prediction = predictions[0];
79 return boost::python::extract<double>(prediction[1]);
82 boost::python::object prediction = predictions[0];
83 return boost::python::extract<double>(prediction);
85 }
catch (
const boost::python::error_already_set&) {
87 B2ERROR(
"Estimation failed in python object");
96 boost::python::object io = boost::python::import(
"io");
97 boost::python::object pickle = boost::python::import(
"pickle");
98 boost::python::object pickleFile = io.attr(
"open")(absPickleFilePath,
"rb");
99 boost::python::object estimator = pickle.attr(
"load")(pickleFile);
101 }
catch (
const boost::python::error_already_set&) {
103 B2ERROR(
"Could not open pickle file " << pickleFileName);
109 }
catch (
const boost::python::error_already_set&) {
113 B2INFO(
"Estimator in " <<
m_pickleFileName <<
" is not a binary classifier. Trying as regressor");
117 }
catch (
const boost::python::error_already_set&) {
119 B2ERROR(
"Could neither find 'predict' not 'predict_proba' in python estimator from file " << pickleFileName);
129 boost::python::object numpy = boost::python::import(
"numpy");
130 boost::python::object shape = boost::python::make_tuple(1, nVars);
132 }
catch (
const boost::python::error_already_set&) {
134 B2ERROR(
"Resize failed");
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
An abstract tuple of float value where each value has an associated name.
void expand(size_t nVars)
Reserves space for at least n variable in the input array.
void unpickleEstimator(const std::string &pickleFileName)
Load the estimator object from the pickled file.
std::string m_pickleFileName
File name of the pickle file that contains the trained estimator.
PyEstimator(const std::string &pickleFileName)
Construct the Estimator.
double predict(const std::vector< double > &inputVariables)
Call the predict method of the estimator.
boost::python::object m_estimator
Retrained python estimator object.
boost::python::object m_predict
Python bound prediction method - cached to avoid repeated lookup.
size_t m_nCurrent
Cache for the current length of the input array.
bool m_is_binary_classification
Internal flag to keep track whether a binary classification with predict_proba is evaluated.
boost::python::object m_array
Array to be served to the estimator.
Abstract base class for different kinds of events.