pvlib-python icon indicating copy to clipboard operation
pvlib-python copied to clipboard

`scipy.interpolate.interp1d` is discouraged

Open kandersolar opened this issue 10 months ago • 6 comments

SciPy now discourages the use of interp1d. From https://docs.scipy.org/doc/scipy-1.15.2/reference/generated/scipy.interpolate.interp1d.html :

This class is considered legacy and will no longer receive updates. While we currently have no plans to remove it, we recommend that new code uses more modern alternatives instead. For a guide to the intended replacements for interp1d see 1-D interpolation.

We use interp1d in a few places:

$ git grep -n interp1d
docs/examples/shading/plot_partial_module_shading_simple.py:41:from scipy.interpolate import interp1d
docs/examples/shading/plot_partial_module_shading_simple.py:181:    """convenience wrapper around scipy.interpolate.interp1d"""
docs/examples/shading/plot_partial_module_shading_simple.py:182:    f_interp = interp1d(np.flipud(df['i']), np.flipud(df['v']), kind='linear',
pvlib/iam.py:443:        See scipy.interpolate.interp1d for more options.
pvlib/iam.py:473:    from scipy.interpolate import interp1d
pvlib/iam.py:486:    interpolator = interp1d(theta_ref, iam_ref, kind=method,
pvlib/spectrum/response.py:9:from scipy.interpolate import interp1d
pvlib/spectrum/response.py:70:    interpolator = interp1d(SR_DATA[0], SR_DATA[1],

I suppose there is no rush to change, but updating these to use whatever scipy recommends at that link might be a good first PR for some pvlib user interested in becoming a contributor.

kandersolar avatar Feb 20 '25 23:02 kandersolar

I'll be glad to work on this!

prady0t avatar Mar 01 '25 20:03 prady0t

I am interested in contributing and resolve this issue. May I please be assigned to it

MihirGore23 avatar Mar 02 '25 06:03 MihirGore23

I updated the interpolation code to use make_interp_spline instead of interp1d for better accuracy and stability. I also made sure the extrapolation and boundary conditions work properly across all input ranges. Kindly review in Pull Request #2401

MihirGore23 avatar Mar 02 '25 06:03 MihirGore23

Hi @cwhanse and maintainers,

I've reviewed the existing PR #2401 and would like to propose an alternative approach to replace interp1d that maintains backward compatibility while using SciPy's recommended modern alternatives:

  1. For linear interpolation: Use numpy.interp which is simpler and more efficient
  2. For cubic interpolation: Use scipy.interpolate.CubicSpline with proper boundary handling
  3. Maintain existing parameters (bounds_error, fill_value) for backward compatibility

This approach:

  • Handles all boundary conditions identically to the original implementation
  • Maintains the same function signatures
  • Reduces dependencies (uses NumPy where possible)
  • Follows SciPy's migration recommendations
  • Passes all existing test cases

I've prepared a complete implementation that addresses all three files:

  1. docs/examples/shading/plot_partial_module_shading_simple.py
  2. pvlib/iam.py
  3. pvlib/spectrum/response.py

If #2401 isn't merge-ready, I'd be happy to submit this as an alternative solution. Let me know how you'd like to proceed!

kadheer avatar Jun 17 '25 19:06 kadheer

That sounds good, @kadheer. I'll close #2401

cwhanse avatar Jun 17 '25 20:06 cwhanse

:

👋 Hi, I’d like to work on this issue.

I see that scipy.interpolate.interp1d is discouraged, and it’s currently used in a few places in the codebase (iam.py, spectrum/response.py, and the shading example script). My plan is to replace these calls with a modern alternative such as RegularGridInterpolator (or another method you recommend).

Could you please confirm if RegularGridInterpolator is the preferred replacement, or if there’s another approach you’d like me to follow?

Thanks! 🙏

Michaelobot1 avatar Sep 27 '25 15:09 Michaelobot1