Skip to content

Commit

Permalink
remove reduce arg
Browse files Browse the repository at this point in the history
  • Loading branch information
hollymandel committed Oct 22, 2024
1 parent 60f5afb commit 047ee75
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 71 deletions.
45 changes: 14 additions & 31 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2217,20 +2217,15 @@ def interp(
coords: Mapping[Any, Any] | None = None,
method: InterpOptions = "linear",
assume_sorted: bool = False,
reduce: bool = True,
kwargs: Mapping[str, Any] | None = None,
**coords_kwargs: Any,
) -> Self:
"""
Interpolate a DataArray onto new coordinates.
Performs univariate or multivariate interpolation of a Dataset onto new coordinates,
utilizing either NumPy or SciPy interpolation routines.
When interpolating along multiple dimensions, the process attempts to decompose the
interpolation into independent interpolations along one dimension at a time, unless
`reduce=False` is passed. The specific interpolation method and dimensionality
determine which interpolant is used:
utilizing either NumPy or SciPy interpolation routines. The specific interpolation
method and dimensionality determine which interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:class:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
Expand Down Expand Up @@ -2269,10 +2264,6 @@ def interp(
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
values.
reduce : bool, default: True
If True, the interpolation is decomposed into independent interpolations along one dimension at a time,
where the interpolation coordinates are independent. Setting this to be True alters the behavior of certain
multi-dimensional interpolants compared to the default SciPy output.
kwargs : dict-like or None, default: None
Additional keyword arguments passed to scipy's interpolator. Valid
options and their behavior depend whether ``interp1d`` or
Expand All @@ -2289,8 +2280,9 @@ def interp(
Notes
-----
- SciPy is required for certain interpolation methods.
- Allowing `reduce=True` (the default) may alter the behavior of interpolation along multiple dimensions
compared to the default behavior in SciPy.
- 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.
See Also
--------
Expand Down Expand Up @@ -2377,7 +2369,6 @@ def interp(
method=method,
kwargs=kwargs,
assume_sorted=assume_sorted,
reduce=reduce,
**coords_kwargs,
)
return self._from_temp_dataset(ds)
Expand All @@ -2387,16 +2378,13 @@ def interp_like(
other: T_Xarray,
method: InterpOptions = "linear",
assume_sorted: bool = False,
reduce: bool = True,
kwargs: Mapping[str, Any] | None = None,
) -> Self:
"""Interpolate this object onto the coordinates of another object,
filling out of range values with NaN.
When interpolating along multiple dimensions, the process attempts to decompose the
interpolation into independent interpolations along one dimension at a time, unless
`reduce=False` is passed. The specific interpolation method and dimensionality
determine which interpolant is used:
The specific interpolation method and dimensionality determine which
interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:class:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
Expand Down Expand Up @@ -2432,10 +2420,6 @@ def interp_like(
in any order and they are sorted first. If True, interpolated
coordinates are assumed to be an array of monotonically increasing
values.
reduce : bool, default: True
If True, the interpolation is decomposed into independent interpolations along one dimension at a time,
where the interpolation coordinates are independent. Setting this to be True alters the behavior of certain
multi-dimensional interpolants compared to the default SciPy output.
kwargs : dict, optional
Additional keyword arguments passed to the interpolant.
Expand All @@ -2447,9 +2431,12 @@ def interp_like(
Notes
-----
scipy is required.
If the dataarray has object-type coordinates, reindex is used for these
coordinates instead of the interpolation.
- 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.
See Also
--------
Expand Down Expand Up @@ -2518,11 +2505,7 @@ def interp_like(
f"interp only works for a numeric type array. Given {self.dtype}."
)
ds = self._to_temp_dataset().interp_like(
other,
method=method,
kwargs=kwargs,
assume_sorted=assume_sorted,
reduce=reduce,
other, method=method, kwargs=kwargs, assume_sorted=assume_sorted
)
return self._from_temp_dataset(ds)

Expand Down
46 changes: 14 additions & 32 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3884,7 +3884,6 @@ def interp(
coords: Mapping[Any, Any] | None = None,
method: InterpOptions = "linear",
assume_sorted: bool = False,
reduce: bool = True,
kwargs: Mapping[str, Any] | None = None,
method_non_numeric: str = "nearest",
**coords_kwargs: Any,
Expand All @@ -3893,12 +3892,8 @@ def interp(
Interpolate a Dataset onto new coordinates.
Performs univariate or multivariate interpolation of a Dataset onto new coordinates,
utilizing either NumPy or SciPy interpolation routines.
When interpolating along multiple dimensions, the process attempts to decompose the
interpolation into independent interpolations along one dimension at a time, unless
`reduce=False` is passed. The specific interpolation method and dimensionality
determine which interpolant is used:
utilizing either NumPy or SciPy interpolation routines. The specific interpolation
method and dimensionality determine which interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:class:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
Expand Down Expand Up @@ -3938,10 +3933,6 @@ def interp(
in any order and they are sorted first. If True, interpolated
coordinates are assumed to be an array of monotonically increasing
values.
reduce : bool, default: True
If True, the interpolation is decomposed into independent interpolations of minimal dimensionality such that
the interpolation coordinates are independent. Setting this to be True alters the behavior of certain
multi-dimensional interpolants compared to the default SciPy output.
kwargs : dict, optional
Additional keyword arguments passed to the interpolator. Valid
options and their behavior depend which interpolant is used.
Expand All @@ -3961,10 +3952,9 @@ def interp(
Notes
-----
- SciPy is required for certain interpolation methods.
- Allowing `reduce=True` (the default) may alter the behavior of interpolation along multiple dimensions
compared to the default behavior in SciPy.
See Also
- 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.
--------
scipy.interpolate.interp1d
scipy.interpolate.interpn
Expand Down Expand Up @@ -4125,9 +4115,7 @@ def _validate_interp_indexer(x, new_x):
if dtype_kind in "uifc":
# For normal number types do the interpolation:
var_indexers = {k: v for k, v in use_indexers.items() if k in var.dims}
variables[name] = missing.interp(
var, var_indexers, method, reduce=reduce, **kwargs
)
variables[name] = missing.interp(var, var_indexers, method, **kwargs)
elif dtype_kind in "ObU" and (use_indexers.keys() & var.dims):
# For types that we do not understand do stepwise
# interpolation to avoid modifying the elements.
Expand Down Expand Up @@ -4188,19 +4176,14 @@ def interp_like(
other: T_Xarray,
method: InterpOptions = "linear",
assume_sorted: bool = False,
reduce: bool = True,
kwargs: Mapping[str, Any] | None = None,
method_non_numeric: str = "nearest",
) -> Self:
"""Interpolate this object onto the coordinates of another object.
Performs univariate or multivariate interpolation of a Dataset onto new coordinates,
utilizing either NumPy or SciPy interpolation routines.
When interpolating along multiple dimensions, the process attempts to decompose the
interpolation into independent interpolations along one dimension at a time, unless
`reduce=False` is passed. The specific interpolation method and dimensionality
determine which interpolant is used:
utilizing either NumPy or SciPy interpolation routines. The specific interpolation
method and dimensionality determine which interpolant is used:
1. **Interpolation along one dimension of 1D data (`method='linear'`)**
- Uses :py:class:`numpy.interp`, unless `fill_value='extrapolate'` is provided via `kwargs`.
Expand Down Expand Up @@ -4239,10 +4222,6 @@ def interp_like(
in any order and they are sorted first. If True, interpolated
coordinates are assumed to be an array of monotonically increasing
values.
reduce : bool, default: True
If True, the interpolation is decomposed into independent 1-dimensional interpolations such that
the interpolation coordinates are independent. Setting this to be True alters the behavior of certain
multi-dimensional interpolants compared to the default SciPy output.
kwargs : dict, optional
Additional keyword arguments passed to the interpolator. Valid
options and their behavior depend which interpolant is use
Expand All @@ -4258,9 +4237,12 @@ def interp_like(
Notes
-----
scipy is required.
If the dataset has object-type coordinates, reindex is used for these
coordinates instead of the interpolation.
- scipy is required.
- If the dataset 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.
See Also
--------
Expand Down
12 changes: 4 additions & 8 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ def _get_interpolator(
interp_class = _import_interpolant("KroghInterpolator", method)
elif method == "pchip":
kwargs.update(axis=-1)
# pchip default behavior is to extrapolate
kwargs.setdefault("extrapolate", False)
interp_class = _import_interpolant("PchipInterpolator", method)
elif method == "spline":
Expand Down Expand Up @@ -601,7 +602,7 @@ def _floatize_x(x, new_x):
return x, new_x


def interp(var, indexes_coords, method: InterpOptions, reduce: bool = True, **kwargs):
def interp(var, indexes_coords, method: InterpOptions, **kwargs):
"""Make an interpolation of Variable
Parameters
Expand All @@ -615,10 +616,6 @@ def interp(var, indexes_coords, method: InterpOptions, reduce: bool = True, **kw
One of {'linear', 'nearest', 'zero', 'slinear', 'quadratic',
'cubic'}. For multidimensional interpolation, only
{'linear', 'nearest'} can be used.
reduce:
Decompose the interpolation along independent interpolation dimensions when
possible. This will likely improve performance over true multi-dimensional
interpolation but will alter the result for certain interpolators.
**kwargs
keyword arguments to be passed to scipy.interpolate
Expand All @@ -636,9 +633,8 @@ def interp(var, indexes_coords, method: InterpOptions, reduce: bool = True, **kw

result = var

if reduce:
# decompose the interpolation into a succession of independent interpolation. This may
# affect the mathematical behavior of certain nd interpolants.
if method in ["linear", "nearest"]:
# decompose the interpolation into a succession of independent interpolation.
indexes_coords = decompose_interp(indexes_coords)
else:
indexes_coords = [indexes_coords]
Expand Down

0 comments on commit 047ee75

Please sign in to comment.