playnano.analysis.modules.particle_region_extraction module

Analysis module that cuts out each tracked particle’s region, frame by frame.

For each track from particle_tracking and each frame it spans, the module finds the particle’s labelled region in the feature_detection output and records:

  • its tight bounding box,

  • a bounding box enlarged by padding, made square or set to a fixed size,

  • a crop of the frame to that box (optional),

  • a crop of the particle’s mask to that box (optional).

It only organises data: per-frame measurements come from the detection step, and later modules such as tracked_particle_boundary_size and particle_region_media_export use its output.

class playnano.analysis.modules.particle_region_extraction.ParticleRegionExtractionModule[source]

Bases: AnalysisModule

Cut out each tracked particle’s region in every frame of its track.

Pipeline name: "particle_region_extraction". It needs the outputs of particle_tracking and of a detection step that gives labelled regions (feature_detection). See run() for the parameters and outputs.

Bounding boxes are (min_row, min_col, max_row, max_col) in pixels, with the maxima exclusive, so frame[min_row:max_row, min_col:max_col] is the crop. Image crops are taken from stack.data, so from processed data if the stack was processed.

Examples

>>> import numpy as np
>>> from playnano.afm_stack import AFMImageStack
>>> from playnano.analysis.pipeline import AnalysisPipeline
>>> data = np.zeros((3, 64, 64))
>>> for t in range(3):
...     data[t, 10 + t : 16 + t, 10:16] = 3.0
>>> stack = AFMImageStack(
...     data,
...     pixel_size_nm=2.0,
...     channel="height_trace",
...     file_path="demo.h5-jpk",
...     frame_metadata=[{"timestamp": 0.5 * t} for t in range(3)],
... )
>>> pipeline = AnalysisPipeline()
>>> pipeline.add("feature_detection", mask_fn="mask_threshold", threshold=1.0)
>>> pipeline.add("particle_tracking", max_distance=5)
>>> pipeline.add("particle_region_extraction", padding=2)
>>> record = pipeline.run(stack)
>>> output = record["analysis"]["step_3_particle_region_extraction"]
>>> region = output["per_track"][0]["regions"][1]
>>> region["bbox_tight"], region["bbox_padded"], region["image_crop"].shape
((11, 10, 17, 16), (9, 8, 19, 18), (10, 10))
property name: str

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

requires = ['particle_tracking']
run(stack: Any, previous_results: Dict[str, Any] | None = None, *, tracking_module: str = 'particle_tracking', detection_module: str = 'feature_detection', padding: int = 0, fixed_box_size: int | None = None, square: bool = False, fixed_size_mode: str = 'global', include_image: bool = True, include_mask: bool = True, include_bbox: bool = True, label_key: str = 'label') → Dict[str, Any][source]

Find, box and crop each tracked particle’s region in each frame.

Parameters:
  • stack (AFMImageStack) – The stack: crops are taken from stack.data and times from its frame metadata (the frame index if a frame has no time).

  • previous_results (dict) – Outputs of earlier steps. Must hold the outputs of tracking_module and detection_module.

  • tracking_module (str, optional) – Name of the tracking step. Default is "particle_tracking".

  • detection_module (str, optional) – Name of the detection step. Its output must have "labeled_masks" and "features_per_frame". Default is "feature_detection".

  • padding (int, optional) – Pixels added on each side of the tight box, limited by the frame edges. Default is 0.

  • fixed_box_size (int, optional) – Make every box this many pixels square, centred on the padded box and shifted to stay inside the frame. Overrides square. Default is None.

  • square (bool, optional) – Make the padded box square, with sides equal to its longer side. Not used if a fixed size is set. Default is False.

  • fixed_size_mode ({"global", "per_track"}, optional) – "global" uses fixed_box_size for every track. "per_track" gives each track its own size: its largest tight box side plus 2 * padding. "per_track" only takes effect if fixed_box_size is also set (to any value, which is then not used). Default is "global".

  • include_image (bool, optional) – Add "image_crop" to each region. Default is True.

  • include_mask (bool, optional) – Add "mask_crop" to each region. Default is True.

  • include_bbox (bool, optional) – Add the boxes to each region and to flat_table. Default is True.

  • label_key (str, optional) – Key of each feature’s label in labeled_masks. Default is "label".

Returns:

"per_track"

One dict per track, with track_id, frames, timestamps (s), fixed_box_size (the size used for this track, or None) and regions: for each frame, None if the particle was not detected, otherwise a dict with:

  • frame, timestamp and label;

  • bbox_tight: the box around the largest connected piece of the label, or None if the label is not in the mask;

  • bbox_padded: the box after padding, square and fixed_box_size;

  • image_crop: the frame cropped to bbox_padded (array);

  • mask_crop: boolean mask of the particle in that crop.

"flat_table"

One dict per track and frame, with track_id, frame, timestamp, label and, if include_bbox, the box corners as bbox_tight_minr, bbox_tight_minc, bbox_tight_maxr, bbox_tight_maxc and the same for bbox_padded. Missing values are NaN. Crops are not included. Ready for pandas.DataFrame.

"summary"

Counts (n_tracks, n_rows, n_skipped_index_errors, n_missing_region_measurements) and the settings used.

Frames or feature indices out of range give a warning and a None region.

Return type:

dict

Raises:
  • RuntimeError – If previous_results is None or lacks the tracking or detection output (or their "tracks", "labeled_masks" or "features_per_frame"), or a tracked feature has no label_key.

  • ValueError – If fixed_size_mode is not recognised.

version = '0.1.0'