Skip to content

Reject n_samples<=0 in mcda sensitivity (#1371)#1373

Merged
brendancol merged 1 commit into
mainfrom
issue-1371-sensitivity-nsamples
Apr 29, 2026
Merged

Reject n_samples<=0 in mcda sensitivity (#1371)#1373
brendancol merged 1 commit into
mainfrom
issue-1371-sensitivity-nsamples

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

Closes #1371.

sensitivity(..., method="monte_carlo", n_samples=0) used to silently divide by zero inside _monte_carlo and hand the caller back a raster of zeros. With n_samples=0 the sample loop never runs, running_m2 stays zero, and the trailing np.where(running_mean != 0, ...) masks the resulting NaN as 0.0. Negative values fell through the same way because range(-1) is empty.

Cat 3 sibling of the NaN-weight bug fixed in #1312. Both came out of the same mcda audit; this one was deferred per the one-fix-per-PR convention.

Changes

  • xrspatial/mcda/sensitivity.py: validate n_samples >= 1 at the public sensitivity() entry point when method="monte_carlo", raise ValueError naming the parameter. Docstring updated to note the minimum.
  • xrspatial/tests/test_mcda.py: 3 new tests covering n_samples=0 (raises), n_samples=-1 (raises), and n_samples=1 (succeeds with no NaNs). One sample is the meaningful minimum; test_single_sample_zero_cv already exercised n_samples=1, so this just adds a sanity assertion that the new check doesn't break it.

All 169 mcda tests pass.

Test plan

`sensitivity(..., method="monte_carlo", n_samples=0)` used to silently
divide by zero in `_monte_carlo` and return a raster of zeros. The loop
body never runs, `running_m2` stays at zero, and `0 / n_samples` is
masked back to 0.0 by the trailing `np.where`. Negative `n_samples`
fell through the same way (`range(-1)` is empty).

Validate `n_samples >= 1` at the public entry point and raise
ValueError naming the parameter. Three new tests cover n_samples=0,
n_samples=-1, and n_samples=1 (the meaningful minimum).

Cat 3 sibling of the NaN-weight bug fixed in #1312.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 29, 2026
@brendancol brendancol merged commit 46522f7 into main Apr 29, 2026
11 checks passed
@brendancol brendancol deleted the issue-1371-sensitivity-nsamples branch May 4, 2026 19:50
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.

sensitivity: n_samples=0 produces silent NaN output instead of ValueError

1 participant