playnano.analysis.modules.feature_detection module

Analysis module that detects features using a mask of each frame.

Pixels selected by a mask (for example, everything above a height threshold) are grouped into connected features, which can be separated, filtered by size and edge contact, and measured.

Added in version 0.2.0.

See also

playnano.analysis.modules.log_blob_detection

Detects round features without a mask.

playnano.analysis.modules.particle_tracking

Links detected features across frames.

class playnano.analysis.modules.feature_detection.FeatureDetectionModule[source]

Bases: AnalysisModule

Detect connected features in each frame using a mask.

Pipeline name: "feature_detection". For each frame:

  1. A mask is made with mask_fn (or read using mask_key).

  2. If fill_holes, holes in the mask are filled.

  3. Groups of mask pixels that touch, at a side or a corner, become features.

  4. If morph_opening, touching features are separated.

  5. Features smaller than min_size, or touching the frame edge if remove_edge, are removed.

  6. The remaining features are numbered from 1 and measured.

Version 0.2.0 added morph_opening. See run() for the parameters and outputs.

Examples

>>> import numpy as np
>>> from playnano.afm_stack import AFMImageStack
>>> data = np.zeros((3, 64, 64))
>>> for t in range(3):
...     data[t, 10 + t : 16 + t, 10:16] = 3.0  # a 6 x 6 pixel particle
>>> 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)],
... )
>>> from playnano.analysis.modules.feature_detection import (
...     FeatureDetectionModule,
... )
>>> result = FeatureDetectionModule().run(
...     stack, mask_fn="mask_threshold", threshold=1.0
... )
>>> result["summary"]
{'total_frames': 3, 'total_features': 3, 'avg_features_per_frame': 1.0}
>>> feature = result["features_per_frame"][1][0]
>>> feature["area"], feature["centroid"], feature["mean"]
(36.0, (13.5, 12.5), 3.0)

In a pipeline, the same step is added with pipeline.add("feature_detection", mask_fn="mask_threshold", threshold=1.0).

property name: str

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

run(stack, previous_results: dict[str, Any] | None = None, *, mask_fn: callable | None = None, mask_key: str | None = None, morph_opening: bool = False, sep_radius: int = 6, min_size: int = 10, remove_edge: bool = True, fill_holes: bool = False, hole_area: int | None = None, **mask_kwargs) → dict[str, Any][source]

Detect and measure features in each frame of stack.data.

Parameters:
  • stack (AFMImageStack) – The stack to analyse.

  • previous_results (dict, optional) – Outputs of earlier steps; used only with mask_key. Default is None.

  • mask_fn (callable or str, optional) – Masking function, called as mask_fn(frame, **mask_kwargs) and returning a boolean array the shape of the frame, or the name of one in playnano.processing.mask_generators, for example "mask_threshold". Give either this or mask_key.

  • mask_key (str, optional) – Key of a boolean array, shape (n_frames, height, width), in previous_results. Note that previous_results is keyed by module name and holds dicts, so through a pipeline this cannot currently find a mask.

  • morph_opening (bool, optional) – Separate touching features with a morphological opening (with a disk of radius sep_radius), applied to each feature on its own. A feature that would lose more than 60% of its area, or all of it, is kept unchanged. Default is False.

  • sep_radius (int, optional) – Radius of the opening disk, in pixels. Used only if morph_opening. Default is 6.

  • min_size (int, optional) – Smallest feature kept, in pixels. Default is 10.

  • remove_edge (bool, optional) – Remove features that touch the edge of the frame. Default is True.

  • fill_holes (bool, optional) – Fill holes in the mask before finding features. Default is False.

  • hole_area (int, optional) – With fill_holes, fill only holes smaller than this, in pixels. If None, fill all holes. Default is None.

  • **mask_kwargs – Passed to mask_fn, for example threshold=1.0. If mask_fn raises TypeError, it is called again without them.

Returns:

"features_per_frame"

For each frame, a list of dicts, one per feature, with:

  • frame_timestamp (float): the frame’s time, in s, or the frame index if the stack has no times.

  • label (int): the feature’s value in labeled_masks.

  • area (float): in pixels.

  • min, max, mean (float): height inside the feature, in the units of the data (usually nm).

  • bbox (tuple of int): (min_row, min_col, max_row, max_col), with the maxima exclusive.

  • centroid (tuple of float): (row, col), in pixels.

"labeled_masks"

For each frame, an int array the shape of the frame: 0 for background and each feature’s label inside it.

"summary"

total_frames, total_features and avg_features_per_frame.

Return type:

dict

Raises:
  • ValueError – If stack.data is not a 3D array, neither mask_fn nor mask_key is given, mask_fn names no masking function, or a mask is not a boolean array of the right shape.

  • KeyError – If mask_key is not in previous_results.

version = '0.2.0'