Skip to content

Commit

Permalink
support for additional scipy nd interpolants (#9599)
Browse files Browse the repository at this point in the history
* nd interpolants added

* remove reduce arg

* docstring formatting

* list of string args for method

* moving list of interpolants

* formatting

* doc build issues

* remove reduce from tests

* removing other interpolants from test_interpolate_chunk_1d since cannot be 1d decomposed

* nd_nd test update

* formatting

* scipy ref

* docstrings

* Update xarray/tests/test_interp.py

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>

* test method changes

* formatting

* fix typing

* removing cubic from chunk 1d test because of timeout

* rerun tests

* try again

* formatting

* Skip fewer tests

* skip quintic for high dim interp

---------

Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
Co-authored-by: Deepak Cherian <deepak@cherian.net>
Co-authored-by: Justus Magin <keewis@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 6, 2024
1 parent fad2773 commit a26c816
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 178 deletions.
5 changes: 3 additions & 2 deletions doc/user-guide/interpolation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ It is now possible to safely compute the difference ``other - interpolated``.
Interpolation methods
---------------------

We use :py:class:`scipy.interpolate.interp1d` for 1-dimensional interpolation.
We use either :py:class:`scipy.interpolate.interp1d` or special interpolants from
:py:class:`scipy.interpolate` for 1-dimensional interpolation (see :py:meth:`~xarray.Dataset.interp`).
For multi-dimensional interpolation, an attempt is first made to decompose the
interpolation in a series of 1-dimensional interpolations, in which case
:py:class:`scipy.interpolate.interp1d` is used. If a decomposition cannot be
the relevant 1-dimensional interpolator is used. If a decomposition cannot be
made (e.g. with advanced interpolation), :py:func:`scipy.interpolate.interpn` is
used.

Expand Down
131 changes: 80 additions & 51 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2228,16 +2228,13 @@ def interp(
kwargs: Mapping[str, Any] | None = None,
**coords_kwargs: Any,
) -> Self:
"""Interpolate a DataArray onto new coordinates
"""
Interpolate a DataArray onto new coordinates.
Performs univariate or multivariate interpolation of a Dataset onto new coordinates,
utilizing either NumPy or SciPy interpolation routines.
Performs univariate or multivariate interpolation of a DataArray onto
new coordinates using scipy's interpolation routines. If interpolating
along an existing dimension, either :py:class:`scipy.interpolate.interp1d`
or a 1-dimensional scipy interpolator (e.g. :py:class:`scipy.interpolate.KroghInterpolator`)
is called. When interpolating along multiple existing dimensions, an
attempt is made to decompose the interpolation into multiple
1-dimensional interpolations. If this is possible, the 1-dimensional interpolator is called.
Otherwise, :py:func:`scipy.interpolate.interpn` is called.
Out-of-range values are filled with NaN, unless specified otherwise via `kwargs` to the numpy/scipy interpolant.
Parameters
----------
Expand All @@ -2246,16 +2243,9 @@ def interp(
New coordinate can be a scalar, array-like or DataArray.
If DataArrays are passed as new coordinates, their dimensions are
used for the broadcasting. Missing values are skipped.
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial"}, default: "linear"
The method used to interpolate. The method should be supported by
the scipy interpolator:
- ``interp1d``: {"linear", "nearest", "zero", "slinear",
"quadratic", "cubic", "polynomial"}
- ``interpn``: {"linear", "nearest"}
If ``"polynomial"`` is passed, the ``order`` keyword argument must
also be provided.
method : { "linear", "nearest", "zero", "slinear", "quadratic", "cubic", \
"quintic", "polynomial", "pchip", "barycentric", "krogh", "akima", "makima" }
Interpolation method to use (see descriptions above).
assume_sorted : bool, default: False
If False, values of x can be in any order and they are sorted
first. If True, x has to be an array of monotonically increasing
Expand All @@ -2275,12 +2265,37 @@ def interp(
Notes
-----
scipy is required.
- SciPy is required for certain interpolation methods.
- When interpolating along multiple dimensions with methods `linear` and `nearest`,
the process attempts to decompose the interpolation into independent interpolations
along one dimension at a time.
- The specific interpolation method and dimensionality determine which
interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:func:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
2. **Interpolation along one dimension of N-dimensional data (N ≥ 1)**
- Methods {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "quintic", "polynomial"}
use :py:func:`scipy.interpolate.interp1d`, unless conditions permit the use of :py:func:`numpy.interp`
(as in the case of `method='linear'` for 1D data).
- If `method='polynomial'`, the `order` keyword argument must also be provided.
3. **Special interpolants for interpolation along one dimension of N-dimensional data (N ≥ 1)**
- Depending on the `method`, the following interpolants from :py:class:`scipy.interpolate` are used:
- `"pchip"`: :py:class:`scipy.interpolate.PchipInterpolator`
- `"barycentric"`: :py:class:`scipy.interpolate.BarycentricInterpolator`
- `"krogh"`: :py:class:`scipy.interpolate.KroghInterpolator`
- `"akima"` or `"makima"`: :py:class:`scipy.interpolate.Akima1dInterpolator`
(`makima` is handled by passing the `makima` flag).
4. **Interpolation along multiple dimensions of multi-dimensional data**
- Uses :py:func:`scipy.interpolate.interpn` for methods {"linear", "nearest", "slinear",
"cubic", "quintic", "pchip"}.
See Also
--------
scipy.interpolate.interp1d
scipy.interpolate.interpn
:mod:`scipy.interpolate`
:doc:`xarray-tutorial:fundamentals/02.2_manipulating_dimensions`
Tutorial material on manipulating data resolution using :py:func:`~xarray.DataArray.interp`
Expand Down Expand Up @@ -2376,43 +2391,67 @@ def interp_like(
"""Interpolate this object onto the coordinates of another object,
filling out of range values with NaN.
If interpolating along a single existing dimension,
:py:class:`scipy.interpolate.interp1d` is called. When interpolating
along multiple existing dimensions, an attempt is made to decompose the
interpolation into multiple 1-dimensional interpolations. If this is
possible, :py:class:`scipy.interpolate.interp1d` is called. Otherwise,
:py:func:`scipy.interpolate.interpn` is called.
Parameters
----------
other : Dataset or DataArray
Object with an 'indexes' attribute giving a mapping from dimension
names to an 1d array-like, which provides coordinates upon
which to index the variables in this dataset. Missing values are skipped.
method : {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "polynomial"}, default: "linear"
The method used to interpolate. The method should be supported by
the scipy interpolator:
- {"linear", "nearest", "zero", "slinear", "quadratic", "cubic",
"polynomial"} when ``interp1d`` is called.
- {"linear", "nearest"} when ``interpn`` is called.
If ``"polynomial"`` is passed, the ``order`` keyword argument must
also be provided.
method : { "linear", "nearest", "zero", "slinear", "quadratic", "cubic", \
"quintic", "polynomial", "pchip", "barycentric", "krogh", "akima", "makima" }
Interpolation method to use (see descriptions above).
assume_sorted : bool, default: False
If False, values of coordinates that are interpolated over can be
in any order and they are sorted first. If True, interpolated
coordinates are assumed to be an array of monotonically increasing
values.
kwargs : dict, optional
Additional keyword passed to scipy's interpolator.
Additional keyword arguments passed to the interpolant.
Returns
-------
interpolated : DataArray
Another dataarray by interpolating this dataarray's data along the
coordinates of the other object.
Notes
-----
- scipy is required.
- If the dataarray has object-type coordinates, reindex is used for these
coordinates instead of the interpolation.
- When interpolating along multiple dimensions with methods `linear` and `nearest`,
the process attempts to decompose the interpolation into independent interpolations
along one dimension at a time.
- The specific interpolation method and dimensionality determine which
interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:func:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
2. **Interpolation along one dimension of N-dimensional data (N ≥ 1)**
- Methods {"linear", "nearest", "zero", "slinear", "quadratic", "cubic", "quintic", "polynomial"}
use :py:func:`scipy.interpolate.interp1d`, unless conditions permit the use of :py:func:`numpy.interp`
(as in the case of `method='linear'` for 1D data).
- If `method='polynomial'`, the `order` keyword argument must also be provided.
3. **Special interpolants for interpolation along one dimension of N-dimensional data (N ≥ 1)**
- Depending on the `method`, the following interpolants from :py:class:`scipy.interpolate` are used:
- `"pchip"`: :py:class:`scipy.interpolate.PchipInterpolator`
- `"barycentric"`: :py:class:`scipy.interpolate.BarycentricInterpolator`
- `"krogh"`: :py:class:`scipy.interpolate.KroghInterpolator`
- `"akima"` or `"makima"`: :py:class:`scipy.interpolate.Akima1dInterpolator`
(`makima` is handled by passing the `makima` flag).
4. **Interpolation along multiple dimensions of multi-dimensional data**
- Uses :py:func:`scipy.interpolate.interpn` for methods {"linear", "nearest", "slinear",
"cubic", "quintic", "pchip"}.
See Also
--------
:func:`DataArray.interp`
:func:`DataArray.reindex_like`
:mod:`scipy.interpolate`
Examples
--------
>>> data = np.arange(12).reshape(4, 3)
Expand Down Expand Up @@ -2468,18 +2507,8 @@ def interp_like(
Coordinates:
* x (x) int64 32B 10 20 30 40
* y (y) int64 24B 70 80 90
Notes
-----
scipy is required.
If the dataarray has object-type coordinates, reindex is used for these
coordinates instead of the interpolation.
See Also
--------
DataArray.interp
DataArray.reindex_like
"""

if self.dtype.kind not in "uifc":
raise TypeError(
f"interp only works for a numeric type array. Given {self.dtype}."
Expand Down
Loading

0 comments on commit a26c816

Please sign in to comment.