FIX: guard against divide-by-zero in dispersion_index#647
Open
pragati-566 wants to merge 2 commits intoPennLINC:mainfrom
Open
FIX: guard against divide-by-zero in dispersion_index#647pragati-566 wants to merge 2 commits intoPennLINC:mainfrom
pragati-566 wants to merge 2 commits intoPennLINC:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #647 +/- ##
==========================================
+ Coverage 76.24% 76.27% +0.02%
==========================================
Files 40 40
Lines 5002 5007 +5
==========================================
+ Hits 3814 3819 +5
Misses 1188 1188 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Changes proposed in this pull request
Fixes a potential divide-by-zero bug in
dispersion_index(aslprep/utils/confounds.py).Two failure modes:
Degenerate tissue masks — if any of the GM, WM, or CSF binary masks contained fewer than 2 voxels,
np.var()on an empty array would silently returnnan, and the pooled-variance denominator(n_gm + n_wm + n_csf - 3)could reach zero (e.g. exactly 1 voxel per mask →1 + 1 + 1 - 3 = 0), producing a silentZeroDivisionErrorornanresult with no indication of what went wrong.Zero mean GM CBF — if the mean CBF across the GM mask was exactly zero, the final
V / np.abs(mean_gm_cbf)return would silently produceinf.This PR resolves the TODO left in the original code (line 301) by adding explicit pre-flight
ValueErrorchecks before either computation is attempted. The error messages name the specific tissue that failed, which makes debugging in QC pipelines straightforward.I found the comment as a TODO in the code, so haven't opened a new issue for this.
No changes to the public API or existing behaviour for valid inputs.
Documentation that should be reviewed
No documentation changes required. The fix is internal to the function and the docstring contract (returns a
float) is unchanged for valid inputs.