10#include <mva/interface/Dataset.h>
11#include <mva/interface/Expert.h>
12#include <mva/interface/Options.h>
13#include <mva/interface/Teacher.h>
14#include <mva/interface/Weightfile.h>
16#include <framework/logging/Logger.h>
18#include <boost/property_tree/ptree_fwd.hpp>
19#include <boost/program_options.hpp>
33 template<
class BaseClassifierOptions>
37 void load(
const boost::property_tree::ptree& pt)
override;
40 void save(boost::property_tree::ptree& pt)
const override;
92 void loadEvent(
unsigned int iEvent)
override;
95 std::vector<float>
getFeature(
unsigned int iFeature)
override;
98 std::vector<float>
getSpectator(
unsigned int iSpectator)
override;
138 template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
143 const RegressionClassifierOptions& specific_options);
178 template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
185 std::vector<float>
apply(
Dataset& test_data)
const override;
196template<
class BaseClassifierOptions>
199 m_numberOfBins = pt.get<
unsigned int>(getMethod() +
"_numberOfBins");
202template<
class BaseClassifierOptions>
205 return m_numberOfBins;
208template<
class BaseClassifierOptions>
211 m_numberOfBins = maximalBinNumber;
214template<
class BaseClassifierOptions>
217 return m_baseClassifierOptions;
220template<
class BaseClassifierOptions>
223 return m_baseClassifierOptions;
226template<
class BaseClassifierOptions>
229 pt.put(getMethod() +
"_numberOfBins", m_numberOfBins);
232template<
class BaseClassifierOptions>
235 auto description = getBaseClassifierOptions().getDescription();
237 description.add_options()
238 (
"numberOfBins", po::value<unsigned int>(&m_numberOfBins),
239 "Number of bins to split the target variable into");
244template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
252 for (
unsigned int binNumber = 1; binNumber < m_specific_options.getMaximalBinNumber(); binNumber++) {
254 1.0 / m_specific_options.getMaximalBinNumber() * binNumber);
255 Weightfile specificWeightFile = m_baseClassifierTeacher.train(specificDataSet);
258 Weightfile::saveToXMLFile(specificWeightFile, file);
259 weightfile.
addFile(
"BaseClassifier_WeightFile_" + std::to_string(binNumber), file);
264template<
class BaseClassifierTeacher,
class RegressionClassifierOptions>
267 const RegressionClassifierOptions& specific_options) :
268 Teacher(general_options), m_specific_options(specific_options),
269 m_baseClassifierTeacher(general_options, specific_options.getBaseClassifierOptions())
273template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
276 RegressionClassifierOptions specific_options;
279 m_baseClassifierExperts.resize(specific_options.getMaximalBinNumber());
280 for (
unsigned int binNumber = 1; binNumber < specific_options.getMaximalBinNumber(); binNumber++) {
282 weightfile.
getFile(
"BaseClassifier_WeightFile_" + std::to_string(binNumber), file);
285 m_baseClassifierExperts[binNumber].load(specificWeightFile);
289template<
class BaseClassifierExpert,
class RegressionClassifierOptions>
293 std::vector<float> sum;
294 for (
const auto& expert : m_baseClassifierExperts) {
297 sum = expert.apply(test_data);
301 const auto& expertResult = expert.apply(test_data);
302 if (sum.size() != expertResult.size()) {
303 B2FATAL(
"There is one expert in the list that returned not the same number of results than the others!");
306 for (
unsigned int index = 0; index < sum.size(); index++) {
307 sum[index] += expertResult[index];
311 for (
unsigned int index = 0; index < sum.size(); index++) {
312 sum[index] /= m_baseClassifierExperts.size();
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Abstract base class of all Expert Each MVA library has its own implementation of this class,...
General options which are shared by all MVA trainings.
Dataset needed during the training of a regression method.
std::vector< float > getTargets() override
Return the targets from the real dataset.
unsigned int getNumberOfEvents() const override
Return the number of events from the real dataset.
std::vector< float > getSpectator(unsigned int iSpectator) override
Return a specific spectator from the real dataset.
unsigned int getNumberOfSpectators() const override
Return the number of spectators from the real dataset.
unsigned int getNumberOfFeatures() const override
Return the number of features from the real dataset.
std::vector< float > getFeature(unsigned int iFeature) override
Return a specific feature from the real dataset.
std::vector< float > getWeights() override
Return the weights from the real dataset.
void loadEvent(unsigned int iEvent) override
Load an event. Sets all internal variables and sets the isSignal variable dependent on the cut value.
Dataset * m_childDataSet
The real data set (our child)
double m_cutValue
The cut value.
Generic expert for the regression applications.
std::vector< float > apply(Dataset &test_data) const override
Apply the loaded experts by averaging over the single expert decisions.
std::vector< BaseClassifierExpert > m_baseClassifierExperts
The list of single experts.
void load(Weightfile &weightfile) override
Load the expert from the weightfile by loading each stored single classifier one ofter the other.
Generic options of the Regression MVA methods hosting the number of bins (and the base classifier opt...
BaseClassifierOptions m_baseClassifierOptions
Options of the base classifier.
const BaseClassifierOptions & getBaseClassifierOptions() const
Return options of the base classifier (const version)
unsigned int getMaximalBinNumber() const
Return the number of bins to use.
BaseClassifierOptions & getBaseClassifierOptions()
Return options of the base classifier (non-const version)
unsigned int m_numberOfBins
How many bins to use.
po::options_description getDescription() override
Get the descriptions for these options.
void load(const boost::property_tree::ptree &pt) override
Load the options from a boost property tree. Only loads the maximal bin number.
void setMaximalBinNumber(unsigned int maximalBinNumber)
Set the number of bins to use.
void save(boost::property_tree::ptree &pt) const override
Save the options from a boost property tree. Only saves the maximal bin number.
Core class for the training of regression methods based on binary classifiers.
BaseClassifierTeacher m_baseClassifierTeacher
The teacher to use for training.
RegressionClassifierOptions m_specific_options
The method specific options.
RegressionTeacher(const GeneralOptions &general_options, const RegressionClassifierOptions &specific_options)
Create a new teacher out of the general and the regression method specific options.
Weightfile train(Dataset &training_data) const override
Call the train function.
Specific Options, all method Options have to inherit from this class.
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
The Weightfile class serializes all information about a training into an xml tree.
void addFile(const std::string &identifier, const std::string &custom_weightfile)
Add a file (mostly a weightfile from a MVA library) to our Weightfile.
static Weightfile loadFromXMLFile(const std::string &filename)
Static function which loads a Weightfile from a XML file.
void addOptions(const Options &options)
Add an Option object to the xml tree.
void getOptions(Options &options) const
Fills an Option object from the xml tree.
std::string generateFileName(const std::string &suffix="")
Returns a temporary filename with the given suffix.
void getFile(const std::string &identifier, const std::string &custom_weightfile)
Creates a file from our weightfile (mostly this will be a weightfile of an MVA library)
Abstract base class for different kinds of events.