Belle II Software  release-06-00-14
hep_ml_uboost.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2_mva
12 import basf2_mva_util
13 import subprocess
14 import time
15 
16 if __name__ == "__main__":
17  from basf2 import conditions
18  # NOTE: do not use testing payloads in production! Any results obtained like this WILL NOT BE PUBLISHED
19  conditions.testing_payloads = [
20  'localdb/database.txt'
21  ]
22 
23  variables = ['p', 'pt', 'pz',
24  'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)',
25  'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)',
26  'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)',
27  'chiProb', 'dr', 'dz',
28  'daughter(0, dr)', 'daughter(1, dr)',
29  'daughter(0, dz)', 'daughter(1, dz)',
30  'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)',
31  'daughter(0, kaonID)', 'daughter(0, pionID)',
32  'daughterInvariantMass(0, 1)', 'daughterInvariantMass(0, 2)', 'daughterInvariantMass(1, 2)']
33 
34  general_options = basf2_mva.GeneralOptions()
35  general_options.m_datafiles = basf2_mva.vector("train.root")
36  general_options.m_treename = "tree"
37  general_options.m_variables = basf2_mva.vector(*variables)
38  # Spectators are the variables for which the selection should be uniform
39  general_options.m_spectators = basf2_mva.vector('M')
40  general_options.m_target_variable = "isSignal"
41  general_options.m_identifier = "HepMLUBoost"
42 
43  specific_options = basf2_mva.PythonOptions()
44  specific_options.m_steering_file = 'mva/examples/python/hep_ml_uboost.py'
45  # Set the parameters of the uBoostClassifier,
46  # defaults are 50, which is reasonable, but I want to have a example runtime < 2 minutes
47  import json
48  specific_options.m_config = json.dumps({'n_neighbors': 5, 'n_estimators': 5})
49  specific_options.m_framework = 'hep_ml'
50 
51  training_start = time.time()
52  basf2_mva.teacher(general_options, specific_options)
53  training_stop = time.time()
54  training_time = training_stop - training_start
55  method = basf2_mva_util.Method(general_options.m_identifier)
56  inference_start = time.time()
57  p, t = method.apply_expert(basf2_mva.vector("test.root"), general_options.m_treename)
58  inference_stop = time.time()
59  inference_time = inference_stop - inference_start
61  print("HepML", training_time, inference_time, auc)
62 
63  subprocess.call('basf2_mva_evaluate.py -c -o latex.pdf -train train.root -data test.root -i HepMLUBoost', shell=True)
def calculate_roc_auc(p, t)