Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bce12e6

Browse files
committed
TST: use no_0d assertions in some places
[skip circle]
1 parent 11af99c commit bce12e6

11 files changed

Lines changed: 47 additions & 43 deletions

scipy/interpolate/tests/test_bsplines.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
xp_assert_equal, xp_assert_close, xp_default_dtype, concat_1d, make_xp_test_case,
1414
xp_ravel, _xp_copy_to_numpy, array_namespace
1515
)
16+
from scipy._lib._array_api_no_0d import xp_assert_close as xp_assert_close_no_0d
1617
import scipy._external.array_api_extra as xpx
1718
from pytest import raises as assert_raises
1819
import pytest
@@ -3849,7 +3850,7 @@ def test_fitpack_F(self, k):
38493850
f = F(x, y[:, None], t, k, s) # F expects y to be 2D
38503851
f_d = F_dense(x, y, t, k, s)
38513852
for p in [1, 10, 100]:
3852-
xp_assert_close(f(p), f_d(p), atol=1e-15)
3853+
xp_assert_close_no_0d(f(p), f_d(p), atol=1e-15)
38533854

38543855
@pytest.mark.parametrize("k", [1, 2, 3, 4, 5, 6])
38553856
def test_fitpack_F_with_weights(self, k):
@@ -3865,7 +3866,7 @@ def test_fitpack_F_with_weights(self, k):
38653866
f_d = F_dense(x, y, t, k, s) # no weights
38663867

38673868
for p in [1, 10, 100]:
3868-
xp_assert_close(fw(p), fw_d(p), atol=1e-15)
3869+
xp_assert_close_no_0d(fw(p), fw_d(p), atol=1e-15)
38693870
assert not np.allclose(f_d(p), fw_d(p), atol=1e-15)
38703871

38713872
def test_disc_matrix(self):

scipy/interpolate/tests/test_fitpack2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_roots(self):
9090
x = [1, 3, 5, 7, 9]
9191
y = [0, 4, 9, 12, 21]
9292
spl = UnivariateSpline(x, y, k=3)
93-
assert_almost_equal(spl.roots()[0], 1.050290639101332)
93+
assert np.isclose(spl.roots()[0], 1.050290639101332)
9494

9595
def test_roots_length(self): # for gh18335
9696
x = np.linspace(0, 50 * np.pi, 1000)
@@ -196,7 +196,7 @@ def test_derivative_and_antiderivative(self):
196196
xp_assert_close(spl(0.3), spl2(0.3))
197197

198198
spl2 = spl.antiderivative(1)
199-
xp_assert_close(spl2(0.6) - spl2(0.2),
199+
xp_assert_close(np.asarray(spl2(0.6) - spl2(0.2)),
200200
spl.integral(0.2, 0.6))
201201

202202
def test_derivative_extrapolation(self):

scipy/interpolate/tests/test_polyint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,9 @@ def test_exponential(self):
337337
with pytest.warns(DeprecationWarning, match="`approximate_taylor_polynomial`"):
338338
p = approximate_taylor_polynomial(np.exp, 0, degree, 1, 15)
339339
for i in range(degree+1):
340-
assert_almost_equal(p(0),1)
340+
assert np.isclose(p(0), 1, atol=0)
341341
p = p.deriv()
342-
assert_almost_equal(p(0),0)
342+
assert np.isclose(p(0), 0, atol=0)
343343

344344

345345
class TestBarycentric:

scipy/interpolate/tests/test_rgi.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
from numpy.exceptions import ComplexWarning
77

88
from scipy._lib._array_api import (
9-
xp_assert_equal, xp_assert_close, assert_array_almost_equal,
10-
make_xp_test_case
9+
xp_assert_equal, assert_array_almost_equal, make_xp_test_case
1110
)
11+
from scipy._lib._array_api_no_0d import xp_assert_close
12+
1213
from scipy.conftest import skip_xp_invalid_arg
1314

1415
from pytest import raises as assert_raises
@@ -446,7 +447,7 @@ def f(x, y):
446447
# check exrapolation w/ fill_value
447448
xp_assert_close(interp(np.array([1.1, 2.4])),
448449
interp.fill_value,
449-
check_dtype=False, check_shape=False, check_0d=False,
450+
check_dtype=False, check_shape=False, check_0d=True,
450451
atol=1e-14)
451452

452453
# check extrapolation: linear along the `y` axis, const along `x`

scipy/signal/tests/test_filter_design.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
import pytest
1111
from pytest import raises as assert_raises
1212
from scipy._lib._array_api import (
13-
xp_assert_close, xp_assert_equal, array_namespace,
13+
xp_assert_equal, array_namespace,
1414
assert_array_almost_equal, xp_size, xp_default_dtype, is_numpy,
1515
make_xp_test_case, make_xp_pytest_param, is_cupy, is_torch, scipy_namespace_for,
1616
_xp_copy_to_numpy, xp_assert_close_nulp
1717
)
18+
from scipy._lib._array_api_no_0d import xp_assert_close
1819
import scipy._external.array_api_extra as xpx
1920

2021
from numpy import array, spacing, sin, pi
@@ -268,7 +269,7 @@ def test_complexk(self, xp):
268269
b_np, a_np = map(_xp_copy_to_numpy, (b, a))
269270
z_np, p_np, k_np = tf2zpk(b_np, a_np)
270271
z, p, k = map(xp.asarray, (z_np, p_np, k_np))
271-
xp_assert_close(k, xp.asarray(1j), check_0d=False)
272+
xp_assert_close(k, xp.asarray(1j), check_0d=True)
272273
bp, ap = zpk2tf(z, p, k)
273274
xp_assert_close(b, bp)
274275
xp_assert_close(a, ap)
@@ -1195,13 +1196,13 @@ def test_freqz_sos_design(self, xp):
11951196
w = w / xp.pi
11961197
xp_assert_close(20 * xp.log10(h[w <= 0.1]),
11971198
zero, atol=3.01,
1198-
check_shape=False)
1199+
check_shape=False, check_0d=True)
11991200
xp_assert_close(20 * xp.log10(h[w >= 0.6]),
12001201
zero, atol=3.01,
1201-
check_shape=False)
1202+
check_shape=False, check_0d=True)
12021203
xp_assert_close(h[(w >= 0.2) & (w <= 0.5)],
12031204
zero, atol=1e-3,
1204-
check_shape=False) # <= -60 dB
1205+
check_shape=False, check_0d=True) # <= -60 dB
12051206

12061207
N, Wn = cheb2ord([0.1, 0.6], [0.2, 0.5], 3, 150)
12071208
sos = cheby2(N, 150, Wn, 'stop', output='sos')
@@ -1210,8 +1211,8 @@ def test_freqz_sos_design(self, xp):
12101211
w, h = freqz_sos(sos)
12111212
dB = 20*xp.log10(xp.abs(h))
12121213
w = w / xp.pi
1213-
xp_assert_close(dB[w <= 0.1], zero, atol=3.01, check_shape=False)
1214-
xp_assert_close(dB[w >= 0.6], zero, atol=3.01, check_shape=False)
1214+
xp_assert_close(dB[w <= 0.1], zero, atol=3.01, check_shape=False, check_0d=True)
1215+
xp_assert_close(dB[w >= 0.6], zero, atol=3.01, check_shape=False, check_0d=True)
12151216
assert xp.all(dB[(w >= 0.2) & (w <= 0.5)] < -149.9)
12161217

12171218
# from cheb1ord
@@ -1223,9 +1224,9 @@ def test_freqz_sos_design(self, xp):
12231224
h = xp.abs(h)
12241225
w = w / xp.pi
12251226
xp_assert_close(20 * xp.log10(h[w <= 0.2]), zero, atol=3.01,
1226-
check_shape=False)
1227+
check_shape=False, check_0d=True)
12271228
xp_assert_close(h[w >= 0.3], zero, atol=1e-2,
1228-
check_shape=False) # <= -40 dB
1229+
check_shape=False, check_0d=True) # <= -40 dB
12291230

12301231
N, Wn = cheb1ord(0.2, 0.3, 1, 150)
12311232
sos = cheby1(N, 1, Wn, 'low', output='sos')
@@ -1234,7 +1235,7 @@ def test_freqz_sos_design(self, xp):
12341235
w, h = freqz_sos(sos)
12351236
dB = 20*xp.log10(xp.abs(h))
12361237
w /= np.pi
1237-
xp_assert_close(dB[w <= 0.2], zero, atol=1.01, check_shape=False)
1238+
xp_assert_close(dB[w <= 0.2], zero, atol=1.01, check_shape=False, check_0d=True)
12381239
assert xp.all(dB[w >= 0.3] < -149.9)
12391240

12401241
# adapted from ellipord
@@ -1246,9 +1247,9 @@ def test_freqz_sos_design(self, xp):
12461247
h = xp.abs(h)
12471248
w = w / xp.pi
12481249
xp_assert_close(20 * xp.log10(h[w >= 0.3]), zero, atol=3.01,
1249-
check_shape=False)
1250+
check_shape=False, check_0d=True)
12501251
xp_assert_close(h[w <= 0.1], zero, atol=1.5e-3,
1251-
check_shape=False) # <= -60 dB (approx)
1252+
check_shape=False, check_0d=True) # <= -60 dB (approx)
12521253

12531254
# adapted from buttord
12541255
N, Wn = buttord([0.2, 0.5], [0.14, 0.6], 3, 40)
@@ -1260,11 +1261,11 @@ def test_freqz_sos_design(self, xp):
12601261
w = w / xp.pi
12611262

12621263
h014 = h[w <= 0.14]
1263-
xp_assert_close(h014, xp.zeros_like(h014), atol=1e-2) # <= -40 dB
1264+
xp_assert_close(h014, xp.zeros_like(h014), atol=1e-2, check_0d=True) # <= -40 dB
12641265
h06 = h[w >= 0.6]
1265-
xp_assert_close(h06, xp.zeros_like(h06), atol=1e-2) # <= -40 dB
1266+
xp_assert_close(h06, xp.zeros_like(h06), atol=1e-2, check_0d=True) # <= -40 dB
12661267
h0205 = 20 * xp.log10(h[(w >= 0.2) & (w <= 0.5)])
1267-
xp_assert_close(h0205, xp.zeros_like(h0205), atol=3.01)
1268+
xp_assert_close(h0205, xp.zeros_like(h0205), atol=3.01, check_0d=True)
12681269

12691270
N, Wn = buttord([0.2, 0.5], [0.14, 0.6], 3, 100)
12701271
sos = butter(N, Wn, 'band', output='sos')
@@ -1277,7 +1278,7 @@ def test_freqz_sos_design(self, xp):
12771278
assert xp.all(dB[(w > 0) & (w <= 0.14)] < -99.9)
12781279
assert xp.all(dB[w >= 0.6] < -99.9)
12791280
db0205 = dB[(w >= 0.2) & (w <= 0.5)]
1280-
xp_assert_close(db0205, xp.zeros_like(db0205), atol=3.01)
1281+
xp_assert_close(db0205, xp.zeros_like(db0205), atol=3.01, check_0d=True)
12811282

12821283
def test_freqz_sos_design_ellip(self, xp):
12831284
N, Wn = ellipord(0.3, 0.1, 3, 60)
@@ -5096,9 +5097,11 @@ def test_frequency_response(self, xp):
50965097

50975098
# Check that the peak magnitude is 1 and the frequency is 1000 Hz.
50985099
xp_assert_close(response_max,
5099-
xp.ones_like(response_max), rtol=1e-2, check_0d=False)
5100-
xp_assert_close(freq_hz,
5101-
1000*xp.ones_like(freq_hz), rtol=1e-2, check_0d=False)
5100+
xp.ones_like(response_max), rtol=1e-2, check_0d=True)
5101+
xp_assert_close(
5102+
freq_hz, xp.asarray(1000*xp.ones_like(freq_hz)),
5103+
rtol=1e-2, check_0d=True
5104+
)
51025105

51035106
# All built-in IIR filters are real, so should have perfectly
51045107
# symmetrical poles and zeros. Then ba representation (using

scipy/signal/tests/test_ltisys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ def test_08(self):
11921192
# Test that bode() return continuous phase, issues/2331.
11931193
system = lti([], [-10, -30, -40, -60, -70], 1)
11941194
w, mag, phase = system.bode(w=np.logspace(-3, 40, 100))
1195-
assert_almost_equal(min(phase), -450, decimal=15)
1195+
assert np.isclose(min(phase), -450, atol=1e-16, rtol=0)
11961196

11971197
def test_from_state_space(self):
11981198
# Ensure that bode works with a system that was created from the

scipy/signal/tests/test_max_len_seq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_mls_output(self):
5050
# impulse amplitude == test_len
5151
err_msg = "mls impulse has incorrect value"
5252
xp_assert_close(tester[0],
53-
float(out_len),
53+
np.float64(out_len),
5454
err_msg=err_msg
5555
)
5656
# steady-state is -1

scipy/signal/tests/test_short_time_fft.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import numpy as np
2626
import pytest
27-
from scipy._lib._array_api import xp_assert_close, xp_assert_equal
27+
from scipy._lib._array_api_no_0d import xp_assert_close, xp_assert_equal
2828
from scipy.fft import fftshift
2929
from scipy.stats import norm as normal_distribution # type: ignore
3030
from scipy.signal import check_COLA, get_window, welch, stft, istft, spectrogram

scipy/signal/tests/test_signaltools.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
from scipy._lib import _testutils
2929

3030
from scipy._lib._array_api import (
31-
xp_assert_close, xp_assert_equal, is_numpy, is_torch, is_jax, is_cupy,
31+
xp_assert_close, is_numpy, is_torch, is_jax, is_cupy,
3232
assert_array_almost_equal, assert_almost_equal,
3333
xp_copy, xp_size, xp_default_dtype, array_namespace, make_xp_test_case,
3434
make_xp_pytest_param, SCIPY_DEVICE, _xp_copy_to_numpy
3535
)
36+
from scipy._lib._array_api_no_0d import xp_assert_equal
37+
3638
skip_xp_backends = pytest.mark.skip_xp_backends
3739
xfail_xp_backends = pytest.mark.xfail_xp_backends
3840

@@ -2689,11 +2691,11 @@ def test_rank0(self, dt_name, xp):
26892691
assert y.dtype == dt
26902692

26912693
xp_assert_equal(correlate([1], [2j]), np.asarray(correlate(1, 2j)),
2692-
check_shape=False)
2694+
check_shape=False, check_0d=True)
26932695
xp_assert_equal(correlate([2j], [3j]), np.asarray(correlate(2j, 3j)),
2694-
check_shape=False)
2696+
check_shape=False, check_0d=True)
26952697
xp_assert_equal(correlate([3j], [4]), np.asarray(correlate(3j, 4)),
2696-
check_shape=False)
2698+
check_shape=False, check_0d=True)
26972699

26982700

26992701

scipy/signal/tests/test_waveforms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import pytest
33
from pytest import raises as assert_raises
44
from scipy._lib._array_api import (
5-
assert_almost_equal, xp_assert_equal, xp_assert_close, _xp_copy_to_numpy,
6-
make_xp_test_case, xp_default_dtype
5+
xp_assert_equal, _xp_copy_to_numpy, make_xp_test_case, xp_default_dtype
76
)
7+
from scipy._lib._array_api_no_0d import assert_almost_equal, xp_assert_close
88

99
import scipy.signal._waveforms as waveforms
1010
from scipy.signal import square, sawtooth # type:ignore[attr-defined]

0 commit comments

Comments
 (0)