BaseApp#
The BaseApp includes the structure that all applications must adhere to
for allowing them to run in the AppRunner
.
The basic functions in Apps are used as follows:
Call app.multiprocessing_pre_run to perform general setup tasks for multiprocessing. This method should only be called once because it might be expensive to run.
Run a loop <for index in tasks>:
Check app.multiprocessing_carryon value. If False, wait and repeat the check. If True, continue with processing.
Call app.multiprocessing_func(index) to perform the main calculation task and put the results in a queue or transfer via a signal.
Call app.multiprocessing_store_results (optionally). If the apps are running in parallel, they will skip this step and instead, they will send the results via queue to the AppRunner.
Call app.multiprocessing_post_run to perform cleanup steps.
BaseApp documentation with own methods only
BaseApp documentation with inherited methods too
BaseApp with own methods only#
- class pydidas.core.BaseApp(*args: tuple, **kwargs: dict)#
Bases:
ObjectWithParameterCollection
The BaseApp is the base class for all pydidas applications.
It includes core functionalities and pre-defines the template of required methods for Apps to allow running the multiprocessing
pydidas.multiprocessing.AppRunner
.- Parameters:
*args (tuple) – Any arguments. Defined by the concrete implementation of the app.
**kwargs (dict) – A dictionary of keyword arguments. Defined by the concrete implementation of the app.
- copy(slave_mode: bool = False) Self #
Get a copy of the App.
- Parameters:
slave_mode (bool, optional) – Keyword to toggle creation of a slave app which does not include attributes marked in the classes slave_mode attribute.
- Returns:
App – A copy of the App instance.
- Return type:
- export_state() dict #
Get the sanitized app Parameters and configuration for export.
- Returns:
The state dictionary.
- Return type:
dict
- get_config() dict #
Get the App configuration.
- Returns:
The App configuration stored in the _config dictionary.
- Return type:
dict
- import_state(state: dict)#
Import a stored state including Parameters and configuration.
- Parameters:
state (dict) – The stored state.
Initialize the shared memory array for the master app.
Note: This method is not required for apps without a shared memory.
- multiprocessing_carryon() bool #
Wait for specific tasks to give the clear signal.
This method is called in the parallel multiprocessing process prior to the actual calculation. Apps can specify wait conditions that need to be fulfilled before carrying on. The method returns a boolean flag whether a timeout was encountered.
- Returns:
Flag whether processing can continue or should wait.
- Return type:
bool
- multiprocessing_func(index: int)#
Perform key operation with parallel processing.
This method must be implemented by the BaseApp subclasses.
- Parameters:
index (int) – The index to be processed.
- multiprocessing_get_tasks()#
Return all tasks required in multiprocessing.
This method must be implemented by the BaseApp subclasses.
- multiprocessing_post_run()#
Perform operations after running main parallel processing function.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- multiprocessing_pre_cycle(index: int)#
Perform operations in the pre-cycle of every task.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- Parameters:
index (int) – The index to be processed.
- multiprocessing_pre_run()#
Perform operations prior to running main parallel processing function.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- multiprocessing_store_results(index: int, *args: tuple)#
Store the multiprocessing results for other pydidas apps and processes.
This method must be implemented by the BaseApp subclasses.
- Parameters:
index (int) – The frame index
args (tuple) – The results. The specific type depends on the app.
- parse_args_and_set_params()#
Parse the command line arguments and update the corresponding Parameter values.
- run()#
Run the app serially without multiprocessing support.
BaseApp with inherited methods too#
- class pydidas.core.BaseApp(*args: tuple, **kwargs: dict)
Bases:
ObjectWithParameterCollection
The BaseApp is the base class for all pydidas applications.
It includes core functionalities and pre-defines the template of required methods for Apps to allow running the multiprocessing
pydidas.multiprocessing.AppRunner
.- Parameters:
*args (tuple) – Any arguments. Defined by the concrete implementation of the app.
**kwargs (dict) – A dictionary of keyword arguments. Defined by the concrete implementation of the app.
- add_param(param: Parameter)
Add a parameter to the ParameterCollection.
This is a wrapper for the ParameterCollection.add_parameter method.
- Parameters:
param (Parameter) – An instance of a Parameter object.
- add_params(*params: tuple[Parameter | dict | ParameterCollection])
Add parameters to the object.
This method adds Parameters to the ParameterCollection of the object. Parameters can be either supplies as args or a ParameterCollection or dictionary in the form of <ref_key>: <Parameter>. This method is explicitly separate from the __init__ method to allow subclasses full control over args and kwargs.
- Parameters:
*params (Tuple[Union[Parameter, dict, ParameterCollection]]) – Any Parameter or ParameterCollection
- copy(slave_mode: bool = False) Self
Get a copy of the App.
- Parameters:
slave_mode (bool, optional) – Keyword to toggle creation of a slave app which does not include attributes marked in the classes slave_mode attribute.
- Returns:
App – A copy of the App instance.
- Return type:
- deepcopy() Self
Get a deepcopy of the object.
- Returns:
The object’s deepcopy.
- Return type:
- export_state() dict
Get the sanitized app Parameters and configuration for export.
- Returns:
The state dictionary.
- Return type:
dict
- get_config() dict
Get the App configuration.
- Returns:
The App configuration stored in the _config dictionary.
- Return type:
dict
- classmethod get_default_params_copy() ParameterCollection
Get a copy of the default ParameterCollection.
- Returns:
A copy of the default ParameterCollection.
- Return type:
- get_param(param_key: str) Parameter
Get a parameter.
Note: This method returns the Parameter itself, not a copy.
- Parameters:
param_key (str) – The key name of the Parameter.
- Returns:
The Parameter object.
- Return type:
- get_param_keys() list[str]
Get the keys of all registered Parameters.
- Returns:
The keys of all registered Parameters.
- Return type:
list
- get_param_value(param_key: str, *default: object, dtype: type | None = None, for_export: bool = False) object
Get a Parameter value.
- Parameters:
param_key (str) – The key name of the Parameter.
default (object) – The default value if the param_key does not exist.
dtype (type, optional) – A datatype to convert the value into. If None, the native datatype is returned. The default is None.
for_export (bool, optional) – An optional flag to force converting the Parameter value to an export-compatible format. This flag is not compatible with a specific dtype. The default is False.
- Returns:
The value of the Parameter.
- Return type:
object
- get_param_values_as_dict(filter_types_for_export: bool = False) dict
Get a dictionary with Parameter names and values only.
- Parameters:
filter_types_for_export (bool) – Flag to return objects in types suitable for exporting (i.e. pickleable).
- Returns:
name_val_pairs – The dictionary with Parameter <name>: <value> pairs.
- Return type:
dict
- get_params(*param_keys: tuple[str, ...]) list[Parameter]
Get multiple parameters based on their reference keys.
- Parameters:
*param_keys (Tuple[str, ...]) – Any number of reference keys.
- Returns:
A list with the Parameter instances referenced by the supplied keys.
- Return type:
list
- import_state(state: dict)
Import a stored state including Parameters and configuration.
- Parameters:
state (dict) – The stored state.
- initialize_shared_memory()
Initialize the shared memory array for the master app.
Note: This method is not required for apps without a shared memory.
- multiprocessing_carryon() bool
Wait for specific tasks to give the clear signal.
This method is called in the parallel multiprocessing process prior to the actual calculation. Apps can specify wait conditions that need to be fulfilled before carrying on. The method returns a boolean flag whether a timeout was encountered.
- Returns:
Flag whether processing can continue or should wait.
- Return type:
bool
- multiprocessing_func(index: int)
Perform key operation with parallel processing.
This method must be implemented by the BaseApp subclasses.
- Parameters:
index (int) – The index to be processed.
- multiprocessing_get_tasks()
Return all tasks required in multiprocessing.
This method must be implemented by the BaseApp subclasses.
- multiprocessing_post_run()
Perform operations after running main parallel processing function.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- multiprocessing_pre_cycle(index: int)
Perform operations in the pre-cycle of every task.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- Parameters:
index (int) – The index to be processed.
- multiprocessing_pre_run()
Perform operations prior to running main parallel processing function.
The generic method simple performs no operation and subclasses only need to re-implement it when they explicitly need it.
- multiprocessing_store_results(index: int, *args: tuple)
Store the multiprocessing results for other pydidas apps and processes.
This method must be implemented by the BaseApp subclasses.
- Parameters:
index (int) – The frame index
args (tuple) – The results. The specific type depends on the app.
- property param_keys: list[str]
Get the keys of all stored Parameters.
- Returns:
The keys of all stored Parameters.
- Return type:
list[str]
- property param_values: dict
Get the values of all stored Parameters along with their refkeys.
- Returns:
The refkey, value pairs for all stored Parameters.
- Return type:
Dict
- parse_args_and_set_params()
Parse the command line arguments and update the corresponding Parameter values.
- print_param_values()
Print the name and value of all Parameters.
- q_settings_get(key: str, dtype: type | None = None, default: object | None = None) object
Get the value from a QSetting key.
- Parameters:
key (str) – The QSetting reference key.
dtype (Union[type, None], optional) – A return datatype. If not None, the output will be returned as dtype(value), otherwise, the generic string/int will be returned. The default is None.
default (type, optional) – The default value which is returned if the key defaults to None. The default is None.
- Returns:
value – The value, converted to the type associated with the Parameter referenced by param_key or dtype, if given.
- Return type:
object
- q_settings_set(key: str, value: object)
Set the value of a QSettings key.
- Parameters:
key (str) – The name of the key.
value (object) – The value to be stored.
- restore_all_defaults(confirm: bool = False)
Restore the default values to all entries.
- Parameters:
confirm (bool) – Confirmation flag as safety feature.
- run()
Run the app serially without multiprocessing support.
- set_default_params()
Set default entries.
This method will go through the supplied defaults iterable. If there are no entries for the Parameter keys, it will add a Parameter with default value.
- set_param_value(param_key: str, value: object)
Set a parameter value.
- Parameters:
param_key (str) – The key name of the Parameter.
value (object) – The value to be set. This has to be the datatype associated with the Parameter.
- set_param_values(**kwargs: dict)
Set multiple parameter values at once.
- Parameters:
**kwargs (dict) – The reference key and value pairs for all Parameters to be set.
- set_param_values_from_dict(value_dict: dict)
Set the Parameter values from a dict with name, value paris.
- Parameters:
value_dict (dict) – The dictionary with the stored information.
- update_param_values_from_kwargs(**kwargs: dict)
Update the Parameter values corresponding to the given keys.
- Parameters:
**kwargs (dict) – The dictionary with Parameter refkeys and values.
- update_params_from_init(*args: tuple, **kwargs: dict)
Update the Parameters from the given init args and kwargs.
- Parameters:
*args (Tuple) – The input arguments.
**kwargs (Dict) – The input keyword arguments.