Fixes #881: dask-safe unique in zonal_stats and crosstab#894
Merged
brendancol merged 1 commit intomasterfrom Feb 25, 2026
Merged
Fixes #881: dask-safe unique in zonal_stats and crosstab#894brendancol merged 1 commit intomasterfrom
brendancol merged 1 commit intomasterfrom
Conversation
…onal.py np.unique(zones[np.isfinite(zones)]) silently materialises the full dask array into RAM, causing OOM on large rasters. Replace with da.unique() which reduces per-chunk and only .compute()s the tiny set of distinct zone IDs.
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
zonal_stats()andcrosstab()callednp.unique(zones[np.isfinite(zones)])at the top of their dask code paths. Bothnp.isfinite(boolean fancy-indexing) andnp.uniquesilently force full materialisation of the dask array into RAM — guaranteed OOM on large rasters._unique_finite_zones()and_unique_finite_cats()helpers that useda.unique()(per-chunk reduction) then.compute()only the tiny result (just the distinct zone IDs). Updated all 5 call sites (2 dask paths + 2 numpy paths for consistency + 1 in_find_cats)._stats_dask_numpywithda.from_delayed(..., shape=(np.nan,))to preserve unknown-chunk metadata expected by downstreamdd.from_dask_array.Test plan
test_zonal.pytests pass unchangedtest_stats_does_not_materialise_dask_zones— monkeypatchesnp.uniqueto raise if passed ada.Arraytest_crosstab_does_not_materialise_dask_zones— same guard for crosstab