Belle II Software  release-05-01-25
test_Combination.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/Combination.h>
11 #include <mva/methods/Trivial.h>
12 #include <mva/interface/Interface.h>
13 #include <framework/utilities/TestHelpers.h>
14 
15 #include <gtest/gtest.h>
16 
17 using namespace Belle2;
18 
19 namespace {
20 
21  TEST(CombinationTest, CombinationOptions)
22  {
23 
24  MVA::CombinationOptions specific_options;
25 
26  EXPECT_EQ(specific_options.m_weightfiles.size(), 0);
27 
28  specific_options.m_weightfiles = {"A", "B"};
29 
30  boost::property_tree::ptree pt;
31  specific_options.save(pt);
32  EXPECT_FLOAT_EQ(pt.get<unsigned int>("Combination_number_of_weightfiles"), 2);
33  EXPECT_EQ(pt.get<std::string>("Combination_weightfile0"), "A");
34  EXPECT_EQ(pt.get<std::string>("Combination_weightfile1"), "B");
35 
36  MVA::CombinationOptions specific_options2;
37  specific_options2.load(pt);
38 
39  EXPECT_EQ(specific_options2.m_weightfiles.size(), 2);
40  EXPECT_EQ(specific_options2.m_weightfiles[0], "A");
41  EXPECT_EQ(specific_options2.m_weightfiles[1], "B");
42 
43  EXPECT_EQ(specific_options.getMethod(), std::string("Combination"));
44 
45  // Test if po::options_description is created without crashing
46  auto description = specific_options.getDescription();
47  EXPECT_EQ(description.options().size(), 1);
48 
49  // Check for B2ERROR and throw if version is wrong
50  // we try with version 100, surely we will never reach this!
51  pt.put("Combination_version", 100);
52  try {
53  EXPECT_B2ERROR(specific_options2.load(pt));
54  } catch (...) {
55 
56  }
57  EXPECT_THROW(specific_options2.load(pt), std::runtime_error);
58  }
59 
60  class TestDataset : public MVA::Dataset {
61  public:
62  explicit TestDataset(const std::vector<float>& data) : MVA::Dataset(MVA::GeneralOptions()), m_data(data)
63  {
64  m_input = {0.0};
65  m_target = 0.0;
66  m_isSignal = false;
67  m_weight = 1.0;
68  }
69 
70  [[nodiscard]] unsigned int getNumberOfFeatures() const override { return 1; }
71  [[nodiscard]] unsigned int getNumberOfSpectators() const override { return 0; }
72  [[nodiscard]] unsigned int getNumberOfEvents() const override { return m_data.size(); }
73  void loadEvent(unsigned int iEvent) override { m_input[0] = m_data[iEvent]; m_target = iEvent % 2; m_isSignal = m_target == 1; };
74  float getSignalFraction() override { return 0.1; };
75  std::vector<float> getFeature(unsigned int) override { return m_data; }
76 
77  std::vector<float> m_data;
78 
79  };
80 
81 
82  TEST(CombinationTest, CombinationInterface)
83  {
85 
86  MVA::GeneralOptions general_options;
87  general_options.m_method = "Trivial";
88  TestDataset dataset({1.0, 1.0, 1.0, 1.0, 2.0, 3.0, 2.0, 3.0});
89 
91 
92  MVA::TrivialOptions trivial_options;
93  trivial_options.m_output = 0.1;
94  auto trivial_teacher1 = trivial.getTeacher(general_options, trivial_options);
95  auto trivial_weightfile1 = trivial_teacher1->train(dataset);
96  MVA::Weightfile::saveToXMLFile(trivial_weightfile1, "weightfile1.xml");
97 
98  trivial_options.m_output = 0.6;
99  auto trivial_teacher2 = trivial.getTeacher(general_options, trivial_options);
100  auto trivial_weightfile2 = trivial_teacher2->train(dataset);
101  MVA::Weightfile::saveToXMLFile(trivial_weightfile2, "weightfile2.xml");
102 
104  general_options.m_method = "Combination";
105  MVA::CombinationOptions specific_options;
106  specific_options.m_weightfiles = {"weightfile1.xml", "weightfile2.xml"};
107  auto teacher = combination.getTeacher(general_options, specific_options);
108  auto weightfile = teacher->train(dataset);
109 
110  auto expert = combination.getExpert();
111  expert->load(weightfile);
112  auto probabilities = expert->apply(dataset);
113  EXPECT_EQ(probabilities.size(), dataset.getNumberOfEvents());
114  for (unsigned int i = 0; i < dataset.getNumberOfEvents(); ++i)
115  EXPECT_FLOAT_EQ(probabilities[i], (0.1 * 0.6) / (0.1 * 0.6 + (1 - 0.1) * (1 - 0.6)));
116 
117  // Error and throw runtime error if method does not exist
118  trivial_weightfile2.addElement("method", "DOESNOTEXIST");
119  MVA::Weightfile::saveToXMLFile(trivial_weightfile2, "weightfile2.xml");
120 
121  auto weightfile2 = teacher->train(dataset);
122  try {
123  EXPECT_B2ERROR(expert->load(weightfile2));
124  } catch (...) {
125 
126  }
127  EXPECT_THROW(expert->load(weightfile2), std::runtime_error);
128 
129 
130  }
131 
132 }
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::TestHelpers::TempDirCreator
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:57
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::CombinationOptions
Options for the Combination MVA method.
Definition: Combination.h:30
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::MVA::Weightfile::saveToXMLFile
static void saveToXMLFile(Weightfile &weightfile, const std::string &filename)
Static function which saves a Weightfile to a XML file.
Definition: Weightfile.cc:184
Belle2::TEST
TEST(TestgetDetectorRegion, TestgetDetectorRegion)
Test Constructors.
Definition: utilityFunctions.cc:18
Belle2::MVA::CombinationOptions::m_weightfiles
std::vector< std::string > m_weightfiles
Weightfiles of all methods we want to combine.
Definition: Combination.h:55
Belle2::MVA::CombinationOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: Combination.cc:31
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