From 047ee7503af962cb9ce3440952ac507446ead3ed Mon Sep 17 00:00:00 2001 From: hollymandel Date: Tue, 22 Oct 2024 11:05:52 -0700 Subject: [PATCH] remove reduce arg --- xarray/core/dataarray.py | 45 ++++++++++++--------------------------- xarray/core/dataset.py | 46 ++++++++++++---------------------------- xarray/core/missing.py | 12 ++++------- 3 files changed, 32 insertions(+), 71 deletions(-) diff --git a/xarray/core/dataarray.py b/xarray/core/dataarray.py index 8d347f7d33d..44a331b47f2 100644 --- a/xarray/core/dataarray.py +++ b/xarray/core/dataarray.py @@ -2217,7 +2217,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, **coords_kwargs: Any, ) -> Self: @@ -2225,12 +2224,8 @@ def interp( 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`. @@ -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 @@ -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 -------- @@ -2377,7 +2369,6 @@ def interp( method=method, kwargs=kwargs, assume_sorted=assume_sorted, - reduce=reduce, **coords_kwargs, ) return self._from_temp_dataset(ds) @@ -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`. @@ -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. @@ -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 -------- @@ -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) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index a9bb2d26242..3af51449939 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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, @@ -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`. @@ -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. @@ -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 @@ -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. @@ -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`. @@ -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 @@ -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 -------- diff --git a/xarray/core/missing.py b/xarray/core/missing.py index 83fcee47a3b..593720352f6 100644 --- a/xarray/core/missing.py +++ b/xarray/core/missing.py @@ -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": @@ -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 @@ -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 @@ -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]