-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_measures.py
167 lines (122 loc) · 5.1 KB
/
test_measures.py
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
import torch
from measures import (
binary_accuracy,
convergence_score,
cumulative_average,
normalized_editdistance,
pairwise_dedup,
production_similarity,
semantic_difference,
)
EPS = 1e-5
def test_semantic_difference():
# maximum
assert semantic_difference((1, 180), (2, 0)) == 2.0
# middle
assert semantic_difference((2, 135), (1, 0)) == 1.75
assert semantic_difference((2, 90), (1, 0)) == 1.5
assert semantic_difference((2, 90), (1, 45)) == 1.25
assert semantic_difference((1, 180), (1, 0)) == 1.0
assert semantic_difference((1, 180), (1, 90)) == 0.5
assert semantic_difference((3, 90), (3, 0)) == 0.5
assert semantic_difference((1, 45), (1, 90)) == 0.25
# minimum
assert semantic_difference((1, 180), (1, 180)) == 0.0
def test_binary_accuracy():
acc, correct = binary_accuracy(["a", "b", "c"], ["a", "b", "c"])
assert acc == 1.0
assert all(correct)
acc, correct = binary_accuracy(["aa", "bbb", "c"], ["aa", "bbb", "c"])
assert acc == 1.0
assert all(correct)
acc, correct = binary_accuracy(["aa", "bxb"], ["aa", "bbb"])
assert acc == 0.5
assert correct[0] == 1
assert correct[1] == 0
def test_normalized_editdistance():
assert normalized_editdistance("a", "a") == 0
assert normalized_editdistance("a", "b") == 1
assert normalized_editdistance("abcd", "ab") == 2 / 4
assert abs(normalized_editdistance("banana", "bahama") - 2 / 6) < EPS
assert normalized_editdistance("cdefgah", "cdefgah") == 0
def test_production_similarity():
assert production_similarity("a", "a") == 1
assert production_similarity("a", "b") == 0
assert production_similarity("abcd", "ab") == 2 / 4
assert abs(production_similarity("banana", "bahama") - 4 / 6) < EPS
assert production_similarity("cdefgah", "cdefgah") == 1
def test_pairwise_dedup():
xs = []
result = list(pairwise_dedup(lambda a, b: a - b, xs))
assert result == []
xs = [20, 10]
result = list(pairwise_dedup(lambda a, b: a - b, xs))
assert result == [10]
xs = [5, 4, 3]
result = list(pairwise_dedup(lambda a, b: a - b, xs))
assert result == [1, 2, 1]
def test_pairwise_dedup_editdistance():
"""These tests reflect the behaviour of the orig R script
> allpairslev(c("aa", "ab", "ac", "de"))
[1] 0.5 0.5 1.0 0.5 1.0 1.0
"""
xs = ["aa", "bb"]
result = list(pairwise_dedup(normalized_editdistance, xs))
assert result == [1.0]
xs = ["aa", "bb", "cc"]
result = list(pairwise_dedup(normalized_editdistance, xs))
assert result == [1.0, 1.0, 1.0]
xs = ["aa", "ab", "ac", "de"]
result = list(pairwise_dedup(normalized_editdistance, xs))
assert result == [0.5, 0.5, 1.0, 0.5, 1.0, 1.0]
def test_cumulative_average():
x = [10, 20, 30, 40, 50]
ca = cumulative_average(x)
assert ca == 30
x = range(1, 100)
ca = cumulative_average(x)
assert ca == 50
def test_convergence_score_with_prodsim():
msgs = ["aaa", "aaa", "aaa", "aaa"]
assert convergence_score(msgs, metric="production_similarity") == 1.0
msgs = ["a", "b"]
assert convergence_score(msgs, metric="production_similarity") == 0.0
msgs = ["aa", "bb"]
assert convergence_score(msgs, metric="production_similarity") == 0.0
msgs = ["ab", "bb"]
assert convergence_score(msgs, metric="production_similarity") == 0.5
msgs = ["a", "b", "c", "d"]
assert convergence_score(msgs, metric="production_similarity") == 0.0
msgs = ["a", "a", "b", "b"]
assert abs(convergence_score(msgs, metric="production_similarity") - (1 / 3)) < 1e-8
msgs = ["aa", "aa", "bb", "bb"]
assert abs(convergence_score(msgs, metric="production_similarity") - (1 / 3)) < 1e-8
msgs = ["aa", "aa", "bb", "bb", "cc", "cc"]
assert abs(convergence_score(msgs, metric="production_similarity") - 0.20) < 1e-8
msgs = ["aa", "aa", "bb", "bb", "ccaa", "ccaa"]
assert abs(convergence_score(msgs, metric="production_similarity") - (1 / 3)) < 1e-8
def test_convergence_score_with_normalized_editdistance():
msgs = ["aaa", "aaa", "aaa", "aaa"]
assert convergence_score(msgs, metric="normalized_editdistance") == 0.0
msgs = ["a", "b"]
assert convergence_score(msgs, metric="normalized_editdistance") == 1.0
msgs = ["aa", "bb"]
assert convergence_score(msgs, metric="normalized_editdistance") == 1.0
msgs = ["ab", "bb"]
assert convergence_score(msgs, metric="normalized_editdistance") == 0.5
msgs = ["a", "b", "c", "d"]
assert convergence_score(msgs, metric="normalized_editdistance") == 1.0
msgs = ["a", "a", "b", "b"]
assert (
abs(convergence_score(msgs, metric="normalized_editdistance") - (2 / 3)) < 1e-8
)
msgs = ["aa", "aa", "bb", "bb"]
assert (
abs(convergence_score(msgs, metric="normalized_editdistance") - (2 / 3)) < 1e-8
)
msgs = ["aa", "aa", "bb", "bb", "cc", "cc"]
assert abs(convergence_score(msgs, metric="normalized_editdistance") - 0.80) < 1e-8
msgs = ["aa", "aa", "bb", "bb", "ccaa", "ccaa"]
assert (
abs(convergence_score(msgs, metric="normalized_editdistance") - (2 / 3)) < 1e-8
)