Skip to content

Speed up sieve labeling and add convergence warning#1163

Merged
brendancol merged 2 commits into
masterfrom
issue-1162
Apr 5, 2026
Merged

Speed up sieve labeling and add convergence warning#1163
brendancol merged 2 commits into
masterfrom
issue-1162

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Closes #1162

Summary

  • Replace per-value scipy.ndimage.label calls with a single-pass numba union-find, cutting labeling cost from O(k*n) to O(n)
  • Rework adjacency builder to use 1-D np.unique on encoded int64 pairs instead of slower 2-D np.unique(axis=0)
  • Emit UserWarning when the 50-iteration merge loop doesn't converge (previously silent)

Test plan

  • All 40 existing sieve tests pass
  • New convergence warning test (patches _MAX_ITERATIONS=0)
  • New 100x100 noisy classification test (verifies quadrant interiors clean after sieve)
  • New 20x20 checkerboard test (worst-case region count, verifies full unification)
  • Verify no regressions in other test modules

Replace per-value scipy.ndimage.label calls with a single-pass numba
union-find for connected-component labeling.  Rework the adjacency
builder to use 1-D np.unique on encoded int64 pairs instead of the
slower np.unique(axis=0) on 2-D arrays.  Add a UserWarning when the
50-iteration merge loop doesn't converge.
Move the convergence warning from _sieve_numpy into sieve() so
stacklevel=2 always points at the caller regardless of backend.
Return (result, converged) tuple from all backend functions.  Remove
unused import in test_sieve_convergence_warning.  Add int32 limit
note to _label_connected docstring.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 5, 2026
@brendancol brendancol merged commit 3b56af8 into master Apr 5, 2026
11 checks passed
@brendancol brendancol deleted the issue-1162 branch May 4, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sieve: fix convergence warning and speed up labeling/adjacency

1 participant