Belle II Software development
SingleIOV Class Reference
Inheritance diagram for SingleIOV:
AlgorithmStrategy

Public Member Functions

 __init__ (self, algorithm)
 
 run (self, iov, iteration, queue)
 
 setup_from_dict (self, params)
 
 is_valid (self)
 
 find_iov_gaps (self)
 
 any_failed_iov (self)
 
 send_result (self, result)
 
 send_final_state (self, state)
 

Public Attributes

 machine = AlgorithmMachine(self.algorithm)
 :py:class:caf.state_machines.AlgorithmMachine used to help set up and execute CalibrationAlgorithm It gets setup properly in :py:func:run
 
 algorithm = algorithm
 Algorithm() class that we're running.
 
list input_files = []
 Collector output files, will contain all files returned by the output patterns.
 
str output_dir = ""
 The algorithm output directory which is mostly used to store the stdout file.
 
str output_database_dir = ""
 The output database directory for the localdb that the algorithm will commit to.
 
list database_chain = []
 User defined database chain i.e.
 
list dependent_databases = []
 CAF created local databases from previous calibrations that this calibration/algorithm depends on.
 
list ignored_runs = []
 Runs that will not be included in ANY execution of the algorithm.
 
list results = []
 The list of results objects which will be sent out before the end.
 
 queue = None
 The multiprocessing Queue we use to pass back results one at a time.
 

Static Public Attributes

dict usable_params = {"apply_iov": IoV}
 The params that you could set on the Algorithm object which this Strategy would use.
 
list required_attrs
 Required attributes that must exist before the strategy can run properly.
 
list required_true_attrs
 Attributes that must have a value that returns True when tested by :py:meth:is_valid.
 
list allowed_granularities = ["run", "all"]
 Granularity of collector that can be run by this algorithm properly.
 
str FINISHED_RESULTS = "DONE"
 Signal value that is put into the Queue when there are no more results left.
 
str COMPLETED = "COMPLETED"
 Completed state.
 
str FAILED = "FAILED"
 Failed state.
 

Detailed Description

The fastest and simplest Algorithm strategy. Runs the algorithm only once over all of the input
data or only the data corresponding to the requested IoV. The payload IoV is the set to the same as the one
that was executed.

This uses a `caf.state_machines.AlgorithmMachine` to actually execute the various steps rather than operating on
a CalibrationAlgorithm C++ class directly.

Definition at line 181 of file strategies.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
algorithm )
 

Definition at line 193 of file strategies.py.

193 def __init__(self, algorithm):
194 """
195 """
196 super().__init__(algorithm)
197
199 self.machine = AlgorithmMachine(self.algorithm)
200

Member Function Documentation

◆ any_failed_iov()

any_failed_iov ( self)
inherited
Returns:
    bool: If any result in the current results list has a failed algorithm code we return True

Definition at line 152 of file strategies.py.

152 def any_failed_iov(self):
153 """
154 Returns:
155 bool: If any result in the current results list has a failed algorithm code we return True
156 """
157 failed_results = []
158 for result in self.results:
159 if result.result == AlgResult.failure.value or result.result == AlgResult.not_enough_data.value:
160 failed_results.append(result)
161 if failed_results:
162 B2WARNING("Failed results found.")
163 for result in failed_results:
164 if result.result == AlgResult.failure.value:
165 B2ERROR(f"c_Failure returned for {result.iov}.")
166 elif result.result == AlgResult.not_enough_data.value:
167 B2WARNING(f"c_NotEnoughData returned for {result.iov}.")
168 return True
169 else:
170 return False
171

◆ find_iov_gaps()

find_iov_gaps ( self)
inherited
Finds and prints the current gaps between the IoVs of the strategy results. Basically these are the IoVs
not covered by any payload. It CANNOT find gaps if they exist across an experiment boundary. Only gaps
within the same experiment are found.

Returns:
    iov_gaps(list[IoV])

Definition at line 132 of file strategies.py.

132 def find_iov_gaps(self):
133 """
134 Finds and prints the current gaps between the IoVs of the strategy results. Basically these are the IoVs
135 not covered by any payload. It CANNOT find gaps if they exist across an experiment boundary. Only gaps
136 within the same experiment are found.
137
138 Returns:
139 iov_gaps(list[IoV])
140 """
141 iov_gaps = find_gaps_in_iov_list(sorted([result.iov for result in self.results]))
142 if iov_gaps:
143 gap_msg = ["Found gaps between IoVs of algorithm results (regardless of result)."]
144 gap_msg.append("You may have requested these gaps deliberately by not passing in data containing these runs.")
145 gap_msg.append("This may not be a problem, but you will not have payoads defined for these IoVs")
146 gap_msg.append("unless you edit the final database.txt yourself.")
147 B2INFO_MULTILINE(gap_msg)
148 for iov in iov_gaps:
149 B2INFO(f"{iov} not covered by any execution of the algorithm.")
150 return iov_gaps
151

◆ is_valid()

is_valid ( self)
inherited
Returns:
    bool: Whether or not this strategy has been set up correctly with all its necessary attributes.

Definition at line 114 of file strategies.py.

114 def is_valid(self):
115 """
116 Returns:
117 bool: Whether or not this strategy has been set up correctly with all its necessary attributes.
118 """
119 B2INFO("Checking validity of current AlgorithmStrategy setup.")
120 # Check if we're somehow missing a required attribute (should be impossible since they get initialised in init)
121 for attribute_name in self.required_attrs:
122 if not hasattr(self, attribute_name):
123 B2ERROR(f"AlgorithmStrategy attribute {attribute_name} doesn't exist.")
124 return False
125 # Check if any attributes that need actual values haven't been set or were empty
126 for attribute_name in self.required_true_attrs:
127 if not getattr(self, attribute_name):
128 B2ERROR(f"AlgorithmStrategy attribute {attribute_name} returned False.")
129 return False
130 return True
131

◆ run()

run ( self,
iov,
iteration,
queue )
Runs the algorithm machine over the collected data and fills the results.

Reimplemented from AlgorithmStrategy.

Definition at line 201 of file strategies.py.

201 def run(self, iov, iteration, queue):
202 """
203 Runs the algorithm machine over the collected data and fills the results.
204 """
205 if not self.is_valid():
206 raise StrategyError("This AlgorithmStrategy was not set up correctly!")
207 self.queue = queue
208
209 B2INFO(f"Setting up {self.__class__.__name__} strategy for {self.algorithm.name}.")
210 # Now add all the necessary parameters for a strategy to run
211 machine_params = {}
212 machine_params["database_chain"] = self.database_chain
213 machine_params["dependent_databases"] = self.dependent_databases
214 machine_params["output_dir"] = self.output_dir
215 machine_params["output_database_dir"] = self.output_database_dir
216 machine_params["input_files"] = self.input_files
217 machine_params["ignored_runs"] = self.ignored_runs
218 self.machine.setup_from_dict(machine_params)
219 # Start moving through machine states
220 B2INFO(f"Starting AlgorithmMachine of {self.algorithm.name}.")
221 self.machine.setup_algorithm(iteration=iteration)
222 # After this point, the logging is in the stdout of the algorithm
223 B2INFO(f"Beginning execution of {self.algorithm.name} using strategy {self.__class__.__name__}.")
224
225 all_runs_collected = set(runs_from_vector(self.algorithm.algorithm.getRunListFromAllData()))
226 # If we were given a specific IoV to calibrate we just execute all runs in that IoV at once
227 if iov:
228 runs_to_execute = runs_overlapping_iov(iov, all_runs_collected)
229 else:
230 runs_to_execute = all_runs_collected
231
232 # Remove the ignored runs from our run list to execute
233 if self.ignored_runs:
234 B2INFO(f"Removing the ignored_runs from the runs to execute for {self.algorithm.name}.")
235 runs_to_execute.difference_update(set(self.ignored_runs))
236 # Sets aren't ordered so lets go back to lists and sort
237 runs_to_execute = sorted(runs_to_execute)
238 apply_iov = None
239 if "apply_iov" in self.algorithm.params:
240 apply_iov = self.algorithm.params["apply_iov"]
241 self.machine.execute_runs(runs=runs_to_execute, iteration=iteration, apply_iov=apply_iov)
242 B2INFO(f"Finished execution with result code {self.machine.result.result}.")
243
244 # Send out the result to the runner
245 self.send_result(self.machine.result)
246
247 # Make sure the algorithm state and commit is done
248 if (self.machine.result.result == AlgResult.ok.value) or (self.machine.result.result == AlgResult.iterate.value):
249 # Valid exit codes mean we can complete properly
250 self.machine.complete()
251 # Commit all the payloads and send out the results
252 self.machine.algorithm.algorithm.commit()
253 self.send_final_state(self.COMPLETED)
254 else:
255 # Either there wasn't enough data or the algorithm failed
256 self.machine.fail()
257 self.send_final_state(self.FAILED)
258
259

◆ send_final_state()

send_final_state ( self,
state )
inherited
send final state

Definition at line 176 of file strategies.py.

176 def send_final_state(self, state):
177 """send final state"""
178 self.queue.put({"type": "final_state", "value": state})
179
180

◆ send_result()

send_result ( self,
result )
inherited
send result

Definition at line 172 of file strategies.py.

172 def send_result(self, result):
173 """send result"""
174 self.queue.put({"type": "result", "value": result})
175

◆ setup_from_dict()

setup_from_dict ( self,
params )
inherited
Parameters:
    params (dict): Dictionary containing values to be assigned to the strategy attributes of the same name.

Definition at line 106 of file strategies.py.

106 def setup_from_dict(self, params):
107 """
108 Parameters:
109 params (dict): Dictionary containing values to be assigned to the strategy attributes of the same name.
110 """
111 for attribute_name, value in params.items():
112 setattr(self, attribute_name, value)
113

Member Data Documentation

◆ algorithm

algorithm = algorithm
inherited

Algorithm() class that we're running.

Definition at line 80 of file strategies.py.

◆ allowed_granularities

list allowed_granularities = ["run", "all"]
staticinherited

Granularity of collector that can be run by this algorithm properly.

Definition at line 65 of file strategies.py.

◆ COMPLETED

str COMPLETED = "COMPLETED"
staticinherited

Completed state.

Definition at line 71 of file strategies.py.

◆ database_chain

list database_chain = []
inherited

User defined database chain i.e.

the default global tag, or if you have localdb's/tags for custom alignment etc

Definition at line 88 of file strategies.py.

◆ dependent_databases

list dependent_databases = []
inherited

CAF created local databases from previous calibrations that this calibration/algorithm depends on.

Definition at line 90 of file strategies.py.

◆ FAILED

str FAILED = "FAILED"
staticinherited

Failed state.

Definition at line 74 of file strategies.py.

◆ FINISHED_RESULTS

str FINISHED_RESULTS = "DONE"
staticinherited

Signal value that is put into the Queue when there are no more results left.

Definition at line 68 of file strategies.py.

◆ ignored_runs

list ignored_runs = []
inherited

Runs that will not be included in ANY execution of the algorithm.

Usually set by Calibration.ignored_runs. The different strategies may handle the resulting run gaps differently.

Definition at line 93 of file strategies.py.

◆ input_files

list input_files = []
inherited

Collector output files, will contain all files returned by the output patterns.

Definition at line 82 of file strategies.py.

◆ machine

machine = AlgorithmMachine(self.algorithm)

:py:class:caf.state_machines.AlgorithmMachine used to help set up and execute CalibrationAlgorithm It gets setup properly in :py:func:run

Definition at line 199 of file strategies.py.

◆ output_database_dir

str output_database_dir = ""
inherited

The output database directory for the localdb that the algorithm will commit to.

Definition at line 86 of file strategies.py.

◆ output_dir

str output_dir = ""
inherited

The algorithm output directory which is mostly used to store the stdout file.

Definition at line 84 of file strategies.py.

◆ queue

queue = None
inherited

The multiprocessing Queue we use to pass back results one at a time.

Definition at line 97 of file strategies.py.

◆ required_attrs

list required_attrs
staticinherited
Initial value:
= ["algorithm",
"database_chain",
"dependent_databases",
"output_dir",
"output_database_dir",
"input_files",
"ignored_runs"
]

Required attributes that must exist before the strategy can run properly.

Some are allowed be values that return False when tested e.g. "" or []

Definition at line 48 of file strategies.py.

◆ required_true_attrs

list required_true_attrs
staticinherited
Initial value:
= ["algorithm",
"output_dir",
"output_database_dir",
"input_files"
]

Attributes that must have a value that returns True when tested by :py:meth:is_valid.

Definition at line 58 of file strategies.py.

◆ results

list results = []
inherited

The list of results objects which will be sent out before the end.

Definition at line 95 of file strategies.py.

◆ usable_params

dict usable_params = {"apply_iov": IoV}
static

The params that you could set on the Algorithm object which this Strategy would use.

Just here for documentation reasons.

Definition at line 191 of file strategies.py.


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