Add sieve filter to remove small raster clumps#1159
Merged
brendancol merged 2 commits intomasterfrom Apr 2, 2026
Merged
Conversation
Implements a sieve() function that identifies connected components of same-value pixels and replaces regions smaller than a threshold with the value of their largest spatial neighbor. Supports 4- and 8-connectivity, selective sieving via skip_values, and all four backends (numpy, cupy via CPU fallback, dask+numpy, dask+cupy).
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.
Summary
sieve()function: removes small connected clumps from classified rasters by replacing regions below a pixel-count threshold with their largest neighbor's valueskip_values, all four backends (numpy and dask+numpy native, cupy and dask+cupy with CPU fallback)Details
Labels connected components per unique value with
scipy.ndimage.label, builds a region adjacency graph from vectorized array shifts, then merges the smallest regions into their largest spatial neighbor until everything meets the threshold.Dask paths compute the full array into memory first (same as
regions()) because connected-component labeling is a global operation. Memory guards prevent OOM on large arrays.New files:
xrspatial/sieve.py-- implementationxrspatial/tests/test_sieve.py-- 40 testsexamples/user_guide/48_Sieve_Filter.ipynb-- user guide notebookModified files:
xrspatial/__init__.py-- addedsieveexportdocs/source/reference/zonal.rst-- added API entryREADME.md-- added row to Morphological feature matrixCloses #1149
Test plan
pytest xrspatial/tests/test_sieve.py-- 40/40 passingmake html