9#include <mva/methods/PDF.h>
10#include <mva/interface/Interface.h>
11#include <framework/utilities/TestHelpers.h>
13#include <gtest/gtest.h>
19 TEST(PDFTest, PDFOptions)
24 EXPECT_EQ(specific_options.m_mode,
"probability");
25 EXPECT_EQ(specific_options.m_binning,
"frequency");
26 EXPECT_EQ(specific_options.m_nBins, 100);
28 specific_options.m_mode =
"mode";
29 specific_options.m_binning =
"binning";
30 specific_options.m_nBins = 3;
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);
40 specific_options2.
load(pt);
42 EXPECT_EQ(specific_options2.
m_mode,
"mode");
43 EXPECT_EQ(specific_options2.
m_binning,
"binning");
44 EXPECT_EQ(specific_options2.
m_nBins, 3);
46 EXPECT_EQ(specific_options.getMethod(), std::string(
"PDF"));
49 auto description = specific_options.getDescription();
50 EXPECT_EQ(description.options().size(), 3);
54 pt.put(
"PDF_version", 100);
56 EXPECT_B2ERROR(specific_options2.
load(pt));
60 EXPECT_THROW(specific_options2.
load(pt), std::runtime_error);
65 explicit TestDataset(
const std::vector<float>& data) : MVA::Dataset(MVA::GeneralOptions()), m_data(data)
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; };
78 std::vector<float>
getFeature(
unsigned int)
override {
return m_data; }
80 std::vector<float> m_data;
85 TEST(PDFTest, PDFInterface)
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});
94 auto teacher = interface.
getTeacher(general_options, specific_options);
95 auto weightfile = teacher->train(dataset);
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);
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
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>
virtual float getSignalFraction()
Returns the signal fraction of the whole sample.
General options which are shared by all MVA trainings.
Template class to easily construct a interface for an MVA library using a library-specific Options,...
virtual std::unique_ptr< MVA::Expert > getExpert() const override
Get Exoert of this MVA library.
virtual std::unique_ptr< Teacher > getTeacher(const GeneralOptions &general_options, const SpecificOptions &specific_options) const override
Get Teacher of this MVA library.
Options for the PDF MVA method.
unsigned int m_nBins
number of bins used to bin the data
std::string m_binning
which type of binning is performed e.g.
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism (used by Weightfile) to load Options from a xml tree.
std::string m_mode
mode which defines the final output e.g.
Abstract base class for different kinds of events.