Belle II Software light-2509-fornax
all_classifiers.py
1#!/usr/bin/env python3
2
3
10
11import base64
12import basf2_mva
13import basf2
14from subprocess import PIPE, run
15import b2test_utils
16
17variables = ['p', 'pz', 'daughter(0, p)', 'daughter(0, pz)', 'daughter(1, p)', 'daughter(1, pz)',
18 'chiProb', 'dr', 'dz', 'daughter(0, dr)', 'daughter(1, dr)', 'daughter(0, chiProb)', 'daughter(1, chiProb)',
19 'daughter(0, kaonID)', 'daughter(0, pionID)', 'daughterAngle(0, 1)']
20
21# base64-encoded ONNX model in mva/methods/tests/ONNX.xml
22# created from mva/methods/tests/test_write_onnx.py
23onnx_model_b64 = (
24 b'CAgSB3B5dG9yY2gaBTIuMi4yOv8BCloKBWlucHV0CgZ3ZWlnaHQKBGJpYXMSBm9'
25 b'1dHB1dBoFL0dlbW0iBEdlbW0qDwoFYWxwaGEVAACAP6ABASoOCgRiZXRhFQAAgD'
26 b'+gAQEqDQoGdHJhbnNCGAGgAQISCm1haW5fZ3JhcGgqUAgBCBAQAUIGd2VpZ2h0S'
27 b'kC4r0M+4XpUPo4GcL16Nms+93VgvTtwTj3ZPfm9vVIWPoqwYT42zTu+5INePlux'
28 b'Pz3/IT0+utoKPbTI9j28lhC9KhAIARABQgRiaWFzSgQmU0U+WhcKBWlucHV0Eg4'
29 b'KDAgBEggKAggBCgIIEGIYCgZvdXRwdXQSDgoMCAESCAoCCAEKAggBQgIQEQ=='
30)
31
32if __name__ == "__main__":
33
34 # Skip test if files are not available
35 try:
36 train_file = basf2.find_file('mva/train_D0toKpipi.root', 'examples', False)
37 test_file = basf2.find_file('mva/test_D0toKpipi.root', 'examples', False)
38 except BaseException:
39 b2test_utils.skip_test('Necessary files "train.root" and "test.root" not available.')
40
41 general_options = basf2_mva.GeneralOptions()
42 general_options.m_datafiles = basf2_mva.vector(train_file)
43 general_options.m_treename = "tree"
44 general_options.m_variables = basf2_mva.vector(*variables)
45 general_options.m_target_variable = "isSignal"
46 general_options.m_max_events = 200
47
48 methods = [
49 ('Trivial.xml', basf2_mva.TrivialOptions(), None),
50 ('FastBDT.xml', basf2_mva.FastBDTOptions(), None),
51 ('TMVAClassification.xml', basf2_mva.TMVAOptionsClassification(), None),
52 ('FANN.xml', basf2_mva.FANNOptions(), None),
53 ('Python_sklearn.xml', basf2_mva.PythonOptions(), 'sklearn'),
54 ('Python_xgb.xml', basf2_mva.PythonOptions(), 'xgboost'),
55 ('Python_tensorflow.xml', basf2_mva.PythonOptions(), 'tensorflow'),
56 ('Python_torch.xml', basf2_mva.PythonOptions(), 'torch'),
57 ('ONNX.xml', basf2_mva.ONNXOptions(), None),
58 ]
59
60 # we create payloads so let's switch to an empty, temporary directory
62 for identifier, specific_options, framework in methods:
63 general_options.m_identifier = identifier
64 if framework is not None:
65 specific_options.m_framework = framework
66 if isinstance(specific_options, type(basf2_mva.ONNXOptions())):
67 # ONNX doesn't do training, so we need to provide a model
68 specific_options.m_modelFilename = "model.onnx"
69 with open("model.onnx", "wb") as f:
70 f.write(base64.b64decode(onnx_model_b64))
71 basf2_mva.teacher(general_options, specific_options)
72
73 basf2_mva.expert(basf2_mva.vector(*[i for i, _, _ in methods]),
74 basf2_mva.vector(train_file), 'tree', 'expert.root')
75
76 # don't compile the evaluation as this fails on the build bot due to missing latex files
77 command = f'basf2_mva_evaluate.py -o latex.pdf -train {train_file}'\
78 f' -data {test_file} -i {" ".join([i for i, _, _ in methods])}'
79
80 result = run(command,
81 stdout=PIPE, stderr=PIPE,
82 text=True, shell=True)
83 assert result.returncode == 0, 'basf2_mva_evaluate.py failed!'
skip_test(reason, py_case=None)
Definition __init__.py:31
clean_working_directory()
Definition __init__.py:198