-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtest_annotation_functions.py
More file actions
66 lines (56 loc) · 2.36 KB
/
Copy pathtest_annotation_functions.py
File metadata and controls
66 lines (56 loc) · 2.36 KB
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
import numpy as np
import pytest
from types import SimpleNamespace
import pyreason.scripts.annotation_functions.annotation_functions as af
def _interval(lower, upper):
return SimpleNamespace(lower=lower, upper=upper)
def _example_annotations():
"""Return sample annotations and weights used across tests."""
annotations = [
[_interval(0.1, 0.2), _interval(0.3, 0.4)],
[_interval(0.5, 0.6)],
]
weights = [1.0, 2.0]
return annotations, weights
def test_get_weighted_sum_modes():
annotations, weights = _example_annotations()
lower_sum, cnt_l = af._get_weighted_sum(annotations, weights, mode="lower")
upper_sum, cnt_u = af._get_weighted_sum(annotations, weights, mode="upper")
invalid_sum, cnt_i = af._get_weighted_sum(annotations, weights, mode="invalid")
np.testing.assert_allclose(lower_sum, np.array([0.4, 1.0]))
np.testing.assert_allclose(upper_sum, np.array([0.6, 1.2]))
np.testing.assert_allclose(invalid_sum, np.array([0.0, 0.0]))
assert cnt_l == cnt_u == cnt_i == 3
@pytest.mark.parametrize(
"lower, upper, expected",
[
(0.9, 0.8, (0, 1)),
(1.2, 1.5, (1, 1)),
],
)
def test_check_bound(lower, upper, expected):
assert af._check_bound(lower, upper) == expected
def test_average():
annotations, weights = _example_annotations()
result = af.average(annotations, weights)
assert isinstance(result, tuple), f"expected tuple, got {type(result)}"
assert result[0] == pytest.approx(1.4 / 3)
assert result[1] == pytest.approx(0.6)
def test_average_lower():
annotations, weights = _example_annotations()
result = af.average_lower(annotations, weights)
assert isinstance(result, tuple), f"expected tuple, got {type(result)}"
assert result[0] == pytest.approx(1.4 / 3)
assert result[1] == pytest.approx(0.6)
def test_maximum():
annotations, weights = _example_annotations()
result = af.maximum(annotations, weights)
assert isinstance(result, tuple), f"expected tuple, got {type(result)}"
assert result[0] == pytest.approx(1.0)
assert result[1] == pytest.approx(1.0)
def test_minimum():
annotations, weights = _example_annotations()
result = af.minimum(annotations, weights)
assert isinstance(result, tuple), f"expected tuple, got {type(result)}"
assert result[0] == pytest.approx(0.4)
assert result[1] == pytest.approx(0.6)