|
8 | 8 |
|
9 | 9 | # Common |
10 | 10 | import numpy as np |
| 11 | +import scipy as scp |
11 | 12 | import scipy.interpolate as scpinterp |
12 | 13 |
|
13 | 14 |
|
|
16 | 17 | from . import _class02_bsplines_operators_rect |
17 | 18 |
|
18 | 19 |
|
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 | +): |
21 | 27 |
|
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" |
53 | 54 | ) |
| 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) |
54 | 83 |
|
55 | 84 |
|
56 | 85 | # ################################################################ |
|
0 commit comments