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 itsrunmethod. Parameters whose condition (seeplaynano.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.analysisplugin.- Returns:
Parameter names and values.
- Return type:
- 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,maskandstack) 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:
- 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.
- playnano.cli.utils.is_valid_analysis_step(name: str) bool[source]¶
Check whether a name is an analysis module.
- 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 underplaynano.filtersorplaynano.video_processing. Masked filters are not steps of their own: they are used automatically when a mask is set.- Return type:
- 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
analysislist, or is just the list; each entry has anameand 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.0e3rather than1e3, 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:
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If the file cannot be read, has neither an
analysislist nor a list of steps, or an entry has nonameor 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:
- Raises:
ValueError – If a name is not an analysis module (see
is_valid_analysis_step()), or a parameter is notkey=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
--processingand--analysis-stepsstrings 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/Falsefortrue/falseandNonefornone/null(any case); otherwise a Python literal (number, list, tuple, dict or quoted string) if the text is one, a float fornanorinf, 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
1e3as a string andon/off/yes/noas 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
filterslist; each entry has anameand 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: write1.0e3rather than1e3, 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:
- Raises:
FileNotFoundError – If the file does not exist.
ValueError – If the file cannot be read, has no
filterslist, or an entry has nonameor 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 askey=value, separated by,. Separators inside brackets or quotes do not count, soindices_to_drop=[0,1]stays whole. Values are read withparse_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:
- Raises:
ValueError – If a step name is not a processing step (see
is_valid_step()), or a parameter is notkey=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]})]