Skip to content

Correspondences: candidate buffer grows instead of silently dropping candidates - #48

Merged
alexlib merged 4 commits into
mainfrom
fix/correspondence-candidate-buffer
Nov 24, 2026
Merged

alexlib merged 4 commits into
mainfrom
fix/correspondence-candidate-buffer

Conversation

@alexlib

@alexlib alexlib commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Problem

The correspondence stages (4-, 3- and 2-camera) write their candidate matches into a buffer of 4 * NMAX = 80960 rows and return the moment it is full. Targets are visited in camera-1 order, so every candidate of the targets visited last is lost, with no warning. With a wide epipolar band (criteria.eps0) and dense seeding the particles at the end of that order are never matched and their trajectories end mid-volume. A user report: eps0 = 0.15 mm, upper aorta never matched; narrowing the band to 0.08 mm hid it.

Reproduction (compiled code, before the fix)

  • 150 matchable particles, eps0 = 1.0: 66 quads + 93 triplets (mostly wrong) instead of 150 quads, i.e. the default 80960 rows overflowed.
  • Same frame with a 600-row buffer: 7 matches instead of 150.

Fix (algorithms/correspondences.py)

  • _run_matching_stage: a stage that fills its buffer runs again on a larger one, up to MAX_CANDIDATE_ROWS = 2**23 (about 0.4 GB). At the cap the remaining candidates are dropped with a RuntimeWarning that names eps0.
  • Sized from the data, not hard-coded: growth never passes an upper bound computed from the per-target candidate counts (_bound_quads/_triplets/_pairs), and is projected from how far the full stage got (its last row names the target it reached: _progress_*), so a very full stage is rerun once instead of once per doubling. Doubling stays the floor and the retry stays the guarantee.
  • Warning (once per process) when a stage finds >= 100000 candidates and >= 20 per target: the epipolar band is far too wide for the data (about 0.5 candidates per target is normal). The result is complete, but matching is slow and most candidates are false.
  • The target-usage table (tusage) and the result list are sized from the number of targets (never below NMAX); the 3- and 2-camera stages check bounds against the real table width. More than NMAX targets in a camera used to index past the table (boundscheck is off) and every target from NMAX on was skipped.
  • take_best_candidates: typed contiguous order array (same order, ties included), no Python-object indexing per candidate.
  • Thin Python-level entries _stage_four/three/pair because the ccall stages cannot be passed around as objects.

Speed (150 particles, wide band, same results)

eps0 candidates before after one pre-sized run
1.5 828 k 0.39 s 0.20 s 0.19 s
2.0 2.7 M 1.48 s 0.65 s 0.64 s

take_best_candidates at 2.7 M candidates: 0.34 s to 0.22 s. Default settings: unchanged (a real frame takes 107 ms; the bounds add 0.02 ms).

Checks

  • New tests (TestCandidateBuffer, TestCandidateBufferEstimates, take_best_candidates against the previous loop with ties): wide band at the default size gives 150 quads with consistent identities; a 16-row initial buffer and an NMAX = 8 table give identical matches to the default; the cap and the wide-band warning; the stage runner (doubling, bound, projection); the bounds against a brute-force count; progress readers; attempts per frame.
  • Compiled (Cython rebuilt) and pure-Python fallback: test_correspondences.py + test_correspondences_coverage.py pass in both (82 tests).
  • Full unit suite: 2149 passed; 1 failure, test_sequence_triangulation_method::test_lsq_triangulation_reprojects_better_than_pairs (run.zarr does not exist), which also fails on main without this change.
  • Default settings are unchanged: 10 real wp2 and 10 rendered syn1 frames give the same per-camera blob ids as the stored runs, and no stage grows its buffer.
  • Real recording at a wide band: in progress (wp2, about 1500 targets per camera, eps0 widened, new buffer against the old fixed one); results will follow as a comment.

CI note

The Windows test jobs fail in Install dependencies (loro==1.19.0 has no Windows wheel); main is red for the same reason, independent of this change.

…ing candidates

The matching stages wrote their candidates into a buffer of 4 * NMAX = 80960 rows and
returned the moment it was full. Targets are visited in camera-1 order, so every
candidate of the targets visited last was lost, without any warning: with a wide
epipolar band (criteria.eps0) and dense seeding the particles at the end of that order
were never matched and their trajectories ended mid-volume.

- _run_matching_stage: a stage that fills its buffer runs again on a larger one, up to
  MAX_CANDIDATE_ROWS = 2**23 (about 0.4 GB); at the cap the rest is dropped with a
  RuntimeWarning that names eps0.
- Growth is sized from the data: never beyond an upper bound computed from the
  per-target candidate counts (_bound_quads/_triplets/_pairs), and projected from how far
  the full stage got (the last row written names the target; _progress_*), so a very
  full stage is rerun once, not once per doubling. Doubling stays the floor.
- Warning (once) when a stage finds >= 100000 candidates and >= 20 per target: the
  epipolar band is far too wide for the data (about 0.5 per target is normal).
- The target-usage table and the result list are sized from the number of targets
  (never below NMAX); the 3- and 2-camera stages check bounds against the real table
  width. More than NMAX targets in a camera used to index past the table.
- take_best_candidates: typed contiguous order array, same order (ties included), no
  Python-object indexing per candidate.

Reproduced on the compiled code: 150 matchable particles, eps0 = 1.0: 66 quads + 93
triplets (mostly wrong) instead of 150 quads; with a 600-row buffer, 7 matches instead
of 150. Wide band, 150 particles: 2.7 M candidates at eps0 = 2.0, 1.48 s -> 0.65 s (one
pre-sized run: 0.64 s); 828 k at 1.5: 0.39 s -> 0.20 s. At default settings nothing
changes: 20 real and synthetic frames give the same blob ids, no stage grows its buffer,
the bounds cost 0.02 ms per frame.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 22:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…d case with truth (syn1), new candidate buffer against the old fixed one

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@alexlib

alexlib commented Nov 24, 2026

Copy link
Copy Markdown
Owner Author

Real-recording wide-band test (the check that was still open)

bench/limits/wide_band_real.py (added to this PR): one frame, criteria.eps0 widened, the new buffer against an emulation of the old one (candidate buffer capped at 4 x 20240 rows, so a full stage drops the candidates of the targets visited last).

Real recording wp2, frame 100 (about 1500 targets per camera, default eps0 0.02 mm)

eps0 (mm) quads, new / old quad candidates camera-1 targets matched per x-quartile, new same, old
0.10 974 / 974 23,501 74 / 85 / 82 / 75 % same (buffer not yet full)
0.15 1059 / 449 146,267 78 / 87 / 83 / 76 % 79 / 91 / 89 / 30 %
0.25 1150 / 44 1,588,983 82 / 91 / 85 / 77 % 42 / 0 / 0 / 0 %
0.40 646 / 23 cap (8.4 M rows) uses the cap and warns 23 / 0 / 0 / 0 %

This is the reported symptom on real detections: at the reported band (0.15 mm) the old buffer leaves the last x-quartile of camera 1 two thirds unmatched; at 0.25 mm everything after the first quartile. Time with the new buffer: 0.27 s (0.15 mm), 0.94 s (0.25 mm). At 0.40 mm (5,600 candidates per target) the 8.4 M-row cap is reached and the cap warning fires, as designed.

Rendered case with truth, syn1 frame 100 (a quad counts as correct when its four blobs belong to one true particle; merged blobs are counted wrong, so absolute shares are low, the comparison is what matters)

eps0 (mm) new: quads / correct old: quads / correct
0.02 (default) 784 / 533
0.10 964 / 549 964 / 549
0.15 1052 / 549 594 / 297
0.25 1126 / 549 43 / 6

The new buffer keeps every correct match as the band widens (549 at 0.10, 0.15 and 0.25). The extra quads at wider bands are false matches that the band itself allows (precision 57 % to 49 %), which is what the new wide-band warning (fires at 0.15 mm: 146 k to 165 k candidates for about 1500 targets) is for: the result is complete, but narrowing the band is better. The old buffer loses half the correct matches at 0.15 and nearly all of them at 0.25.

alexlib and others added 2 commits November 25, 2026 00:31
…r skips releases without Windows wheels

loro 1.19.0 (via marimo) was published on 2026-11-23 with no Windows wheel; uv sync picks the
newest release for the universal lock and then cannot install it on Windows, so both Windows test
jobs failed in 'Install dependencies' (main too). With required-environments the lock uses loro
1.18.0 for Windows and keeps 1.19.0 elsewhere. Python 3.14 is excluded: optv has no Windows wheel for it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… viz extra

Only the marimo notebooks (openptv2/gui/*_nb.py, optimize_calibration.py) and one script import marimo;
nothing in the headless batch runtime does, and self_check already treats it as optional. As a core
dependency it pulled marimo and its tree (loro, ...) into every install, including the batch/cloud image,
and made a release without wheels on one platform (loro 1.19.0, Windows) break plain installs of the lock.

The viz extra (included in dev, so CI and contributors are unchanged) now carries marimo>=0.24.0 and
nbformat>=5.11.1, the minimums the core list had. The Windows lock requirement stays for CI (dev includes viz).

Checked in a venv without marimo, loro, nbformat, Qt or TraitsUI: the unit suite gives the same results as
with them. The 4 differences from the full environment (autocalibrate_cavity, 2 residual-field tests,
self_check main) fail identically with marimo and nbformat installed: they need matplotlib / imagecodecs,
which are gui/viz extras, not core.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@alexlib
alexlib merged commit d770f07 into main Nov 24, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants