5 """ Convert XGBoost model to TTree to be used with MVAUtils. """
7 __author__ =
"Yuan-Tang Chou"
13 print(
"""cannot load xgboost. Try to install it with
20 logging.basicConfig(level=logging.DEBUG)
25 Adaptor from XGBoost dictionary to tree
26 * XGboost Yes is < and No is >=
36 if 'split_condition' in self:
37 return self[
'split_condition']
42 if 'children' not in self:
48 if 'children' not in self:
54 for idx, children
in enumerate(self[
'children']):
55 if children[
'nodeid'] == self[node_type]:
59 if 'children' not in self:
69 dump a single decision tree to arrays to be written into the TTree
79 split_features.append(node.get_split_feature())
80 split_values.append(node.get_value())
81 default_left.append(node.get_default_left())
84 if node.get_left()
is not None:
85 preorder(node.get_left())
87 if node.get_right()
is not None:
88 preorder(node.get_right())
91 return split_features, split_values, default_left
93 def dump2ROOT(model, output_filename, output_treename='xgboost'):
94 model.dump_model(
'dump_model.json', dump_format=
'json')
95 with open(
'dump_model.json',
'r')
as dump_json:
96 model_dump = dump_json.read()
97 trees = json.loads(model_dump)
98 fout = ROOT.TFile.Open(output_filename,
'recreate')
100 features_array = ROOT.std.vector(
'int')()
101 values_array = ROOT.std.vector(
'float')()
102 default_lefts_array = ROOT.std.vector(
'bool')()
104 title =
'creator=xgboost'
105 root_tree = ROOT.TTree(output_treename, title)
106 root_tree.Branch(
'vars',
'vector<int>', ROOT.AddressOf(features_array))
107 root_tree.Branch(
'values',
'vector<float>', ROOT.AddressOf(values_array))
108 root_tree.Branch(
'default_left',
'vector<bool>', ROOT.AddressOf(default_lefts_array))
110 logging.info(
"tree support nan: using XGBoost implementation")
113 tree_structure = tree
114 features, values, default_lefts =
dump_tree(tree_structure)
116 features_array.clear()
118 default_lefts_array.clear()
121 values_array.push_back(value)
122 for feature
in features:
123 features_array.push_back(feature)
124 for default_left
in default_lefts:
125 default_lefts_array.push_back(default_left)
131 return output_treename
135 Model: - a string, in this case, it is the name of the input file containing the xgboost model
136 you can get this model with xgboost with `bst.save_model('my_model.model')
137 - directly a xgboost booster object
139 if type(model)
is str:
141 bst.load_model(model)
142 return dump2ROOT(bst, output_filename, tree_name)
144 return dump2ROOT(model, output_filename, tree_name)
147 def test(model_file, tree_file, objective, tree_name='xgboost', ntests=10000, test_file=None):
149 bst.load_model(model_file)
150 f = ROOT.TFile.Open(tree_file)
151 tree = f.Get(tree_name)
153 _ = ROOT.MVAUtils.BDT
155 print(
"cannot import MVAUtils")
158 mva_utils = ROOT.MVAUtils.BDT(tree)
160 if 'binary' in objective:
161 logging.info(
"testing binary")
162 return test_binary(bst, mva_utils, objective, ntests, test_file)
163 elif 'multi' in objective:
164 logging.info(
"testing multi-class")
167 logging.info(
"testing regression")
172 logging.info(
"Tesing input features with regression")
174 if test_file
is not None:
175 data_input = np.load(test_file)
176 logging.info(
"using as input %s inputs from file %s", len(data_input), test_file)
178 logging.error(
"Please provide an input test file for testing")
181 dTest = xgb.DMatrix(data_input)
182 results_xgboost = booster.predict(dTest)
183 logging.info(
"xgboost (vectorized) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
185 input_values_vector = ROOT.std.vector(
"float")()
186 results_MVAUtils = []
188 for input_values
in data_input:
189 input_values_vector.clear()
190 for v
in input_values:
191 input_values_vector.push_back(v)
192 output_MVAUtils = mva_utils.GetResponse(input_values_vector)
193 results_MVAUtils.append(output_MVAUtils)
194 logging.info(
"mvautils (not vectorized+overhead) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
196 for input_values, output_xgb, output_MVAUtils
in zip(data_input, results_xgboost, results_MVAUtils):
197 if not np.allclose(output_xgb, output_MVAUtils, rtol=1E-4):
198 logging.info(
"output are different:"
201 "inputs: %s", output_MVAUtils, output_xgb, input_values)
206 def test_binary(booster, mva_utils, objective, ntests=10000, test_file=None):
208 logging.info(
"Testing input features with binary classification")
209 if test_file
is not None:
210 data_input = np.load(test_file)
211 logging.info(
"using as input %s inputs from file %s", len(data_input), test_file)
213 logging.error(
"Please provide an input test file for testing")
216 dTest = xgb.DMatrix(data_input)
217 results_xgboost = booster.predict(dTest)
218 logging.info(
"xgboost (vectorized) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
220 input_values_vector = ROOT.std.vector(
"float")()
221 results_MVAUtils = []
223 for input_values
in data_input:
224 input_values_vector.clear()
225 for v
in input_values:
226 input_values_vector.push_back(v)
227 output_MVAUtils = mva_utils.GetClassification(input_values_vector)
228 results_MVAUtils.append(output_MVAUtils)
229 logging.info(
"mvautils (not vectorized+overhead) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
231 for input_values, output_xgb, output_MVAUtils
in zip(data_input, results_xgboost, results_MVAUtils):
232 if not np.allclose(output_xgb, output_MVAUtils):
233 logging.info(
"output are different:"
236 "inputs: %s", output_MVAUtils, output_xgb, input_values)
242 logging.info(
"using multiclass model")
244 if test_file
is not None:
245 data_input = np.load(test_file)
246 logging.info(
"using as input %s inputs from file %s", len(data_input), test_file)
248 logging.error(
"Please provide an input test file for testing")
251 dTest = xgb.DMatrix(data_input)
252 results_xgboost = booster.predict(dTest)
254 nclasses = results_xgboost.shape[1]
255 logging.info(
"xgboost (vectorized) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
257 input_values_vector = ROOT.std.vector(
"float")()
258 results_MVAUtils = []
260 for input_values
in data_input:
261 input_values_vector.clear()
262 for v
in input_values:
263 input_values_vector.push_back(v)
264 output_MVAUtils = mva_utils.GetMultiResponse(input_values_vector, nclasses)
265 results_MVAUtils.append(output_MVAUtils)
267 logging.info(
"mvautils (not vectorized+overhead) timing = %s ms/input", (time.time() - start) * 1000 / len(data_input))
269 for input_values, output_xgb, output_MVAUtils
in zip(data_input, results_xgboost, results_MVAUtils):
270 if not np.allclose(output_xgb, output_MVAUtils):
271 logging.info(
"output are different:"
274 "inputs: %s", output_MVAUtils, output_xgb, input_values)
280 f = ROOT.TFile.Open(fn)
281 keys = f.GetListOfKeys()
284 logging.info(
"file %s is empty", fn)
286 tree = f.Get(keys[0].GetName())
287 if type(tree)
is not ROOT.TTree:
288 logging.info(
"cannot find TTree in file %s", fn)
290 if not tree.GetEntries():
291 logging.info(
"tree is empty")
296 if __name__ ==
"__main__":
299 parser = argparse.ArgumentParser(description=__doc__)
300 parser.add_argument(
'input', help=
'input xgboost model')
301 parser.add_argument(
'output', type=str, default =
'xgboost_model.root', help=
'Output file name, it must end with .root')
302 parser.add_argument(
'--tree-name', type=str, default =
'xgboost', help=
"tree name in Output root file")
303 parser.add_argument(
'--no-test', action=
'store_true', help=
"don't run test (not suggested)")
304 parser.add_argument(
'--ntests', type=int, default=1000, help=
"number of random test, default=1000")
305 parser.add_argument(
'--test-file', type=str, help=
'numpy table')
306 parser.add_argument(
'--objective', type=str, help=
'Specify the learning task and the corresponding learning objective, currently support options: binary:logistic, reg:linear(squarederror), multi:softprob')
308 args = parser.parse_args()
309 logging.info(
"converting input file %s to root file %s", args.input, args.output)
312 supported_objective = [
'binary:logistic',
'reg:linear',
'reg:squarederror',
'multi:softprob']
314 if args.objective
not in supported_objective:
316 Current version does NOT support this objective!!
317 Only the following objectives are supported and tested:
319 - reg:linear(or squarederror)
324 parser.error(
'Model file name not given!')
326 if "root" not in args.output:
327 parser.error(
"The outputfile name must end with .root!!")
332 print(
"model has not been tested. Do not use it production!")
334 logging.info(
"testing model")
335 if not args.test_file:
336 parser.error(
"Attempting to do test but no test file was provided, pass this with '--test-file <test_file> or use option '--no_test' ")
338 print(
"problem when checking file")
339 result =
test(args.input, args.output, args.objective, args.tree_name, args.ntests, args.test_file)
341 print(
"some problems during test. Have you setup athena? Do not use this in production!")
343 print(
u"::: everything fine: XGBoost output == MVAUtils output :::")
344 objective = args.objective
346 data = np.load(args.test_file)
347 if 'binary' in objective:
348 print(
'''In c++ use your BDT as:
349 #include "MVAUtils/BDT.h"
351 TFile* f = TFile::Open("%s");
352 TTree* tree = nullptr;
353 f->GetObject("%s", tree);
354 MVAUtils::BDT my_bdt(tree);
356 // std::vector<float> input_values(%d, 0.);
357 // fill the vector using the order as in the trainig
359 float output = my_bdt.GetClassification(input_values);
360 ''' % (args.output, output_treename, len(data[0])))
361 elif 'reg' in objective:
362 print(
'''In c++ use your BDT as:
363 #include "MVAUtils/BDT.h"
365 TFile* f = TFile::Open("%s");
366 TTree* tree = nullptr;
367 f->GetObject("%s", tree);
368 MVAUtils::BDT my_bdt(tree);
370 // std::vector<float> input_values(%d, 0.);
371 // fill the vector using the order as in the trainig
373 float output = my_bdt.Predict(input_values);
374 ''' % (args.output, output_treename, len(data[0])))
375 elif "multi" in objective:
376 print(
'''In c++ use your BDT as:
377 #include "MVAUtils/BDT.h"
379 TFile* f = TFile::Open("%s");
380 TTree* tree = nullptr;
381 f->GetObject("%s", tree);
382 MVAUtils::BDT my_bdt(tree);
384 // std::vector<float> input_values(%d, 0.);
385 // fill the vector using the order as in the trainig
387 float output = my_bdt.GetMultiResponse(input_values, nclasses);
388 ''' % (args.output, output_treename, len(data[0])))