playnano.cli.utils module

Helpers for the command line.

Checking step names, reading processing and analysis steps from text or files, and asking for parameters interactively.

playnano.cli.utils.ask_for_analysis_params(module_name: str) → dict[str, Any][source]

Ask in the terminal for each parameter of an analysis module.

Uses the module’s parameters() list if it has one, otherwise the arguments of its run method. Parameters whose condition (see playnano.utils.param_utils.param_conditions()) is not met are not asked. Pressing Enter uses the default; typed values are converted to the parameter’s type.

Parameters:

module_name (str) – Built-in analysis module or playnano.analysis plugin.

Returns:

Parameter names and values.

Return type:

dict

Raises:

ValueError – If no module has that name.

playnano.cli.utils.ask_for_processing_params(step_name: str) → dict[str, Any][source]

Ask in the terminal for each parameter of a processing step.

The parameters are read from the step’s function. Data arguments (such as data, frame, mask and stack) are skipped, as are parameters whose condition is not met. Pressing Enter uses the default (None if there is none); typed values are converted to the parameter’s type.

Parameters:

step_name (str) – Processing step name.

Returns:

Parameter names and values.

Return type:

dict

Raises:

ValueError – If no processing step has that name.

playnano.cli.utils.get_processing_step_type(step_name: str) → str[source]

Name the kind of a processing step, for messages.

Parameters:

step_name (str) – Step name.

Returns:

"filter", "mask generator", "mask filter", "plugin filter", "video plugin", "video filter", "stack edit" or "unknown".

Return type:

str

playnano.cli.utils.is_valid_analysis_step(name: str) → bool[source]

Check whether a name is an analysis module.

Parameters:

name (str) – Module name.

Returns:

True for built-in modules and plugins registered under playnano.analysis.

Return type:

bool

playnano.cli.utils.is_valid_step(name: str) → bool[source]

Check whether a name is a processing step.

Parameters:

name (str) – Step name.

Returns:

True for "clear", built-in filters, masks, video filters and stack edits, and plugins registered under playnano.filters or playnano.video_processing. Masked filters are not steps of their own: they are used automatically when a mask is set.

Return type:

bool

playnano.cli.utils.parse_analysis_file(path: str) → list[tuple[str, dict[str, object]]][source]

Read analysis steps from a YAML or JSON file, as given to --analysis-file.

The file has an analysis list, or is just the list; each entry has a name and its parameters:

analysis:
  - name: feature_detection
    mask_fn: mask_threshold
    threshold: 1.0
    min_size: 10
  - name: particle_tracking
    max_distance: 5.0

Values keep the types YAML or JSON gives them (in YAML, write 1.0e3 rather than 1e3, which is read as text).

Parameters:

path (str or pathlib.Path) – The file: read as JSON if it ends in .json, otherwise as YAML.

Returns:

(module_name, parameters) for each step, in order.

Return type:

list of tuple

Raises:
  • FileNotFoundError – If the file does not exist.

  • ValueError – If the file cannot be read, has neither an analysis list nor a list of steps, or an entry has no name or names an unknown module.

playnano.cli.utils.parse_analysis_string(analysis_str: str) → list[tuple[str, dict[str, object]]][source]

Read analysis steps from text, as given to --analysis-steps.

Written in the same way as for parse_processing_string().

Parameters:

analysis_str (str) – For example "feature_detection:mask_fn=mask_threshold,threshold=1;particle_tracking".

Returns:

(module_name, parameters) for each step, in order.

Return type:

list of tuple

Raises:

ValueError – If a name is not an analysis module (see is_valid_analysis_step()), or a parameter is not key=value.

Examples

>>> from playnano.cli.utils import parse_analysis_string
>>> parse_analysis_string("feature_detection:threshold=1;particle_tracking")
[('feature_detection', {'threshold': 1}), ('particle_tracking', {})]
playnano.cli.utils.parse_param_value(text: str) → Any[source]

Convert one parameter value from the command line into a Python value.

Used for --processing and --analysis-steps strings and for values typed into the wizard, so all three read values the same way.

Parameters:

text (str) – The value as typed, for example "2", "1e3", "[0, 1]" or "none".

Returns:

True/False for true/false and None for none/null (any case); otherwise a Python literal (number, list, tuple, dict or quoted string) if the text is one, a float for nan or inf, and the text itself otherwise.

Return type:

Any

Notes

Python literal syntax is used rather than YAML because PyYAML follows YAML 1.1, which reads 1e3 as a string and on/off/yes/no as booleans. To keep a value that looks like a number as text, quote it: name='1'.

Examples

>>> parse_param_value("1e3")
1000.0
>>> parse_param_value("[0, 1]")
[0, 1]
>>> parse_param_value("None")
>>> parse_param_value("otsu")
'otsu'
playnano.cli.utils.parse_processing_file(path: str) → list[tuple[str, dict[str, object]]][source]

Read processing steps from a YAML or JSON file, as given to --processing-file.

The file has a filters list; each entry has a name and its parameters:

filters:
  - name: remove_plane
  - name: gaussian_filter
    sigma: 2.0
  - name: polynomial_flatten
    order: 2

Values keep the types YAML gives them. YAML reads some values differently from --processing: write 1.0e3 rather than 1e3, which YAML reads as text.

Parameters:

path (str or pathlib.Path) – The file. It is read as YAML, then as JSON if that fails.

Returns:

(step_name, parameters) for each step, in order.

Return type:

list of tuple

Raises:
  • FileNotFoundError – If the file does not exist.

  • ValueError – If the file cannot be read, has no filters list, or an entry has no name or names an unknown step.

playnano.cli.utils.parse_processing_string(processing_str: str) → list[tuple[str, dict[str, object]]][source]

Read processing steps from text, as given to --processing.

Steps are separated by ;. A step can be followed by : and parameters as key=value, separated by ,. Separators inside brackets or quotes do not count, so indices_to_drop=[0,1] stays whole. Values are read with parse_param_value().

Parameters:

processing_str (str) – For example "remove_plane; gaussian_filter:sigma=2.0".

Returns:

(step_name, parameters) for each step, in order.

Return type:

list of tuple

Raises:

ValueError – If a step name is not a processing step (see is_valid_step()), or a parameter is not key=value.

Examples

>>> from playnano.cli.utils import parse_processing_string
>>> parse_processing_string("remove_plane; gaussian_filter:sigma=2.0")
[('remove_plane', {}), ('gaussian_filter', {'sigma': 2.0})]
>>> parse_processing_string("drop_frames:indices_to_drop=[0,1]")
[('drop_frames', {'indices_to_drop': [0, 1]})]