Reject n_samples<=0 in mcda sensitivity (#1371)#1373
Merged
brendancol merged 1 commit intomainfrom Apr 29, 2026
Merged
Conversation
`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.
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.
Summary
Closes #1371.
sensitivity(..., method="monte_carlo", n_samples=0)used to silently divide by zero inside_monte_carloand hand the caller back a raster of zeros. Withn_samples=0the sample loop never runs,running_m2stays zero, and the trailingnp.where(running_mean != 0, ...)masks the resulting NaN as 0.0. Negative values fell through the same way becauserange(-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: validaten_samples >= 1at the publicsensitivity()entry point whenmethod="monte_carlo", raise ValueError naming the parameter. Docstring updated to note the minimum.xrspatial/tests/test_mcda.py: 3 new tests coveringn_samples=0(raises),n_samples=-1(raises), andn_samples=1(succeeds with no NaNs). One sample is the meaningful minimum;test_single_sample_zero_cvalready exercisedn_samples=1, so this just adds a sanity assertion that the new check doesn't break it.All 169 mcda tests pass.
Test plan
pytest xrspatial/tests/test_mcda.py -k "n_samples_zero or n_samples_negative or n_samples_one"-- 3/3 passpytest xrspatial/tests/test_mcda.py-- 169/169 passn_samples