Validate matching shapes for precomputed corridor surfaces#3447
Open
brendancol wants to merge 3 commits into
Open
Validate matching shapes for precomputed corridor surfaces#3447brendancol wants to merge 3 commits into
brendancol wants to merge 3 commits into
Conversation
least_cost_corridor(precomputed=True) summed two cost-distance surfaces without checking they shared a shape. Mismatched surfaces were silently aligned by xarray on the coordinate intersection, producing a truncated, wrong-valued corridor. Add a _validate_matching_shape check across the precomputed surfaces, matching what cost_distance already enforces.
brendancol
commented
Jun 22, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
PR Review: Validate matching shapes for precomputed corridor surfaces
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
- The guard is shape-only, so two same-shape surfaces with non-overlapping coordinates still fall through to the old failure mode: xarray aligns them to an empty intersection and
nanminraises an opaque error instead of a clear validation message. This matchescost_distance's own shape-only check, so the scope boundary is reasonable, but a one-line comment or follow-up note saying coordinate mismatch is intentionally out of scope would help the next reader.
Nits (optional improvements)
- The two new tests run numpy-only. The validation happens before backend dispatch, so parametrizing over backends adds little, but a single dask-backed case would confirm
.shapeaccess stays lazy (no implicit compute). Optional.
What looks good
- The fix lands exactly on the gap: the precomputed path bypassed
cost_distance, which is where the shape guard lived. Reusing_validate_matching_shapekeeps the error message format consistent with the rest of the codebase. - One loop covers both the two-source and pairwise paths.
- The comment explains why the check is needed (silent xarray alignment), which is the non-obvious part.
- Tests assert on the real failure mode and use the concrete 4x4 + 3x3 -> all-zero 3x3 example from the issue.
Checklist
- Algorithm matches reference/paper (N/A, validation only)
- All implemented backends produce consistent results (validation is backend-agnostic)
- NaN handling is correct (unchanged)
- Edge cases are covered by tests
- Dask chunk boundaries handled correctly (N/A)
- No premature materialization or unnecessary copies
- Benchmark exists or is not needed (not needed)
- README feature matrix updated (not applicable, no new function)
- Docstrings present and accurate (unchanged)
brendancol
commented
Jun 22, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
Follow-up review
Both items from the first pass are handled in a987a48:
- Suggestion (coordinate-mismatch scope): added an inline comment by the shape check stating that, like
cost_distance, the guard is shape-only and same-shape surfaces with mismatched coordinates are out of scope. - Nit (dask coverage): added
test_precomputed_mismatched_shape_dask_raises, which confirms the check fires on dask-backed surfaces without forcing a compute.
Full corridor suite: 35 passed. No remaining findings.
…orridor-2026-06-22-01
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.
Closes #3445
least_cost_corridor(..., precomputed=True)summed two cost-distance surfaces (cd_a + cd_b) without checking they shared a shape. Mismatched surfaces were silently aligned by xarray on the intersection of their coordinates, producing a truncated, wrong-valued corridor (e.g. 4x4 + 3x3 returned an all-zero 3x3 result). The non-precomputed path was already safe becausecost_distancerejects mismatched raster/friction shapes._validate_matching_shapecheck across the precomputed surfaces, the same guardcost_distancealready enforces. Applies to both the two-source and pairwise paths.cost_distance's own behavior (it validates.shape, not coordinate values).Backends: pure xarray arithmetic; the validation runs identically across numpy, cupy, dask+numpy, and dask+cupy.
Test plan: