playnano.analysis.modules.particle_tracking module¶
Analysis module that links detected features from frame to frame into tracks.
Each feature is linked to the nearest feature of an existing track that is close enough; features that are not linked start new tracks. Tracks can continue across frames where the particle was not detected.
Added in version 0.2.0.
See also
playnano.analysis.modules.feature_detectionDetects the features to track.
- class playnano.analysis.modules.particle_tracking.ParticleTrackingModule[source]¶
Bases:
AnalysisModuleLink features detected in successive frames into tracks.
Pipeline name:
"particle_tracking". It needs the output offeature_detectionfrom an earlier step.In each frame, tracks are extended in the order they started: each takes the nearest feature not yet taken that is closer than
max_distanceto its last position. This is quick but not globally optimal: when particles come close, an older track can take a feature a newer one fits better. Features left over start new tracks.Version 0.2.0 added
max_missinganddistance_scale. 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, ... ) >>> from playnano.analysis.modules.particle_tracking import ( ... ParticleTrackingModule, ... ) >>> detected = FeatureDetectionModule().run( ... stack, mask_fn="mask_threshold", threshold=1.0 ... ) >>> result = ParticleTrackingModule().run( ... stack, {"feature_detection": detected}, max_distance=5 ... ) >>> track = result["tracks"][0] >>> track["frames"], track["coords"] ([0, 1, 2], [(12.5, 12.5), (13.5, 12.5), (14.5, 12.5)])
- requires = ['feature_detection', 'log_blob_detection']¶
- run(stack: AFMImageStack, previous_results: dict[str, Any] | None = None, detection_module: str = 'feature_detection', coord_key: str = 'features_per_frame', coord_columns: Sequence[str] = ('centroid_x', 'centroid_y'), max_distance: float = 5.0, max_missing: int = 0, distance_scale: str = 'constant', **params) dict[str, Any][source]¶
Link the features detected in each frame into tracks.
- Parameters:
stack (AFMImageStack) – The stack; not used directly.
previous_results (dict) – Outputs of earlier steps. Must hold a detection output with the
coord_keylist and"labeled_masks".detection_module (str, optional) – Name of the detection step to use. If it has not run,
log_blob_detectionis tried, thenfeature_detection. Default is"feature_detection".coord_key (str, optional) – Key of the per-frame feature lists in the detection output. Default is
"features_per_frame".coord_columns (list or tuple of str, optional) – Keys of each feature’s two coordinates. If a feature lacks them, its
centroidis used instead.feature_detectionhas nocentroid_xorcentroid_y, so with the default, coordinates arecentroid: (row, col), in pixels. Default is("centroid_x", "centroid_y").max_distance (float, optional) – Distance a particle can move between frames and still be linked, in the same units as the coordinates (pixels for
feature_detection). Only moves strictly shorter than this are linked. Default is 5.0.max_missing (int, optional) – Number of frames in a row a track can go without a feature before it ends. Default is 0: a track ends at its first missed frame.
distance_scale ({"constant", "linear", "sqrt"}, optional) – How
max_distancegrows after missed frames, withdtthe number of frames since the track’s last feature:"constant"keeps it,"linear"multiplies it bydtand"sqrt"bysqrt(dt). Default is"constant".**params – Not used; ignored.
- Returns:
"tracks"One dict per track, with:
id(int): from 0, in the order tracks started.frames(list of int): every frame from the track’s first feature to its last.point_indices(list of int or None): the index of the track’s feature in each frame’s list in the detection output, or None for a missed frame.coords(list of tuple or None): the feature’s coordinates in each frame, or None for a missed frame.
"track_masks"Maps each track ID to a boolean mask of its last feature, if the feature has a
label."n_tracks"The number of tracks.
- Return type:
- Raises:
RuntimeError – If there is no detection output, or it lacks
coord_keyor"labeled_masks".ValueError – If
max_distanceormax_missingis negative, ordistance_scaleis not recognised.KeyError – If a feature has neither
coord_columnsnorcentroid.
- version = '0.2.0'¶