Precompute unit-pair spatial overlap once in find_collisions - #4797
Open
JESUSROYETH wants to merge 1 commit into
Open
JESUSROYETH wants to merge 1 commit into
JESUSROYETH wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
find_collisions(used bycompute("amplitude_scalings")whenhandle_collisions=True, the default) calls_are_units_spatially_overlapping(sparsity_mask, i, j)once per temporally-overlapping spike-pair candidate. That helper recomputesnp.any(sparsity_mask[i] & sparsity_mask[j])from scratch every time, but the answer only depends on the pair of unit indices — a fixed fact ofsparsity_maskwith at mostnum_units**2distinct values. On a realistic recording this ends up being recomputed millions of times over the course of a run.This replaces it with
_unit_pair_overlap_matrix(sparsity_mask), a single integer matrix multiplication that returns the full unit-pair overlap matrix. Sincesparsity_maskis fixed for the whole node's lifetime, it's precomputed once inAmplitudeScalingNode.__init__(not once per chunk insidecompute()/find_collisions, which is where the matrix multiplication originally landed) and looked up with vectorized fancy indexing instead of a per-candidate Python loop. Same boolean logic, so results are unchanged.Performance
Representative public call,
sorting_analyzer.compute("amplitude_scalings", n_jobs=1), on a 384-channel/150 s/200-unit ground-truth recording (450,920 spikes, materialized to binary before timing), withwaveforms/templates/noise_levelsprecomputed outside the timed region. Measured on a GCPc3-standard-8(Intel Sapphire Rapids), numerical threads pinned to 1, 5 repeats with baseline/patched order alternated, 1 discarded warmup:Per-repeat speedup: 12.18%, 13.74%, 12.20%, 11.14%, 12.29% (median 12.20%, same sign every time).
Validation
find_collisionsbefore/after (precomputed-matrix call included) — 0 mismatches.test_unit_pair_overlap_matrix_matches_naive_reference: checks the overlap matrix against a directnp.any(mask[i] & mask[j])reference for every unit pair, including the empty-row case. Fails withImportErroron unpatchedmain(function doesn't exist yet), passes on this patch.test_find_collisions_with_margin_indices(added in Speed up collision-aware amplitude scaling #4786).compute("amplitude_scalings", handle_collisions=True), real 18,027-scaling output) before/after — bit-identical.black/style clean; no collision with the open draft PR touching this file ([DRAFT] Extract waveforms to zarr dataset #4708, unrelated import only).Tested on Linux only (no macOS/Windows access here); the change has no platform-dependent code path, and this module is already covered by the ubuntu/macOS/Windows × py3.10/3.13 CI matrix. The overlap matrix is
num_units x num_units, so memory grows quadratically with unit count — negligible here (~500 KB at 200 units) but worth knowing about at extreme unit counts (~500 MB at 10,000 units).