polygonize: reject non-finite simplify_tolerance (nan, inf)#2584
Open
brendancol wants to merge 5 commits into
Open
polygonize: reject non-finite simplify_tolerance (nan, inf)#2584brendancol wants to merge 5 commits into
brendancol wants to merge 5 commits into
Conversation
Mirror the atol/rtol validation already used in the same function: require simplify_tolerance to be finite and non-negative. Previously only negative values were rejected. NaN silently disabled simplification because the gating check `simplify_tolerance > 0` is False for NaN, and +inf collapsed every polygon to empty output. Both now raise ValueError with a clear message. Tests cover nan, +inf, -inf (regression for sign-only rejection of -inf), the existing negative case, and 0.0 / small-positive happy paths so an over-eager fix that bans non-positive values would also fail.
brendancol
commented
May 28, 2026
Contributor
Author
brendancol
left a comment
There was a problem hiding this comment.
PR Review: polygonize: reject non-finite simplify_tolerance (nan, inf)
Blockers (must fix before merge)
- None.
Suggestions (should fix, not blocking)
- None.
Nits (optional improvements)
- None. The
simplify_tolerancevalidation could be co-located with theatol/rtolvalidation block atxrspatial/polygonize.py:2013for tighter grouping, but the current placement keeps it next tosimplify_methodvalidation and matches the pre-existing structure. Moving it would be churn.
What looks good
- The check
not np.isfinite(simplify_tolerance) or simplify_tolerance < 0is correct:np.isfinitereturns False for nan / +inf / -inf and True for normal finite floats including 0. Combined with the existing< 0guard, this allows only finite non-negative values (which includes 0.0). Pythonorshort-circuits, so the< 0comparison never sees nan and we avoid theRuntimeWarning: invalid value encountered in lessthat bare numpy comparisons would emit. - Validation lives in the public
polygonizeentry point beforeArrayTypeFunctionMappingdispatch, so the new contract applies uniformly to numpy, cupy, dask+numpy, and dask+cupy without per-backend duplication. - Error message matches the wording style of the existing
atol/rtolerrors ("must be a non-negative finite number, got ..."), andmatch="simplify_tolerance"in the tests stays stable across future message tweaks. - Tests use a uniform 2x2 raster for the failure cases (no actual polygon edges) and a 2x4 two-label raster for the happy-path 0.0 / 1e-9 case so simplification has something to act on. The split is appropriate -- validation runs before geometry work, so the failure-case raster does not need real edges.
- The
test_zero_and_small_positive_tolerance_still_workregression guard is a nice touch: it would catch a future "fix" that bans non-positive along with non-finite. - CHANGELOG entry under Unreleased / Fixed is accurate and references the issue.
Checklist
- Algorithm matches reference/paper -- N/A, input validation only
- All implemented backends produce consistent results -- validation runs pre-dispatch
- NaN handling is correct -- nan rejected up front, no downstream NaN propagation concerns
- Edge cases are covered by tests -- nan, +inf, -inf, -1.0, 0.0, 1e-9
- Dask chunk boundaries handled correctly -- N/A, no chunk-level code touched
- No premature materialization or unnecessary copies -- N/A, scalar validation only
- Benchmark exists or is not needed -- not needed for a one-line validation check
- README feature matrix updated (if applicable) -- N/A, no API surface change
- Docstrings present and accurate -- existing
simplify_tolerancedocstring at line 1877 still applies; consistent withatol/rtolwhich also do not enumerate their validation in the docstring
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
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
nan,+inf,-inf) forpolygonize'ssimplify_toleranceargument with a clearValueError. Previouslynansilently disabled simplification (becausenan > 0is False) and+infcollapsed every polygon to empty output.atol/rtolvalidation contract in the same function: finite and non-negative.Closes #2575
Backend coverage
Validation lives in the public
polygonizeentry point, before backend dispatch, so the new check applies uniformly to numpy, cupy, dask+numpy, and dask+cupy.Test plan
simplify_tolerance=nanraisesValueErrorsimplify_tolerance=+infraisesValueErrorsimplify_tolerance=-infraisesValueError(was rejected by sign, now by finite check too)simplify_tolerance=-1.0still raises (regression)simplify_tolerance=0.0andsimplify_tolerance=1e-9still succeed (guard against over-eager fix)TestPolygonizeSimplifyclass passes locally (18 passed, 1 skipped)