1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
"""Unit tests for utils"""
import collections
import pytest
import numpy as np
import mir_eval
from mir_eval import util
def test_interpolate_intervals():
"""Check that an interval set is interpolated properly, with boundaries
conditions and out-of-range values.
"""
labels = list("abc")
intervals = np.array([(n, n + 1.0) for n in range(len(labels))])
time_points = [-1.0, 0.1, 0.9, 1.0, 2.3, 4.0]
expected_ans = ["N", "a", "a", "b", "c", "N"]
assert (
util.interpolate_intervals(intervals, labels, time_points, "N") == expected_ans
)
def test_interpolate_intervals_gap():
"""Check that an interval set is interpolated properly, with gaps."""
labels = list("abc")
intervals = np.array([[0.5, 1.0], [1.5, 2.0], [2.5, 3.0]])
time_points = [0.0, 0.75, 1.25, 1.75, 2.25, 2.75, 3.5]
expected_ans = ["N", "a", "N", "b", "N", "c", "N"]
assert (
util.interpolate_intervals(intervals, labels, time_points, "N") == expected_ans
)
@pytest.mark.xfail(raises=ValueError)
def test_interpolate_intervals_badtime():
"""Check that interpolate_intervals throws an exception if
input is unordered.
"""
labels = list("abc")
intervals = np.array([(n, n + 1.0) for n in range(len(labels))])
time_points = [-1.0, 0.1, 0.9, 0.8, 2.3, 4.0]
mir_eval.util.interpolate_intervals(intervals, labels, time_points)
def test_intervals_to_samples():
"""Check that an interval set is sampled properly, with boundaries
conditions and out-of-range values.
"""
labels = list("abc")
intervals = np.array([(n, n + 1.0) for n in range(len(labels))])
expected_times = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
expected_labels = ["a", "a", "b", "b", "c", "c"]
result = util.intervals_to_samples(
intervals, labels, offset=0, sample_size=0.5, fill_value="N"
)
assert result[0] == expected_times
assert result[1] == expected_labels
expected_times = [0.25, 0.75, 1.25, 1.75, 2.25, 2.75]
expected_labels = ["a", "a", "b", "b", "c", "c"]
result = util.intervals_to_samples(
intervals, labels, offset=0.25, sample_size=0.5, fill_value="N"
)
assert result[0] == expected_times
assert result[1] == expected_labels
def test_intersect_files():
"""Check that two non-identical produce correct results."""
flist1 = ["/a/b/abc.lab", "/c/d/123.lab", "/e/f/xyz.lab"]
flist2 = ["/g/h/xyz.npy", "/i/j/123.txt", "/k/l/456.lab"]
sublist1, sublist2 = util.intersect_files(flist1, flist2)
assert sublist1 == ["/e/f/xyz.lab", "/c/d/123.lab"]
assert sublist2 == ["/g/h/xyz.npy", "/i/j/123.txt"]
sublist1, sublist2 = util.intersect_files(flist1[:1], flist2[:1])
assert sublist1 == []
assert sublist2 == []
def test_merge_labeled_intervals():
"""Check that two labeled interval sequences merge correctly."""
x_intvs = np.array([[0.0, 0.44], [0.44, 2.537], [2.537, 4.511], [4.511, 6.409]])
x_labels = ["A", "B", "C", "D"]
y_intvs = np.array([[0.0, 0.464], [0.464, 2.415], [2.415, 4.737], [4.737, 6.409]])
y_labels = [0, 1, 2, 3]
expected_intvs = [
[0.0, 0.44],
[0.44, 0.464],
[0.464, 2.415],
[2.415, 2.537],
[2.537, 4.511],
[4.511, 4.737],
[4.737, 6.409],
]
expected_x_labels = ["A", "B", "B", "B", "C", "D", "D"]
expected_y_labels = [0, 0, 1, 2, 2, 2, 3]
new_intvs, new_x_labels, new_y_labels = util.merge_labeled_intervals(
x_intvs, x_labels, y_intvs, y_labels
)
assert new_x_labels == expected_x_labels
assert new_y_labels == expected_y_labels
assert new_intvs.tolist() == expected_intvs
# Check that invalid inputs raise a ValueError
y_intvs[-1, -1] = 10.0
with pytest.raises(ValueError):
util.merge_labeled_intervals(x_intvs, x_labels, y_intvs, y_labels)
def test_boundaries_to_intervals():
# Basic tests
boundaries = np.arange(10)
correct_intervals = np.array([np.arange(10 - 1), np.arange(1, 10)]).T
intervals = mir_eval.util.boundaries_to_intervals(boundaries)
assert np.all(intervals == correct_intervals)
def test_adjust_events():
# Test appending at the end
events = np.arange(1, 11)
labels = [str(n) for n in range(10)]
new_e, new_l = mir_eval.util.adjust_events(events, labels, 0.0, 11.0)
assert new_e[0] == 0.0
assert new_l[0] == "__T_MIN"
assert new_e[-1] == 11.0
assert new_l[-1] == "__T_MAX"
assert np.all(new_e[1:-1] == events)
assert new_l[1:-1] == labels
# Test trimming
new_e, new_l = mir_eval.util.adjust_events(events, labels, 0.0, 9.0)
assert new_e[0] == 0.0
assert new_l[0] == "__T_MIN"
assert new_e[-1] == 9.0
assert np.all(new_e[1:] == events[:-1])
assert new_l[1:] == labels[:-1]
def test_bipartite_match():
# This test constructs a graph as follows:
# v9 -- (u0)
# v8 -- (u0, u1)
# v7 -- (u0, u1, u2)
# ...
# v0 -- (u0, u1, ..., u9)
#
# This structure and ordering of this graph should force Hopcroft-Karp to
# hit each algorithm/layering phase
#
G = collections.defaultdict(list)
u_set = [f"u{_:d}" for _ in range(10)]
v_set = [f"v{_:d}" for _ in range(len(u_set) + 1)]
for i, u in enumerate(u_set):
for v in v_set[: -i - 1]:
G[v].append(u)
matching = util._bipartite_match(G)
# Make sure that each u vertex is matched
assert len(matching) == len(u_set)
# Make sure that there are no duplicate keys
lhs = {k for k in matching}
rhs = {matching[k] for k in matching}
assert len(matching) == len(lhs)
assert len(matching) == len(rhs)
# Finally, make sure that all detected edges are present in G
for k in matching:
v = matching[k]
assert v in G[k] or k in G[v]
def test_outer_distance_mod_n():
ref = [1.0, 2.0, 3.0]
est = [1.1, 6.0, 1.9, 5.0, 10.0]
expected = np.array(
[
[0.1, 5.0, 0.9, 4.0, 3.0],
[0.9, 4.0, 0.1, 3.0, 4.0],
[1.9, 3.0, 1.1, 2.0, 5.0],
]
)
actual = mir_eval.util._outer_distance_mod_n(ref, est)
assert np.allclose(actual, expected)
ref = [13.0, 14.0, 15.0]
est = [1.1, 6.0, 1.9, 5.0, 10.0]
expected = np.array(
[
[0.1, 5.0, 0.9, 4.0, 3.0],
[0.9, 4.0, 0.1, 3.0, 4.0],
[1.9, 3.0, 1.1, 2.0, 5.0],
]
)
actual = mir_eval.util._outer_distance_mod_n(ref, est)
assert np.allclose(actual, expected)
def test_match_events():
ref = [1.0, 2.0, 3.0]
est = [1.1, 6.0, 1.9, 5.0, 10.0]
expected = [(0, 0), (1, 2)]
actual = mir_eval.util.match_events(ref, est, 0.5)
assert actual == expected
ref = [1.0, 2.0, 3.0, 11.9]
est = [1.1, 6.0, 1.9, 5.0, 10.0, 0.0]
expected = [(0, 0), (1, 2), (3, 5)]
actual = mir_eval.util.match_events(
ref, est, 0.5, distance=mir_eval.util._outer_distance_mod_n
)
assert actual == expected
def test_fast_hit_windows():
ref = [1.0, 2.0, 3.0]
est = [1.1, 6.0, 1.9, 5.0, 10.0]
ref_fast, est_fast = mir_eval.util._fast_hit_windows(ref, est, 0.5)
ref_slow, est_slow = np.where(np.abs(np.subtract.outer(ref, est)) <= 0.5)
assert np.all(ref_fast == ref_slow)
assert np.all(est_fast == est_slow)
@pytest.mark.xfail(raises=ValueError)
@pytest.mark.parametrize(
"intervals",
[
# Test for ValueError when interval shape is invalid
np.array([[1.0], [2.5], [5.0]]),
# Test for ValueError when times are negative
np.array([[1.0, -2.0], [2.5, 3.0], [5.0, 6.0]]),
# Test for ValueError when duration is zero
np.array([[1.0, 2.0], [2.5, 2.5], [5.0, 6.0]]),
# Test for ValueError when duration is negative
np.array([[1.0, 2.0], [2.5, 1.5], [5.0, 6.0]]),
],
)
def test_validate_intervals(intervals):
mir_eval.util.validate_intervals(intervals)
@pytest.mark.xfail(raises=ValueError)
@pytest.mark.parametrize(
"events",
[
# Test for ValueError when max_time is violated
np.array([100.0, 100000.0]),
# Test for ValueError when events aren't 1-d arrays
np.array([[1.0, 2.0], [3.0, 4.0]]),
# Test for ValueError when event times are not increasing
np.array([1.0, 2.0, 5.0, 3.0]),
],
)
def test_validate_events(events):
mir_eval.util.validate_events(events)
@pytest.mark.xfail(raises=ValueError)
@pytest.mark.parametrize(
"freqs",
[
# Test for ValueError when max_freq is violated
np.array([100, 10000]),
# Test for ValueError when min_freq is violated
np.array([2, 200]),
# Test for ValueError when events aren't 1-d arrays
np.array([[100, 200], [300, 400]]),
# Test for ValueError when allow_negatives is false and negative values
# are passed
np.array([-100, 200]),
],
)
def test_validate_frequencies(freqs):
mir_eval.util.validate_frequencies(freqs, 5000, 20, allow_negatives=False)
@pytest.mark.xfail(raises=ValueError)
@pytest.mark.parametrize(
"freqs",
[
# Test for ValueError when max_freq is violated and allow_negatives=True
np.array([100, -100000]),
# Test for ValueError when min_freq is violated and allow_negatives=True
np.array([-2, 200]),
],
)
def test_validate_frequencies_negative(freqs):
mir_eval.util.validate_frequencies(freqs, 5000, 20, allow_negatives=True)
def test_has_kwargs():
def __test(target, f):
assert target == mir_eval.util.has_kwargs(f)
def f1(_):
return None
def f2(_=5):
return None
def f3(*_):
return None
def f4(_, **kw):
return None
def f5(_=5, **kw):
return None
assert not mir_eval.util.has_kwargs(f1)
assert not mir_eval.util.has_kwargs(f2)
assert not mir_eval.util.has_kwargs(f3)
assert mir_eval.util.has_kwargs(f4)
assert mir_eval.util.has_kwargs(f5)
@pytest.mark.parametrize(
"x,labels,x_true,lab_true",
[
(
np.asarray([[10, 20], [0, 10]]),
["a", "b"],
np.asarray([[0, 10], [10, 20]]),
["b", "a"],
),
(
np.asarray([[0, 10], [10, 20]]),
["b", "a"],
np.asarray([[0, 10], [10, 20]]),
["b", "a"],
),
],
)
def test_sort_labeled_intervals_with_labels(x, labels, x_true, lab_true):
xs, ls = mir_eval.util.sort_labeled_intervals(x, labels)
assert np.allclose(xs, x_true)
assert ls == lab_true
@pytest.mark.parametrize(
"x,x_true",
[
(np.asarray([[10, 20], [0, 10]]), np.asarray([[0, 10], [10, 20]])),
(np.asarray([[0, 10], [10, 20]]), np.asarray([[0, 10], [10, 20]])),
],
)
def test_sort_labeled_intervals_without_labels(x, x_true):
xs = mir_eval.util.sort_labeled_intervals(x)
assert np.allclose(xs, x_true)
|