-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_metrics.py
151 lines (128 loc) · 5.1 KB
/
test_metrics.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
import unittest
from metric.seqeval_metric import Seqeval
import torch
from torch import tensor
class TestMetric(unittest.TestCase):
def AssertResultClose(self, actual, expected, rtol=1e-4, atol=1e-4):
self.assertEqual(set(actual.keys()), set(expected.keys()))
for k, v in expected.items():
torch.testing.assert_close(actual=actual[k],
expected=v,
rtol=rtol,
atol=atol)
def test_1(self):
metric = Seqeval(labels=["MISC", "PER"])
predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'],
['B-PER', 'I-PER', 'O']]
references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'],
['B-PER', 'I-PER', 'O']]
actual = metric(predictions, references)
expected = {
'MISC_precision': tensor(0.),
'PER_precision': tensor(1.),
'MISC_recall': tensor(0.),
'PER_recall': tensor(1.),
'MISC_f1': tensor(0.),
'PER_f1': tensor(1.),
'MISC_number': tensor(1),
'PER_number': tensor(1),
'overall_precision': tensor(0.5000),
'overall_recall': tensor(0.5000),
'overall_f1': tensor(0.5000)
}
self.AssertResultClose(actual, expected)
def test_2(self):
"""
Evaluation test is performed for the following dataset.
https://www.clips.uantwerpen.be/conll2000/chunking/output.html
To reproduce:
1) Download https://www.clips.uantwerpen.be/conll2000/chunking/output.txt.gz
2) Exctact files from archive
3) Move `output.txt` to `./data/`
"""
def load_test_output(path: str):
with open(path) as inp:
references = []
predictions = []
reference = []
prediction = []
for line in inp:
row = line.strip()
if row:
token, pos, y_true, y_pred = row.split()
reference.append(y_true)
prediction.append(y_pred)
else:
references.append(reference)
predictions.append(prediction)
reference = []
prediction = []
if reference:
references.append(reference)
predictions.append(prediction)
return references, predictions
metric = Seqeval(labels=["ADJP", "ADVP", "NP", "PP", "SBAR", "VP"])
references, predictions = load_test_output(path='./data/output.txt')
actual = metric(predictions, references)
expected = {
'ADJP_precision': tensor(0.),
'ADVP_precision': tensor(0.4545),
'NP_precision': tensor(0.6498),
'PP_precision': tensor(0.8318),
'SBAR_precision': tensor(0.6667),
'VP_precision': tensor(0.6900),
'ADJP_recall': tensor(0.),
'ADVP_recall': tensor(0.6250),
'NP_recall': tensor(0.7863),
'PP_recall': tensor(0.9889),
'SBAR_recall': tensor(0.3333),
'VP_recall': tensor(0.7931),
'ADJP_f1': tensor(0.),
'ADVP_f1': tensor(0.5263),
'NP_f1': tensor(0.7116),
'PP_f1': tensor(0.9036),
'SBAR_f1': tensor(0.4444),
'VP_f1': tensor(0.7380),
'ADJP_number': tensor(6),
'ADVP_number': tensor(8),
'NP_number': tensor(262),
'PP_number': tensor(90),
'SBAR_number': tensor(6),
'VP_number': tensor(87),
'overall_precision': tensor(0.6883),
'overall_recall': tensor(0.8083),
'overall_f1': tensor(0.7435)
}
self.AssertResultClose(actual, expected)
def test_3(self):
metric = Seqeval(labels=["NP"])
references = [['B-NP', 'I-NP', 'O']]
predictions = [['I-NP', 'I-NP', 'O']]
actual = metric(predictions, references)
expected = {
'NP_precision': tensor(1.),
'NP_recall': tensor(1.),
'NP_f1': tensor(1.),
'NP_number': tensor(1),
'overall_precision': tensor(1.),
'overall_recall': tensor(1.),
'overall_f1': tensor(1.)
}
self.AssertResultClose(actual, expected)
def test_4(self):
metric = Seqeval(labels=["NP"], mode='strict', scheme='IOB2')
references = [['B-NP', 'I-NP', 'O']]
predictions = [['I-NP', 'I-NP', 'O']]
actual = metric(predictions, references)
expected = {
'NP_precision': tensor(0.),
'NP_recall': tensor(0.),
'NP_f1': tensor(0.),
'NP_number': tensor(1),
'overall_precision': tensor(0.),
'overall_recall': tensor(0.),
'overall_f1': tensor(0.)
}
self.AssertResultClose(actual, expected)
if __name__ == "__main__":
unittest.main()