Belle II Software development
Method Class Reference

Public Member Functions

 __init__ (self, identifier)
 
 train_teacher (self, datafiles, treename, general_options=None, specific_options=None)
 
 apply_expert (self, datafiles, treename)
 

Public Attributes

 identifier = identifier
 Identifier of the method.
 
 weightfile = ROOT.Belle2.MVA.Weightfile.load(self.identifier)
 Weightfile of the method.
 
 general_options = basf2_mva.GeneralOptions()
 General options of the method.
 
 specific_options = None
 Specific options of the method.
 
dict importances = {k: importances[k] for k in variables}
 Dictionary of the variable importances calculated by the method.
 
 variables = list(sorted(variables, key=lambda v: self.importances.get(v, 0.0)))
 List of variables sorted by their importance.
 
list root_variables = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.variables]
 List of the variable importances calculated by the method, but with the root compatible variable names.
 
dict root_importances = {k: importances[k] for k in self.root_variables}
 Dictionary of the variables sorted by their importance but with root compatoble variable names.
 
 description = str(basf2_mva.info(self.identifier))
 Description of the method as a xml string returned by basf2_mva.info.
 
list spectators = [str(v) for v in self.general_options.m_spectators]
 List of spectators.
 
list root_spectators = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.spectators]
 List of spectators with root compatible names.
 

Detailed Description

Wrapper class providing an interface to the method stored under the given identifier.
It loads the Options, can apply the expert and train new ones using the current as a prototype.
This class is used by the basf_mva_evaluation tools

Definition at line 122 of file basf2_mva_util.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
identifier )
Load a method stored under the given identifier
@param identifier identifying the method

Definition at line 129 of file basf2_mva_util.py.

129 def __init__(self, identifier):
130 """
131 Load a method stored under the given identifier
132 @param identifier identifying the method
133 """
134 # Always avoid the top-level 'import ROOT'.
135 import ROOT # noqa
136 # Initialize all the available interfaces
137 ROOT.Belle2.MVA.AbstractInterface.initSupportedInterfaces()
138
139 self.identifier = identifier
140
141 self.weightfile = ROOT.Belle2.MVA.Weightfile.load(self.identifier)
142
143 self.general_options = basf2_mva.GeneralOptions()
144 self.general_options.load(self.weightfile.getXMLTree())
145
146 # This piece of code should be correct but leads to random segmentation faults
147 # inside python, llvm or pyroot, therefore we use the more dirty code below
148 # Ideas why this is happening:
149 # 1. Ownership of the unique_ptr returned by getOptions()
150 # 2. Some kind of object slicing, although pyroot identifies the correct type
151 # 3. Bug in pyroot
152 # interfaces = ROOT.Belle2.MVA.AbstractInterface.getSupportedInterfaces()
153 # self.interface = interfaces[self.general_options.m_method]
154 # self.specific_options = self.interface.getOptions()
155
156
157 self.specific_options = None
158 if self.general_options.m_method == "FastBDT":
159 self.specific_options = basf2_mva.FastBDTOptions()
160 elif self.general_options.m_method == "TMVAClassification":
161 self.specific_options = basf2_mva.TMVAOptionsClassification()
162 elif self.general_options.m_method == "TMVARegression":
163 self.specific_options = basf2_mva.TMVAOptionsRegression()
164 elif self.general_options.m_method == "FANN":
165 self.specific_options = basf2_mva.FANNOptions()
166 elif self.general_options.m_method == "Python":
167 self.specific_options = basf2_mva.PythonOptions()
168 elif self.general_options.m_method == "PDF":
169 self.specific_options = basf2_mva.PDFOptions()
170 elif self.general_options.m_method == "Combination":
171 self.specific_options = basf2_mva.CombinationOptions()
172 elif self.general_options.m_method == "Reweighter":
173 self.specific_options = basf2_mva.ReweighterOptions()
174 elif self.general_options.m_method == "Trivial":
175 self.specific_options = basf2_mva.TrivialOptions()
176 elif self.general_options.m_method == "ONNX":
177 self.specific_options = basf2_mva.ONNXOptions()
178 else:
179 raise RuntimeError("Unknown method " + self.general_options.m_method)
180
181 self.specific_options.load(self.weightfile.getXMLTree())
182
183 variables = [str(v) for v in self.general_options.m_variables]
184 importances = self.weightfile.getFeatureImportance()
185
186
187 self.importances = {k: importances[k] for k in variables}
188
189 self.variables = list(sorted(variables, key=lambda v: self.importances.get(v, 0.0)))
190
191 self.root_variables = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.variables]
192
193 self.root_importances = {k: importances[k] for k in self.root_variables}
194
195 self.description = str(basf2_mva.info(self.identifier))
196
197 self.spectators = [str(v) for v in self.general_options.m_spectators]
198
199 self.root_spectators = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.spectators]
200

Member Function Documentation

◆ apply_expert()

apply_expert ( self,
datafiles,
treename )
Apply the expert of the method to data and return the calculated probability and the target
@param datafiles the datafiles
@param treename the name of the tree containing the data

Definition at line 231 of file basf2_mva_util.py.

231 def apply_expert(self, datafiles, treename):
232 """
233 Apply the expert of the method to data and return the calculated probability and the target
234 @param datafiles the datafiles
235 @param treename the name of the tree containing the data
236 """
237 import ROOT # noqa
238 if isinstance(datafiles, str):
239 datafiles = [datafiles]
240 with tempfile.TemporaryDirectory() as tempdir:
241 identifier = tempdir + "/weightfile.xml"
242 ROOT.Belle2.MVA.Weightfile.save(self.weightfile, identifier)
243
244 rootfilename = tempdir + '/expert.root'
245 basf2_mva.expert(basf2_mva.vector(identifier),
246 basf2_mva.vector(*datafiles),
247 treename,
248 rootfilename)
249 chain = ROOT.TChain("variables")
250 chain.Add(rootfilename)
251
252 expert_target = identifier + '_' + self.general_options.m_target_variable
253 stripped_expert_target = self.identifier + '_' + self.general_options.m_target_variable
254
255 output_names = [self.identifier]
256 branch_names = [
257 ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(identifier),
258 ]
259 if self.general_options.m_nClasses > 2:
260 output_names = [self.identifier+f'_{i}' for i in range(self.general_options.m_nClasses)]
261 branch_names = [
262 ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(
263 identifier +
264 f'_{i}') for i in range(
265 self.general_options.m_nClasses)]
266
267 d = chain2dict(
268 chain,
269 [*branch_names, ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(expert_target)],
270 [*output_names, stripped_expert_target])
271
272 return (d[str(self.identifier)] if self.general_options.m_nClasses <= 2 else np.array([d[x]
273 for x in output_names]).T), d[stripped_expert_target]
274
275

◆ train_teacher()

train_teacher ( self,
datafiles,
treename,
general_options = None,
specific_options = None )
Train a new method using this method as a prototype
@param datafiles the training datafiles
@param treename the name of the tree containing the training data
@param general_options general options given to basf2_mva.teacher
  (if None the options of this method are used)
@param specific_options specific options given to basf2_mva.teacher
  (if None the options of this method are used)

Definition at line 201 of file basf2_mva_util.py.

201 def train_teacher(self, datafiles, treename, general_options=None, specific_options=None):
202 """
203 Train a new method using this method as a prototype
204 @param datafiles the training datafiles
205 @param treename the name of the tree containing the training data
206 @param general_options general options given to basf2_mva.teacher
207 (if None the options of this method are used)
208 @param specific_options specific options given to basf2_mva.teacher
209 (if None the options of this method are used)
210 """
211 # Always avoid the top-level 'import ROOT'.
212 import ROOT # noqa
213 if isinstance(datafiles, str):
214 datafiles = [datafiles]
215 if general_options is None:
216 general_options = self.general_options
217 if specific_options is None:
218 specific_options = self.specific_options
219
220 with tempfile.TemporaryDirectory() as tempdir:
221 identifier = tempdir + "/weightfile.xml"
222
223 general_options.m_datafiles = basf2_mva.vector(*datafiles)
224 general_options.m_identifier = identifier
225
226 basf2_mva.teacher(general_options, specific_options)
227
228 method = Method(identifier)
229 return method
230

Member Data Documentation

◆ description

description = str(basf2_mva.info(self.identifier))

Description of the method as a xml string returned by basf2_mva.info.

Definition at line 195 of file basf2_mva_util.py.

◆ general_options

general_options = basf2_mva.GeneralOptions()

General options of the method.

Definition at line 143 of file basf2_mva_util.py.

◆ identifier

identifier = identifier

Identifier of the method.

Definition at line 139 of file basf2_mva_util.py.

◆ importances

dict importances = {k: importances[k] for k in variables}

Dictionary of the variable importances calculated by the method.

Definition at line 187 of file basf2_mva_util.py.

◆ root_importances

dict root_importances = {k: importances[k] for k in self.root_variables}

Dictionary of the variables sorted by their importance but with root compatoble variable names.

Definition at line 193 of file basf2_mva_util.py.

◆ root_spectators

list root_spectators = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.spectators]

List of spectators with root compatible names.

Definition at line 199 of file basf2_mva_util.py.

◆ root_variables

list root_variables = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.variables]

List of the variable importances calculated by the method, but with the root compatible variable names.

Definition at line 191 of file basf2_mva_util.py.

◆ specific_options

specific_options = None

Specific options of the method.

Definition at line 157 of file basf2_mva_util.py.

◆ spectators

list spectators = [str(v) for v in self.general_options.m_spectators]

List of spectators.

Definition at line 197 of file basf2_mva_util.py.

◆ variables

variables = list(sorted(variables, key=lambda v: self.importances.get(v, 0.0)))

List of variables sorted by their importance.

Definition at line 189 of file basf2_mva_util.py.

◆ weightfile

weightfile = ROOT.Belle2.MVA.Weightfile.load(self.identifier)

Weightfile of the method.

Definition at line 141 of file basf2_mva_util.py.


The documentation for this class was generated from the following file: