Belle II Software  release-05-01-25
test_PDF.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/PDF.h>
11 #include <mva/interface/Interface.h>
12 #include <framework/utilities/TestHelpers.h>
13 
14 #include <gtest/gtest.h>
15 
16 using namespace Belle2;
17 
18 namespace {
19 
20  TEST(PDFTest, PDFOptions)
21  {
22 
23  MVA::PDFOptions specific_options;
24 
25  EXPECT_EQ(specific_options.m_mode, "probability");
26  EXPECT_EQ(specific_options.m_binning, "frequency");
27  EXPECT_EQ(specific_options.m_nBins, 100);
28 
29  specific_options.m_mode = "mode";
30  specific_options.m_binning = "binning";
31  specific_options.m_nBins = 3;
32 
33  boost::property_tree::ptree pt;
34  specific_options.save(pt);
35  EXPECT_EQ(pt.get<std::string>("PDF_mode"), "mode");
36  EXPECT_EQ(pt.get<std::string>("PDF_binning"), "binning");
37  EXPECT_EQ(pt.get<unsigned int>("PDF_nBins"), 3);
38 
39 
40  MVA::PDFOptions specific_options2;
41  specific_options2.load(pt);
42 
43  EXPECT_EQ(specific_options2.m_mode, "mode");
44  EXPECT_EQ(specific_options2.m_binning, "binning");
45  EXPECT_EQ(specific_options2.m_nBins, 3);
46 
47  EXPECT_EQ(specific_options.getMethod(), std::string("PDF"));
48 
49  // Test if po::options_description is created without crashing
50  auto description = specific_options.getDescription();
51  EXPECT_EQ(description.options().size(), 3);
52 
53  // Check for B2ERROR and throw if version is wrong
54  // we try with version 100, surely we will never reach this!
55  pt.put("PDF_version", 100);
56  try {
57  EXPECT_B2ERROR(specific_options2.load(pt));
58  } catch (...) {
59 
60  }
61  EXPECT_THROW(specific_options2.load(pt), std::runtime_error);
62  }
63 
64  class TestDataset : public MVA::Dataset {
65  public:
66  explicit TestDataset(const std::vector<float>& data) : MVA::Dataset(MVA::GeneralOptions()), m_data(data)
67  {
68  m_input = {0.0};
69  m_target = 0.0;
70  m_isSignal = false;
71  m_weight = 1.0;
72  }
73 
74  [[nodiscard]] unsigned int getNumberOfFeatures() const override { return 1; }
75  [[nodiscard]] unsigned int getNumberOfSpectators() const override { return 0; }
76  [[nodiscard]] unsigned int getNumberOfEvents() const override { return m_data.size(); }
77  void loadEvent(unsigned int iEvent) override { m_input[0] = m_data[iEvent]; m_target = iEvent % 2; m_isSignal = m_target == 1; };
78  float getSignalFraction() override { return 0.1; };
79  std::vector<float> getFeature(unsigned int) override { return m_data; }
80 
81  std::vector<float> m_data;
82 
83  };
84 
85 
86  TEST(PDFTest, PDFInterface)
87  {
89 
90  MVA::GeneralOptions general_options;
91  MVA::PDFOptions specific_options;
92  specific_options.m_nBins = 4;
93  TestDataset dataset({1.0, 1.0, 1.0, 1.0, 2.0, 3.0, 2.0, 3.0});
94 
95  auto teacher = interface.getTeacher(general_options, specific_options);
96  auto weightfile = teacher->train(dataset);
97 
98  auto expert = interface.getExpert();
99  expert->load(weightfile);
100  auto probabilities = expert->apply(dataset);
101  EXPECT_EQ(probabilities.size(), dataset.getNumberOfEvents());
102  EXPECT_FLOAT_EQ(probabilities[0], 0.5);
103  EXPECT_FLOAT_EQ(probabilities[1], 0.5);
104  EXPECT_FLOAT_EQ(probabilities[2], 0.5);
105  EXPECT_FLOAT_EQ(probabilities[3], 0.5);
106  EXPECT_FLOAT_EQ(probabilities[4], 0.0);
107  EXPECT_FLOAT_EQ(probabilities[5], 1.0);
108  EXPECT_FLOAT_EQ(probabilities[6], 0.0);
109  EXPECT_FLOAT_EQ(probabilities[7], 1.0);
110 
111  }
112 
113 }
Belle2::MVA::PDFOptions::m_nBins
unsigned int m_nBins
number of bins used to bin the data
Definition: PDF.h:58
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::MVA::PDFOptions::m_mode
std::string m_mode
mode which defines the final output e.g.
Definition: PDF.h:57
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::PDFOptions::m_binning
std::string m_binning
which type of binning is performed e.g.
Definition: PDF.h:56
Belle2::MVA::PDFOptions
Options for the PDF MVA method.
Definition: PDF.h:31
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::PDFOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism (used by Weightfile) to load Options from a xml tree.
Definition: PDF.cc:29
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