Belle II Software development
python_normalization.py
1#!/usr/bin/env python3
2
3
10
11import basf2_mva
12import numpy as np
13import b2test_utils
14import basf2
15
16variables = ['M', 'p', 'pt', 'pz',
17 'daughter(0, p)', 'daughter(0, pz)', 'daughter(0, pt)',
18 'daughter(1, p)', 'daughter(1, pz)', 'daughter(1, pt)',
19 'daughter(2, p)', 'daughter(2, pz)', 'daughter(2, pt)',
20 'chiProb', 'dr', 'dz',
21 'daughter(0, dr)', 'daughter(1, dr)',
22 'daughter(0, dz)', 'daughter(1, dz)',
23 'daughter(0, chiProb)', 'daughter(1, chiProb)', 'daughter(2, chiProb)',
24 'daughter(0, kaonID)', 'daughter(0, pionID)',
25 'daughterInvM(0, 1)', 'daughterInvM(0, 2)', 'daughterInvM(1, 2)']
26
27
28def apply(state, X):
29 """
30 Test apply function
31 """
32 # with the limited nomber of events we use for the test, we can only loosely check the
33 # normalization. Check that on average we get means near 0 and stddevs near 1.0.
34 # For individual variables these can still be quite different.
35 assert abs(np.mean(X.mean(axis=0))) < 0.10, 'Normalization appears to have failed'
36 assert abs(np.mean(X.std(axis=0)) - 1) < 0.10, 'Normalization appears to have failed'
37 p = np.zeros(len(X))
38 return np.require(p, dtype=np.float32, requirements=['A', 'W', 'C', 'O'])
39
40
41def partial_fit(state, X, S, y, w, epoch, batch):
42 """
43 Test partial_fit function
44 """
45 return True
46
47
48print("Executed python script")
49
50if __name__ == "__main__":
51
52 # Skip test if files are not available
53 try:
54 train_file = basf2.find_file('mva/train_D0toKpipi.root', 'examples', False)
55 test_file = basf2.find_file('mva/test_D0toKpipi.root', 'examples', False)
56 except BaseException:
57 b2test_utils.skip_test('Necessary files "train.root" and "test.root" not available.')
58
59 general_options = basf2_mva.GeneralOptions()
60 general_options.m_datafiles = basf2_mva.vector(train_file)
61 general_options.m_treename = "tree"
62 general_options.m_variables = basf2_mva.vector(*variables)
63 general_options.m_target_variable = "isSignal"
64 general_options.m_identifier = "Python.xml"
65 general_options.m_max_events = 1000
66
67 specific_options = basf2_mva.PythonOptions()
68 specific_options.m_training_fraction = 0.9
69 specific_options.m_nIterations = 1
70 specific_options.m_mini_batch_size = 0
71 specific_options.m_framework = 'test'
72 specific_options.m_steering_file = 'mva/tests/python_normalization.py'
73 specific_options.m_normalize = True
74
75 # we create payloads so let's switch to an empty, temporary directory
77 basf2_mva.teacher(general_options, specific_options)
78 basf2_mva.expert(basf2_mva.vector("Python.xml"),
79 basf2_mva.vector(test_file), 'tree', 'expert.root')
def clean_working_directory()
Definition: __init__.py:189
def skip_test(reason, py_case=None)
Definition: __init__.py:31