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_detectionDetects round features without a mask.
playnano.analysis.modules.particle_trackingLinks detected features across frames.
- class playnano.analysis.modules.feature_detection.FeatureDetectionModule[source]¶
Bases:
AnalysisModuleDetect connected features in each frame using a mask.
Pipeline name:
"feature_detection". For each frame:A mask is made with
mask_fn(or read usingmask_key).If
fill_holes, holes in the mask are filled.Groups of mask pixels that touch, at a side or a corner, become features.
If
morph_opening, touching features are separated.Features smaller than
min_size, or touching the frame edge ifremove_edge, are removed.The remaining features are numbered from 1 and measured.
Version 0.2.0 added
morph_opening. Seerun()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).- 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 inplaynano.processing.mask_generators, for example"mask_threshold". Give either this ormask_key.mask_key (str, optional) – Key of a boolean array, shape (n_frames, height, width), in
previous_results. Note thatprevious_resultsis 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 examplethreshold=1.0. Ifmask_fnraisesTypeError, 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 inlabeled_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
labelinside it."summary"total_frames,total_featuresandavg_features_per_frame.
- Return type:
- Raises:
ValueError – If
stack.datais not a 3D array, neithermask_fnnormask_keyis given,mask_fnnames no masking function, or a mask is not a boolean array of the right shape.KeyError – If
mask_keyis not inprevious_results.
- version = '0.2.0'¶