Skip to content

Commit f25a4da

Browse files
committed
TST: interpolate: adapt tests to xp_assert_* infra
1 parent 5d0646e commit f25a4da

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

scipy/interpolate/tests/test_bsplines.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import math
55

66
import numpy as np
7+
from numpy.testing import suppress_warnings
78
from scipy._lib._array_api import xp_assert_equal, xp_assert_close
89
from pytest import raises as assert_raises
910
import pytest
@@ -1969,9 +1970,9 @@ def test_vs_full(self):
19691970

19701971
# signs may differ
19711972
xp_assert_close(np.minimum(R.todense() + r,
1972-
R.todense() - r), 0, atol=1e-15)
1973+
R.todense() - r), np.zeros_like(r), atol=1e-15)
19731974
xp_assert_close(np.minimum(abs(qTy - y_[:, 0]),
1974-
abs(qTy + y_[:, 0])), 0, atol=2e-13)
1975+
abs(qTy + y_[:, 0])), np.zeros_like(qTy), atol=2e-13)
19751976

19761977
# sign changes are consistent between Q and R:
19771978
c_full = sl.solve(r, qTy)
@@ -1999,7 +2000,7 @@ def test_py_vs_compiled(self):
19992000
_bspl._qr_reduce(A, offset, nc , y_) # in-place
20002001

20012002
xp_assert_close(RR.a, R.a, atol=1e-15)
2002-
assert_equal(RR.offset, R.offset)
2003+
xp_assert_equal(RR.offset, R.offset)
20032004
assert RR.nc == R.nc
20042005
xp_assert_close(yy, y_, atol=1e-15)
20052006

@@ -2018,7 +2019,7 @@ def test_data_matrix(self):
20182019
offset_ = a_w.indices[::(k+1)]
20192020

20202021
xp_assert_close(A, A_, atol=1e-15)
2021-
assert_equal(offset, offset_)
2022+
xp_assert_equal(offset, offset_, check_dtype=False)
20222023
assert nc == t.shape[0] - k - 1
20232024

20242025
def test_fpback(self):
@@ -3054,7 +3055,7 @@ def _split(x, t, k, residuals):
30543055

30553056
fparts[-1] += residuals[-1] # add the contribution of the last knot
30563057

3057-
assert_allclose(sum(fparts), sum(residuals), atol=1e-15)
3058+
xp_assert_close(sum(fparts), sum(residuals), atol=1e-15)
30583059

30593060
return fparts, ix
30603061

@@ -3096,7 +3097,7 @@ def test_split_add_knot(self):
30963097
new_t = _fr.add_knot(x, t, k, residuals)
30973098
new_t_py = _add_knot(x, t, k, residuals)
30983099

3099-
assert_allclose(new_t, new_t_py, atol=1e-15)
3100+
xp_assert_close(new_t, new_t_py, atol=1e-15)
31003101

31013102
# redo with new knots
31023103
spl2 = make_lsq_spline(x, y, k=k, t=new_t)
@@ -3105,31 +3106,31 @@ def test_split_add_knot(self):
31053106
new_t2 = _fr.add_knot(x, new_t, k, residuals2)
31063107
new_t2_py = _add_knot(x, new_t, k, residuals2)
31073108

3108-
assert_allclose(new_t2, new_t2_py, atol=1e-15)
3109+
xp_assert_close(new_t2, new_t2_py, atol=1e-15)
31093110

31103111
@pytest.mark.parametrize('k', [1, 2, 3, 4, 5])
31113112
def test_s0(self, k):
3112-
x = np.arange(8)
3113+
x = np.arange(8, dtype=np.float64)
31133114
y = np.sin(x*np.pi/8)
31143115
t = list(generate_knots(x, y, k=k, s=0))[-1]
31153116

31163117
tt = splrep(x, y, k=k, s=0)[0]
3117-
assert_allclose(t, tt, atol=1e-15)
3118+
xp_assert_close(t, tt, atol=1e-15)
31183119

31193120
def test_s0_1(self):
31203121
# with these data, naive algorithm tries to insert >= nmax knots
31213122
n = 10
31223123
x = np.arange(n)
31233124
y = x**3
31243125
knots = list(generate_knots(x, y, k=3, s=0)) # does not error out
3125-
assert_allclose(knots[-1], _not_a_knot(x, 3), atol=1e-15)
3126+
xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15)
31263127

31273128
def test_s0_n20(self):
31283129
n = 20
31293130
x = np.arange(n)
31303131
y = x**3
31313132
knots = list(generate_knots(x, y, k=3, s=0))
3132-
assert_allclose(knots[-1], _not_a_knot(x, 3), atol=1e-15)
3133+
xp_assert_close(knots[-1], _not_a_knot(x, 3), atol=1e-15)
31333134

31343135
def test_s0_nest(self):
31353136
# s=0 and non-default nest: not implemented, errors out
@@ -3175,11 +3176,11 @@ def test_s_switch(self):
31753176

31763177
assert len(knots) == len(wanted)
31773178
for t, tt in zip(knots, wanted):
3178-
assert_allclose(t, tt, atol=1e-15)
3179+
xp_assert_close(t, tt, atol=1e-15)
31793180

31803181
# also check that the last knot vector matches FITPACK
31813182
t, _, _ = splrep(x, y, k=k, s=1e-7)
3182-
assert_allclose(knots[-1], t, atol=1e-15)
3183+
xp_assert_close(knots[-1], t, atol=1e-15)
31833184

31843185
def test_list_input(self):
31853186
# test that list inputs are accepted
@@ -3194,7 +3195,7 @@ def test_nest(self):
31943195
s = 1e-7
31953196

31963197
knots = list(generate_knots(x, y, k=3, s=s, nest=10))
3197-
assert_allclose(knots[-1],
3198+
xp_assert_close(knots[-1],
31983199
[0., 0., 0., 0., 2., 4., 7., 7., 7., 7.], atol=1e-15)
31993200

32003201
with assert_raises(ValueError):
@@ -3228,7 +3229,7 @@ def test_vs_splrep(self, s, npts):
32283229
t = splrep(x, y, k=k, s=s)[0]
32293230
tt = list(generate_knots(x, y, k=k, s=s))[-1]
32303231

3231-
assert_allclose(tt, t, atol=1e-15)
3232+
xp_assert_close(tt, t, atol=1e-15)
32323233

32333234
def test_s_too_small(self):
32343235
n = 14
@@ -3242,7 +3243,7 @@ def test_s_too_small(self):
32423243
r = sup.record(RuntimeWarning)
32433244
tck = splrep(x, y, k=3, s=1e-50)
32443245
assert len(r) == 1
3245-
assert_equal(knots[-1], tck[0])
3246+
xp_assert_equal(knots[-1], tck[0])
32463247

32473248

32483249
def disc_naive(t, k):
@@ -3391,7 +3392,7 @@ def test_fitpack_F(self):
33913392
f = F(x, y[:, None], t, k, s) # F expects y to be 2D
33923393
f_d = F_dense(x, y, t, k, s)
33933394
for p in [1, 10, 100]:
3394-
assert_allclose(f(p), f_d(p), atol=1e-15)
3395+
xp_assert_close(f(p), f_d(p), atol=1e-15)
33953396

33963397
def test_fitpack_F_with_weights(self):
33973398
# repeat test_fitpack_F, with weights
@@ -3405,7 +3406,7 @@ def test_fitpack_F_with_weights(self):
34053406
f_d = F_dense(x, y, t, k, s) # no weights
34063407

34073408
for p in [1, 10, 100]:
3408-
assert_allclose(fw(p), fw_d(p), atol=1e-15)
3409+
xp_assert_close(fw(p), fw_d(p), atol=1e-15)
34093410
assert not np.allclose(f_d(p), fw_d(p), atol=1e-15)
34103411

34113412
def test_disc_matrix(self):
@@ -3420,7 +3421,7 @@ def test_disc_matrix(self):
34203421
D = PackedMatrix(*_fr.disc(t, k)).todense()
34213422
D_dense = disc_naive(t, k)
34223423
assert D.shape[0] == n - 2*k - 2 # number of internal knots
3423-
assert_allclose(D, D_dense, atol=1e-15)
3424+
xp_assert_close(D, D_dense, atol=1e-15)
34243425

34253426
def test_simple_vs_splrep(self):
34263427
x, y, k, s, tt = self._get_xykt()
@@ -3430,7 +3431,7 @@ def test_simple_vs_splrep(self):
34303431
assert all(t == tt)
34313432

34323433
spl = make_splrep(x, y, k=k, s=s)
3433-
assert_allclose(c[:spl.c.size], spl.c, atol=1e-15)
3434+
xp_assert_close(c[:spl.c.size], spl.c, atol=1e-15)
34343435

34353436
def test_with_knots(self):
34363437
x, y, k, s, _ = self._get_xykt()
@@ -3440,9 +3441,9 @@ def test_with_knots(self):
34403441
spl_auto = make_splrep(x, y, k=k, s=s)
34413442
spl_t = make_splrep(x, y, t=t, k=k, s=s)
34423443

3443-
assert_allclose(spl_auto.t, spl_t.t, atol=1e-15)
3444-
assert_allclose(spl_auto.c, spl_t.c, atol=1e-15)
3445-
assert_allclose(spl_auto.k, spl_t.k, atol=1e-15)
3444+
xp_assert_close(spl_auto.t, spl_t.t, atol=1e-15)
3445+
xp_assert_close(spl_auto.c, spl_t.c, atol=1e-15)
3446+
assert spl_auto.k == spl_t.k
34463447

34473448
def test_no_internal_knots(self):
34483449
# should not fail if there are no internal knots
@@ -3460,7 +3461,7 @@ def test_default_s(self):
34603461
spl = make_splrep(x, y, k=3)
34613462
spl_i = make_interp_spline(x, y, k=3)
34623463

3463-
assert_allclose(spl.c, spl_i.c, atol=1e-15)
3464+
xp_assert_close(spl.c, spl_i.c, atol=1e-15)
34643465

34653466
def test_s_too_small(self):
34663467
# both splrep and make_splrep warn that "s too small": ier=2
@@ -3473,8 +3474,8 @@ def test_s_too_small(self):
34733474
tck = splrep(x, y, k=3, s=1e-50)
34743475
spl = make_splrep(x, y, k=3, s=1e-50)
34753476
assert len(r) == 2
3476-
assert_equal(spl.t, tck[0])
3477-
assert_allclose(np.r_[spl.c, [0]*(spl.k+1)],
3477+
xp_assert_equal(spl.t, tck[0])
3478+
xp_assert_close(np.r_[spl.c, [0]*(spl.k+1)],
34783479
tck[1], atol=5e-13)
34793480

34803481
def test_shape(self):
@@ -3531,18 +3532,18 @@ def test_simple_vs_splprep(self, s):
35313532
spl, u = make_splprep(y, s=s)
35323533

35333534
# parameters
3534-
assert_allclose(u, u_, atol=1e-15)
3535+
xp_assert_close(u, u_, atol=1e-15)
35353536

35363537
# knots
3537-
assert_allclose(spl.t, t, atol=1e-15)
3538+
xp_assert_close(spl.t, t, atol=1e-15)
35383539
assert len(t) == num_knots[s]
35393540

35403541
# coefficients: note the transpose
35413542
cc = np.asarray(c).T
3542-
assert_allclose(spl.c, cc, atol=1e-15)
3543+
xp_assert_close(spl.c, cc, atol=1e-15)
35433544

35443545
# values: note axis=1
3545-
assert_allclose(spl(u),
3546+
xp_assert_close(spl(u),
35463547
BSpline(t, c, k, axis=1)(u), atol=1e-15)
35473548

35483549
@pytest.mark.parametrize('s', [0, 0.1, 1e-3, 1e-5])
@@ -3555,23 +3556,25 @@ def test_array_not_list(self, s):
35553556
# assert the behavior of FITPACK's splrep
35563557
tck, u = splprep(y, s=s)
35573558
tck_a, u_a = splprep(np.asarray(y), s=s)
3558-
assert_allclose(u, u_a, atol=s)
3559-
assert_allclose(tck[0], tck_a[0], atol=1e-15)
3560-
assert_allclose(tck[1], tck_a[1], atol=1e-15)
3559+
xp_assert_close(u, u_a, atol=s)
3560+
xp_assert_close(tck[0], tck_a[0], atol=1e-15)
3561+
assert len(tck[1]) == len(tck_a[1])
3562+
for c1, c2 in zip(tck[1], tck_a[1]):
3563+
xp_assert_close(c1, c2, atol=1e-15)
35613564
assert tck[2] == tck_a[2]
35623565
assert np.shape(splev(u, tck)) == np.shape(y)
35633566

35643567
spl, u = make_splprep(y, s=s)
3565-
assert_allclose(u, u_a, atol=1e-15)
3566-
assert_allclose(spl.t, tck_a[0], atol=1e-15)
3567-
assert_allclose(spl.c.T, tck_a[1], atol=1e-15)
3568+
xp_assert_close(u, u_a, atol=1e-15)
3569+
xp_assert_close(spl.t, tck_a[0], atol=1e-15)
3570+
xp_assert_close(spl.c.T, tck_a[1], atol=1e-15)
35683571
assert spl.k == tck_a[2]
35693572
assert spl(u).shape == np.shape(y)
35703573

35713574
spl, u = make_splprep(np.asarray(y), s=s)
3572-
assert_allclose(u, u_a, atol=1e-15)
3573-
assert_allclose(spl.t, tck_a[0], atol=1e-15)
3574-
assert_allclose(spl.c.T, tck_a[1], atol=1e-15)
3575+
xp_assert_close(u, u_a, atol=1e-15)
3576+
xp_assert_close(spl.t, tck_a[0], atol=1e-15)
3577+
xp_assert_close(spl.c.T, tck_a[1], atol=1e-15)
35753578
assert spl.k == tck_a[2]
35763579
assert spl(u).shape == np.shape(y)
35773580

@@ -3582,7 +3585,7 @@ def test_default_s_is_zero(self):
35823585
x, y, k = self._get_xyk(m=10)
35833586

35843587
spl, u = make_splprep(y)
3585-
assert_allclose(spl(u), y, atol=1e-15)
3588+
xp_assert_close(spl(u), y, atol=1e-15)
35863589

35873590
def test_s_zero_vs_near_zero(self):
35883591
# s=0 and s \approx 0 are consistent
@@ -3591,9 +3594,9 @@ def test_s_zero_vs_near_zero(self):
35913594
spl_i, u_i = make_splprep(y, s=0)
35923595
spl_n, u_n = make_splprep(y, s=1e-15)
35933596

3594-
assert_allclose(u_i, u_n, atol=1e-15)
3595-
assert_allclose(spl_i(u_i), y, atol=1e-15)
3596-
assert_allclose(spl_n(u_n), y, atol=1e-7)
3597+
xp_assert_close(u_i, u_n, atol=1e-15)
3598+
xp_assert_close(spl_i(u_i), y, atol=1e-15)
3599+
xp_assert_close(spl_n(u_n), y, atol=1e-7)
35973600
assert spl_i.axis == spl_n.axis
35983601
assert spl_i.c.shape == spl_n.c.shape
35993602

@@ -3612,5 +3615,5 @@ def test_1D(self):
36123615
spl, u = make_splprep([x], s=1e-5)
36133616

36143617
assert spl(u).shape == (1, 8)
3615-
assert_allclose(spl(u), [x], atol=1e-15)
3618+
xp_assert_close(spl(u), [x], atol=1e-15)
36163619

scipy/interpolate/tests/test_fitpack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def err_est(k, d):
8484
if not per:
8585
spl = make_splrep(x, v, k=k, s=s, xb=xb, xe=xe)
8686
if len(spl.t) == len(tck[0]):
87-
assert_allclose(spl.t, tck[0], atol=1e-15)
88-
assert_allclose(spl.c, tck[1][:spl.c.size], atol=1e-13)
87+
xp_assert_close(spl.t, tck[0], atol=1e-15)
88+
xp_assert_close(spl.c, tck[1][:spl.c.size], atol=1e-13)
8989
else:
9090
assert k == 5 # knot length differ in some k=5 cases
9191

scipy/interpolate/tests/test_fitpack2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def test_linear_constant(self):
3030
assert_array_almost_equal(lut([1, 1.5, 2]), [3, 3, 3])
3131

3232
spl = make_splrep(x, y, k=1, s=len(x))
33-
assert_allclose(spl.t[1:-1], lut.get_knots(), atol=1e-15)
34-
assert_allclose(spl.c, lut.get_coeffs(), atol=1e-15)
33+
xp_assert_close(spl.t[1:-1], lut.get_knots(), atol=1e-15)
34+
xp_assert_close(spl.c, lut.get_coeffs(), atol=1e-15)
3535

3636
def test_preserve_shape(self):
3737
x = [1, 2, 3]

0 commit comments

Comments
 (0)