playnano.analysis.modules.particle_region_media_export module

Analysis module that saves tracked particle regions as GIFs, videos or image sequences.

It uses the crops made by particle_region_extraction. For each track, the crops are padded to the size of the track’s largest crop and stacked, then written with the same rendering as the whole-stack exports:

Any combination of the three can be written in one step.

class playnano.analysis.modules.particle_region_media_export.ParticleRegionMediaExportModule[source]

Bases: AnalysisModule

Save each tracked particle’s crops as a GIF, video and/or image sequence.

Pipeline name: "particle_region_media_export". It needs the output of particle_region_extraction, run with include_image=True (the default). Files are written to output_folder, overwriting any with the same name:

  • GIF: <name>_track_<id>.gif;

  • video: <name>_track_<id>.<video_fmt>;

  • image sequence: a folder <name>_track_<id>/ of frame_0000.png, frame_0001.png and so on.

Frames where the particle was not detected are filled with fill_value. See run() for the parameters and outputs.

Examples

After detection, tracking and region extraction, write a GIF of the ten tracks with the most detections:

>>> pipeline.add(
...     "particle_region_media_export",
...     export_gif=True,
...     export_video=False,
...     sort_by="n_detections",
...     max_tracks=10,
... )
property name: str

Name of this module in a pipeline, "particle_region_media_export".

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]

Write the GIFs, videos and image sequences for the selected tracks.

Parameters:
  • stack (AFMImageStack) – The stack: its pixel_size_nm sets the scale bar and its file name the default output name.

  • previous_results (dict) – Outputs of earlier steps. Must hold the output of extraction_module.

  • extraction_module (str, optional) – Name of the region extraction step. Default is "particle_region_extraction".

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

  • output_name (str, optional) – Start of each file name. If None, the name of the file the stack was loaded from.

  • track_ids (list of int, optional) – Export only these tracks. IDs not found are ignored. If None, all tracks. Default is None.

  • max_tracks (int, optional) – Export at most this many tracks, after track_ids and sort_by. Default is None (no limit).

  • sort_by ({"track_id", "n_frames", "n_detections"}, optional) – Order of the tracks before max_tracks is applied: by ID (ascending), by number of frames spanned, or by number of frames with a detection (both descending). Default is "track_id".

  • export_gif (bool, optional) – Write a GIF per track. Default is False.

  • export_video (bool, optional) – Write a video per track. Default is True.

  • video_fmt ({"mp4", "avi", "mov", "mkv"}, optional) – Video format. Default is "mp4".

  • export_sequence (bool, optional) – Write a folder of images per track. Default is False.

  • sequence_fmt ({"png", "jpg", "jpeg"}, optional) – Image format of the sequences. Default is "png".

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

  • cmap_name (str, optional) – Name of a registered matplotlib colormap. Default is "afm_brown".

  • zmin (float or str, optional) – Colour scale limits, as for playnano.io.gif_export.export_gif(): both None scales each frame separately; "auto" uses the 1st or 99th percentile of the track. Padding and missing frames (fill_value) count towards both, so they can change the colours. Default is None.

  • zmax (float or str, optional) – Colour scale limits, as for playnano.io.gif_export.export_gif(): both None scales each frame separately; "auto" uses the 1st or 99th percentile of the track. Padding and missing frames (fill_value) count towards both, so they can change the colours. Default is None.

  • draw_ts (bool, optional) – Draw each frame’s time. Default is True.

  • draw_scale (bool, optional) – Draw a scale bar. Default is True.

  • scale_bar_length_nm (int, optional) – Length of the scale bar, in nm; 0 hides it. Default is 100.

  • font_scale (float, optional) – Size of the annotation text for a frame 512 pixels high. Default is 2.0.

  • fill_value (float, optional) – Value used for padding and for frames where the particle was not detected. Default is 0.0.

Returns:

"gif_paths", "video_paths", "sequence_folders"

Absolute paths of what was written, as str.

"per_track"

One dict per track written, with track_id, n_frames, frame_size (height, width, in pixels) and its gif_path, video_path and sequence_folder.

"summary"

Counts of tracks found, selected, written, skipped (no crops) and filtered out, and the settings used.

Return type:

dict

Raises:
  • RuntimeError – If no export is enabled, previous_results is None or lacks the extraction output, or the extraction ran with include_image=False.

  • ValueError – If sort_by is not recognised, sequence_fmt is not supported, or zmin is greater than zmax.

Notes

Formats are not checked before writing starts, so with an unsupported format some files may already be written when the error is raised. An unsupported video_fmt raises FileNotFoundError rather than ValueError. The scale bar uses the stack’s pixel size, not each frame’s.

version = '0.1.0'