Skip to content

The dynamic cutoff in C++, with SMAP's nearest-rank quantiles - #9

Open
lanceXwq wants to merge 1 commit into
mainfrom
dynamic-cutoff-nearest-rank
Open

lanceXwq wants to merge 1 commit into
mainfrom
dynamic-cutoff-nearest-rank

Conversation

@lanceXwq

@lanceXwq lanceXwq commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

What

Detection's dynamic cutoff (median + factor × the 20–80 % slope of each frame's local maxima) is now computed in C++, and takes its quantiles the way SMAP does.

  • Speed. The cutoff was a Python loop with three np.quantile calls per frame. csrc/cutoff.hpp now finds the three order statistics with nth_element, with frames split over threads (_fit3d.segment_cutoffs, called from DynamicCutoff.thresholds):

    200 frames before after
    cutoff step, 1500 maxima per frame 9.3 ms 1.1 ms
    find_candidates, 200 px 22 ms 15 ms
    find_candidates, 200 px, split frame 32 ms 17 ms

    The lexsort that NOTES.md suggested turns out to be slower than the loop (52 ms), and NOTES.md now says so.

  • SMAP's quantiles. A quantile is now the ⌈n·p⌉-th smallest maximum (SMAP's myquantilefast), and the slope is divided by 0.8 - 0.2, as in SMAP's getdynamiccutoff. smappy used np.quantile's linear interpolation before, which is numpy's default; nothing records choosing it.

Effect on results

Measured on 2000 simulated frames at 200 px and 64 px, with and without a split:

  • The cutoff differs in ~90 % of frames: by 0.04 % at 1500 maxima per frame and ~1 % at 140 maxima per frame. It always moves upward.
  • 0.01–0.14 % of the candidates are dropped, all of them at the threshold. None are added.
  • The four Fit plugins' versions are bumped (Gaussian 2D 2→3, Spline 3D 2→3, Spline 3D 2C 5→6, Gaussian 2D 2C 3→4). Their help pages and the example chain follow, so batch runs refit files done with the old cutoff.

Checks

  • DynamicCutoff.__call__ matches a line-by-line transcription of SMAP's getdynamiccutoff bit for bit (5,000 random frames).
  • The C++ kernel matches __call__ bit for bit (55,196 segments: several factors, one or many threads, ties, negative values, frames with fewer than 10 maxima). The final multiply-add is kept from being fused into an FMA. Clang fuses by default, and that moved a threshold by one ulp during development.
  • New tests in tests/test_detect_roi.py:
    • a worked example of the nearest-rank quantiles;
    • block thresholds equal the per-frame cutoff, with and without the extension;
    • find_candidates keeps exactly what the per-frame loop keeps, with and without a split.
  • pytest tests --slow -n auto: 1371 passed, 5 skipped (no GPU, Piper, calibration data or uiPSF on this machine). ci/smoke.py passes.

Not in this PR

The fit plugin's single-frame preview reports its cutoff from the top 1 % of all pixels in the filtered frame (plugins/fit.py, preview), not from the local maxima that detection uses. The reported number is far off the real one: 322 against 8.23 on the test_preview.py fixture. The candidates it draws are correct. This will be a separate fix.

🤖 Generated with Claude Code

The per-frame cutoff was a Python loop, three np.quantile calls a frame
(9.3 ms per 200 frames of 1500 maxima, 80 % of it numpy's dispatch).  It
needs three order statistics, not a sort: csrc/cutoff.hpp finds them with
nth_element, frames over threads, in 1.1 ms; a split frame's find_candidates
goes from 32 to 17 ms.  A lexsort over the block, which NOTES suggested, is
slower than the loop (52 ms).

The quantiles are now SMAP's (myquantilefast: the ceil(n p)-th smallest)
instead of np.quantile's linear interpolation, which was only numpy's
default.  The cutoff rises slightly and 0.01-0.14 % of the candidates are
dropped, those at the threshold; DynamicCutoff.__call__ matches SMAP's
getdynamiccutoff bit for bit, and the kernel matches __call__.  The four
Fit plugins' versions are bumped for it.

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

This branch has not been deployed

No deployments
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.

1 participant