Skip to content

FIX: guard against divide-by-zero in dispersion_index#647

Open
pragati-566 wants to merge 2 commits intoPennLINC:mainfrom
pragati-566:fix/dispersion-index-divide-by-zero
Open

FIX: guard against divide-by-zero in dispersion_index#647
pragati-566 wants to merge 2 commits intoPennLINC:mainfrom
pragati-566:fix/dispersion-index-divide-by-zero

Conversation

@pragati-566
Copy link

Changes proposed in this pull request

Fixes a potential divide-by-zero bug in dispersion_index (aslprep/utils/confounds.py).

Two failure modes:

  1. 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 return nan, 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 silent ZeroDivisionError or nan result with no indication of what went wrong.

  2. 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 produce inf.

This PR resolves the TODO left in the original code (line 301) by adding explicit pre-flight ValueError checks 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.

@codecov-commenter
Copy link

codecov-commenter commented Mar 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.27%. Comparing base (1609108) to head (711eab5).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants