forked from alisw/AliPhysics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliMLResponse.cxx
192 lines (162 loc) · 6.4 KB
/
AliMLResponse.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright CERN. This software is distributed under the terms of the GNU
// General Public License v3 (GPL Version 3).
//
// See http://www.gnu.org/licenses/ for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file AliMLResponse.cxx
/// \author [email protected], [email protected]
#include "AliMLResponse.h"
#include "yaml-cpp/yaml.h"
#include "AliLog.h"
#include "AliExternalBDT.h"
using std::map;
using std::pair;
using std::string;
using std::vector;
/// \cond CLASSIMP
ClassImp(AliMLResponse);
/// \endcond
//_______________________________________________________________________________
AliMLResponse::AliMLResponse()
: TNamed(), fConfigFilePath{}, fModels{}, fCentClasses{}, fBins{}, fVariableNames{}, fNBins{}, fNVariables{},
fBinsBegin{}, fRaw{} {
//
// Default constructor
//
}
//_______________________________________________________________________________
AliMLResponse::AliMLResponse(const Char_t *name, const Char_t *title)
: TNamed(name, title), fConfigFilePath{""}, fModels{}, fCentClasses{}, fBins{}, fVariableNames{}, fNBins{},
fNVariables{}, fBinsBegin{}, fRaw{} {
//
// Standard constructor
//
}
//_______________________________________________________________________________
AliMLResponse::~AliMLResponse() {
//
// Destructor
//
}
//_______________________________________________________________________________
AliMLResponse::AliMLResponse(const AliMLResponse &source)
: TNamed(source.GetName(), source.GetTitle()), fConfigFilePath{source.fConfigFilePath}, fModels{source.fModels},
fCentClasses{source.fCentClasses}, fBins{source.fBins}, fVariableNames{source.fVariableNames},
fNBins{source.fNBins}, fNVariables{source.fNVariables}, fBinsBegin{source.fBinsBegin}, fRaw{source.fRaw} {
//
// Copy constructor
//
}
AliMLResponse &AliMLResponse::operator=(const AliMLResponse &source) {
//
// assignment operator
//
if (&source == this) return *this;
TNamed::operator=(source);
fConfigFilePath = source.fConfigFilePath;
fModels = source.fModels;
fCentClasses = source.fCentClasses;
fBins = source.fBins;
fVariableNames = source.fVariableNames;
fNBins = source.fNBins;
fNVariables = source.fNVariables;
fBinsBegin = source.fBinsBegin;
fRaw = source.fRaw;
return *this;
}
//_______________________________________________________________________________
void AliMLResponse::CheckConfigFile(YAML::Node nodelist) {
/// error for empty config file
if (nodelist.IsNull()) {
AliFatal("Empty .yaml config file, please check it! Exit");
}
/// error for bin/model number inconsistencies
if ((nodelist["BINS"].as<vector<float>>().size() - 1) != nodelist["N_MODELS"].as<unsigned int>() ||
(nodelist["N_MODELS"].as<unsigned int>() != nodelist["MODELS"].size())) {
AliFatal("Inconsistency found in the number of bins/models, please check it! Exit");
}
/// error for variables/numberofvariable inconsistency
if (nodelist["NUM_VAR"].as<unsigned int>() != nodelist["VAR_NAMES"].size()) {
AliFatal("Inconsistency found in the number of variables, please check it! Exit");
}
return;
}
//_______________________________________________________________________________
void AliMLResponse::MLResponseInit() {
/// import config file from alien path
string configPath = AliMLModelHandler::ImportFile(fConfigFilePath);
YAML::Node nodeList;
/// manage wrong config file path
try {
nodeList = YAML::LoadFile(configPath);
} catch (std::exception &e) {
AliFatal(Form("Yaml-ccp error: %s! Exit", e.what()));
}
/// manage inconsistencies in config file
CheckConfigFile(nodeList);
fVariableNames = nodeList["VAR_NAMES"].as<vector<string>>();
fBins = nodeList["BINS"].as<vector<float>>();
fNBins = nodeList["N_MODELS"].as<int>();
fNVariables = nodeList["NUM_VAR"].as<int>();
fRaw = nodeList["RAW_SCORE"].as<bool>();
fBinsBegin = fBins.begin();
for (const auto &model : nodeList["MODELS"]) {
fModels.push_back(AliMLModelHandler{model});
}
for (auto &model : fModels) {
bool comp = model.CompileModel();
if (!comp) {
AliFatal("Error in model compilation! Exit");
}
}
}
//_______________________________________________________________________________
int AliMLResponse::FindBin(double binvar) {
vector<float>::iterator low;
low = std::lower_bound(fBins.begin(), fBins.end(), binvar);
return low - fBinsBegin;
}
//_______________________________________________________________________________
double AliMLResponse::Predict(double binvar, map<string, double> varmap) {
if ((int)varmap.size() < fNVariables) {
AliFatal("The variable map you provided to the predictor has a size smaller than the variable list size! Exit");
}
vector<double> features;
for (const auto &varname : fVariableNames) {
if (varmap.find(varname) == varmap.end()) {
AliFatal(Form("Variable |%s| not found in variable list provided in config! Exit", varname.data()));
}
features.push_back(varmap[varname]);
}
int bin = FindBin(binvar);
if (bin == 0 || bin == fNBins) {
AliWarning("Binned variable outside range, no model available!");
return -999.;
}
return fModels[bin - 1].GetModel()->Predict(&features[0], fNVariables, fRaw);
}
//________________________________________________________________
double AliMLResponse::Predict(double binvar, vector<double> variables) {
if ((int)variables.size() != fNVariables) {
AliFatal(Form("Number of variables passed (%d) different from the one used in the model (%d)! Exit", (int)variables.size(), fNVariables));
}
int bin = FindBin(binvar);
if (bin == 0 || bin == fNBins) {
AliWarning("Binned variable outside range, no model available!");
return -999.;
}
return fModels[bin - 1].GetModel()->Predict(&variables[0], fNVariables, fRaw);
}
//________________________________________________________________
bool AliMLResponse::IsSelected(double binvar, std::map<std::string, double> varmap) {
double score{0.};
return IsSelected(binvar, varmap, score);
}
//________________________________________________________________
bool AliMLResponse::IsSelected(double binvar, std::vector<double> variables) {
double score{0.};
return IsSelected(binvar, variables, score);
}