Handle empty annulus in local common reference - #4728
Open
adityasingh2400 wants to merge 1 commit into
Open
Conversation
When no channel lies beyond the exclude radius, the local kernel row was built with an empty neighbor list, so 1 / len(neighbors_i) raised a ZeroDivisionError at init. Such channels are now left unreferenced and a warning is raised instead.
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.
Fixes #4193
Building a
common_referencerecording withreference="local"raisesZeroDivisionError: division by zerowhenever a channel has no other channel beyond the exclude (inner) radius. A tetrode reproduces it with the defaultlocal_radius=(30, 55), since all four contacts sit within 30 um of each other:The root cause is in the kernel construction. When the annulus holds fewer than
min_local_neighborschannels, the code falls back to the closest channels beyond the inner radius, then unconditionally doeslocal_kernel[i, neighbors_i] = 1 / len(neighbors_i). That fallback normally finds something, but if nothing lies beyond the inner radius at all thenneighbors_iis empty and the division blows up. The user only sees a bareZeroDivisionErrorwith no indication that the radius is the problem.The fix follows what @alejoe91 suggested in the issue. Channels with no available neighbor keep their kernel row at zero, which leaves them unreferenced, and a warning naming those channels is raised at init. The average operator already computes a zero shift from a zero kernel row, so only the median branch needed a guard: an empty neighborhood there would have produced
nanfromnp.medianof an empty slice, so it now passes the trace through unchanged.Tested with four new cases in
test_common_reference.py, parametrized over both operators. The first covers the tetrode geometry where every channel is unreferenced, the second a cross layout where only the center channel has nothing beyond the radius while the four arms are still referenced to their opposite arm. All four fail on main (two withZeroDivisionErrorat construction, two withDID NOT WARN) and pass with the fix. The fulltest_common_reference.pyfile passes, 11 passed and 1 skipped.