Skip to content

Commit 08040d3

Browse files
authored
Merge pull request #147 from ToFuProject/Issue126_scipy1160
[#126] Attempt at fixing evaluate_spline for `scipy >= 1.16.0`
2 parents 6e203c4 + cb6c723 commit 08040d3

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

bsplines2d/_class02_bsplines_rect.py

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# Common
1010
import numpy as np
11+
import scipy as scp
1112
import scipy.interpolate as scpinterp
1213

1314

@@ -16,41 +17,69 @@
1617
from . import _class02_bsplines_operators_rect
1718

1819

19-
if hasattr(scpinterp._bspl, 'evaluate_spline'):
20-
evaluate_spline = scpinterp._bspl.evaluate_spline
20+
# Should work for scipy >= 1.15.0
21+
# see https://github.com/ToFuProject/bsplines2d/pull/147
22+
if (
23+
hasattr(scpinterp, '_bsplines')
24+
and hasattr(scpinterp._bsplines, '_dierckx')
25+
and hasattr(scpinterp._bsplines._dierckx, 'evaluate_spline')
26+
):
2127

22-
else:
23-
msg = (
24-
"\n\n"
25-
"bsplines2d using a new version of scipy"
26-
" with no scpinterp._bspl.evaluate_spline()\n"
27-
"Instead using scpinterp._bspl.evaluate_ndspline()\n"
28-
"Prototypal and not thoroughly tested!\n"
29-
)
30-
warnings.warn(msg)
31-
32-
def evaluate_spline(t, c, k, xp, nu, extrapolate, out):
33-
ndim = 1
34-
c1 = c.reshape(c.shape[:ndim] + (-1,))
35-
num_c_tr = c1.shape[-1]
36-
strides_c1 = [stride // c.dtype.itemsize for stride in c.strides]
37-
indices_k1d = np.unravel_index(
38-
np.arange((k+1)**ndim),
39-
(k+1,)*ndim,
40-
)[0][:, None]
41-
return scpinterp._bspl.evaluate_ndbspline(
42-
xp[:, None],
43-
t[None, :],
44-
np.array([t.size], dtype=np.int32),
45-
np.array([k], dtype=np.int32),
46-
np.array([nu], dtype=np.int32),
47-
extrapolate,
48-
c.ravel(),
49-
num_c_tr,
50-
np.array(strides_c1, dtype=np.intp),
51-
indices_k1d,
52-
out,
28+
if scp.__version__.startswith('1.15'):
29+
evaluate_spline = scpinterp._bsplines._dierckx.evaluate_spline
30+
else:
31+
def evaluate_spline(t, c, k, xp, nu, extrapolate, out):
32+
out[...] = scpinterp._bsplines._dierckx.evaluate_spline(
33+
t, # 1d contiguous array of floats
34+
c, # 2d contiguous array of floats
35+
k, # int
36+
xp, # 1d contiguous array of floats
37+
nu, # int
38+
extrapolate, # bool
39+
)
40+
return
41+
42+
elif hasattr(scpinterp, '_bspl'):
43+
44+
if hasattr(scpinterp._bspl, 'evaluate_spline'):
45+
evaluate_spline = scpinterp._bspl.evaluate_spline
46+
47+
else:
48+
msg = (
49+
"\n\n"
50+
"bsplines2d using a new version of scipy"
51+
" with no scpinterp._bspl.evaluate_spline()\n"
52+
"Instead using scpinterp._bspl.evaluate_ndspline()\n"
53+
"Prototypal and not thoroughly tested!\n"
5354
)
55+
warnings.warn(msg)
56+
57+
def evaluate_spline(t, c, k, xp, nu, extrapolate, out):
58+
ndim = 1
59+
c1 = c.reshape(c.shape[:ndim] + (-1,))
60+
num_c_tr = c1.shape[-1]
61+
strides_c1 = [stride // c.dtype.itemsize for stride in c.strides]
62+
indices_k1d = np.unravel_index(
63+
np.arange((k+1)**ndim),
64+
(k+1,)*ndim,
65+
)[0][:, None]
66+
return scpinterp._bspl.evaluate_ndbspline(
67+
xp[:, None],
68+
t[None, :],
69+
np.array([t.size], dtype=np.int32),
70+
np.array([k], dtype=np.int32),
71+
np.array([nu], dtype=np.int32),
72+
extrapolate,
73+
c.ravel(),
74+
num_c_tr,
75+
np.array(strides_c1, dtype=np.intp),
76+
indices_k1d,
77+
out,
78+
)
79+
80+
else:
81+
msg = f"scipy {scp.__version__} has no evaluate_spline"
82+
raise Exception(msg)
5483

5584

5685
# ################################################################

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ keywords = [
4242
# scipy 1.15.3 and 1.16.0 have no wheel for python 3.14
4343
requires-python = ">=3.10,<3.14"
4444
dependencies = [
45-
"scipy<1.16.0", # until https://github.com/ToFuProject/bsplines2d/issues/126 solved
45+
"scipy", # see https://github.com/ToFuProject/bsplines2d/pull/147
4646
"contourpy",
4747
'datastock>=0.0.56',
4848
]

0 commit comments

Comments
 (0)