Belle II Software  release-08-01-10
all_classifiers.py
1 #!/usr/bin/env python3
2 
3 
10 
11 import basf2_mva
12 import basf2
13 from subprocess import PIPE, run
14 import b2test_utils
15 
16 variables = ['p', 'pz', 'daughter(0, p)', 'daughter(0, pz)', 'daughter(1, p)', 'daughter(1, pz)',
17  'chiProb', 'dr', 'dz', 'daughter(0, dr)', 'daughter(1, dr)', 'daughter(0, chiProb)', 'daughter(1, chiProb)',
18  'daughter(0, kaonID)', 'daughter(0, pionID)', 'daughterAngle(0, 1)']
19 
20 if __name__ == "__main__":
21 
22  # Skip test if files are not available
23  try:
24  train_file = basf2.find_file('mva/train_D0toKpipi.root', 'examples', False)
25  test_file = basf2.find_file('mva/test_D0toKpipi.root', 'examples', False)
26  except BaseException:
27  b2test_utils.skip_test('Necessary files "train.root" and "test.root" not available.')
28 
29  general_options = basf2_mva.GeneralOptions()
30  general_options.m_datafiles = basf2_mva.vector(train_file)
31  general_options.m_treename = "tree"
32  general_options.m_variables = basf2_mva.vector(*variables)
33  general_options.m_target_variable = "isSignal"
34  general_options.m_max_events = 200
35 
36  methods = [
37  ('Trivial.xml', basf2_mva.TrivialOptions(), None),
38  ('FastBDT.xml', basf2_mva.FastBDTOptions(), None),
39  ('TMVAClassification.xml', basf2_mva.TMVAOptionsClassification(), None),
40  ('FANN.xml', basf2_mva.FANNOptions(), None),
41  ('Python_sklearn.xml', basf2_mva.PythonOptions(), 'sklearn'),
42  ('Python_xgb.xml', basf2_mva.PythonOptions(), 'xgboost'),
43  ('Python_tensorflow.xml', basf2_mva.PythonOptions(), 'tensorflow'),
44  ]
45 
46  # we create payloads so let's switch to an empty, temporary directory
48  for identifier, specific_options, framework in methods:
49  general_options.m_identifier = identifier
50  if framework is not None:
51  specific_options.m_framework = framework
52  basf2_mva.teacher(general_options, specific_options)
53 
54  basf2_mva.expert(basf2_mva.vector(*[i for i, _, _ in methods]),
55  basf2_mva.vector(train_file), 'tree', 'expert.root')
56 
57  # don't compile the evaluation as this fails on the build bot due to missing latex files
58  command = f'basf2_mva_evaluate.py -o latex.pdf -train {train_file}'\
59  f' -data {test_file} -i {" ".join([i for i, _, _ in methods])}'
60 
61  result = run(command,
62  stdout=PIPE, stderr=PIPE,
63  text=True, shell=True)
64  assert result.returncode == 0, 'basf2_mva_evaluate.py failed!'
def clean_working_directory()
Definition: __init__.py:189
def skip_test(reason, py_case=None)
Definition: __init__.py:31