Belle II Software  release-08-01-10
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 
42 namespace Belle2 {
47  namespace MVA {
48 
52  class PythonOptions : public SpecificOptions {
53 
54  public:
59  virtual void load(const boost::property_tree::ptree& pt) override;
60 
65  virtual void save(boost::property_tree::ptree& pt) const override;
66 
70  virtual po::options_description getDescription() override;
71 
75  virtual std::string getMethod() const override { return "Python"; }
76 
77  std::string m_framework = "sklearn";
78  std::string m_steering_file = "";
79  std::string m_config = "null";
80  unsigned int m_mini_batch_size = 0;
81  unsigned int m_nIterations = 1;
82  double m_training_fraction = 1.0;
83  bool m_normalize = false;
84  };
85 
86 
90  class PythonTeacher : public Teacher {
91 
92  public:
98  PythonTeacher(const GeneralOptions& general_options, const PythonOptions& specific_options);
99 
104  virtual Weightfile train(Dataset& training_data) const override;
105 
106  private:
108  };
109 
113  class PythonExpert : public MVA::Expert {
114 
115  public:
119  PythonExpert();
120 
125  virtual void load(Weightfile& weightfile) override;
126 
131  virtual std::vector<float> apply(Dataset& test_data) const override;
132 
137  virtual std::vector<std::vector<float>> applyMulticlass(Dataset& test_data) const override;
138 
139  protected:
141  boost::python::object m_framework;
142  boost::python::object m_state;
143  std::vector<float> m_means;
144  std::vector<float> m_stds;
145  };
146 
147  }
149 }
150 #endif
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
Expert for the Python MVA method.
Definition: Python.h:113
PythonExpert()
Constructs a new Python Expert.
Definition: Python.cc:381
boost::python::object m_state
current state object of method
Definition: Python.h:142
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:144
boost::python::object m_framework
Framework module.
Definition: Python.h:141
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
Definition: Python.cc:425
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:140
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: Python.cc:387
std::vector< float > m_means
Means of all features for normalization.
Definition: Python.h:143
virtual std::vector< std::vector< float > > applyMulticlass(Dataset &test_data) const override
Apply this expert onto a dataset for multiclass problem.
Definition: Python.cc:466
Options for the Python MVA method.
Definition: Python.h:52
unsigned int m_nIterations
Number of iterations through the whole data.
Definition: Python.h:81
std::string m_steering_file
steering file provided by the user to override the functions in the framework
Definition: Python.h:78
virtual std::string getMethod() const override
Return method name.
Definition: Python.h:75
std::string m_framework
framework to use e.g.
Definition: Python.h:77
std::string m_config
Config string in json, which is passed to the get model function.
Definition: Python.h:79
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: Python.cc:63
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:83
double m_training_fraction
Fraction of data passed as training data, rest is passed as test data.
Definition: Python.h:82
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Python.cc:30
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: Python.cc:51
unsigned int m_mini_batch_size
Mini batch size, 0 passes the whole data in one call.
Definition: Python.h:80
Teacher for the Python MVA method.
Definition: Python.h:90
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:152
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:107
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: Python.cc:160
Specific Options, all method Options have to inherit from this class.
Definition: Options.h:98
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.