-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathevaluate_F1_rc.py
56 lines (46 loc) · 1.32 KB
/
evaluate_F1_rc.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
import json
dev_file = './ccl-cfn/cfn-dev.json'
predictions_file = './ccl-cfn/result/task3_dev.json'
with open(dev_file, 'r') as f:
dev_data = json.load(f)
with open(predictions_file, 'r') as f:
predictions_data = json.load(f)
# assert len(dev_data) == len(predictions_data)
j = 0
total_TP = 0
total_FP = 0
total_FN = 0
preds = {}
labels = {}
for i, span in enumerate(predictions_data):
preds.setdefault(span[0], set())
preds[span[0]].add((span[1], span[2], span[3]))
for i, data in enumerate(dev_data):
labels.setdefault(data['task_id'], set())
for span in data['cfn_spans']:
labels[data['task_id']].add((span[0], span[1], span[2]))
for taskid in labels:
TP = 0
FP = 0
FN = 0
if taskid not in preds:
FN += len(labels[taskid])
total_FN += FN
continue
for pred in preds[taskid]:
if pred in labels[taskid]:
TP += 1
else:
FP += 1
for label in labels[taskid]:
if label not in preds[taskid]:
FN += 1
total_TP += TP
total_FP += FP
total_FN += FN
# break
print(total_TP, total_FP, total_FN)
precision = total_TP / (total_TP + total_FP)
recall = total_TP / (total_TP + total_FN)
F1 = 2 * precision * recall / (precision + recall)
print(f'precision: {precision}, recall: {recall}, F1: {F1}')