Cleaning chain

flowtracks.clean.clean_trajectories() turns raw tracker output (one row per point with a trajectory id, positions in metres) into smooth trajectories in one call: repair, Savitzky-Golay smoothing, a split at fast violent turns the repair missed, two stitching passes (the second tighter) with smoothing in between, and a final smoothing.

Every setting has a default in flowtracks.clean.CLEAN_DEFAULTS except the frame rate, which must be stated. flowtracks.clean.resolve_settings() merges a parameter-file section and explicit overrides onto the defaults and reports where each value came from, so nothing is hidden in a script:

from flowtracks.clean import clean_trajectories, resolve_settings
from flowtracks.io import save_zarr_trajectories

settings, sources = resolve_settings(yaml_section, {"gap1": 8})
trajs = clean_trajectories(trajid, frame, pos, settings)
save_zarr_trajectories(trajs, "clean.zarr", overwrite=True)

The pieces are usable on their own: flowtracks.clean.trajectories_from_arrays() (flat arrays to Trajectory objects) and flowtracks.clean.kink_split().

Cleaning chain for raw tracker output: repair, kink split, smoothing, stitching.

Raw linkage from a tracker (one row per point, with a trajectory id) is turned into smooth trajectories in one call, clean_trajectories():

1. repair (flowtracks.repair.repair_arrays()): cut wrong links, attach single points, join pieces across short gaps; 2. Savitzky-Golay smoothing (flowtracks.smoothing.savitzky_golay()); 3. kink split: a fast, violent turn (larger than kink_deg on steps longer than kink_step_mm) is a wrong link the repair missed; the pieces get fresh ids; 4. smoothing, a first stitch (gap1, dist1, vd1), smoothing; 5. a second kink split, smoothing, a second, tighter stitch (gap2, dist2, vd2); 6. final smoothing (optionally a longer window for long trajectories).

Positions are in metres, so velocities are in m/s at the given frame rate. Every setting has a default in CLEAN_DEFAULTS; fps has none on purpose – the frame rate must be stated. resolve_settings() merges a parameter-file section and overrides onto the defaults and reports where each value came from.

flowtracks.clean.CLEAN_DEFAULTS = {'dist1': 0.05, 'dist2': 0.025, 'fps': None, 'gap1': 12, 'gap2': 8, 'kink_deg': 90.0, 'kink_step_mm': 25.0, 'len_long': 25, 'order_long': 3, 'repair_attach': True, 'repair_cut': True, 'repair_cut_factor': 2.0, 'repair_join': True, 'repair_link_percentile': 99.9, 'repair_max_distance_mm': 0.3, 'repair_max_gap': 1, 'sg_order': 2, 'sg_window': 7, 'smooth_method': 'spline', 'spline_lam': None, 'vd1': 0.5, 'vd2': 0.35, 'win_long': 0}

Default settings of clean_trajectories() (positions in metres).

flowtracks.clean.resolve_settings(section=None, overrides=None, source_name='parameter file')[source]

Settings of clean_trajectories(): defaults <- section <- overrides.

Arguments

  • section: mapping read from a parameter file (may be None).

  • overrides: mapping of explicit values (e.g. command-line flags); None values are ignored.

  • source_name: how section is named in the returned sources.

Returns

(settings, sources) - two dicts keyed like CLEAN_DEFAULTS; sources say “override”, source_name or “default” for every key.

Raises ValueError on an unknown key in section or a missing fps.

flowtracks.clean.trajectories_from_arrays(trajid, time, pos)[source]

Trajectory objects (zero velocity) from flat one-row-per-point arrays.

Rows are grouped by trajid and ordered by time inside each group; groups of a single point are dropped.

flowtracks.clean.kink_split(trajs, max_deg, step_min, fresh_start=10000000)[source]

Split trajectories at fast, violent turns.

A point is a cut point when the velocity turns by more than max_deg degrees there and both the step into it and the step out of it are longer than step_min (position units). Every piece of a cut trajectory gets a fresh id counting from fresh_start, so readers grouping by id cannot put the pieces back together.

Returns

(trajectories, number of cuts)

flowtracks.clean.clean_trajectories(trajid, time, pos, settings, log=<built-in function print>)[source]

Run the cleaning chain (module docstring) on flat tracker output.

Arguments

  • trajid, time, pos: (N,), (N,), (N, 3) one row per point, pos in metres.

  • settings: complete settings, e.g. from resolve_settings().

  • log: callable for progress lines, or None for silence.

Returns

list of smoothed Trajectory objects.