"""Handlers for the playNano CLI commands."""
import argparse
import logging
import re
import sys
from pathlib import Path
from playnano.cli.actions import (
Wizard,
analyze_pipeline_mode,
play_pipeline_mode,
process_pipeline_mode,
)
[docs]
def handle_play(args: argparse.Namespace) -> None:
"""
Handle the 'play' subcommand: feed arguments into the run_pipeline_mode function.
Parameters
----------
args : argparse.Namespace
Must contain:
- input_file (str): path to AFM data
- channel (str)
- processing_str (str or None)
- processing_file (str or None)
- output_folder (str or None)
- output_name (str or None)
- scale_bar_nm (int or None), 0 turns off scale bar
- zmin (float or str), minimum Z-value for normalization
('auto` for 1st percentile)
- zmax (float or str), maximum Z-value for normalization
('auto` for 99th percentile)
- cmap (str), name of colormap to use for visualisation (default=afm_brown)
Returns
-------
None
"""
if isinstance(args.input_file, str):
raw_input = args.input_file.strip()
# Check if argument flags are embedded in the path (a quoted Windows path
# ending in a backslash swallows the rest of the command line). Only a
# flag after whitespace counts, so folder names like "run--2" are allowed.
if re.search(r"(^|\s)--[A-Za-z]", raw_input):
raise ValueError(
f"\nInvalid input path: '{raw_input}'\n"
"⚠️ It looks like your input path includes CLI flags (e.g., '--channel').\n" # noqa: E501
"This usually happens when a quoted Windows path ends with a backslash, " # noqa: E501
"which escapes the closing quote.\n\n"
"💡 FIX: Either:\n"
' - Add another backslash: "C:\\path\\to\\folder\\\\"\n'
' - Remove the trailing backslash: "C:\\path\\to\\folder"\n'
' - Use forward slashes: "C:/path/to/folder"\n'
)
file_path = Path(raw_input).expanduser()
else:
file_path = Path(args.input_file)
try:
play_pipeline_mode(
input_file=file_path,
channel=args.channel,
processing_str=args.processing,
processing_file=args.processing_file,
output_folder=args.output_folder,
output_name=args.output_name,
scale_bar_nm=args.scale_bar_nm,
zmin=args.zmin,
zmax=args.zmax,
cmap=args.cmap,
fps=args.fps,
)
except Exception as e:
logging.getLogger(__name__).error(e)
sys.exit(1)
[docs]
def handle_process(args: argparse.Namespace) -> None:
"""
Handle the 'process' subcommand: feed arguments into the run_pipeline_mode function.
Parameters
----------
args : argparse.Namespace
Must contain:
- input_file (str): path to AFM data
- channel (str)
- processing_str (str or None)
- processing_file (str or None)
- export (str or None), comma-separated formats like "tif,npz,h5"
- make_gif (bool), whether to create a GIF
- output_folder (str or None)
- output_name (str or None)
- scale_bar_nm (int or None), 0 turns off scale bar
- zmin (float or str), minimum Z-value for normalization
('auto` for 1st percentile)
- zmax (float or str), maximum Z-value for normalization
('auto` for 99th percentile)
- cmap (str), name of colormap to use for animated visualisation export
(default=afm_brown)
Returns
-------
None
"""
try:
process_pipeline_mode(
input_file=args.input_file,
channel=args.channel,
processing_str=args.processing,
processing_file=args.processing_file,
export=args.export,
make_gif=args.make_gif,
make_video=args.make_video,
make_sequence=args.make_sequence,
output_folder=args.output_folder,
output_name=args.output_name,
scale_bar_nm=args.scale_bar_nm,
zmin=args.zmin,
zmax=args.zmax,
draw_ts=args.draw_ts,
cmap=args.cmap,
fps=args.fps,
)
except Exception as e:
logging.getLogger(__name__).error(e)
sys.exit(1)
[docs]
def handle_wizard(args: argparse.Namespace) -> None:
"""
Interactive wizard for building both processing and analysis pipelines.
Usage:
playnano processing <input_file> [--channel ...] \
[--output-folder ...] \
[--output-name ...]
After launch, commands include:
add/remove/move/list/save/load/run for processing steps
aadd/aremove/amove/alist/asave/aload/arun for analysis steps
help show available commands
quit exit without running
Parameters
----------
args : argparse.Namespace
Must contain:
- input_file (str): path to AFM data
- channel (str)
- output_folder (str or None)
- output_name (str or None)
- scale_bar_nm (int or None), 0 disables scale bar
- cmap (str), name of colormap to use for visualisation (default=afm_brown)
Returns
-------
None
"""
try:
Wizard(
input_file=args.input_file,
channel=args.channel,
output_folder=args.output_folder,
output_name=args.output_name,
scale_bar_nm=args.scale_bar_nm,
cmap=args.cmap,
).run()
except Exception as e:
logging.getLogger(__name__).error(e)
sys.exit(1)
[docs]
def handle_analyze(args: argparse.Namespace) -> None:
"""Handle the 'analyze' subcommand: run only analysis & export results."""
try:
analyze_pipeline_mode(
input_file=args.input_file,
channel=args.channel,
analysis_str=args.analysis_steps,
analysis_file=args.analysis_file,
output_folder=args.output_folder,
output_name=args.output_name,
)
except Exception as e:
logging.getLogger(__name__).error(e, exc_info=True)
sys.exit(1)