Belle II Software  light-2212-foldex
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 
17 #include <TRandom.h>
18 
24 #if !defined(__CLING__)
25 #include <Python.h>
26 #endif
27 
28 #if !defined(__GNUG__) || defined(__ICC)
29 #else
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
32 #pragma GCC diagnostic ignored "-Wunused-parameter"
33 #endif
34 #include <boost/python/object.hpp>
35 #include <boost/python/dict.hpp>
36 #include <boost/python/import.hpp>
37 #include <boost/python/extract.hpp>
38 #if !defined(__GNUG__) || defined(__ICC)
39 #else
40 #pragma GCC diagnostic pop
41 #endif
42 
43 
44 namespace Belle2 {
49  namespace MVA {
50 
54  class PythonOptions : public SpecificOptions {
55 
56  public:
61  virtual void load(const boost::property_tree::ptree& pt) override;
62 
67  virtual void save(boost::property_tree::ptree& pt) const override;
68 
72  virtual po::options_description getDescription() override;
73 
77  virtual std::string getMethod() const override { return "Python"; }
78 
79  std::string m_framework = "sklearn";
80  std::string m_steering_file = "";
81  std::string m_config = "null";
82  unsigned int m_mini_batch_size = 0;
83  unsigned int m_nIterations = 1;
84  double m_training_fraction = 1.0;
85  bool m_normalize = false;
86  };
87 
88 
92  class PythonTeacher : public Teacher {
93 
94  public:
100  PythonTeacher(const GeneralOptions& general_options, const PythonOptions& specific_options);
101 
106  virtual Weightfile train(Dataset& training_data) const override;
107 
108  private:
111  //--- Structs to help simplify the process ------------------------------------------------------------------------
113  struct TRandomWrapper {
115  typedef unsigned int result_type;
116 
118  static constexpr result_type min() { return 0; }
119 
121  static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
122 
124  result_type operator()() { return gRandom->Integer(max()); }
125  };
126  };
127 
131  class PythonExpert : public MVA::Expert {
132 
133  public:
137  PythonExpert();
138 
143  virtual void load(Weightfile& weightfile) override;
144 
149  virtual std::vector<float> apply(Dataset& test_data) const override;
150 
155  virtual std::vector<std::vector<float>> applyMulticlass(Dataset& test_data) const override;
156 
157  protected:
159  boost::python::object m_framework;
160  boost::python::object m_state;
161  std::vector<float> m_means;
162  std::vector<float> m_stds;
163  };
164 
165  }
167 }
168 #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:131
PythonExpert()
Constructs a new Python Expert.
Definition: Python.cc:381
boost::python::object m_state
current state object of method
Definition: Python.h:160
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:162
boost::python::object m_framework
Framework module.
Definition: Python.h:159
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:158
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:161
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:54
unsigned int m_nIterations
Number of iterations through the whole data.
Definition: Python.h:83
std::string m_steering_file
steering file provided by the user to override the functions in the framework
Definition: Python.h:80
virtual std::string getMethod() const override
Return method name.
Definition: Python.h:77
std::string m_framework
framework to use e.g.
Definition: Python.h:79
std::string m_config
Config string in json, which is passed to the get model function.
Definition: Python.h:81
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: Python.cc:61
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:85
double m_training_fraction
Fraction of data passed as training data, rest is passed as test data.
Definition: Python.h:84
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Python.cc:28
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: Python.cc:49
unsigned int m_mini_batch_size
Mini batch size, 0 passes the whole data in one call.
Definition: Python.h:82
Teacher for the Python MVA method.
Definition: Python.h:92
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:150
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:109
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: Python.cc:158
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.
Definition: ClusterUtils.h:23
Wrap TRandom to be useable as a uniform random number generator with std algorithms like std::shuffle...
Definition: Python.h:113
static constexpr result_type max()
Maximum value returned by the random number generator.
Definition: Python.h:121
result_type operator()()
Return a random value in the range [min(), max()].
Definition: Python.h:124
static constexpr result_type min()
Minimum value returned by the random number generator.
Definition: Python.h:118
unsigned int result_type
Define the result type to be a normal unsigned int.
Definition: Python.h:115