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