Skip to content

Faster neighbor_stat, lazy unstructured slicing, lighter upscaled/idw and nearest neighbor cache - #14

Merged
matschaer merged 7 commits into
mainfrom
perf/neighbor-stat-lazy-slice
Nov 19, 2026
Merged

matschaer merged 7 commits into
mainfrom
perf/neighbor-stat-lazy-slice

Conversation

@matschaer

Copy link
Copy Markdown
Collaborator

Performance follow-up of #13 for the operational cases (data4web-pipelines LPI, interpolation of native ICON), and small internal cleanups. 6 commits:

  • Lazy unstructured sources are sliced by chunks. Unstructured grids opened lazily from a file (e.g. xarray.open_dataset of a netCDF file, without dask) are read by chunks of 16 MB, each with one slice, then loaded. Before, the selected points were read one by one. Native ICON-CH1 netCDF to the seamless 2 km grid, slice_grid and loading: 50 s → 1.4 s. Sources in memory, dask arrays and regular grids are unchanged.
  • ens_stats.neighbor_stat (and upscaled_prob) use scipy.ndimage filters on regular grids and along time, applied to all the dimensions of the window at once. Unstructured grids use the sparse neighbor matrix.
    • ICON-CH1-EPS gridefix max, windows 17 / 3 (data4web LPI): 3.8 s → 1.1 s, peak memory 1.07 → 0.32 GB.
    • Regular grid 34 x 1072², window 5: mean 4.3 → 0.6 s, sum 2.4 → 0.5 s, max 3.6 → 1.0 s.
  • upscaled and idw use less memory. upscaled computes its statistic by blocks of POIs; idw is a product with a sparse weight matrix. Results are identical.
    • Native ICON-CH1-EPS to the 5,622 production POIs, upscaled with k=289: 1.19 → 0.39 s, peak +2.5 → +0.3 GB.
    • Gridefix ICON-CH1-EPS, 3 variables, to the seamless 2 km grid with idw: 0.78 → 0.43 s, peak +0.92 → +0.31 GB.
  • nn_lookup_cached has a new distances parameter (part of the cache key; the distances are None if False) and returns int32 indices. Methods that don't need distances cache 4 times less memory.
  • Internal cleanups:
    • the sparse helpers shared by grid_interp and ens_stats moved to the private module grid_process/_sparse.py, so no private names are imported across modules;
    • new public data_model.get_spatial_coord_names. The averaging of interp2grid and slice_coarsen uses it, so it no longer averages position coordinates that are recognised only by CF attributes (e.g. rotated rlat/rlon of an unstructured grid that also has lat/lon).

Changes of results and API (see CHANGELOG, Unreleased):

  • neighbor_stat with mean and NaN gives the exact mean of the valid values over the whole space x time window, instead of the mean of the means along each dimension. Without NaN, and for sum/min/max, results are unchanged: min/max identical, sum/mean within float32 rounding.
  • neighbor_stat returns float32 for 8/16-bit integer and boolean data (mean/sum were float64, and so was max of booleans).
  • nn_lookup_cached returns int32 indices and has the new distances parameter.

All other results were checked against the previous version on the operational grids (identical).


Related issue

Follow-up of #13


Checklist

  • Code is well documented and follows project conventions (linted, formatted)
  • CHANGELOG updated (if applicable)
  • License header is added to all new files
  • For a new contributors, their name has been added to the AUTHORS file
  • No sensitive data or secrets are exposed
  • Unit tests added covering the new code

🤖 Generated with Claude Code

Mathieu Schaer and others added 7 commits November 18, 2026 17:50
Indexing a lazily opened netCDF variable with the boolean mask of an
unstructured grid reads the selected points one by one. The variables
neither in memory nor dask are now read by chunks of 16 MB (one slice
each, only the chunks with selected points) and loaded: native ICON-CH1
netCDF sliced to the seamless 2 km grid 50 s -> 1.4 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One function over all the dimensions of the window: scipy.ndimage
filters along regular dimensions and time (instead of rolling), the
neighbors along an unstructured dimension. With NaN, mean is the mean of
the valid values over the whole space x time window (was a mean of
means); sum, min and max unchanged. Results in float32 for 8/16 bit
integers and booleans.

ICON-CH1-EPS gridefix max with windows 17 and 3: 3.8 s -> 1.1 s, peak
1.07 GB -> 0.32 GB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- interp_upscaled: agg_func by blocks of POIs (4 MB of gathered
  neighbor values), instead of gathering all of them at once.
- interp_idw: product with the sparse matrix of the weights.
- nn_lookup_cached: distances parameter (None if False, in the cache key)
  and int32 indices, used without copy by the sparse matrices.

Native ICON-CH1-EPS to the 5,622 production POIs with upscaled k=289:
1.19 s -> 0.39 s, peak +2.5 GB -> +0.3 GB, identical results.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sparse_matmul and rows_matrix are used by grid_interp and ens_stats: public names in a package internal module
instead of private names imported across modules. No behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
All the coordinates giving horizontal positions: spatial dimensions, configured y/x pairs and CF axis,
standard_name or long_name, without parsing the CRS. Replaces grid_interp._spatial_coord_names in the
averaging of interp2grid and slice_coarsen, which missed CF position coordinates next to another pair (e.g.
rotated rlat/rlon of an unstructured grid with lat/lon) and averaged them. Same names for the operational
grids.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The NaN and dtype rules are in the slice_coarsen docstring it refers to.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…and _cf_latlon

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matschaer
matschaer merged commit a81744d into main Nov 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant