Add kernel density estimation (KDE)#1170
Merged
brendancol merged 7 commits intomasterfrom Apr 6, 2026
Merged
Conversation
_extract_transect was calling .compute() on the full dask array just to read a handful of transect cells. Now uses vindex fancy indexing so only the relevant chunks are materialized. cumulative_viewshed was allocating a full-size np.zeros count array and calling .values on each viewshed result, forcing materialization every iteration. Now accumulates lazily with da.zeros and dask array addition when the input is dask-backed.
The dask Tier B memory guard underestimated peak usage at 280 bytes/pixel. Actual peak during lexsort reaches ~360 bytes/pixel (sorted + unsorted event_list coexist) plus 8 bytes/pixel for the computed raster. Updated estimate to 368 bytes/pixel to prevent borderline OOM. Also use astype(copy=False) to skip the float64 copy when data is already float64.
Implements kde() and line_density() for point-to-raster and line-to-raster density surfaces. Supports Gaussian, Epanechnikov, and quartic kernels with automatic bandwidth selection via Silverman's rule. All four backends: numpy, cupy, dask+numpy, dask+cupy.
35 tests covering correctness, edge cases, kernel types, bandwidth selection, weights, and cross-backend parity (dask+numpy, cupy). Removes hard cutoff from GPU Gaussian kernel to avoid box-vs-circle mismatch with CPU.
Creates docs/source/reference/kde.rst with autosummary entries for kde() and line_density(). Adds both functions to __init__.py and the docs toctree.
Covers Gaussian/Epanechnikov/quartic kernels, bandwidth effects, weighted KDE, and line density with synthetic earthquake and road network data.
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.
The repo has
rasterize()for burning discrete values onto a grid but nothing for continuous density surfaces. This adds that.Closes #1143
Summary
kde()turns point coordinates into a density raster. Gaussian, Epanechnikov, and quartic kernels. Automatic bandwidth (Silverman's rule) or manual. Optional per-point weights. All four backends: numpy, cupy, dask+numpy, dask+cupy.line_density()does the same for line segments. numpy only for now.examples/user_guide/49_KDE.ipynb) with earthquake cluster and road network examples.docs/source/reference/kde.rstadded, README feature matrix updated.Test plan
pytest xrspatial/tests/test_kde.py-- 35/35 passingjupyter nbconvert --executemake html