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 141 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 148 of file basf2_mva_util.py.

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

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 250 of file basf2_mva_util.py.

250 def apply_expert(self, datafiles, treename):
251 """
252 Apply the expert of the method to data and return the calculated probability and the target
253 @param datafiles the datafiles
254 @param treename the name of the tree containing the data
255 """
256 import ROOT # noqa
257 if isinstance(datafiles, str):
258 datafiles = [datafiles]
259 with tempfile.TemporaryDirectory() as tempdir:
260 identifier = tempdir + "/weightfile.xml"
261 ROOT.Belle2.MVA.Weightfile.save(self.weightfile, identifier)
262
263 rootfilename = tempdir + '/expert.root'
264 basf2_mva.expert(basf2_mva.vector(identifier),
265 basf2_mva.vector(*datafiles),
266 treename,
267 rootfilename)
268 chain = ROOT.TChain("variables")
269 chain.Add(rootfilename)
270
271 expert_target = identifier + '_' + self.general_options.m_target_variable
272 stripped_expert_target = self.identifier + '_' + self.general_options.m_target_variable
273
274 output_names = [self.identifier]
275 branch_names = [
276 ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(identifier),
277 ]
278 if self.general_options.m_nClasses > 2:
279 output_names = [self.identifier+f'_{i}' for i in range(self.general_options.m_nClasses)]
280 branch_names = [
281 ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(
282 identifier +
283 f'_{i}') for i in range(
284 self.general_options.m_nClasses)]
285
286 d = chain2dict(
287 chain,
288 [*branch_names, ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(expert_target)],
289 [*output_names, stripped_expert_target])
290
291 return (d[str(self.identifier)] if self.general_options.m_nClasses <= 2 else np.array([d[x]
292 for x in output_names]).T), d[stripped_expert_target]

◆ 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 220 of file basf2_mva_util.py.

220 def train_teacher(self, datafiles, treename, general_options=None, specific_options=None):
221 """
222 Train a new method using this method as a prototype
223 @param datafiles the training datafiles
224 @param treename the name of the tree containing the training data
225 @param general_options general options given to basf2_mva.teacher
226 (if None the options of this method are used)
227 @param specific_options specific options given to basf2_mva.teacher
228 (if None the options of this method are used)
229 """
230 # Always avoid the top-level 'import ROOT'.
231 import ROOT # noqa
232 if isinstance(datafiles, str):
233 datafiles = [datafiles]
234 if general_options is None:
235 general_options = self.general_options
236 if specific_options is None:
237 specific_options = self.specific_options
238
239 with tempfile.TemporaryDirectory() as tempdir:
240 identifier = tempdir + "/weightfile.xml"
241
242 general_options.m_datafiles = basf2_mva.vector(*datafiles)
243 general_options.m_identifier = identifier
244
245 basf2_mva.teacher(general_options, specific_options)
246
247 method = Method(identifier)
248 return method
249

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 214 of file basf2_mva_util.py.

◆ general_options

general_options = basf2_mva.GeneralOptions()

General options of the method.

Definition at line 162 of file basf2_mva_util.py.

◆ identifier

identifier = identifier

Identifier of the method.

Definition at line 158 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 206 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 212 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 218 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 210 of file basf2_mva_util.py.

◆ specific_options

specific_options = None

Specific options of the method.

Definition at line 176 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 216 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 208 of file basf2_mva_util.py.

◆ weightfile

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

Weightfile of the method.

Definition at line 160 of file basf2_mva_util.py.


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