Repository navigation
Faster neighbor_stat, lazy unstructured slicing, lighter upscaled/idw and nearest neighbor cache - #14
Merged
Merged
Conversation
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>
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.
Performance follow-up of #13 for the operational cases (data4web-pipelines LPI, interpolation of native ICON), and small internal cleanups. 6 commits:
xarray.open_datasetof 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_gridand loading: 50 s → 1.4 s. Sources in memory, dask arrays and regular grids are unchanged.ens_stats.neighbor_stat(andupscaled_prob) usescipy.ndimagefilters on regular grids and along time, applied to all the dimensions of the window at once. Unstructured grids use the sparse neighbor matrix.max, windows 17 / 3 (data4web LPI): 3.8 s → 1.1 s, peak memory 1.07 → 0.32 GB.mean4.3 → 0.6 s,sum2.4 → 0.5 s,max3.6 → 1.0 s.upscaledandidwuse less memory.upscaledcomputes its statistic by blocks of POIs;idwis a product with a sparse weight matrix. Results are identical.upscaledwithk=289: 1.19 → 0.39 s, peak +2.5 → +0.3 GB.idw: 0.78 → 0.43 s, peak +0.92 → +0.31 GB.nn_lookup_cachedhas a newdistancesparameter (part of the cache key; the distances areNoneifFalse) and returns int32 indices. Methods that don't need distances cache 4 times less memory.grid_interpandens_statsmoved to the private modulegrid_process/_sparse.py, so no private names are imported across modules;data_model.get_spatial_coord_names. The averaging ofinterp2gridandslice_coarsenuses it, so it no longer averages position coordinates that are recognised only by CF attributes (e.g. rotatedrlat/rlonof an unstructured grid that also haslat/lon).Changes of results and API (see CHANGELOG, Unreleased):
neighbor_statwithmeanand 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 forsum/min/max, results are unchanged:min/maxidentical,sum/meanwithin float32 rounding.neighbor_statreturns float32 for 8/16-bit integer and boolean data (mean/sumwere float64, and so wasmaxof booleans).nn_lookup_cachedreturns int32 indices and has the newdistancesparameter.All other results were checked against the previous version on the operational grids (identical).
Related issue
Follow-up of #13
Checklist
🤖 Generated with Claude Code