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_detection

Detects the features to track.

class playnano.analysis.modules.particle_tracking.ParticleTrackingModule[source]

Bases: AnalysisModule

Link features detected in successive frames into tracks.

Pipeline name: "particle_tracking". It needs the output of feature_detection from 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_distance to 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_missing and distance_scale. 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,
... )
>>> 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)])
property name: str

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

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_key list and "labeled_masks".

  • detection_module (str, optional) – Name of the detection step to use. If it has not run, log_blob_detection is tried, then feature_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 centroid is used instead. feature_detection has no centroid_x or centroid_y, so with the default, coordinates are centroid: (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_distance grows after missed frames, with dt the number of frames since the track’s last feature: "constant" keeps it, "linear" multiplies it by dt and "sqrt" by sqrt(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:

dict

Raises:
  • RuntimeError – If there is no detection output, or it lacks coord_key or "labeled_masks".

  • ValueError – If max_distance or max_missing is negative, or distance_scale is not recognised.

  • KeyError – If a feature has neither coord_columns nor centroid.

version = '0.2.0'