Skip to content

convolution: validate kernel and agg inputs (#3623)#3624

Open
brendancol wants to merge 2 commits into
xarray-contrib:mainfrom
brendancol:deep-sweep-error-handling-convolution-2026-07-02
Open

convolution: validate kernel and agg inputs (#3623)#3624
brendancol wants to merge 2 commits into
xarray-contrib:mainfrom
brendancol:deep-sweep-error-handling-convolution-2026-07-02

Conversation

@brendancol

Copy link
Copy Markdown
Contributor

What

Validate inputs to the two public convolution entry points.

  • convolve_2d: new _validate_kernel rejects a non-array, non-2D, or even-sided kernel with a ValueError that names kernel. These previously reached the numba kernel and raised a TypingError, or (even side lengths) returned a silently off-center result. The check is duck-typed on ndim/shape, so numpy and cupy kernels both pass.
  • convolution_2d: validate that agg is a 2D DataArray with _validate_raster. A numpy input previously failed with "'memoryview' object has no attribute 'astype'".

Tests

xrspatial/tests/test_convolution.py gains cases for None/1D/3D/list kernels, even-sided kernels, and a non-DataArray agg, plus positive paths for both functions. test_focal.py and test_edge_detection.py pass 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

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.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Jul 2, 2026
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.

convolve_2d / convolution_2d skip input validation and silently accept even-sided kernels

1 participant