Belle II Software  release-06-02-00
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 #ifndef INCLUDE_GUARD_BELLE2_MVA_THEANO_HEADER
11 #define INCLUDE_GUARD_BELLE2_MVA_THEANO_HEADER
12 
13 #include <mva/interface/Options.h>
14 #include <mva/interface/Teacher.h>
15 #include <mva/interface/Expert.h>
16 
22 #if !defined(__CLING__)
23 #include <Python.h>
24 #endif
25 
26 #if !defined(__GNUG__) || defined(__ICC)
27 #else
28 #pragma GCC diagnostic push
29 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
30 #pragma GCC diagnostic ignored "-Wunused-parameter"
31 #endif
32 #include <boost/python/object.hpp>
33 #include <boost/python/dict.hpp>
34 #include <boost/python/import.hpp>
35 #include <boost/python/extract.hpp>
36 #if !defined(__GNUG__) || defined(__ICC)
37 #else
38 #pragma GCC diagnostic pop
39 #endif
40 
41 namespace Belle2 {
46  namespace MVA {
47 
51  class PythonOptions : public SpecificOptions {
52 
53  public:
58  virtual void load(const boost::property_tree::ptree& pt) override;
59 
64  virtual void save(boost::property_tree::ptree& pt) const override;
65 
69  virtual po::options_description getDescription() override;
70 
74  virtual std::string getMethod() const override { return "Python"; }
75 
76  std::string m_framework = "sklearn";
77  std::string m_steering_file = "";
78  std::string m_config = "null";
79  unsigned int m_mini_batch_size = 0;
80  unsigned int m_nIterations = 1;
81  double m_training_fraction = 1.0;
82  bool m_normalize = false;
83  };
84 
85 
89  class PythonTeacher : public Teacher {
90 
91  public:
97  PythonTeacher(const GeneralOptions& general_options, const PythonOptions& specific_options);
98 
103  virtual Weightfile train(Dataset& training_data) const override;
104 
105  private:
107  };
108 
112  class PythonExpert : public MVA::Expert {
113 
114  public:
118  PythonExpert();
119 
124  virtual void load(Weightfile& weightfile) override;
125 
130  virtual std::vector<float> apply(Dataset& test_data) const override;
131 
132  protected:
134  boost::python::object m_framework;
135  boost::python::object m_state;
136  std::vector<float> m_means;
137  std::vector<float> m_stds;
138  };
139 
140  }
142 }
143 #endif
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition: Dataset.h:31
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
Expert for the Python MVA method.
Definition: Python.h:112
PythonExpert()
Constructs a new Python Expert.
Definition: Python.cc:365
boost::python::object m_state
current state object of method
Definition: Python.h:135
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:137
boost::python::object m_framework
Framework module.
Definition: Python.h:134
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
Definition: Python.cc:409
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:133
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: Python.cc:371
std::vector< float > m_means
Means of all features for normalization.
Definition: Python.h:136
Options for the Python MVA method.
Definition: Python.h:51
unsigned int m_nIterations
Number of iterations through the whole data.
Definition: Python.h:80
std::string m_steering_file
steering file provided by the user to override the functions in the framework
Definition: Python.h:77
virtual std::string getMethod() const override
Return method name.
Definition: Python.h:74
std::string m_framework
framework to use e.g.
Definition: Python.h:76
std::string m_config
Config string in json, which is passed to the get model function.
Definition: Python.h:78
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: Python.cc:60
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:82
double m_training_fraction
Fraction of data passed as training data, rest is passed as test data.
Definition: Python.h:81
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Python.cc:27
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: Python.cc:48
unsigned int m_mini_batch_size
Mini batch size, 0 passes the whole data in one call.
Definition: Python.h:79
Teacher for the Python MVA method.
Definition: Python.h:89
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:149
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:106
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: Python.cc:157
Specific Options, all method Options have to inherit from this class.
Definition: Options.h:97
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
Definition: Teacher.h:29
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.