34 :
TMVA::
MethodBase(jobName,
Types::kCrossValidation, methodTitle, theData, theOption), fSplitExpr(
nullptr)
55 DeclareOptionRef(fEncapsulatedMethodName,
"EncapsulatedMethodName",
"");
56 DeclareOptionRef(fEncapsulatedMethodTypeName,
"EncapsulatedMethodTypeName",
"");
57 DeclareOptionRef(fNumFolds,
"NumFolds",
"Number of folds to generate");
58 DeclareOptionRef(fOutputEnsembling =
TString(
"None"),
"OutputEnsembling",
59 "Combines output from contained methods. If None, no combination is performed. (default None)");
62 DeclareOptionRef(fSplitExprString,
"SplitExpr",
"The expression used to assign events to folds");
78 Log() << kDEBUG <<
"ProcessOptions -- fNumFolds: " << fNumFolds <<
Endl;
79 Log() << kDEBUG <<
"ProcessOptions -- fEncapsulatedMethodName: " << fEncapsulatedMethodName <<
Endl;
80 Log() << kDEBUG <<
"ProcessOptions -- fEncapsulatedMethodTypeName: " << fEncapsulatedMethodTypeName <<
Endl;
82 if (fSplitExprString !=
TString(
"")) {
83 fSplitExpr = std::unique_ptr<CvSplitKFoldsExpr>(
new CvSplitKFoldsExpr(DataInfo(), fSplitExprString));
86 for (
UInt_t iFold = 0; iFold < fNumFolds; ++iFold) {
87 TString weightfile = GetWeightFileNameForFold(iFold);
89 Log() << kINFO <<
"Reading weightfile: " << weightfile <<
Endl;
90 MethodBase *fold_method = InstantiateMethodFromXML(fEncapsulatedMethodTypeName, weightfile);
91 fEncapsulatedMethods.push_back(fold_method);
100 fMulticlassValues = std::vector<Float_t>(DataInfo().GetNClasses());
101 fRegressionValues = std::vector<Float_t>(DataInfo().GetNTargets());
115 if (iFold >= fNumFolds) {
116 Log() << kFATAL << iFold <<
" out of range. "
117 <<
"Should be < " << fNumFolds <<
"." <<
Endl;
122 TString weightfile = fileDir +
"/" + fJobName +
"_" + fEncapsulatedMethodName +
"_" + foldStr +
".weights.xml";
159 Log() << kFATAL <<
"MethodCategory not supported for the moment." <<
Endl;
163 m->SetWeightFileDir(fileDir);
166 m->SetAnalysisType(fAnalysisType);
168 m->ReadStateFromFile();
184 gTools().
AddAttr(wght,
"EncapsulatedMethodName", fEncapsulatedMethodName);
185 gTools().
AddAttr(wght,
"EncapsulatedMethodTypeName", fEncapsulatedMethodTypeName);
186 gTools().
AddAttr(wght,
"OutputEnsembling", fOutputEnsembling);
188 for (
UInt_t iFold = 0; iFold < fNumFolds; ++iFold) {
189 TString weightfile = GetWeightFileNameForFold(iFold);
212 gTools().
ReadAttr(parent,
"EncapsulatedMethodName", fEncapsulatedMethodName);
213 gTools().
ReadAttr(parent,
"EncapsulatedMethodTypeName", fEncapsulatedMethodTypeName);
217 for (
UInt_t iFold = 0; iFold < fNumFolds; ++iFold) {
218 TString weightfile = GetWeightFileNameForFold(iFold);
220 Log() << kINFO <<
"Reading weightfile: " << weightfile <<
Endl;
221 MethodBase *fold_method = InstantiateMethodFromXML(fEncapsulatedMethodTypeName, weightfile);
222 fEncapsulatedMethods.push_back(fold_method);
226 if (fSplitExprString !=
TString(
"")) {
227 fSplitExpr = std::unique_ptr<CvSplitKFoldsExpr>(
new CvSplitKFoldsExpr(DataInfo(), fSplitExprString));
229 Log() << kFATAL <<
"MethodCrossValidation supports XML reading only for deterministic splitting !" <<
Endl;
239 Log() << kFATAL <<
"CrossValidation currently supports only reading from XML." <<
Endl;
247 const Event *ev = GetEvent();
249 if (fOutputEnsembling ==
"None") {
250 if (fSplitExpr !=
nullptr) {
252 UInt_t iFold = fSplitExpr->Eval(fNumFolds, ev);
253 return fEncapsulatedMethods.at(iFold)->GetMvaValue(err, errUpper);
256 UInt_t iFold = fEventToFoldMapping.at(Data()->GetEvent());
257 return fEncapsulatedMethods.at(iFold)->GetMvaValue(err, errUpper);
259 }
else if (fOutputEnsembling ==
"Avg") {
261 for (
auto &
m : fEncapsulatedMethods) {
262 val +=
m->GetMvaValue(err, errUpper);
264 return val / fEncapsulatedMethods.size();
266 Log() << kFATAL <<
"Ensembling type " << fOutputEnsembling <<
" unknown" <<
Endl;
276 const Event *ev = GetEvent();
278 if (fOutputEnsembling ==
"None") {
279 if (fSplitExpr !=
nullptr) {
281 UInt_t iFold = fSplitExpr->Eval(fNumFolds, ev);
282 return fEncapsulatedMethods.at(iFold)->GetMulticlassValues();
285 UInt_t iFold = fEventToFoldMapping.at(Data()->GetEvent());
286 return fEncapsulatedMethods.at(iFold)->GetMulticlassValues();
288 }
else if (fOutputEnsembling ==
"Avg") {
290 for (
auto &
e : fMulticlassValues) {
294 for (
auto &
m : fEncapsulatedMethods) {
295 auto methodValues =
m->GetMulticlassValues();
296 for (
size_t i = 0; i < methodValues.size(); ++i) {
297 fMulticlassValues[i] += methodValues[i];
301 for (
auto &
e : fMulticlassValues) {
302 e /= fEncapsulatedMethods.size();
305 return fMulticlassValues;
308 Log() << kFATAL <<
"Ensembling type " << fOutputEnsembling <<
" unknown" <<
Endl;
309 return fMulticlassValues;
318 const Event *ev = GetEvent();
320 if (fOutputEnsembling ==
"None") {
321 if (fSplitExpr !=
nullptr) {
323 UInt_t iFold = fSplitExpr->Eval(fNumFolds, ev);
324 return fEncapsulatedMethods.at(iFold)->GetRegressionValues();
327 UInt_t iFold = fEventToFoldMapping.at(Data()->GetEvent());
328 return fEncapsulatedMethods.at(iFold)->GetRegressionValues();
330 }
else if (fOutputEnsembling ==
"Avg") {
332 for (
auto &
e : fRegressionValues) {
336 for (
auto &
m : fEncapsulatedMethods) {
337 auto methodValues =
m->GetRegressionValues();
338 for (
size_t i = 0; i < methodValues.size(); ++i) {
339 fRegressionValues[i] += methodValues[i];
343 for (
auto &
e : fRegressionValues) {
344 e /= fEncapsulatedMethods.size();
347 return fRegressionValues;
350 Log() << kFATAL <<
"Ensembling type " << fOutputEnsembling <<
" unknown" <<
Endl;
351 return fRegressionValues;
372 <<
"Method CrossValidation should not be created manually,"
373 " only as part of using TMVA::Reader."
401 Log() << kWARNING <<
"MakeClassSpecific not implemented for CrossValidation" <<
Endl;
409 Log() << kWARNING <<
"MakeClassSpecificHeader not implemented for CrossValidation" <<
Endl;
#define REGISTER_METHOD(CLASS)
for example
R__EXTERN TSystem * gSystem
IMethod * Create(const std::string &name, const TString &job, const TString &title, DataSetInfo &dsi, const TString &option)
creates the method if needed based on the method name using the creator function the factory has stor...
static ClassifierFactory & Instance()
access to the ClassifierFactory singleton creates the instance if needed
Class that contains all the data information.
Virtual base Class for all MVA method.
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
friend class MethodCrossValidation
void Train(void) override
Call the Optimizer with the set of parameters and ranges that are meant to be tuned.
const Ranking * CreateRanking() override
void WriteMonitoringHistosToFile(void) const override
write special monitoring histograms to file dummy implementation here --------------—
void GetHelpMessage() const override
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) override
void AddWeightsXMLTo(void *parent) const override
Write weights to XML.
void DeclareCompatibilityOptions() override
Options that are used ONLY for the READER to ensure backward compatibility.
void Init(void) override
Common initialisation with defaults for the Method.
void MakeClassSpecific(std::ostream &, const TString &) const override
Make ROOT-independent C++ class for classifier response (classifier-specific implementation).
TString GetWeightFileNameForFold(UInt_t iFold) const
Returns filename of weight file for a given fold.
const std::vector< Float_t > & GetMulticlassValues() override
Get the multiclass MVA response.
MethodBase * InstantiateMethodFromXML(TString methodTypeName, TString weightfile) const
Reads in a weight file an instantiates the corresponding method.
void ReadWeightsFromStream(std::istream &istr) override
Read the weights.
void ProcessOptions() override
The option string is decoded, for available options see "DeclareOptions".
Double_t GetMvaValue(Double_t *err=nullptr, Double_t *errUpper=nullptr) override
void Reset(void) override
Reset the method, as if it had just been instantiated (forget all training etc.).
const std::vector< Float_t > & GetRegressionValues() override
Get the regression value generated by the containing methods.
void ReadWeightsFromXML(void *parent) override
Reads from the xml file.
void MakeClassSpecificHeader(std::ostream &, const TString &) const override
Specific class header.
virtual ~MethodCrossValidation(void)
Destructor.
void DeclareOptions() override
Ranking for variables in method (implementation)
Singleton class for Global types used by TMVA.
const char * Data() const
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
create variable transformations
MsgLogger & Endl(MsgLogger &ml)