Trajectory repair

A tracker leaves three kinds of damage in a trajectory database: links to the wrong particle, trajectories broken by a missed detection or a rejected link, and particles left unlinked as single points. flowtracks.repair.repair_trajectories() repairs all three in one call, before any smoothing or averaging:

  1. cut – every link inside a trajectory gets a two-sided gap check: a straight line is fitted to up to window points before the link and, separately, to up to window points after it; both are evaluated at the middle of the link. A wrong link jumps by about the particle spacing, a correct one only by noise. Clear outliers are cut.

  2. attach – a single point is appended to the end or start of a trajectory whose straight-line prediction (up to max_gap + 1 frames away) it matches.

  3. join – a trajectory end is joined to a trajectory start up to max_gap missing frames later when the same gap check passes. Each end and each start joins at most once, so no piece is ever duplicated.

The check value is the mismatch divided by the size its noise should have, and both the noise scale and the acceptance threshold are calibrated on the data (links inside long trajectories), so no distance or velocity tolerance has to be guessed. No positions are invented: a joined gap stays a gap, and the returned trajectories carry zero velocity for the caller to differentiate over the real frame numbers.

from flowtracks.repair import repair_trajectories

repaired, report = repair_trajectories(trajectories, max_gap=3)
print(report)   # links_cut, points_attached, joins, threshold, noise_scale, ...

For millions of points use the array form, which is what the list form wraps:

from flowtracks.repair import repair_arrays

new_trajid, report = repair_arrays(trajid, frame, pos)   # one row per position

Candidate searches are single KD-tree queries over (x, y, z, frame) and matches are resolved greedily in order of increasing check value, so a 5000-frame, 6-million-point run is repaired in tens of seconds.

Use it before flowtracks.smoothing: smoothing a trajectory that contains a wrong link spreads the jump over the whole smoothing window.

It was validated against synthetic ground truth matched to a 4-camera experiment (openptv-analysis, logs 025-026): attachments 99.3-99.9 % correct, joins 99.9 % correct.

Trajectory repair: cut links that fail a gap check, attach single points, and join trajectory pieces with a checked join.

Works on the trajectories a tracker produced – one segment per linked track, with unlinked particles as 1-point segments. Three steps, each counted in the returned report:

1. cut – for every link inside a trajectory, fit a straight line to up to window points before the link and, separately, to up to window points after it. Both lines are evaluated at the middle of the link; if they disagree by more than cut_factor times the threshold the link is cut. A wrong link (to another particle) jumps by roughly the particle spacing, far above the noise, while a correct link only differs by noise. 2. attach – a 1-point segment is appended to the end (or start) of a trajectory whose straight-line prediction, up to max_gap + 1 frames ahead (or back), it matches within the threshold. 3. join – a trajectory end is joined to a trajectory start 1 .. max_gap + 1 frames later when the same two-sided gap check passes. Each end and each start joins at most once; chains (A->B->C) form through the join map, so no segment is ever copied.

The threshold calibrates itself on the data. The gap-check mismatch divided by the size its noise should have (straight-line fit weights times a per-axis noise scale) gives one number chi per link; the noise scale is the robust spread of the mismatch on links inside long trajectories (long_track points or more), and the threshold T is the link_percentile percentile of chi on those same links (gross outliers excluded). No distance or velocity tolerance has to be guessed.

No positions are invented: a joined gap stays a gap (missing frames), so derivatives taken over the real frame numbers stay honest.

repair_arrays() is the vectorised core working on flat arrays (millions of points: candidate searches are single KD-tree queries over (x, y, z, frame), matches are resolved greedily in order of increasing chi); repair_trajectories() wraps it for lists of Trajectory objects.

Validated in openptv-analysis (logs 025-026) on synthetic ground truth matched to a 4-camera experiment: attachments 99.3-99.9% correct, joins 99.9% correct, trajectories of 10+ frames from a single particle 97-98%.

flowtracks.repair.repair_arrays(trajid, time, pos, max_gap=3, window=5, link_percentile=99.9, long_track=50, cut=True, attach=True, join=True, max_distance=None, attach_rounds=5, cut_factor=2.0)[source]

Repair trajectories given as flat arrays (one row per particle position).

Arguments

  • trajid: (N,) integer trajectory id per row (1-point trajectories are unlinked particles).

  • time: (N,) integer frame number per row.

  • pos: (N, 3) positions (any length unit). max_gap, window, link_percentile, long_track, cut, attach, join,

  • max_distance, attach_rounds, cut_factor: see repair_trajectories().

Returns

(new_trajid, report) - (N,) int64 trajectory ids in input row order, and the report dict. Rows of a trajectory whose frame numbers are not strictly increasing (e.g. an “unlinked particles” bucket) keep their id and are not repaired; repaired trajectories get fresh ids above max(trajid).

flowtracks.repair.repair_trajectories(trajs, max_gap=3, window=5, link_percentile=99.9, long_track=50, cut=True, attach=True, join=True, max_distance=None, attach_rounds=5, cut_factor=2.0)[source]

Cut suspect links, attach single points and join pieces of trajectories.

Arguments

  • trajs: list of Trajectory objects (segments from a tracker, 1-point trajectories for unlinked particles). time() must be integer frame numbers, strictly increasing within a trajectory; trajectories whose times are not (e.g. an “unlinked particles” bucket) are passed through untouched.

  • max_gap: largest number of missing frames an attachment or join may span.

  • window: up to this many points on each side feed the straight-line fits.

  • link_percentile: threshold T is this percentile of the link check value (chi) on links inside trajectories of long_track points or more.

  • long_track: minimum length of the trajectories used to calibrate the noise scale and the threshold (all checkable links if too few).

  • cut, attach, join: enable the individual steps.

  • max_distance: optional upper bound on any attachment/join distance (in position units); by default only the calibrated chi threshold limits it.

  • attach_rounds: repeat attaching while it still finds points (a track may grow by one point per round in each direction).

  • cut_factor: a link is cut only when its chi exceeds cut_factor * T. Attachments and joins are accepted up to T, so a link between T and cut_factor * T – usually a noise spike – is left alone instead of being cut and then refused by the join step for good. Wrong links jump by the particle spacing and land far above either threshold.

Returns

(repaired, report) - a list of Trajectory objects (zero velocity, fresh trajids; pass-through trajectories keep theirs), and a dict of counts: n_in, n_out, points, links_checked, noise_scale (per axis), threshold, cut_threshold, links_cut, points_attached, joins, and skipped with a reason when the data are too short to calibrate.