Skip to content

Commit 699bd45

Browse files
authored
Fixed #1175 Migrate tests/test_aamp_motifs.py to npt.assert_allclose (#1181)
* Migrate tests/test_aamp_motifs.py from npt.assert_almost_equal to npt.assert_allclose * Addressed comments Rename left/right to ref/cmp for consistency, and flip assert_array_equal argument order to match the actual, desired convention.
1 parent 95d0b33 commit 699bd45

1 file changed

Lines changed: 37 additions & 25 deletions

File tree

tests/test_aamp_motifs.py

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def test_aamp_motifs_one_motif():
5151
m = 3
5252
max_motifs = 1
5353

54-
left_indices = [[0, 5]]
55-
left_profile_values = [[0.0, 0.0]]
54+
ref_indices = [[0, 5]]
55+
ref_profile_values = [[0.0, 0.0]]
5656

5757
for p in [1.0, 2.0, 3.0]:
5858
mp = naive.aamp(T, m, p=p)
59-
right_distance_values, right_indices = aamp_motifs(
59+
cmp_distance_values, cmp_indices = aamp_motifs(
6060
T,
6161
mp[:, 0],
6262
max_motifs=max_motifs,
@@ -65,8 +65,8 @@ def test_aamp_motifs_one_motif():
6565
p=p,
6666
)
6767

68-
npt.assert_array_equal(left_indices, right_indices)
69-
npt.assert_almost_equal(left_profile_values, right_distance_values, decimal=4)
68+
npt.assert_array_equal(cmp_indices, ref_indices)
69+
npt.assert_allclose(cmp_distance_values, ref_profile_values, atol=1.5e-04)
7070

7171

7272
def test_aamp_motifs_two_motifs():
@@ -105,16 +105,16 @@ def test_aamp_motifs_two_motifs():
105105

106106
mp = naive.aamp(T, m)
107107

108-
# left_indices = [[70, 170], [10, 210]]
109-
left_profile_values = [
108+
# ref_indices = [[70, 170], [10, 210]]
109+
ref_profile_values = [
110110
[0.0, 0.0],
111111
[
112112
0.0,
113113
naive.distance(T[10:30], T[210:230]),
114114
],
115115
]
116116

117-
right_distance_values, right_indices = aamp_motifs(
117+
cmp_distance_values, cmp_indices = aamp_motifs(
118118
T,
119119
mp[:, 0],
120120
max_motifs=max_motifs,
@@ -124,7 +124,7 @@ def test_aamp_motifs_two_motifs():
124124

125125
# We ignore indices because of sorting ambiguities for equal distances.
126126
# As long as the distances are correct, the indices will be too.
127-
npt.assert_almost_equal(left_profile_values, right_distance_values, decimal=6)
127+
npt.assert_allclose(cmp_distance_values, ref_profile_values, atol=1.5e-06)
128128

129129

130130
def test_aamp_naive_match_exact():
@@ -135,8 +135,8 @@ def test_aamp_naive_match_exact():
135135
excl_zone = int(np.ceil(m / 4))
136136

137137
for p in [1.0, 2.0, 3.0]:
138-
left = [[0, 0], [0, 5]]
139-
right = list(
138+
ref = [[0, 0], [0, 5]]
139+
cmp = list(
140140
naive_aamp_match(
141141
Q,
142142
T,
@@ -147,9 +147,13 @@ def test_aamp_naive_match_exact():
147147
)
148148
# To avoid sorting errors we first sort based on distance and then based on
149149
# indices
150-
right.sort(key=lambda x: (x[1], x[0]))
150+
cmp.sort(key=lambda x: (x[1], x[0]))
151151

152-
npt.assert_almost_equal(left, right)
152+
npt.assert_allclose(
153+
np.array(cmp).astype(np.float64),
154+
np.array(ref).astype(np.float64),
155+
atol=1.5e-07,
156+
)
153157

154158

155159
def test_aamp_naive_match_exclusion_zone():
@@ -166,11 +170,11 @@ def test_aamp_naive_match_exclusion_zone():
166170
excl_zone = m
167171

168172
for p in [1.0, 2.0, 3.0]:
169-
left = [
173+
ref = [
170174
[0, 3],
171175
[naive.distance(Q, T[7 : 7 + m], p=p), 7],
172176
]
173-
right = list(
177+
cmp = list(
174178
naive_aamp_match(
175179
Q,
176180
T,
@@ -181,9 +185,13 @@ def test_aamp_naive_match_exclusion_zone():
181185
)
182186
# To avoid sorting errors we first sort based on distance and then based on
183187
# indices
184-
right.sort(key=lambda x: (x[0], x[1]))
188+
cmp.sort(key=lambda x: (x[0], x[1]))
185189

186-
npt.assert_almost_equal(left, right)
190+
npt.assert_allclose(
191+
np.array(cmp).astype(np.float64),
192+
np.array(ref).astype(np.float64),
193+
atol=1.5e-07,
194+
)
187195

188196

189197
@pytest.mark.parametrize("Q, T", test_data)
@@ -193,23 +201,25 @@ def test_aamp_match(Q, T):
193201
max_distance = 0.3
194202

195203
for p in [1.0, 2.0, 3.0]:
196-
left = naive_aamp_match(
204+
ref = naive_aamp_match(
197205
Q,
198206
T,
199207
p=p,
200208
excl_zone=excl_zone,
201209
max_distance=max_distance,
202210
)
203211

204-
right = aamp_match(
212+
cmp = aamp_match(
205213
Q,
206214
T,
207215
p=p,
208216
max_matches=None,
209217
max_distance=max_distance,
210218
)
211219

212-
npt.assert_almost_equal(left, right)
220+
npt.assert_allclose(
221+
cmp.astype(np.float64), ref.astype(np.float64), atol=1.5e-07
222+
)
213223

214224

215225
@pytest.mark.parametrize("Q, T", test_data)
@@ -220,15 +230,15 @@ def test_aamp_match_T_subseq_isfinite(Q, T):
220230
T, T_subseq_isfinite = core.preprocess_non_normalized(T, len(Q))
221231

222232
for p in [1.0, 2.0, 3.0]:
223-
left = naive_aamp_match(
233+
ref = naive_aamp_match(
224234
Q,
225235
T,
226236
p=p,
227237
excl_zone=excl_zone,
228238
max_distance=max_distance,
229239
)
230240

231-
right = aamp_match(
241+
cmp = aamp_match(
232242
Q,
233243
T,
234244
T_subseq_isfinite,
@@ -237,7 +247,9 @@ def test_aamp_match_T_subseq_isfinite(Q, T):
237247
max_distance=max_distance,
238248
)
239249

240-
npt.assert_almost_equal(left, right)
250+
npt.assert_allclose(
251+
cmp.astype(np.float64), ref.astype(np.float64), atol=1.5e-07
252+
)
241253

242254

243255
def test_aamp_match_query_idx():
@@ -248,12 +260,12 @@ def test_aamp_match_query_idx():
248260

249261
# `mass_absolute` zeroes the self-match distance when told where `Q` lives.
250262
D = core.mass_absolute(Q, T, query_idx=query_idx)
251-
npt.assert_almost_equal(D[query_idx], 0.0)
263+
npt.assert_allclose(D[query_idx], 0.0, atol=1.5e-07)
252264

253265
# A `Q` that is not the subsequence at `query_idx` must still return the
254266
# self-match first, and must warn, exactly as `stumpy.match` does.
255267
with pytest.warns(UserWarning):
256268
out = aamp_match(Q + 0.5, T, query_idx=query_idx, max_distance=1.0)
257269

258270
assert out[0, 1] == query_idx
259-
npt.assert_almost_equal(out[0, 0], 0.0)
271+
npt.assert_allclose(out[0, 0], 0.0, atol=1.5e-07)

0 commit comments

Comments
 (0)