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