Cut head_tail_breaks and box_plot dask re-scans#1213
Merged
brendancol merged 1 commit intomasterfrom Apr 16, 2026
Merged
Conversation
head_tail_breaks (dask) called .compute() three times per iteration of its while-loop (mean, new-mask count, old-mask count) and rebuilt the same data_clean graph every time. For N iterations that was 3N+1 full graph traversals. Persist data_clean once, track the running mask count across iterations, and fuse the mean+head-count reductions into a single dask.compute() per iteration. Wall time drops from ~910 ms to ~340 ms on 256x256 chunks=64. box_plot (dask and dask+cupy) did data_clean[da.isfinite(data_clean)] which is boolean fancy indexing on a dask array. That forces compute_chunk_sizes, materializing a full scan just to know the output chunk layout before percentile can run. Swap in the same seeded _generate_sample_indices sampler that natural_breaks/quantile already use: gather 200k indices on the dask array, compute the sample and the global nanmax in one dask.compute() call, and take percentiles on the finite portion of the sample in numpy.
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
_run_dask_head_tail_breaks: persistdata_cleanonce, track the running mask count across iterations, and fuse the mean and head-count reductions into a singledask.compute()call per iteration. Cuts per-iteration graph traversals from 3 to 1 and eliminates the re-read on every loop pass._run_dask_box_plot(new) and_run_dask_cupy_box_plot: replacedata_clean[da.isfinite(data_clean)](which forcescompute_chunk_sizes) with the same seeded_generate_sample_indicessampler thatnatural_breaksandquantilealready use. Percentiles are then computed on the finite portion of the sample in numpy.Motivation
Static analysis flagged three HIGH-severity patterns on the dask backends of
classify:_run_dask_head_tail_breaksran.compute()inside awhileloop for the mean, new-mask count, and total-mask count — 3 full graph traversals per iteration, N+1 iterations typical._run_box_plot(..., module=da)used boolean fancy indexing on a dask array, which triggerscompute_chunk_sizes()and performs an extra full scan beforeda.percentileruns._run_dask_cupy_box_plothad the same pattern plus a fullmap_blocks(cupy.asnumpy)over the dataset before sampling.Benchmark
head_tail_breaksdask path on a 256×256 gamma-distributed float64 array, chunks=64:box_plotdask path on 512×512, chunks=128:Test plan
pytest xrspatial/tests/test_classify.py— 85 tests passhead_tail_breaksdask output has the same bin count as numpy path on the same seedbox_plotdask output uses sampled quantiles; verify output classes match numpy path within sampling toleranceNotes
Sample size for the box_plot dask path is capped at 200,000 elements (or the full dataset if smaller). This matches the pattern used by
natural_breaksand keeps the percentile computation O(sample) rather than O(dataset).