Repository navigation
Allow interp along dimensions with a single coordinate value - #11916
Open
VanshajPoonia wants to merge 8 commits into
Open
VanshajPoonia wants to merge 8 commits into
VanshajPoonia wants to merge 8 commits into
Conversation
The interpolants need two points or more, so interp returned NaN with a RuntimeWarning, or raised, along a dimension of size 1. Take the data at that point for new coordinates equal to it, and treat the others as out of range. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
|
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
Cubic interpolation can be off by one ulp on some platforms. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
| f"A new coordinate of {dim!r} is out of the interpolation " | ||
| f"range, which is the single point {x.values[0]}." | ||
| ) | ||
| fill = Variable(new_x.dims, where(new_x.data < point, below, above)) |
There was a problem hiding this comment.
Could the fill arrays be broadcast over the remaining data dimensions before combining them with new_x? A tuple of per-y fill arrays works with ordinary interpolation, but this path combines shapes (3,), (2,), (2,) and raises on both NumPy and Dask:
a = xr.DataArray([[2., 5.]], dims=("x", "y"), coords={"x": [6.]})
fill = (np.array([-1., -3.]), np.array([-2., -4.]))
a.interp(x=[4., 6., 8.], kwargs={"fill_value": fill})
# ValueError: operands could not be broadcast together ...The expected rows are [-1., -3.], [2., 5.], [-2., -4.]; the same call on data [[2., 5.], [2., 5.]] with x=[5., 7.] returns those rows.
Contributor
Author
There was a problem hiding this comment.
Fixed in c99a45f.
- Array fill values now broadcast against the dimensions that aren't interpolated, the same way scipy does on the normal path.
- Only a 2-tuple is split into (below, above). A list or array of length 2 is one per-component fill used on both sides, which is also scipy's rule. Before that any 2-item sequence was split.
- Your example now gives [-1, -3], [2, 5], [-2, -4], the same as the two point data.
- Tests: three new cases in test_interp_single_point_out_of_range (tuple of arrays, list, array), each on NumPy and Dask with Dask under raise_if_dask_computes.
- 6 of them fail on the previous head. test_interp + test_missing: 350 pass. pre-commit clean, no new mypy errors.
An array fill value now lines up with the other dimensions, as scipy does on the normal path, and only a tuple splits it into below and above values. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
Description
SciPy's interpolants need at least two points so
interpalong a dimension of size 1 returned NaN with aRuntimeWarning(linear on 2-D data, as in the issue) or raised aValueError(slinear, cubic, pchip, akima and others).interpnow handles those dimensions itself, without going through.sel(): it takes the data at the single point, and a new coordinate equal to that point gets it. Any other new coordinate is out of range, so it gets NaN, or thefill_valuepassed inkwargs(a scalar or a(below, above)pair). Extrapolating (fill_value="extrapolate",extrapolate=Truefor pchip and akima, or krogh and barycentric, which always extrapolate) repeats the value, since a constant is the only curve through one point.bounds_error=Trueraises for an off-point coordinate. The other dimensions are interpolated as they were being before.The check is on the top of
missing.interpso it also covers the multi-dimensionalinterpnpath and it stays lazy for dask.It doesn't touch the parts of
missing.pythat #11708 and #11885 change. #11885's fast path already skips sources with fewer than two points, and the merge of both passestest_interp.pyandtest_missing.py.Checklist
whats-new.rstapi.rstAI Disclosure
Tools: Claude Code (Claude Opus 5.5). Prompt: "Fix Allow interpolating along singleton dimensions #10747 so interp works along size-1 dimensions without routing through .sel(). Keep it small and local in missing.py so it merges with Fast and exact multidimensional cubic/quintic interp #11708 and Speed up interp at a few points of a chunked array #11885, and test 1-D and 2-D cases, off-grid targets, dask and that no RuntimeWarning is raised."