Replace interp1d #2394#2741
Conversation
|
Reason for importing |
|
no particular reason. Just that I had seen I have replaced |
| fill_value='extrapolate') | ||
| aoi_input = aoi | ||
| theta_ref = np.asarray(theta_ref) | ||
| iam_ref = np.asarray(iam_ref) |
There was a problem hiding this comment.
Why is np.asarray needed here?
There was a problem hiding this comment.
Do you mean to use np.asanyarray to keep consistency with np.asanyarray(aoi) below? Or to remove entirely?
np.asarray is to convert to numpy array but I can remove if you prefer.
There was a problem hiding this comment.
I think the issue is that the docstrings states numeric for input parameters. pvlib means numeric as one of float, array or Series. Testing is all in one function test_iam_interp which inputs either list, float or array (but not Series).
My opinion: rewrite the test to remove inputting lists, which obviates np.asarray inside iam.interp. And add a test that inputs Series.
There was a problem hiding this comment.
Hi both, thanks for your comments. Not sure if I got this right, but I have rewritten the test to remove inputting lists. (And removed np.asarray in iam.interp )
I have also added a check for list inputs at the beginning of iam.interp
Let me know if this is OK.
--Looking at it again, it seems that keeping np.asarray might be "cleaner" than the latest version.
| return spline(x) | ||
|
|
||
| else: | ||
| raise ValueError(f"Invalid interpolation method '{method}'.") |
There was a problem hiding this comment.
This is, technically, a breaking change, since the interp1d way also supported 'nearest', 'nearest-up', 'zero', 'slinear', 'previous', and 'next'. I doubt these got much use, if any. Any thoughts on how to handle that?
There was a problem hiding this comment.
I'm not sure I know how to update to support all of those.
Is it OK to update the error message?
There was a problem hiding this comment.
Since interp1d is legacy but not intended for removal, it is still available and we could fall back:
elif method in {'nearest', 'nearest-up', 'zero', 'slinear', 'previous', 'next'}:
interpolator = interp1d(theta_ref, iam_ref, kind=method,
fill_value='extrapolate')
There was a problem hiding this comment.
I think that's reasonable, as long as we deprecate these weird methods too. If someone really wants these special interpolations, they can do it themselves and provide a pre-calculated input.
There was a problem hiding this comment.
None of those options really make any sense here. I would vote for dropping them immediately.
| spline = make_interp_spline(theta_ref, iam_ref, k=3) | ||
|
|
||
| def interpolator(x): | ||
| return spline(x) |
There was a problem hiding this comment.
This can surely be simplified as @cwhanse mentioned in #2741 (comment)
| Specifies the interpolation method. | ||
| Useful options are: 'linear', 'quadratic', 'cubic'. | ||
| See scipy.interpolate.interp1d for more options. | ||
| See scipy.interpolate for more options. |
There was a problem hiding this comment.
This line may need to be edited, depending on https://github.com/pvlib/pvlib-python/pull/2741/changes#r3137773820
| interpolator = make_interp_spline(theta_ref, iam_ref, k=3) | ||
|
|
||
| else: | ||
| raise ValueError(f"Invalid interpolation method '{method}'.") |
There was a problem hiding this comment.
| raise ValueError(f"Invalid interpolation method '{method}'.") | |
| raise ValueError(f"Interpolation method '{method}' is not supported in pvlib-python.") |
|
|
||
| from scipy.interpolate import interp1d | ||
|
|
||
| if isinstance(theta_ref, list): |
There was a problem hiding this comment.
We normally don't check the type of input. If a user inputs a non-allowed type e.g. a list, make_interp_spline will emit an error. That's OK.
| return spline(x) | ||
|
|
||
| else: | ||
| raise ValueError(f"Invalid interpolation method '{method}'.") |
There was a problem hiding this comment.
Since interp1d is legacy but not intended for removal, it is still available and we could fall back:
elif method in {'nearest', 'nearest-up', 'zero', 'slinear', 'previous', 'next'}:
interpolator = interp1d(theta_ref, iam_ref, kind=method,
fill_value='extrapolate')
| if isinstance(iam_ref, list): | ||
| raise TypeError("iam_ref cannot be a list") | ||
| # Scipy doesn't give the clearest feedback, so check number of points here. | ||
| MIN_REF_VALS = {'linear': 2, 'quadratic': 3, 'cubic': 4, 1: 2, 2: 3, 3: 4} |
There was a problem hiding this comment.
You could add a dictionary with names and k values, or something like that, to use below.
| return spline(x) | ||
|
|
||
| else: | ||
| raise ValueError(f"Invalid interpolation method '{method}'.") |
There was a problem hiding this comment.
None of those options really make any sense here. I would vote for dropping them immediately.
| fill_value='extrapolate') | ||
| """convenience wrapper around scipy.interpolate""" | ||
| f_interp = make_interp_spline(np.flipud(df['i']), np.flipud(df['v']), k=1) | ||
|
|
There was a problem hiding this comment.
This is one where performance is likely important. Maybe check if np.interp is faster?
scipy.interpolate.interp1dis discouraged #2394docs/sphinx/source/referencefor API changes.docs/sphinx/source/whatsnewfor all changes. Includes link to the GitHub Issue with:issue:`num`or this Pull Request with:pull:`num`. Includes contributor name and/or GitHub username (link with:ghuser:`user`).remote-data) and Milestone are assigned to the Pull Request and linked Issue.The existing tests pass. I have added 2 in test_iam.py under test_iam_interp.