Guard kriging() against unbounded memory allocations (#1307)#1309
Merged
Conversation
kriging() takes an arbitrary point count N and template grid size with no upper bound. Three eager allocations scale with these inputs: - np.triu_indices(N) in _experimental_variogram (O(N^2) int64 pairs) - the (N+1) x (N+1) kriging matrix and its inverse - the (grid_pixels, N+1) prediction matrix in _kriging_predict A caller passing 50k points or a 5000x5000 template silently triggers tens of GB of allocation before any guard. Add _check_kriging_memory() that estimates the worst case of these three and raises MemoryError when the estimate exceeds 80% of available memory (using xrspatial.zonal._available_memory_bytes, same pattern as balanced_allocation). The error message names which allocation drove the estimate so the user knows whether to reduce N or the grid size.
This was referenced Apr 29, 2026
brendancol
added a commit
that referenced
this pull request
Apr 30, 2026
) The k-nearest path in `idw()` calls `cKDTree.query(query_pts, k=k)`, which returns a `(grid_pixels, k)` float64 distance array and an int64 index array. Peak allocation is `grid_pixels * k * 16` bytes before any IDW arithmetic runs. A 50000 x 50000 template with k=12 needs about 480 GB and OOMs the process with no message naming the inputs that caused it. Add `_check_idw_memory(grid_pixels, k)` and call it at the top of the public `idw()` entrypoint when k is set on a numpy-backed template. Dask templates dispatch `_idw_knearest_numpy` per chunk via `map_blocks`, so chunk size already bounds the per-chunk allocation; the guard skips dask paths to avoid refusing legitimate chunked workloads. GPU backends reject k early. Same shape as the kriging guard from #1309. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: brendancol <433221+brendancol@users.noreply.github.com>
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
kriging(). It estimates the worst case across the three large allocations (variogram pair arrays atN*(N-1)/2, the(N+1)x(N+1)kriging matrix, and the(grid_pixels, N+1)prediction matrix) and raisesMemoryErrorbefore any of them run.0.8 * _available_memory_bytes()threshold, helper imported fromxrspatial.zonal.Nor the template grid.TestKrigingMemoryGuardmonkeypatch_available_memory_bytesto a small number. Covers the prediction-matrix path, the kriging-matrix path, a small-input pass-through, and a direct unit test of the helper.Closes #1307
Test plan
pytest xrspatial/tests/test_interpolation.py(34 passed locally)