Belle II Software development
Python.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <mva/interface/Options.h>
12#include <mva/interface/Teacher.h>
13#include <mva/interface/Expert.h>
14
20#if !defined(__CLING__)
21#include <Python.h>
22#endif
23
24#if !defined(__GNUG__) || defined(__ICC)
25#else
26#pragma GCC diagnostic push
27#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
28#pragma GCC diagnostic ignored "-Wunused-parameter"
29#endif
30#include <boost/python/object.hpp>
31#include <boost/python/dict.hpp>
32#include <boost/python/import.hpp>
33#include <boost/python/extract.hpp>
34#if !defined(__GNUG__) || defined(__ICC)
35#else
36#pragma GCC diagnostic pop
37#endif
38
39
40namespace Belle2 {
45 namespace MVA {
46
51
52 public:
57 virtual void load(const boost::property_tree::ptree& pt) override;
58
63 virtual void save(boost::property_tree::ptree& pt) const override;
64
68 virtual po::options_description getDescription() override;
69
73 virtual std::string getMethod() const override { return "Python"; }
74
75 std::string m_framework = "sklearn";
76 std::string m_steering_file = "";
77 std::string m_config = "null";
78 unsigned int m_mini_batch_size = 0;
79 unsigned int m_nIterations = 1;
80 double m_training_fraction = 1.0;
81 bool m_normalize = false;
82 };
83
84
88 class PythonTeacher : public Teacher {
89
90 public:
96 PythonTeacher(const GeneralOptions& general_options, const PythonOptions& specific_options);
97
102 virtual Weightfile train(Dataset& training_data) const override;
103
104 private:
106 };
107
111 class PythonExpert : public MVA::Expert {
112
113 public:
117 PythonExpert();
118
123 virtual void load(Weightfile& weightfile) override;
124
129 virtual std::vector<float> apply(Dataset& test_data) const override;
130
135 virtual std::vector<std::vector<float>> applyMulticlass(Dataset& test_data) const override;
136
137 protected:
139 boost::python::object m_unique_mva_module;
140 boost::python::object m_state;
141 std::vector<float> m_means;
142 std::vector<float> m_stds;
143 };
144
145 }
147}
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition Dataset.h:33
Abstract base class of all Expert Each MVA library has its own implementation of this class,...
Definition Expert.h:31
General options which are shared by all MVA trainings.
Definition Options.h:62
boost::python::object m_unique_mva_module
python module containing the mva methods
Definition Python.h:139
PythonExpert()
Constructs a new Python Expert.
Definition Python.cc:402
boost::python::object m_state
current state object of method
Definition Python.h:140
std::vector< float > m_stds
Stds of all features for normalization.
Definition Python.h:142
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
Definition Python.cc:462
PythonOptions m_specific_options
Method specific options.
Definition Python.h:138
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition Python.cc:408
std::vector< float > m_means
Means of all features for normalization.
Definition Python.h:141
virtual std::vector< std::vector< float > > applyMulticlass(Dataset &test_data) const override
Apply this expert onto a dataset for multiclass problem.
Definition Python.cc:503
Options for the Python MVA method.
Definition Python.h:50
unsigned int m_nIterations
Number of iterations through the whole data.
Definition Python.h:79
std::string m_steering_file
steering file provided by the user to override the functions in the framework
Definition Python.h:76
virtual std::string getMethod() const override
Return method name.
Definition Python.h:73
std::string m_framework
framework to use e.g.
Definition Python.h:75
std::string m_config
Config string in json, which is passed to the get model function.
Definition Python.h:77
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition Python.cc:80
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition Python.h:81
double m_training_fraction
Fraction of data passed as training data, rest is passed as test data.
Definition Python.h:80
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition Python.cc:47
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition Python.cc:68
unsigned int m_mini_batch_size
Mini batch size, 0 passes the whole data in one call.
Definition Python.h:78
PythonTeacher(const GeneralOptions &general_options, const PythonOptions &specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
Definition Python.cc:166
PythonOptions m_specific_options
Method specific options.
Definition Python.h:105
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition Python.cc:173
Specific Options, all method Options have to inherit from this class.
Definition Options.h:98
Teacher(const GeneralOptions &general_options)
Constructs a new teacher using the GeneralOptions for this training.
Definition Teacher.cc:18
The Weightfile class serializes all information about a training into an xml tree.
Definition Weightfile.h:38
Abstract base class for different kinds of events.