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

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

 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

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

Base class for Algorithm strategies. These do the actual execution of a single
algorithm on collected data. Each strategy may be quite different in terms of how fast it may be,
how database payloads are passed between executions, and whether or not final payloads have an IoV
that is independent to the actual runs used to calculates them.

Parameters:
    algorithm (:py:class:`caf.framework.Algorithm`): The algorithm we will run

This base class defines the basic attributes and methods that will be automatically used by the selected AlgorithmRunner.
When defining a derived class you are free to use these attributes or to implement as much functionality as you want.

If you define your derived class with an __init__ method, then you should first call the base class
`AlgorithmStrategy.__init__()`  method via super() e.g.

>>> def __init__(self):
>>>     super().__init__()

The most important method to implement is :py:meth:`AlgorithmStrategy.run` which will take an algorithm and execute it
in the required way defined by the options you have selected/attributes set.

Definition at line 24 of file strategies.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
algorithm )
 

Definition at line 76 of file strategies.py.

76 def __init__(self, algorithm):
77 """
78 """
79
80 self.algorithm = algorithm
81
82 self.input_files = []
83
84 self.output_dir = ""
85
86 self.output_database_dir = ""
87
88 self.database_chain = []
89
90 self.dependent_databases = []
91
93 self.ignored_runs = []
94
95 self.results = []
96
97 self.queue = None
98

Member Function Documentation

◆ any_failed_iov()

any_failed_iov ( self)
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)
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)
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 )
Abstract method that needs to be implemented. It will be called to actually execute the
algorithm.

Reimplemented in SequentialBoundaries, SequentialRunByRun, SimpleRunByRun, and SingleIOV.

Definition at line 100 of file strategies.py.

100 def run(self, iov, iteration, queue):
101 """
102 Abstract method that needs to be implemented. It will be called to actually execute the
103 algorithm.
104 """
105

◆ send_final_state()

send_final_state ( self,
state )
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 )
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 )
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

Algorithm() class that we're running.

Definition at line 80 of file strategies.py.

◆ allowed_granularities

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

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

Definition at line 65 of file strategies.py.

◆ COMPLETED

str COMPLETED = "COMPLETED"
static

Completed state.

Definition at line 71 of file strategies.py.

◆ database_chain

list database_chain = []

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 = []

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"
static

Failed state.

Definition at line 74 of file strategies.py.

◆ FINISHED_RESULTS

str FINISHED_RESULTS = "DONE"
static

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 = []

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 = []

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

Definition at line 82 of file strategies.py.

◆ output_database_dir

str output_database_dir = ""

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 = ""

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

Definition at line 84 of file strategies.py.

◆ queue

queue = None

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
static
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
static
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 = []

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

Definition at line 95 of file strategies.py.


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