playnano.cli.actions module

Core logic for CLI actions in playNano. What the playnano subcommands do.

process, analyze, play, wizard and env-info.

Called by playnano.cli.handlers; the functions can also be called from Python.

playnano.cli.actions.DEFAULT_PLAYBACK_FPS = 5.0

Playback rate used when a stack has no usable frame timing, or when its real-time rate is below MIN_REALTIME_FPS.

class playnano.cli.actions.IO[source]

Bases: object

Terminal input and output for Wizard; tests replace it.

ask(prompt: str) → str[source]

Show a prompt and return the line typed.

Parameters:

prompt (str) – Text shown before the input.

Returns:

The line typed, without the newline.

Return type:

str

say(msg: str) → None[source]

Print a message.

Parameters:

msg (str) – The message.

playnano.cli.actions.MIN_REALTIME_FPS = 1.0

Slowest real-time rate that is played back as recorded. Slower series (e.g. slow-scan AFM with seconds or minutes per frame) play at DEFAULT_PLAYBACK_FPS instead, which is watchable.

class playnano.cli.actions.Wizard(input_file: str, channel: str, output_folder: str | None, output_name: str | None, scale_bar_nm: int | None, io: IO | None = None, cmap: str = 'afm_brown')[source]

Bases: object

Interactive prompt for building and running processing and analysis.

This is playnano wizard. The stack is loaded when the wizard is made; run() then reads commands until quit. Type help for the list.

Processing commands:

  • add <step>: add a processing step, asking for its parameters;

  • clear: add a step that removes the current mask;

  • remove <n>, move <from> <to>, list: edit and show the steps;

  • save <file>, load <file>: write or read a YAML processing file;

  • run: process the stack, then offer to export data files, a GIF and a video.

Analysis commands:

  • aadd <module> (asks for parameters) or aadd <module>:key=value,...: add an analysis step;

  • aremove <n>, amove <from> <to>, alist: edit and show the steps;

  • asave <file>, aload <file>: write or read an analysis file;

  • arun: run the processing (if there are steps and they changed since the last run) and then the analysis, and save the results as <name>.json and <name>.h5.

Step numbers start at 1. help lists most commands; load and clear are missing from it.

Parameters:
  • input_file (str) – File or folder to load.

  • channel (str) – Channel to load, for example "height_trace".

  • output_folder (str or None) – Folder for exports and analysis results. If None, ./output.

  • output_name (str or None) – Start of the output file names. If None, the input file’s name.

  • scale_bar_nm (int or None) – Scale bar length used when a length typed at the run prompts is not a whole number. If None, 100.

  • io (IO, optional) – Reads commands and prints messages. Default is None (the terminal). Mainly for tests.

  • cmap (str, optional) – Colormap for GIF and video exports. Default is "afm_brown".

afm_stack

The stack as loaded.

Type:

AFMImageStack

input_path

input_file as a path.

Type:

pathlib.Path

process_steps

Processing steps, as (name, parameters).

Type:

list of tuple

analysis_steps

Analysis steps, as (name, parameters).

Type:

list of tuple

processed_stack

Result of the last processing run, reused by arun while the steps are unchanged.

Type:

AFMImageStack or None

processed_steps_snapshot

The processing steps processed_stack was made with.

Type:

list of tuple

Raises:

Examples

>>> wizard = Wizard(
...     "sample.h5-jpk", "height_trace", None, None, None
... )
>>> wizard.run()
handle_aadd(parts: List[str]) → None[source]

aadd: add an analysis step.

aadd <module> asks for each parameter; aadd <module>:key=value,... gives them directly, read as by playnano.cli.utils.parse_analysis_string() (several steps can be given, separated by ;).

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_add(parts: List[str]) → None[source]

add <step>: add a processing step, asking for each parameter.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_alist(parts: List[str]) → None[source]

alist: print the analysis steps.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_aload(parts: List[str]) → None[source]

aload <file>: replace the analysis steps with those in a YAML or JSON file.

Accepts {"analysis": [...]} or a plain list of steps. Text values that are Python literals, such as "[1, 2]", are converted. Entries without a name are skipped.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_amove(parts: List[str]) → None[source]

amove <from> <to>: move an analysis step to a new position (from 1).

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_aremove(parts: List[str]) → None[source]

aremove <n>: remove analysis step n (from 1).

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_arun(parts: List[str]) → None[source]

arun: run the analysis and save the results.

If there are processing steps, the stack is processed first, unless it was already processed with the same steps; otherwise the analysis runs on the stack as loaded. Results are written to <name>.json and <name>.h5 in the output folder, each through a temporary file so a failed write leaves no partial file.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_asave(parts: list[str]) → None[source]

asave <file>: write the analysis steps to a YAML file.

Parameters that are None, or that do not apply, are left out, and values are converted to plain YAML types. Missing folders are created.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_clear(parts: List[str]) → None[source]

clear: add a step that removes the current mask.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_load(parts: List[str]) → None[source]

load <file>: replace the processing steps with those in a YAML file.

Reads a filters: list as written by save; entries without a name are skipped.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_move(parts: List[str]) → None[source]

move <from> <to>: move a processing step to a new position (from 1).

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_remove(parts: List[str]) → None[source]

remove <n>: remove processing step n (from 1).

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_run(parts: List[str]) → None[source]

run: process the stack, then offer to export it.

Asks in turn whether to export data files (and in which formats), a GIF and a video, and for each image export the colour scale limits, timestamps and scale bar. The processed stack is kept for arun.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

handle_save(parts: list[str]) → None[source]

save <file>: write the processing steps to a YAML file.

Parameters that are None, or that do not apply (see playnano.utils.param_utils.prune_kwargs()), are left out. Values are converted to plain YAML types (tuples to lists, NumPy numbers to Python numbers, paths to text), so load and --processing-file can read the file. Missing folders are created.

Parameters:

parts (list of str) – The command line typed at the prompt, split on spaces; parts[0] is the command.

list_analysis() → None[source]

Print the analysis steps, numbered from 1.

list_steps() → None[source]

Print the processing steps, numbered from 1.

print_help() → None[source]

Print the wizard commands.

prompt_for_processing_params(step_name: str) → Dict[str, Any][source]

Ask for a few common parameters of a processing step.

Used by add when the step’s parameters cannot be read from its function. Only these are asked for: sigma (gaussian_filter), order (polynomial_flatten), factor (mask_mean_offset) and threshold (mask_threshold, mask_below_threshold). Pressing Enter uses the default shown; an invalid value is asked for again.

Parameters:

step_name (str) – Name of the processing step.

Returns:

Parameter names and values; empty for other steps.

Return type:

dict

run() → None[source]

Read and carry out commands until quit.

Raises:

SystemExit – With status 0, on quit, exit, Ctrl+C or end of input.

run_processing_and_cache()[source]

Load the file again, process it with the current steps, and keep the result.

Returns:

The processed stack, also stored in processed_stack.

Return type:

AFMImageStack

Raises:

LoadError – If loading or processing fails.

playnano.cli.actions.analyze_pipeline_mode(input_file: str, channel: str, analysis_str: str | None, analysis_file: str | None, output_folder: str | None, output_name: str | None) → None[source]

Analyse a stack and save the results as JSON and HDF5.

This is playnano analyze. The stack is loaded as it is, so load a file saved after processing (or one that needs none); a warning is logged if it has not been processed.

The JSON file holds the full analysis record, with arrays as lists; the HDF5 file holds the same record (see playnano.analysis.utils.common.export_to_hdf5()). Both are written even if one fails.

Parameters:
  • input_file (str) – File or folder to load.

  • channel (str) – Channel to load, for example "height_trace".

  • analysis_str (str or None) – Analysis steps as text, for example "feature_detection:threshold=5;particle_tracking" (see playnano.cli.utils.parse_analysis_string()).

  • analysis_file (str or None) – YAML or JSON analysis file (see playnano.cli.utils.parse_analysis_file()). Used instead of analysis_str if both are given.

  • output_folder (str or None) – Folder for the two files; created if missing. If None, ./output.

  • output_name (str or None) – Name of the two files, <name>.json and <name>.h5. Any folder part and a known extension are removed. If None, the input file’s name.

Raises:
  • ValueError – If an output file would overwrite input_file, or output_name contains invalid characters. Both are checked before the data is loaded.

  • RuntimeError – If the JSON or HDF5 file could not be written.

playnano.cli.actions.play_pipeline_mode(input_file: str, channel: str, processing_str: str | None, processing_file: str | None, output_folder: str | None, output_name: str | None, scale_bar_nm: int | None, fps: float | None, zmin: str = 'auto', zmax: str = 'auto', cmap: str = 'afm_brown') → None[source]

Open a stack in the playNano GUI.

This is playnano play. The processing steps are not applied when the window opens: press F in the window to apply them (or, if none were given, a default set of steps). The window can also export a GIF (G) and data files (E).

Parameters:
  • input_file (str) – File or folder to load.

  • channel (str) – Channel to load, for example "height_trace".

  • processing_str (str or None) – Processing steps as text, as for process_pipeline_mode().

  • processing_file (str or None) – YAML or JSON processing file. Used instead of processing_str if both are given.

  • output_folder (str or None) – Folder for exports from the window.

  • output_name (str or None) – Start of the exported file names.

  • scale_bar_nm (int or None) – Scale bar length, in nm; 0 hides it. If None, 100.

  • fps (float or None) – Playback frame rate. If None, the rate the data was recorded at, or 5 frames per second if that is unknown or slower than 1 frame per second.

  • zmin (float or str, optional) – Colour scale limits: a number, or "auto" (1st and 99th percentiles). A value that is neither is replaced by "auto" with a warning; the limits can be changed in the window. Default is "auto".

  • zmax (float or str, optional) – Colour scale limits: a number, or "auto" (1st and 99th percentiles). A value that is neither is replaced by "auto" with a warning; the limits can be changed in the window. Default is "auto".

  • cmap (str, optional) – Colormap the window opens with. Default is "afm_brown".

Raises:

LoadError – If the file cannot be loaded.

playnano.cli.actions.print_env_info()[source]

Print the software environment as JSON (playnano env-info).

See playnano.utils.system_info.gather_environment_info().

playnano.cli.actions.process_pipeline_mode(input_file: str, channel: str, processing_str: str | None, processing_file: str | None, export: str | None, make_gif: bool, make_video: str | None, make_sequence: bool, output_folder: str | None, output_name: str | None, scale_bar_nm: int | None, zmin: str = 'auto', zmax: str = 'auto', draw_ts: bool = False, cmap: str = 'afm_brown', fps: float = 5.0) → None[source]

Process a stack, then export it as files, a GIF, videos and/or images.

This is playnano process. Output files go in output_folder and are named from output_name; see playnano.io.export_data.export_bundles(), playnano.io.gif_export.export_gif(), playnano.io.video_export.export_video() and playnano.io.image_sequence_export.export_image_sequence().

Parameters:
  • input_file (str) – AFM file or folder to load.

  • channel (str) – Channel to load, for example "height_trace".

  • processing_str (str or None) – Processing steps as text, for example "remove_plane;gaussian_filter:sigma=2" (see playnano.cli.utils.parse_processing_string()).

  • processing_file (str or None) – YAML or JSON processing file (see playnano.cli.utils.parse_processing_file()). Used instead of processing_str if both are given. If neither is given, the data is exported unprocessed.

  • export (str or None) – Comma-separated data formats: "tif", "npz" and/or "h5". Surrounding quotes are removed. None exports no data files.

  • make_gif (bool) – Write an animated GIF.

  • make_video (str or None) – Comma-separated video formats, for example "mp4" or "mp4,avi". None writes no video.

  • make_sequence (bool) – Write a folder of PNG images, one per frame.

  • output_folder (str or None) – Folder for all outputs; created if missing. If None, ./output.

  • output_name (str or None) – Start of the output file names. If None, the input file’s name.

  • scale_bar_nm (int or None) – Scale bar length on the GIF, videos and images, in nm; 0 hides it. If None, 100.

  • zmin (float or str, optional) – Colour scale limits for the GIF, videos and images: a number, "auto" (1st and 99th percentiles of the stack) or None (each frame scaled separately). Default is "auto".

  • zmax (float or str, optional) – Colour scale limits for the GIF, videos and images: a number, "auto" (1st and 99th percentiles of the stack) or None (each frame scaled separately). Default is "auto".

  • draw_ts (bool, optional) – Draw each frame’s time on the GIF, videos and images. Default is False.

  • cmap (str, optional) – Colormap name; an unknown name gives a warning and "afm_brown". Default is "afm_brown".

  • fps (float, optional) – Frames per second of the GIF and videos. Default is 5.0.

Raises:
  • SystemExit – With status 1, if loading or processing fails (the error is logged).

  • ValueError – If export names an unsupported format, or an output name or folder is invalid.

playnano.cli.actions.warn_if_unprocessed(stack: AFMImageStack) → None[source]

Log a warning if a stack has not been through a processing pipeline.

Checks for a "raw" entry in stack.processed, which processing adds.

Parameters:

stack (AFMImageStack) – The stack to check.