Belle II Software  release-05-02-19
Python.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Keck *
7  * Jochen Gemmler *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 #pragma once
13 #ifndef INCLUDE_GUARD_BELLE2_MVA_THEANO_HEADER
14 #define INCLUDE_GUARD_BELLE2_MVA_THEANO_HEADER
15 
16 #include <mva/interface/Options.h>
17 #include <mva/interface/Teacher.h>
18 #include <mva/interface/Expert.h>
19 
25 #if !defined(__CLING__)
26 #include <Python.h>
27 #endif
28 
29 #if !defined(__GNUG__) || defined(__ICC)
30 #else
31 #pragma GCC diagnostic push
32 #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
33 #pragma GCC diagnostic ignored "-Wunused-parameter"
34 #endif
35 #include <boost/python/object.hpp>
36 #include <boost/python/dict.hpp>
37 #include <boost/python/import.hpp>
38 #include <boost/python/extract.hpp>
39 #if !defined(__GNUG__) || defined(__ICC)
40 #else
41 #pragma GCC diagnostic pop
42 #endif
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:
110  };
111 
115  class PythonExpert : public MVA::Expert {
116 
117  public:
121  PythonExpert();
122 
127  virtual void load(Weightfile& weightfile) override;
128 
133  virtual std::vector<float> apply(Dataset& test_data) const override;
134 
135  protected:
137  boost::python::object m_framework;
138  boost::python::object m_state;
139  std::vector<float> m_means;
140  std::vector<float> m_stds;
141  };
142 
143  }
145 }
146 #endif
Belle2::MVA::PythonTeacher::m_specific_options
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:109
Belle2::MVA::PythonOptions::save
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: Python.cc:51
Belle2::MVA::Dataset
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition: Dataset.h:34
Belle2::MVA::PythonExpert::load
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: Python.cc:374
Belle2::MVA::PythonOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Python.cc:30
Belle2::MVA::Weightfile
The Weightfile class serializes all information about a training into an xml tree.
Definition: Weightfile.h:40
Belle2::MVA::PythonTeacher::train
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: Python.cc:160
Belle2::MVA::PythonOptions::m_normalize
bool m_normalize
Normalize the inputs (shift mean to zero and std to 1)
Definition: Python.h:85
Belle2::MVA::SpecificOptions
Specific Options, all mehtod Options have to inherit from this class.
Definition: Options.h:99
Belle2::MVA::PythonExpert::m_framework
boost::python::object m_framework
Framework module.
Definition: Python.h:137
Belle2::MVA::PythonOptions::m_nIterations
unsigned int m_nIterations
Number of iterations trough the whole data.
Definition: Python.h:83
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::Teacher
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
Definition: Teacher.h:31
Belle2::MVA::PythonExpert
Expert for the Python MVA method.
Definition: Python.h:115
Belle2::MVA::PythonExpert::m_state
boost::python::object m_state
current state object of method
Definition: Python.h:138
Belle2::MVA::PythonExpert::m_stds
std::vector< float > m_stds
Stds of all features for normalization.
Definition: Python.h:140
Belle2::MVA::PythonOptions::m_steering_file
std::string m_steering_file
steering file provided by the user to override the functions in the framework
Definition: Python.h:80
Belle2::MVA::Expert
Abstract base class of all Expert Each MVA library has its own implementation of this class,...
Definition: Expert.h:33
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::MVA::PythonOptions::m_training_fraction
double m_training_fraction
Fraction of data passed as training data, rest is passed as test data.
Definition: Python.h:84
Belle2::MVA::PythonOptions::m_mini_batch_size
unsigned int m_mini_batch_size
Mini batch size, 0 passes the whole data in one call.
Definition: Python.h:82
Belle2::MVA::PythonOptions::getDescription
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: Python.cc:63
Belle2::MVA::PythonExpert::apply
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
Definition: Python.cc:412
Belle2::MVA::PythonOptions::getMethod
virtual std::string getMethod() const override
Return method name.
Definition: Python.h:77
Belle2::MVA::PythonExpert::m_specific_options
PythonOptions m_specific_options
Method specific options.
Definition: Python.h:136
Belle2::MVA::PythonExpert::PythonExpert
PythonExpert()
Constructs a new Python Expert.
Definition: Python.cc:368
Belle2::MVA::PythonOptions::m_framework
std::string m_framework
framework to use e.g.
Definition: Python.h:79
Belle2::MVA::PythonOptions
Options for the Python MVA method.
Definition: Python.h:54
Belle2::MVA::PythonExpert::m_means
std::vector< float > m_means
Means of all features for normalization.
Definition: Python.h:139
Belle2::MVA::PythonTeacher::PythonTeacher
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
Belle2::MVA::PythonTeacher
Teacher for the Python MVA method.
Definition: Python.h:92
Belle2::MVA::PythonOptions::m_config
std::string m_config
Config string in json, which is passed to the get model function.
Definition: Python.h:81