convolution: validate kernel and agg inputs (#3623)#3624
Open
brendancol wants to merge 2 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.
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