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:
objectTerminal input and output for
Wizard; tests replace it.
- 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_FPSinstead, 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:
objectInteractive 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 untilquit. Typehelpfor 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) oraadd <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>.jsonand<name>.h5.
Step numbers start at 1.
helplists most commands;loadandclearare 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
runprompts 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:
- input_path¶
input_fileas a path.- Type:
- processed_stack¶
Result of the last processing run, reused by
arunwhile the steps are unchanged.- Type:
AFMImageStack or None
- Raises:
FileNotFoundError – If
input_filedoes not exist.LoadError – If it cannot be loaded.
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 byplaynano.cli.utils.parse_analysis_string()(several steps can be given, separated by;).
- handle_add(parts: List[str]) None[source]¶
add <step>: add a processing step, asking for each parameter.
- 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 anameare skipped.
- handle_amove(parts: List[str]) None[source]¶
amove <from> <to>: move an analysis step to a new position (from 1).
- 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>.jsonand<name>.h5in the output folder, each through a temporary file so a failed write leaves no partial file.
- 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.
- 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 bysave; entries without anameare skipped.
- handle_move(parts: List[str]) None[source]¶
move <from> <to>: move a processing step to a new position (from 1).
- 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.
- 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), soloadand--processing-filecan read the file. Missing folders are created.
- prompt_for_processing_params(step_name: str) Dict[str, Any][source]¶
Ask for a few common parameters of a processing step.
Used by
addwhen 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) andthreshold(mask_threshold,mask_below_threshold). Pressing Enter uses the default shown; an invalid value is asked for again.
- 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:
- 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"(seeplaynano.cli.utils.parse_analysis_string()).analysis_file (str or None) – YAML or JSON analysis file (see
playnano.cli.utils.parse_analysis_file()). Used instead ofanalysis_strif 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>.jsonand<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, oroutput_namecontains 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_strif 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).
- 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 inoutput_folderand are named fromoutput_name; seeplaynano.io.export_data.export_bundles(),playnano.io.gif_export.export_gif(),playnano.io.video_export.export_video()andplaynano.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"(seeplaynano.cli.utils.parse_processing_string()).processing_file (str or None) – YAML or JSON processing file (see
playnano.cli.utils.parse_processing_file()). Used instead ofprocessing_strif 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
exportnames 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 instack.processed, which processing adds.- Parameters:
stack (AFMImageStack) – The stack to check.