Skip to content

Commit f041355

Browse files
Use explicit finite difference instead of scipy.misc.derivative in pvsyst_temperature_coeff (#1674)
* use explicit finite difference instead of scipy.misc.derivative * whatsnew * private function in tools.py Co-Authored-By: Mark Mikofski <[email protected]> --------- Co-authored-by: Mark Mikofski <[email protected]>
1 parent f4d7c6e commit f041355

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

docs/sphinx/source/whatsnew/v0.9.5.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Bug fixes
3232
* When using ``utc_time_range`` with :py:func:`pvlib.iotools.read_ecmwf_macc`,
3333
the time index subset is now selected with ``nearest`` instead of ``before``
3434
and ``after`` for consistency with ``cftime>=1.6.0``. (:issue:`1609`, :pull:`1656`)
35+
* :py:func:`~pvlib.ivtools.sdm.pvsyst_temperature_coeff` no longer raises
36+
a scipy deprecation warning (and is slightly more accurate) (:issue:`1644`, :pull:`1674`)
3537

3638

3739
Testing

pvlib/ivtools/sdm.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
from scipy import constants
1212
from scipy import optimize
1313
from scipy.special import lambertw
14-
from scipy.misc import derivative
1514

1615
from pvlib.pvsystem import calcparams_pvsyst, singlediode, v_from_i
1716
from pvlib.singlediode import bishop88_mpp
1817

1918
from pvlib.ivtools.utils import rectify_iv_curve, _numdiff
2019
from pvlib.ivtools.sde import _fit_sandia_cocontent
2120

21+
from pvlib.tools import _first_order_centered_difference
22+
2223

2324
CONSTANTS = {'E0': 1000.0, 'T0': 25.0, 'k': constants.k, 'q': constants.e}
2425

@@ -1344,5 +1345,6 @@ def maxp(temp_cell, irrad_ref, alpha_sc, gamma_ref, mu_gamma, I_L_ref,
13441345
I_o_ref, R_sh_ref, R_sh_0, R_s, cells_in_series, R_sh_exp, EgRef,
13451346
temp_ref)
13461347
pmp = maxp(temp_ref, *args)
1347-
gamma_pdc = derivative(maxp, temp_ref, args=args)
1348+
gamma_pdc = _first_order_centered_difference(maxp, x0=temp_ref, args=args)
1349+
13481350
return gamma_pdc / pmp

pvlib/tools.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,14 @@ def _degrees_to_index(degrees, coordinate):
458458
index = int(np.around(index))
459459

460460
return index
461+
462+
463+
EPS = np.finfo('float64').eps # machine precision NumPy-1.20
464+
DX = EPS**(1/3) # optimal differential element
465+
466+
467+
def _first_order_centered_difference(f, x0, dx=DX, args=()):
468+
# simple replacement for scipy.misc.derivative, which is scheduled for
469+
# removal in scipy 1.12.0
470+
df = f(x0+dx, *args) - f(x0-dx, *args)
471+
return df / 2 / dx

0 commit comments

Comments
 (0)