Skip to content

Commit eafcae4

Browse files
committed
Migrate tests/test_aamp.py from npt.assert_almost_equal to npt.assert_allclose
npt.assert_almost_equal only checks a fixed absolute tolerance, and NumPy's docs recommend assert_allclose instead. Every comparison in this file is against `ref_mp`/`comp_mp` (or a column slice of it), which combine a float distance column with int index columns and so come back dtype=object - np.isclose can't handle that directly. Cast both sides to float64 before comparing instead of leaving these on the deprecated API; the values are always numeric so the cast is exact. rtol is left at its default rather than pinned to 0. First of a per-file split of #1175, per review feedback.
1 parent 017b904 commit eafcae4

1 file changed

Lines changed: 86 additions & 32 deletions

File tree

tests/test_aamp.py

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ def test_aamp_self_join(T_A, T_B):
3535
comp_mp = aamp(T_B, m, p=p)
3636
naive.replace_inf(ref_mp)
3737
naive.replace_inf(comp_mp)
38-
npt.assert_almost_equal(ref_mp, comp_mp)
38+
npt.assert_allclose(
39+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
40+
)
3941

4042
comp_mp = aamp(pd.Series(T_B), m, p=p)
4143
naive.replace_inf(comp_mp)
42-
npt.assert_almost_equal(ref_mp, comp_mp)
44+
npt.assert_allclose(
45+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
46+
)
4347

4448

4549
@pytest.mark.parametrize("T_A, T_B", test_data)
@@ -50,11 +54,15 @@ def test_aamp_A_B_join(T_A, T_B):
5054
comp_mp = aamp(T_A, m, T_B, ignore_trivial=False, p=p)
5155
naive.replace_inf(ref_mp)
5256
naive.replace_inf(comp_mp)
53-
npt.assert_almost_equal(ref_mp, comp_mp)
57+
npt.assert_allclose(
58+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
59+
)
5460

5561
comp_mp = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False, p=p)
5662
naive.replace_inf(comp_mp)
57-
npt.assert_almost_equal(ref_mp, comp_mp)
63+
npt.assert_allclose(
64+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
65+
)
5866

5967

6068
def test_aamp_constant_subsequence_self_join():
@@ -64,11 +72,15 @@ def test_aamp_constant_subsequence_self_join():
6472
comp_mp = aamp(T_A, m, ignore_trivial=True)
6573
naive.replace_inf(ref_mp)
6674
naive.replace_inf(comp_mp)
67-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
75+
npt.assert_allclose(
76+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
77+
) # ignore indices
6878

6979
comp_mp = aamp(pd.Series(T_A), m, ignore_trivial=True)
7080
naive.replace_inf(comp_mp)
71-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
81+
npt.assert_allclose(
82+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
83+
) # ignore indices
7284

7385

7486
def test_aamp_one_constant_subsequence_A_B_join():
@@ -79,18 +91,24 @@ def test_aamp_one_constant_subsequence_A_B_join():
7991
comp_mp = aamp(T_A, m, T_B, ignore_trivial=False)
8092
naive.replace_inf(ref_mp)
8193
naive.replace_inf(comp_mp)
82-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
94+
npt.assert_allclose(
95+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
96+
) # ignore indices
8397

8498
comp_mp = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False)
8599
naive.replace_inf(comp_mp)
86-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
100+
npt.assert_allclose(
101+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
102+
) # ignore indices
87103

88104
# Swap inputs
89105
ref_mp = naive.aamp(T_B, m, T_B=T_A)
90106
comp_mp = aamp(T_B, m, T_A, ignore_trivial=False)
91107
naive.replace_inf(ref_mp)
92108
naive.replace_inf(comp_mp)
93-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
109+
npt.assert_allclose(
110+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
111+
) # ignore indices
94112

95113

96114
def test_aamp_two_constant_subsequences_A_B_join():
@@ -103,22 +121,30 @@ def test_aamp_two_constant_subsequences_A_B_join():
103121
comp_mp = aamp(T_A, m, T_B, ignore_trivial=False)
104122
naive.replace_inf(ref_mp)
105123
naive.replace_inf(comp_mp)
106-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
124+
npt.assert_allclose(
125+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
126+
) # ignore indices
107127

108128
comp_mp = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False)
109129
naive.replace_inf(comp_mp)
110-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
130+
npt.assert_allclose(
131+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
132+
) # ignore indices
111133

112134
# Swap inputs
113135
ref_mp = naive.aamp(T_B, m, T_B=T_A)
114136
comp_mp = aamp(T_B, m, T_A, ignore_trivial=False)
115137
naive.replace_inf(ref_mp)
116138
naive.replace_inf(comp_mp)
117-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
139+
npt.assert_allclose(
140+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
141+
) # ignore indices
118142

119143
comp_mp = aamp(pd.Series(T_B), m, pd.Series(T_A), ignore_trivial=False)
120144
naive.replace_inf(comp_mp)
121-
npt.assert_almost_equal(ref_mp[:, 0], comp_mp[:, 0]) # ignore indices
145+
npt.assert_allclose(
146+
ref_mp[:, 0].astype(np.float64), comp_mp[:, 0].astype(np.float64), atol=1.5e-07
147+
) # ignore indices
122148

123149

124150
def test_aamp_identical_subsequence_self_join():
@@ -131,14 +157,18 @@ def test_aamp_identical_subsequence_self_join():
131157
comp_mp = aamp(T_A, m, ignore_trivial=True)
132158
naive.replace_inf(ref_mp)
133159
naive.replace_inf(comp_mp)
134-
npt.assert_almost_equal(
135-
ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION
160+
npt.assert_allclose(
161+
ref_mp[:, 0].astype(np.float64),
162+
comp_mp[:, 0].astype(np.float64),
163+
atol=1.5 * 10**-config.STUMPY_TEST_PRECISION,
136164
) # ignore indices
137165

138166
comp_mp = aamp(pd.Series(T_A), m, ignore_trivial=True)
139167
naive.replace_inf(comp_mp)
140-
npt.assert_almost_equal(
141-
ref_mp[:, 0], comp_mp[:, 0], decimal=config.STUMPY_TEST_PRECISION
168+
npt.assert_allclose(
169+
ref_mp[:, 0].astype(np.float64),
170+
comp_mp[:, 0].astype(np.float64),
171+
atol=1.5 * 10**-config.STUMPY_TEST_PRECISION,
142172
) # ignore indices
143173

144174

@@ -153,23 +183,29 @@ def test_aamp_identical_subsequence_A_B_join():
153183
comp_mp = aamp(T_A, m, T_B, ignore_trivial=False)
154184
naive.replace_inf(ref_mp)
155185
naive.replace_inf(comp_mp)
156-
npt.assert_almost_equal(
157-
ref_mp[:, 0], comp_mp[:, 0], config.STUMPY_TEST_PRECISION
186+
npt.assert_allclose(
187+
ref_mp[:, 0].astype(np.float64),
188+
comp_mp[:, 0].astype(np.float64),
189+
atol=1.5 * 10**-config.STUMPY_TEST_PRECISION,
158190
) # ignore indices
159191

160192
comp_mp = aamp(pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False)
161193
naive.replace_inf(comp_mp)
162-
npt.assert_almost_equal(
163-
ref_mp[:, 0], comp_mp[:, 0], config.STUMPY_TEST_PRECISION
194+
npt.assert_allclose(
195+
ref_mp[:, 0].astype(np.float64),
196+
comp_mp[:, 0].astype(np.float64),
197+
atol=1.5 * 10**-config.STUMPY_TEST_PRECISION,
164198
) # ignore indices
165199

166200
# Swap inputs
167201
ref_mp = naive.aamp(T_B, m, T_B=T_A)
168202
comp_mp = aamp(T_B, m, T_A, ignore_trivial=False)
169203
naive.replace_inf(ref_mp)
170204
naive.replace_inf(comp_mp)
171-
npt.assert_almost_equal(
172-
ref_mp[:, 0], comp_mp[:, 0], config.STUMPY_TEST_PRECISION
205+
npt.assert_allclose(
206+
ref_mp[:, 0].astype(np.float64),
207+
comp_mp[:, 0].astype(np.float64),
208+
atol=1.5 * 10**-config.STUMPY_TEST_PRECISION,
173209
) # ignore indices
174210

175211

@@ -189,11 +225,15 @@ def test_aamp_nan_inf_self_join(T_A, T_B, substitute_B, substitution_locations):
189225
comp_mp = aamp(T_B_sub, m, ignore_trivial=True)
190226
naive.replace_inf(ref_mp)
191227
naive.replace_inf(comp_mp)
192-
npt.assert_almost_equal(ref_mp, comp_mp)
228+
npt.assert_allclose(
229+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
230+
)
193231

194232
comp_mp = aamp(pd.Series(T_B_sub), m, ignore_trivial=True)
195233
naive.replace_inf(comp_mp)
196-
npt.assert_almost_equal(ref_mp, comp_mp)
234+
npt.assert_allclose(
235+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
236+
)
197237

198238

199239
@pytest.mark.parametrize("T_A, T_B", test_data)
@@ -219,13 +259,17 @@ def test_aamp_nan_inf_A_B_join(
219259
comp_mp = aamp(T_A_sub, m, T_B_sub, ignore_trivial=False)
220260
naive.replace_inf(ref_mp)
221261
naive.replace_inf(comp_mp)
222-
npt.assert_almost_equal(ref_mp, comp_mp)
262+
npt.assert_allclose(
263+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
264+
)
223265

224266
comp_mp = aamp(
225267
pd.Series(T_A_sub), m, pd.Series(T_B_sub), ignore_trivial=False
226268
)
227269
naive.replace_inf(comp_mp)
228-
npt.assert_almost_equal(ref_mp, comp_mp)
270+
npt.assert_allclose(
271+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
272+
)
229273

230274

231275
def test_aamp_nan_zero_mean_self_join():
@@ -237,7 +281,9 @@ def test_aamp_nan_zero_mean_self_join():
237281

238282
naive.replace_inf(ref_mp)
239283
naive.replace_inf(comp_mp)
240-
npt.assert_almost_equal(ref_mp, comp_mp)
284+
npt.assert_allclose(
285+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
286+
)
241287

242288

243289
@pytest.mark.parametrize("T_A, T_B", test_data)
@@ -249,11 +295,15 @@ def test_aamp_self_join_KNN(T_A, T_B):
249295
comp_mp = aamp(T_B, m, p=p, k=k)
250296
naive.replace_inf(ref_mp)
251297
naive.replace_inf(comp_mp)
252-
npt.assert_almost_equal(ref_mp, comp_mp)
298+
npt.assert_allclose(
299+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
300+
)
253301

254302
comp_mp = aamp(pd.Series(T_B), m, p=p, k=k)
255303
naive.replace_inf(comp_mp)
256-
npt.assert_almost_equal(ref_mp, comp_mp)
304+
npt.assert_allclose(
305+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
306+
)
257307

258308

259309
@pytest.mark.parametrize("T_A, T_B", test_data)
@@ -265,10 +315,14 @@ def test_aamp_A_B_join_KNN(T_A, T_B):
265315
comp_mp = aamp(T_A, m, T_B, ignore_trivial=False, p=p, k=k)
266316
naive.replace_inf(ref_mp)
267317
naive.replace_inf(comp_mp)
268-
npt.assert_almost_equal(ref_mp, comp_mp)
318+
npt.assert_allclose(
319+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
320+
)
269321

270322
comp_mp = aamp(
271323
pd.Series(T_A), m, pd.Series(T_B), ignore_trivial=False, p=p, k=k
272324
)
273325
naive.replace_inf(comp_mp)
274-
npt.assert_almost_equal(ref_mp, comp_mp)
326+
npt.assert_allclose(
327+
ref_mp.astype(np.float64), comp_mp.astype(np.float64), atol=1.5e-07
328+
)

0 commit comments

Comments
 (0)