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