Fixes #899, #902: fix dask zonal.stats() bug, add dask+cupy backend, edge-case tests#911
Merged
brendancol merged 3 commits intomasterfrom Feb 27, 2026
Merged
Conversation
The conditions `if 'mean' or 'std' or 'var' in stats_funcs` always evaluated to True because the string 'mean' is truthy. This caused compute_sum, compute_count, and compute_sum_squares to always be set, wasting work on every dask zonal.stats() call regardless of which stats were requested. Fix: use `any(s in stats_funcs for s in (...))` for correct membership testing. Add regression tests covering 7 stat subsets on both numpy and dask backends to exercise each compute flag independently.
… semantics Replaces the numpy-only, input-mutating apply() with a proper multi-backend implementation (numpy, cupy, dask+numpy, dask+cupy) that returns a new DataArray instead of mutating the input. Uses ArrayTypeFunctionMapping for dispatch and .data.dtype for validation to avoid materializing dask/cupy arrays.
Add _stats_dask_cupy() that converts dask+cupy blocks to numpy via map_blocks(x.get()) then delegates to the existing _stats_dask_numpy pipeline. Wire it into the ArrayTypeFunctionMapping dispatcher. Add five new edge-case test groups (18 test cases across backends): - all-NaN zone: documents per-backend empty-zone behavior - single-cell zones: std/var must be 0, not NaN - negative zone IDs: exercises sort-and-stride with negatives - nodata wipes zone: all finite values match nodata_values - zone in subset of blocks: zone present in only some dask chunks
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
_stats_dask_numpy()boolean short-circuit bug whereif 'mean' or 'std' or 'var' in stats_funcsalways evaluated toTrue(the string'mean'is truthy), causingcompute_sum,compute_count, andcompute_sum_squaresto be set on every call regardless of requested statsany(s in stats_funcs for s in ('mean', 'std', 'var'))for correct membership testing_stats_dask_cupy()backend (Add zonal.stats() dask+cupy backend #902): converts dask+cupy blocks to numpy viamap_blocks(x.get())then delegates to the existing_stats_dask_numpypipeline_stats_dask_cupyinto theArrayTypeFunctionMappingdispatcher (replacingnot_implemented_func)'dask+cupy'totest_default_statsandtest_zone_ids_statsparametrize lists, fix skip guards to use'cupy' in backendnodata_valuesTest plan
test_stats_subset_columns— 7 parametrized stat subsets on numpy and dask+numpytest_default_statsandtest_zone_ids_stats— now includedask+cupybackendtest_zonal.pysuite passes (106 tests)