convolution: validate kernel and agg inputs (#3623)#3624
Open
brendancol wants to merge 4 commits into
Open
Conversation
convolve_2d checked the raster but never the kernel. A None, 1D, 3D, or list kernel reached the numba kernel and raised a TypingError that named nothing the user controls. An even-sided kernel was accepted and produced a silently off-center result, even though custom_kernel already rejects even kernels. Add _validate_kernel (2D, odd side lengths, duck-typed on ndim/shape so numpy and cupy kernels both pass) and call it in convolve_2d. Also call _validate_raster in convolution_2d so a non-DataArray agg raises a clear TypeError instead of "'memoryview' object has no attribute 'astype'". Internal callers (focal, edge_detection, emerging_hotspots) all pass odd 2D arrays, so they are unaffected.
brendancol
commented
Jul 6, 2026
brendancol
left a comment
Contributor
Author
There was a problem hiding this comment.
PR Review: convolution: validate kernel and agg inputs (#3623)
Reviewed the full convolution.py and test_convolution.py on the PR branch, plus _validate_raster/_validate_boundary in utils.py. Ran the test file against the branch: 15 passed. The change does what it says, and the tests cover the cases that mattered.
Blockers (must fix before merge)
None.
Suggestions (should fix, not blocking)
_validate_kernel(xrspatial/convolution.py:356) checks shape and dimensionality but not dtype. A 2D object-dtype or string kernel with odd sides passes the guard and still reaches numba, producing the crypticTypingErrorthis PR is meant to prevent.custom_kernelhas the same gap, so this is not a regression, and dtype validation may be out of scope. Worth a one-line note in the PR if you are leaving it deliberately.
Nits (optional improvements)
- The duck-typed
not hasattr(kernel, 'ndim')branch (convolution.py:356) reports only the type name, while the other two branches report the shape. That is the right call when there is no shape to report; just flagging that the three messages read a bit differently by design.
What looks good
- Keeping
_validate_kernelseparate fromcustom_kernelrather than reusing it is the right call:custom_kernelhard-requiresnp.ndarray, which would reject valid cupy kernels. Duck-typing onndim/shapekeeps all four backends working, and the odd-side-length contract matchescustom_kernel. - Kernel validation runs before the
ArrayTypeFunctionMappingdispatch (convolution.py:514), so every backend gets the same up-front check through one code path. - The
_validate_rasterguard added toconvolution_2d(convolution.py:647) is load-bearing, not redundant with the re-validation insideconvolve_2d. A numpy array has a.dataattribute (a memoryview), soagg.datawould otherwise succeed and fail later with'memoryview' object has no attribute 'astype'. The guard turns that into a clearTypeErrornaming the parameter, and the test pins the old failure mode. - Tests cover the paths that regressed: bad kernel types (None, 1D, 3D, list), even side lengths, non-DataArray input to
convolution_2d, and the positive path.
Checklist
- Algorithm matches reference/paper (validation-only change, no algorithm touched)
- All implemented backends produce consistent results (validation is pre-dispatch, backend-agnostic)
- NaN handling is correct (unchanged)
- Edge cases are covered by tests
- Dask chunk boundaries handled correctly (unchanged)
- No premature materialization or unnecessary copies
- Benchmark exists or is not needed (validation cost is negligible)
- README feature matrix updated (not applicable; no new public function)
- Docstrings present and accurate
…ling-convolution-2026-07-02 # Conflicts: # .claude/sweep-error-handling-state.csv
…y-contrib#3616/xarray-contrib#3619/xarray-contrib#3620 Keep both sides in convolution.py (_validate_kernel from this branch, _PARALLEL_KERNEL_LOCK + parallel=True kernel from main) and both test blocks. State CSV: main's file with this branch's convolution row.
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.
What
Validate inputs to the two public convolution entry points.
convolve_2d: new_validate_kernelrejects a non-array, non-2D, or even-sided kernel with aValueErrorthat nameskernel. These previously reached the numba kernel and raised aTypingError, or (even side lengths) returned a silently off-center result. The check is duck-typed onndim/shape, so numpy and cupy kernels both pass.convolution_2d: validate thataggis a 2D DataArray with_validate_raster. A numpy input previously failed with "'memoryview' object has no attribute 'astype'".Tests
xrspatial/tests/test_convolution.pygains cases for None/1D/3D/list kernels, even-sided kernels, and a non-DataArrayagg, plus positive paths for both functions.test_focal.pyandtest_edge_detection.pypass unchanged; the cupy backend was spot-checked (valid path works, bad kernels rejected identically, cupy kernels still accepted).Compatibility
No exception type changes on existing valid usage. Even-sided kernels used to "work" (silently wrong); they now raise, matching
custom_kernel's long-standing odd-shape contract. Internal callers (focal, edge_detection, emerging_hotspots) all pass odd 2D arrays.Closes #3623