Skip to content

Reject non-positive inputs in wpm() (#1382)#1385

Merged
brendancol merged 1 commit intomainfrom
issue-1382
Apr 30, 2026
Merged

Reject non-positive inputs in wpm() (#1382)#1385
brendancol merged 1 commit intomainfrom
issue-1382

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

Closes #1382.

wpm computes prod(x_i ** w_i). For non-integer weights this is only well-defined when x_i > 0: a zero base collapses the whole product to zero and masks upstream bugs in standardization, and a negative base produces NaN with no error. The docstring said inputs should be standardized 0-1 but nothing enforced that at runtime.

Sibling Cat 3 follow-up to the NaN-weight fix in #1311 / #1312, which flagged this case as deferred.

Changes

  • xrspatial/mcda/combine.py: added _check_wpm_positive, called from wpm after the existing weight checks. It runs nanmin per variable (eager compute for dask, since correctness here is worth one extra pass) and raises ValueError listing every variable whose minimum is <= 0. NaN values pass through, so the documented NaN-propagation behaviour is preserved. Updated the docstring to spell out the positivity requirement.
  • xrspatial/tests/test_mcda.py: rewrote the two tests that asserted zero-collapses-to-zero (test_zero_value_kills_product, test_all_zeros) to expect the new error, and added three more tests: negative input, error message names the offending variable, and a positive-path baseline.

Test plan

  • pytest xrspatial/tests/test_mcda.py -- 169/169 pass
  • Reproduction from the issue now raises ValueError naming 'a' and 'b'
  • NaN inputs still propagate through to the output (existing test_wpm_nan passes)

`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.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 30, 2026
@brendancol brendancol merged commit 8489d55 into main Apr 30, 2026
11 checks passed
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.

wpm() accepts zero-base/negative inputs producing silent zero/NaN

1 participant