Belle II Software  release-05-01-25
training.py
1 import os
2 import os.path
3 import shutil
4 import subprocess
5 
6 from tracking.run.mixins import BrowseTFileOnTerminateRunMixin
7 from tracking.run.mixins import PostProcessingRunMixin
8 from tracking.run.utilities import NonstrictChoices
9 
10 
12  """Prepare and execute a basf2 job to train neural network, postprocess, and inspect"""
13 
14 
15  task = "train"
16 
17 
18  variables = None
19 
20 
21  groupby = None
22 
23 
24  auxiliaries = None
25 
26 
27  truth = "truth"
28 
29  @property
30  def identifier(self):
31  """Database identifier of the filte being trained"""
32  return "trackfindingcdc_" + self.__class__.__name__[:-len("TrainingRun")]
33 
34  @property
35  def sample_file_name(self):
36  """File name of the recorded sample to be trained on
37 
38  Defaults to the class name minus the mandatory TrainingRun postfix
39  """
40  return self.__class__.__name__[:-len("TrainingRun")] + '_' + self.task + '.root'
41 
42  def create_argument_parser(self, **kwds):
43  """Create argument parser"""
44  argument_parser = super().create_argument_parser(**kwds)
45 
46  argument_parser.add_argument(
47  "--task",
48  choices=NonstrictChoices(["train", "eval", "explore", ]),
49  default=self.task,
50  dest="task",
51  help=("Select a prepared recording task")
52  )
53 
54  return argument_parser
55 
56  def postprocess(self):
57  """Run the training as post-processing job
58 
59  To run only the training run with --postprocess-only
60  """
61 
62  if self.task == "train":
63  cmd = [
64  "trackfindingcdc_teacher",
65  ]
66 
67  if self.variables:
68  cmd += ["--variables"]
69  cmd += self.variables
70 
71  cmd += [
72  "--identifier=" + self.identifier,
73  "--truth=" + self.truth,
74  self.sample_file_name,
75  ]
76  print("Running", cmd)
77  subprocess.call(cmd)
78 
79  # Move training file to the right location
80  if self.identifier.endswith(".xml"):
81  tracking_data_dir_path = os.path.join(os.environ["BELLE2_LOCAL_DIR"], "tracking", "data")
82  shutil.copy(self.identifier, tracking_data_dir_path)
83 
84  else:
85  cmd = [
86  "trackfindingcdc-classification-overview",
87  ]
88 
89  if self.variables:
90  cmd += ["-v"]
91  cmd += self.variables
92 
93  if self.groupby is not None:
94  cmd += ["-g"]
95  if isinstance(self.groupby, str):
96  cmd += [self.groupby]
97  else:
98  cmd += self.groupby
99 
100  if self.auxiliaries is not None:
101  cmd += ["-a"]
102  if isinstance(self.auxiliaries, str):
103  cmd += [self.auxiliaries]
104  else:
105  cmd += self.auxiliaries
106 
107  cmd += [
108  "--truth=" + self.truth,
109  self.sample_file_name,
110  ]
111  print("Running", cmd)
112  subprocess.call(cmd)
113 
114  self.output_file_name = self.sample_file_name[:-len(".root")] + ".overview.root"
115 
116  super().postprocess()
tracking.run.mixins
Definition: mixins.py:1
training.TrainingRunMixin.sample_file_name
def sample_file_name(self)
Definition: training.py:35
tracking.run.mixins.BrowseTFileOnTerminateRunMixin
Definition: mixins.py:50
training.TrainingRunMixin.postprocess
def postprocess(self)
Definition: training.py:56
training.TrainingRunMixin.groupby
groupby
Input groupby the classification analysis.
Definition: training.py:21
tracking.run.mixins.PostProcessingRunMixin
Definition: mixins.py:18
tracking.run.utilities
Definition: utilities.py:1
training.TrainingRunMixin.truth
string truth
Truth variable name.
Definition: training.py:27
training.TrainingRunMixin.auxiliaries
auxiliaries
Input auxiliaries the classification analysis.
Definition: training.py:24
training.TrainingRunMixin
Definition: training.py:11
training.TrainingRunMixin.create_argument_parser
def create_argument_parser(self, **kwds)
Definition: training.py:42
training.TrainingRunMixin.task
string task
Recording / training task selected.
Definition: training.py:15
tracking.run.utilities.NonstrictChoices
Definition: utilities.py:24
training.TrainingRunMixin.identifier
def identifier(self)
Definition: training.py:30
training.TrainingRunMixin.variables
variables
Input variable for the training or the classification analysis.
Definition: training.py:18
tracking.run.mixins.BrowseTFileOnTerminateRunMixin.output_file_name
output_file_name
There is no default for the name of the output TFile.
Definition: mixins.py:54