DirectorySpyApp#

DirectorySpyApp with inherited methods#

class pydidas.apps.DirectorySpyApp(*args, **kwargs)#

Bases: BaseApp

An App to scan a folder and load the latest image and keep it in shared memory.

To run the app in the background, please refer to the pydidas.multiprocessing.AppRunner documentation.

Notes

The full list of keyword arguments used by the DirectorySpyApp:

scan_for_allbool, optional

Flag to toggle scanning for all new files or only for files matching the input pattern (defined with the Parameter filename_pattern). The default is False.

filename_patternpathlib.Path, optional

The pattern of the filename. Use hashes “#” for wildcards which will be filled in with numbers. This Parameter must be set if scan_for_all is False. The default is an empty Path().

directory_pathpathlib.Path, optional

The absolute path of the directory to be used. This Parameter is only used when scan_for_all is True, but it is mandatory then. The default is an empty Path().

hdf5_keyHdf5key, optional

Used only for hdf5 files: The dataset key. The default is entry/data/data.

use_det_maskbool, optional

Keyword to enable or disable using the global detector mask as defined by the global mask file and mask value. The default is True.

detector_mask_fileUnion[pathlib.Path, str], optional

The full path to the detector mask file.

det_mask_valfloat, optional

The display value for masked pixels. The default is 0.

use_bg_filebool, optional

Keyword to toggle usage of background subtraction. The default is False.

bg_fileUnion[str, pathlib.Path], optional

The name of the file used for background correction. The default is an empty Path.

bg_hdf5_keyHdf5key, optional

Required for hdf5 background image files: The dataset key with the image for the background file. The default is entry/data/data

bg_hdf5_frameint, optional

Required for hdf5 background image files: The image number of the background image in the dataset. The default is 0.

Parameters:
  • *args (tuple) – Any number of Parameters. These will be added to the app’s ParameterCollection.

  • **kwargs (dict) – Parameters supplied with their reference key as dict key and the Parameter itself as value.

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:

BaseApp

deepcopy() Self#

Get a deepcopy of the object.

Returns:

The object’s deepcopy.

Return type:

ObjectWithParameterCollection

define_path_and_name()#

Define the path and the name pattern to search for files.

Raises:

UserConfigError – If the naming pattern could not be interpreted.

export_state() dict#

Get the sanitized app Parameters and configuration for export.

Returns:

The state dictionary.

Return type:

dict

property filename: str#

Get the currently stored filename.

Returns:

The filename.

Return type:

str

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:

ParameterCollection

get_image(fname: Path | str) Dataset#

Get an image from the given filename.

In case of HDF5 files, the method will always return the last frame.

Parameters:

fname (Union[pathlib.Path, str]) – The filename (including full path) to the image file.

Returns:

The image.

Return type:

np.ndarray

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:

Parameter

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

property image: ndarray#

Get the currently stored image.

Returns:

The image data

Return type:

np.ndarray

property image_metadata: dict#

Get the currently stored image metadata.

For non-Hdf5 files, this will be an empty dictionary.

Returns:

The metadata.

Return type:

dict

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 arrays based on the buffer size and the result shapes.

multiprocessing_carryon() bool#

Wait for specific tasks to give the clear signal.

This method will be re-implemented by the prepare_run method.

Returns:

Flag whether processing can continue or should wait.

Return type:

bool

multiprocessing_func(index: int = -1) Tuple[int, str]#

Read the latest image. If the latest image cannot be read (e.g. the file is currently being written), the 2nd latest file will be read.

Returns:

  • index (Union[int, None]) – The input index. As this parameter is not used for this app and only implemented for compatibility, this will generally be None for the DirectorySpyApp. The default is None.

  • filename (str) – The full filename of the file being read

multiprocessing_get_tasks()#

The DirectorySpyApp does not use tasks and will always return an empty list.

multiprocessing_post_run()#

Perform operations after running main parallel processing function.

multiprocessing_pre_cycle(index: int)#

Perform operations in the pre-cycle of every task.

Parameters:

index (int) – The image index.

multiprocessing_pre_run()#

Perform operations prior to running main parallel processing function.

multiprocessing_store_results(index: int, fname: str, *args)#

Store the multiprocessing results for other pydidas apps and processes.

Parameters:
  • index (int) – The frame index. This entry is kept for compatibility and not used in this app.

  • fname (str) – The filename

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.

parse_func() dict#

Parse the command line arguments for the DirectorySpyApp.

Parameters:

caller (object, optional) – If this function is called by a class as method, it requires a single argument which corresponds to the instance.

Returns:

A dictionary with the parsed arugments which holds all the entries and entered values or - if missing - the default values.

Return type:

dict

prepare_run()#

Prepare running the directory spy app.

For the main App (i.e. running not in slave_mode), this involves the following steps:

  1. Get the shape of all results from the WorkflowTree and store them for internal reference.

  2. Get all multiprocessing tasks from the ScanContext.

  3. Calculate the required buffer size and verify that the memory requirements are okay.

  4. Initialize the shared memory arrays.

Both the slaved and the main applications then initialize local numpy arrays from the shared memory.

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.

reset_runtime_vars()#

Reset the runtime variables for a new run.

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.