Reject NaN/Inf weights in mcda combine and ahp_weights (#1311)#1312
Merged
brendancol merged 2 commits intomainfrom Apr 29, 2026
Merged
Reject NaN/Inf weights in mcda combine and ahp_weights (#1311)#1312brendancol merged 2 commits intomainfrom
brendancol merged 2 commits intomainfrom
Conversation
_validate_weights and the owa() order_weights check only verified that weights summed to ~1.0. Because abs(NaN - 1.0) > 0.01 is False, a NaN weight passed validation, propagated through every pixel, and returned an all-NaN raster with no exception. ahp_weights had the same shape of bug: its `if val <= 0:` guard let NaN slip into the comparison matrix, producing NaN weights from the eigenvector solve. Add explicit np.isfinite checks before the sum-to-1 check in _validate_weights, mirror the same sweep over order_weights inside owa, and extend the ahp_weights value guard to reject NaN/Inf comparisons. Errors name the offending criterion or position. Also record the mcda security audit row in .claude/sweep-security-state.csv.
Contributor
Author
|
@copilot resolve the merge conflicts in this pull request |
Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
Contributor
Merge conflicts resolved in commit |
This was referenced Apr 29, 2026
brendancol
added a commit
that referenced
this pull request
Apr 30, 2026
`wpm` computes `prod(x_i ** w_i)`, which is undefined for `x_i <= 0` when `w_i` is non-integer. A zero base collapses the product to zero and masks upstream standardization bugs; a negative base produces NaN with no warning. The function docstring claimed inputs should be standardized 0-1 but no runtime check enforced it. Add `_check_wpm_positive` to scan each criterion's data and raise `ValueError` naming any variable whose minimum is `<= 0`. NaN values still propagate, preserving the documented behaviour exercised by `test_wpm_nan`. Update the existing zero/all-zero tests to assert the new error and add three more tests covering negative inputs, error text, and the positive-path baseline. Sibling Cat 3 follow-up to #1311 / #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 #1311.
_validate_weightsandowa()'s order-weights check only verified that weights summed to about 1.0. Sinceabs(NaN - 1.0) > 0.01is False, a NaN weight slipped past the check, hit every pixel, and the function happily returned an all-NaN raster with no exception.ahp_weightshas the same problem one level down: itsif val <= 0:guard lets NaN through into the comparison matrix, and the eigenvector solve produces NaN weights from there.Changes
xrspatial/mcda/combine.py: added_check_finite_weightsand called it from_validate_weights. Same idea applied per-position toorder_weightsinowa(). Error messages name the offending criterion or list index.xrspatial/mcda/weights.py:ahp_weightsrejects NaN/Inf comparison values before the existingval <= 0check.xrspatial/tests/test_mcda.py: 18 new tests for NaN/Inf inwlc,wpm,owa(criterion weights and order weights), andahp_weights. A few positive-path tests too, to make sure the new check doesn't break valid inputs.All 166 mcda tests pass.
Test plan
pytest xrspatial/tests/test_mcda.py::TestNonFiniteWeights1311 -v-- 18/18 passpytest xrspatial/tests/test_mcda.py-- 166/166 passValueErrornaming the bad criterionNotes
One fix per PR, per the security-sweep convention. The mcda audit also turned up some MEDIUM items that aren't fixed here:
sensitivity._monte_carloeagerly computing the full dask Dataset,combine.owastacking criteria viaxr.concatwithout a size guard,n_samples=0divides by zero, andwpmwill accept zero-base/negative-weight combinations. Those are recorded in.claude/sweep-security-state.csvfor follow-up.