Skip to content

Refactor Interpolation to Use make_interp_spline with Extrapolation Handling #2401

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions docs/examples/shading/plot_partial_module_shading_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from pvlib import pvsystem, singlediode
import pandas as pd
import numpy as np
from scipy.interpolate import interp1d
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt

from scipy.constants import e as qe, k as kB
Expand Down Expand Up @@ -178,10 +178,13 @@


def interpolate(df, i):
"""convenience wrapper around scipy.interpolate.interp1d"""
f_interp = interp1d(np.flipud(df['i']), np.flipud(df['v']), kind='linear',
fill_value='extrapolate')
return f_interp(i)
x = np.flipud(df['i'])
y = np.flipud(df['v'])

Check failure on line 183 in docs/examples/shading/plot_partial_module_shading_simple.py

View workflow job for this annotation

GitHub Actions / flake8-linter

W293 blank line contains whitespace
# Create a spline interpolation with linear (k=1) and extrapolation (default behavior)

Check failure on line 184 in docs/examples/shading/plot_partial_module_shading_simple.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (90 > 79 characters)
spline = make_interp_spline(x, y, k=1, bc_type='clamped') # Extrapolation is handled by default

Check failure on line 185 in docs/examples/shading/plot_partial_module_shading_simple.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (100 > 79 characters)

Check failure on line 186 in docs/examples/shading/plot_partial_module_shading_simple.py

View workflow job for this annotation

GitHub Actions / flake8-linter

W293 blank line contains whitespace
return spline(i)


def combine_series(dfs):
Expand Down
9 changes: 6 additions & 3 deletions pvlib/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@
'''
# Contributed by Anton Driesse (@adriesse), PV Performance Labs. July, 2019

from scipy.interpolate import interp1d
from scipy.interpolate import make_interp_spline
import numpy as np

# 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}
Expand All @@ -483,8 +484,10 @@
raise ValueError("Negative value(s) found in 'iam_ref'. "
"This is not physically possible.")

interpolator = interp1d(theta_ref, iam_ref, kind=method,
fill_value='extrapolate')
theta_ref = np.asarray(theta_ref)
iam_ref = np.asarray(iam_ref)

interpolator = make_interp_spline(theta_ref, iam_ref, k=method, bc_type='clamped')

Check failure on line 490 in pvlib/iam.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E501 line too long (86 > 79 characters)
aoi_input = aoi

aoi = np.asanyarray(aoi)
Expand Down
13 changes: 6 additions & 7 deletions pvlib/spectrum/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np
import pandas as pd
import scipy.constants
from scipy.interpolate import interp1d
from scipy.interpolate import make_interp_spline


_PLANCK_BY_LIGHT_SPEED_OVER_ELEMENTAL_CHARGE_BY_BILLION = (
Expand Down Expand Up @@ -67,14 +67,13 @@
resolution = 5.0
wavelength = np.arange(280, 1200 + resolution, resolution)

interpolator = interp1d(SR_DATA[0], SR_DATA[1],
kind='cubic',
bounds_error=False,
fill_value=0.0,
copy=False,
assume_sorted=True)
x_data = np.sort(SR_DATA[0])
y_data = SR_DATA[1]

interpolator = make_interp_spline(x_data, y_data, k=3, bc_type='clamped')


sr = pd.Series(data=interpolator(wavelength), index=wavelength)

Check failure on line 76 in pvlib/spectrum/response.py

View workflow job for this annotation

GitHub Actions / flake8-linter

E303 too many blank lines (2)

sr.index.name = 'wavelength'
sr.name = 'spectral_response'
Expand Down
Loading