playnano.analysis.modules.particle_region_media_export module¶
Particle region export module for the playNano analysis pipeline.
This module exports per-particle GIFs, video files, and/or image sequences
using the cropped image regions produced by ParticleRegionExtractionModule.
For each tracked particle the per-frame crops are centre-padded to a
consistent spatial size, assembled once into a (T, H, W) NumPy array, and
passed to whichever of the following rendering functions are enabled:
playnano.io.gif_export.create_gif_with_scale_and_timestamp()for animated GIF output.playnano.io.video_export.create_video_with_scale_and_timestamp()for MP4/AVI/MOV/MKV output.playnano.io.image_sequence_export.create_image_sequence()for per-frame PNG/JPEG output.
Any combination of the three export types can be produced in a single pipeline step from the same assembled stack. At least one must be enabled.
Output paths follow the pattern <base>_track_<id>.<ext>; image sequences
are written into per-track sub-directories <base>_track_<id>/ with frames
named frame_NNNN.<fmt>.
The module is designed to integrate with the playNano AnalysisPipeline.
Its primary side-effects are writing files to disk; the returned results dict
records a manifest of everything written, plus a summary.
- class playnano.analysis.modules.particle_region_media_export.ParticleRegionMediaExportModule[source]¶
Bases:
AnalysisModuleAnalysis module to export GIFs, videos and image sequences of extracted particles.
Export per-track GIFs, videos, and/or image sequences from pre-extracted particle image regions, with optional track filtering and sorting.
One output is produced per tracked particle per enabled export type. All enabled types share the same padded
(T, H, W)stack assembly step, so enabling multiple formats incurs no redundant work beyond the extra write calls.The module requires: - output from
ParticleRegionExtractionModule("particle_region_extraction")run with
include_image=True.Parameters accepted in
run()¶- track_idslist of int or None
Explicit whitelist of track IDs to export.
Noneexports all tracks. Applied beforesort_byandmax_tracks.- max_tracksint or None
Maximum number of tracks to export. Applied after
track_idsfiltering andsort_byordering.Noneexports all remaining tracks.- sort_by{“track_id”, “n_frames”, “n_detections”}
Sort order used before applying
max_tracks."track_id"(default) gives stable ascending order;"n_frames"and"n_detections"sort descending so the most complete tracks are exported first.- export_gifbool
Whether to write an animated GIF per track. Default
False.- export_videobool
Whether to write a video file per track. Default
True.- video_fmtstr
Video container format:
"mp4","avi","mov", or"mkv". Default"mp4".- export_sequencebool
Whether to write a per-frame image sequence per track. Default
False.- sequence_fmtstr
Image format for sequences:
"png","jpg", or"jpeg". Default"png".- output_folderstr or None
Directory to write output into. Defaults to
"output".- output_namestr or None
Base name stem, derived from the stack file name when
None. Videos are written as<base>_track_<id>.<video_fmt>. Sequence sub-directories are named<base>_track_<id>/.- fpsfloat
Playback frame rate in frames per second. Default
5.0.- cmap_namestr
Matplotlib colourmap name. Default
DEFAULT_CMAP.- zmin, zmaxfloat, str, or None
Z-range for normalisation forwarded to the rendering functions.
"auto"uses the 1st/99th percentile across all crops for the track;Nonenormalises each frame independently.- draw_tsbool
Whether to annotate frames with a timestamp. Default
True.- draw_scalebool
Whether to draw a scale bar. Default
True.- scale_bar_length_nmint
Scale bar length in nm. Default
100.- font_scalefloat
Font scale for annotations. Default
DEFAULT_FONT_SCALE.- fill_valuefloat
Fill value for padding and missing-frame placeholders. Default
0.0.- extraction_modulestr
Key of the upstream extraction module in
previous_results. Default"particle_region_extraction".
- requires = ['particle_region_extraction']¶
- run(stack: Any, previous_results: Dict[str, Any] | None = None, *, extraction_module: str = 'particle_region_extraction', output_folder: str | None = None, output_name: str | None = None, track_ids: List[int] | None = None, max_tracks: int | None = None, sort_by: str = 'track_id', export_gif: bool = False, export_video: bool = True, video_fmt: str = 'mp4', export_sequence: bool = False, sequence_fmt: str = 'png', fps: float = 5.0, cmap_name: str = 'afm_brown', zmin: float | str | None = None, zmax: float | str | None = None, draw_ts: bool = True, draw_scale: bool = True, scale_bar_length_nm: int = 100, font_scale: float = 2.0, fill_value: float = 0.0) Dict[str, Any][source]¶
Execute the video and/or image-sequence export.
- Parameters:
stack (AFMImageStack) – Input stack.
stack.pixel_size_nmandstack.file_pathare used for scale bar calibration and default output naming.previous_results (dict, optional) – Outputs from earlier pipeline modules. Must contain output from
extraction_modulewithinclude_image=True.extraction_module (str, default
"particle_region_extraction") – Key identifying the region extraction module output.output_folder (str or None) – Directory to write output. Defaults to
"output".output_name (str or None) – Base file name stem. Track ID is appended automatically.
track_ids (list of int or None, default None) – Explicit whitelist of track IDs to export.
Noneexports all tracks. IDs not present in the extraction output are silently ignored.max_tracks (int or None, default None) – Maximum number of tracks to export after filtering and sorting. Useful for QC runs: e.g.
sort_by="n_frames", max_tracks=10exports the ten longest-lived particles.Noneexports all remaining tracks.sort_by (str, default
"track_id") –Sort key applied after
track_idsfiltering and beforemax_tracks. Options:"track_id": ascending by track ID (stable default)."n_frames": descending by total frame count."n_detections": descending by number of frames with a non-missing detection.
export_gif (bool, default False) – Write an animated GIF (
<base>_track_<id>.gif) per track.export_video (bool, default True) – Write a video file (
<base>_track_<id>.<video_fmt>) per track.video_fmt (str, default
"mp4") – Video container format. One of"mp4","avi","mov","mkv".export_sequence (bool, default False) – Write a folder of per-frame images (
<base>_track_<id>/frame_NNNN.<sequence_fmt>) per track.sequence_fmt (str, default
"png") – Image format for sequence frames. One of"png","jpg","jpeg".fps (float, default 5.0) – Playback frame rate in frames per second (video only).
cmap_name (str, default
DEFAULT_CMAP) – Matplotlib colourmap for false-colour rendering.zmin (float, str, or None) – Lower bound for z-normalisation, forwarded to the rendering functions.
zmax (float, str, or None) – Upper bound for z-normalisation, forwarded to the rendering functions.
draw_ts (bool, default True) – Whether to overlay timestamps on frames.
draw_scale (bool, default True) – Whether to overlay a scale bar on frames.
scale_bar_length_nm (int, default 100) – Scale bar length in nanometres.
font_scale (float, default
DEFAULT_FONT_SCALE) – Font scale for timestamp and scale bar annotations.fill_value (float, default 0.0) – Value used to fill padded pixels and missing-frame placeholders.
- Returns:
Keys:
gif_pathslist of strAbsolute paths of written GIF files. Empty when
export_gif=False.video_pathslist of strAbsolute paths of written video files. Empty when
export_video=False.sequence_folderslist of strAbsolute paths of written sequence folders. Empty when
export_sequence=False.per_tracklist of dictOne entry per track containing
track_id,n_frames,frame_size, and whichever ofgif_path/video_path/sequence_folderwere produced.summarydictBookkeeping counters and configuration echo.
- Return type:
- Raises:
RuntimeError – If none of the export flags are enabled, or if required upstream outputs are missing or misconfigured.
ValueError – If
sort_by,video_fmt, orsequence_fmtis not a supported value.
- version = '0.1.0'¶