-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support vectorized interpolation with more scipy interpolators #9526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,6 +138,7 @@ def __init__( | |
copy=False, | ||
bounds_error=False, | ||
order=None, | ||
axis=-1, | ||
**kwargs, | ||
): | ||
from scipy.interpolate import interp1d | ||
|
@@ -173,6 +174,7 @@ def __init__( | |
bounds_error=bounds_error, | ||
assume_sorted=assume_sorted, | ||
copy=copy, | ||
axis=axis, | ||
**self.cons_kwargs, | ||
) | ||
|
||
|
@@ -479,7 +481,8 @@ def _get_interpolator( | |
interp1d_methods = get_args(Interp1dOptions) | ||
valid_methods = tuple(vv for v in get_args(InterpOptions) for vv in get_args(v)) | ||
|
||
# prioritize scipy.interpolate | ||
# prefer numpy.interp for 1d linear interpolation. This function cannot | ||
# take higher dimensional data but scipy.interp1d can. | ||
if ( | ||
method == "linear" | ||
and not kwargs.get("fill_value", None) == "extrapolate" | ||
|
@@ -492,21 +495,33 @@ def _get_interpolator( | |
if method in interp1d_methods: | ||
kwargs.update(method=method) | ||
interp_class = ScipyInterpolator | ||
elif vectorizeable_only: | ||
raise ValueError( | ||
f"{method} is not a vectorizeable interpolator. " | ||
f"Available methods are {interp1d_methods}" | ||
) | ||
elif method == "barycentric": | ||
kwargs.update(axis=-1) | ||
interp_class = _import_interpolant("BarycentricInterpolator", method) | ||
elif method in ["krogh", "krog"]: | ||
kwargs.update(axis=-1) | ||
interp_class = _import_interpolant("KroghInterpolator", method) | ||
elif method == "pchip": | ||
kwargs.update(axis=-1) | ||
interp_class = _import_interpolant("PchipInterpolator", method) | ||
elif method == "spline": | ||
utils.emit_user_level_warning( | ||
"The 1d SplineInterpolator class is performing an incorrect calculation and " | ||
"is being deprecated. Please use `method=polynomial` for 1D Spline Interpolation.", | ||
PendingDeprecationWarning, | ||
) | ||
if vectorizeable_only: | ||
raise ValueError(f"{method} is not a vectorizeable interpolator. ") | ||
kwargs.update(method=method) | ||
interp_class = SplineInterpolator | ||
elif method == "akima": | ||
kwargs.update(axis=-1) | ||
interp_class = _import_interpolant("Akima1DInterpolator", method) | ||
elif method == "makima": | ||
kwargs.update(method="makima", axis=-1) | ||
interp_class = _import_interpolant("Akima1DInterpolator", method) | ||
elif method == "makima": | ||
kwargs.update(method="makima", axis=-1) | ||
interp_class = _import_interpolant("Akima1DInterpolator", method) | ||
else: | ||
raise ValueError(f"{method} is not a valid scipy interpolator") | ||
|
@@ -525,6 +540,7 @@ def _get_interpolator_nd(method, **kwargs): | |
|
||
if method in valid_methods: | ||
kwargs.update(method=method) | ||
kwargs.setdefault("bounds_error", False) | ||
interp_class = _import_interpolant("interpn", method) | ||
else: | ||
raise ValueError( | ||
|
@@ -614,9 +630,6 @@ def interp(var, indexes_coords, method: InterpOptions, **kwargs): | |
if not indexes_coords: | ||
return var.copy() | ||
|
||
# default behavior | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice cleanup! |
||
kwargs["bounds_error"] = kwargs.get("bounds_error", False) | ||
|
||
result = var | ||
# decompose the interpolation into a succession of independent interpolation | ||
for indep_indexes_coords in decompose_interp(indexes_coords): | ||
|
@@ -663,8 +676,8 @@ def interp_func(var, x, new_x, method: InterpOptions, kwargs): | |
new_x : a list of 1d array | ||
New coordinates. Should not contain NaN. | ||
method : string | ||
{'linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic'} for | ||
1-dimensional interpolation. | ||
{'linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'pchip', 'akima', | ||
'makima', 'barycentric', 'krogh'} for 1-dimensional interpolation. | ||
{'linear', 'nearest'} for multidimensional interpolation | ||
**kwargs | ||
Optional keyword arguments to be passed to scipy.interpolator | ||
|
@@ -756,7 +769,7 @@ def interp_func(var, x, new_x, method: InterpOptions, kwargs): | |
def _interp1d(var, x, new_x, func, kwargs): | ||
# x, new_x are tuples of size 1. | ||
x, new_x = x[0], new_x[0] | ||
rslt = func(x, var, assume_sorted=True, **kwargs)(np.ravel(new_x)) | ||
rslt = func(x, var, **kwargs)(np.ravel(new_x)) | ||
if new_x.ndim > 1: | ||
return reshape(rslt, (var.shape[:-1] + new_x.shape)) | ||
if new_x.ndim == 0: | ||
|
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.