Skip to content

Add morphological raster operators (#949)#958

Merged
brendancol merged 5 commits into
masterfrom
issue-949
Mar 4, 2026
Merged

Add morphological raster operators (#949)#958
brendancol merged 5 commits into
masterfrom
issue-949

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Summary

  • New module xrspatial/morphology.py with four grayscale morphological operators: morph_erode (local minimum), morph_dilate (local maximum), morph_opening (erosion then dilation), morph_closing (dilation then erosion)
  • All four work across numpy, cupy, dask+numpy, and dask+cupy backends
  • Accepts user-defined structuring elements (2D numpy arrays with odd dimensions); defaults to a 3x3 square kernel
  • _circle_kernel(radius) helper for disk-shaped structuring elements
  • Wired into the .xrs accessor on both DataArray and Dataset

Test plan

  • Correctness against hand-computed values for erode, dilate, opening, closing
  • Cross-shaped kernel (non-square footprint)
  • Opening removes single bright spike; closing fills single dark pit
  • Idempotence: opening(opening(x)) == opening(x), same for closing
  • NaN propagation from source cells and padded boundaries
  • Single-cell raster edge case
  • Uniform raster identity
  • Input validation: non-DataArray, 3D input, even kernel, invalid boundary
  • All four boundary modes (nan, nearest, reflect, wrap)
  • Dask backend matches numpy for all four operators
  • CuPy backend matches numpy for all four operators
  • Dask+CuPy backend matches numpy for erode and dilate
  • 5x5 kernel produces wider erosion than 3x3
  • Dataset support via @supports_dataset

Closes #949

New module xrspatial/morphology.py with four functions: morph_erode,
morph_dilate, morph_opening, morph_closing.  All four support numpy,
cupy, dask+numpy, and dask+cupy backends via ArrayTypeFunctionMapping.
32 tests covering correctness, NaN propagation, edge cases,
boundary modes, idempotence, Dataset support, and all four
backends (numpy, cupy, dask+numpy, dask+cupy).
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Mar 4, 2026
@brendancol brendancol merged commit 62d3929 into master Mar 4, 2026
10 of 11 checks passed
@brendancol brendancol deleted the issue-949 branch May 4, 2026 13:06
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.

Add morphological raster operators (erode, dilate, open, close)

1 participant