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 else:
196 raise RuntimeError("Unknown method " + self.general_options.m_method)
197
198 self.specific_options.load(self.weightfile.getXMLTree())
199
200 variables = [str(v) for v in self.general_options.m_variables]
201 importances = self.weightfile.getFeatureImportance()
202
203
204 self.importances = {k: importances[k] for k in variables}
205
206 self.variables = list(sorted(variables, key=lambda v: self.importances.get(v, 0.0)))
207
208 self.root_variables = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.variables]
209
210 self.root_importances = {k: importances[k] for k in self.root_variables}
211
212 self.description = str(basf2_mva.info(self.identifier))
213
214 self.spectators = [str(v) for v in self.general_options.m_spectators]
215
216 self.root_spectators = [ROOT.Belle2.MakeROOTCompatible.makeROOTCompatible(v) for v in self.spectators]
217

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

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

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

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 212 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 204 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 210 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 216 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 208 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 214 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 206 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: