Belle II Software  release-05-02-19
python.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # Thomas Keck 2016
5 
6 import basf2_mva
7 
8 import os
9 import tempfile
10 import sys
11 
12 variables = ['M', 'p', 'pt', 'pz',
13  'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)',
14  'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)',
15  'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)',
16  'chiProb', 'dr', 'dz',
17  'daughter(0, dr)', 'daughter(1, dr)',
18  'daughter(0, dz)', 'daughter(1, dz)',
19  'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)',
20  'daughter(0, kaonID)', 'daughter(0, pionID)',
21  'daughterInvariantMass(0, 1)', 'daughterInvariantMass(0, 2)', 'daughterInvariantMass(1, 2)']
22 
23 
24 def feature_importance(state):
25  """
26  Return a list containing the feature importances
27  """
28  print("Called overwritten feature importance")
29  return []
30 
31 
32 def load(obj):
33  """
34  Test load function
35  """
36  print("Called overwritten load")
37  print(obj)
38  return None
39 
40 
41 print("Executed python script")
42 
43 if __name__ == "__main__":
44 
45  # Skip test if files are not available
46  if not (os.path.isfile('train.root') and os.path.isfile('test.root')):
47  print("TEST SKIPPED: Not runnable on build bot", file=sys.stderr)
48  sys.exit(1)
49 
50  general_options = basf2_mva.GeneralOptions()
51  general_options.m_datafiles = basf2_mva.vector("train.root")
52  general_options.m_treename = "tree"
53  general_options.m_variables = basf2_mva.vector(*variables)
54  general_options.m_target_variable = "isSignal"
55  general_options.m_identifier = "Python.xml"
56 
57  specific_options = basf2_mva.PythonOptions()
58  specific_options.m_training_fraction = 0.9
59  specific_options.m_nIterations = 2
60  specific_options.m_mini_batch_size = 10000
61  specific_options.m_framework = 'test'
62 
63  olddir = os.getcwd()
64  with tempfile.TemporaryDirectory() as tempdir:
65  os.symlink(os.path.abspath('train.root'), tempdir + '/' + os.path.basename('train.root'))
66  os.symlink(os.path.abspath('test.root'), tempdir + '/' + os.path.basename('test.root'))
67  os.chdir(tempdir)
68 
69  basf2_mva.teacher(general_options, specific_options)
70 
71  basf2_mva.expert(basf2_mva.vector("Python.xml"),
72  basf2_mva.vector('train.root'), 'tree', 'expert.root')
73 
74  specific_options.m_steering_file = 'mva/tests/python.py'
75  basf2_mva.teacher(general_options, specific_options)
76 
77  basf2_mva.expert(basf2_mva.vector("Python.xml"),
78  basf2_mva.vector('train.root'), 'tree', 'expert.root')
79 
80  os.chdir(olddir)