Belle II Software  release-05-01-25
test_Trivial.cc
1 /* BASF2 (Belle Analysis Framework 2) *
2  * Copyright(C) 2016 - Belle II Collaboration *
3  * *
4  * Author: The Belle II Collaboration *
5  * Contributors: Thomas Keck *
6  * *
7  * This software is provided "as is" without any warranty. *
8  **************************************************************************/
9 
10 #include <mva/methods/Trivial.h>
11 #include <mva/interface/Interface.h>
12 #include <framework/utilities/FileSystem.h>
13 #include <framework/utilities/TestHelpers.h>
14 
15 #include <gtest/gtest.h>
16 
17 using namespace Belle2;
18 
19 namespace {
20 
21  TEST(TrivialTest, TrivialOptions)
22  {
23 
24  MVA::TrivialOptions specific_options;
25 
26  EXPECT_EQ(specific_options.m_output, 0.5);
27 
28  specific_options.m_output = 0.1;
29 
30  boost::property_tree::ptree pt;
31  specific_options.save(pt);
32  EXPECT_FLOAT_EQ(pt.get<double>("Trivial_output"), 0.1);
33 
34  MVA::TrivialOptions specific_options2;
35  specific_options2.load(pt);
36 
37  EXPECT_EQ(specific_options2.m_output, 0.1);
38 
39  EXPECT_EQ(specific_options.getMethod(), std::string("Trivial"));
40 
41  // Test if po::options_description is created without crashing
42  auto description = specific_options.getDescription();
43  EXPECT_EQ(description.options().size(), 1);
44 
45  // Check for B2ERROR and throw if version is wrong
46  // we try with version 100, surely we will never reach this!
47  pt.put("Trivial_version", 100);
48  try {
49  EXPECT_B2ERROR(specific_options2.load(pt));
50  } catch (...) {
51 
52  }
53  EXPECT_THROW(specific_options2.load(pt), std::runtime_error);
54  }
55 
56  class TestDataset : public MVA::Dataset {
57  public:
58  explicit TestDataset(const std::vector<float>& data) : MVA::Dataset(MVA::GeneralOptions()), m_data(data)
59  {
60  m_input = {0.0};
61  m_target = 0.0;
62  m_isSignal = false;
63  m_weight = 1.0;
64  }
65 
66  [[nodiscard]] unsigned int getNumberOfFeatures() const override { return 1; }
67  [[nodiscard]] unsigned int getNumberOfSpectators() const override { return 0; }
68  [[nodiscard]] unsigned int getNumberOfEvents() const override { return m_data.size(); }
69  void loadEvent(unsigned int iEvent) override { m_input[0] = m_data[iEvent]; m_target = iEvent % 2; m_isSignal = m_target == 1; };
70  float getSignalFraction() override { return 0.1; };
71  std::vector<float> getFeature(unsigned int) override { return m_data; }
72 
73  std::vector<float> m_data;
74 
75  };
76 
77 
78  TEST(TrivialTest, TrivialInterface)
79  {
81 
82  MVA::GeneralOptions general_options;
83  MVA::TrivialOptions specific_options;
84  TestDataset dataset({1.0, 1.0, 1.0, 1.0, 2.0, 3.0, 2.0, 3.0});
85 
86  auto teacher = interface.getTeacher(general_options, specific_options);
87  auto weightfile = teacher->train(dataset);
88 
89  auto expert = interface.getExpert();
90  expert->load(weightfile);
91  auto probabilities = expert->apply(dataset);
92  EXPECT_EQ(probabilities.size(), dataset.getNumberOfEvents());
93  for (unsigned int i = 0; i < dataset.getNumberOfEvents(); ++i)
94  EXPECT_FLOAT_EQ(probabilities[i], 0.5);
95 
96  }
97 
98 }
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::Interface::getTeacher
virtual std::unique_ptr< Teacher > getTeacher(const GeneralOptions &general_options, const SpecificOptions &specific_options) const override
Get Teacher of this MVA library.
Definition: Interface.h:119
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::TrivialOptions
Options for the Trivial MVA method.
Definition: Trivial.h:30
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::MVA::TrivialOptions::m_output
double m_output
Output of the trivial method.
Definition: Trivial.h:55
Belle2::MVA::TrivialOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Trivial.cc:21
Belle2::MVA::Interface::getExpert
virtual std::unique_ptr< MVA::Expert > getExpert() const override
Get Exoert of this MVA library.
Definition: Interface.h:128
Belle2::MVA::Interface
Template class to easily construct a interface for an MVA library using a library-specific Options,...
Definition: Interface.h:101