-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
229 lines (171 loc) · 5.5 KB
/
eval.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
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
# -*- coding: utf-8 -*-
"""
File name: eval.py
Project: Family History Extraction
Description: evaluation script of the OHNLP/BioCreative 2018 Task 1: Family history extraction
Author: Sijia Liu (liu dot sijia at mayo dot edu)
"""
from collections import defaultdict
import argparse
import sys
def parse_s1_output(output_path):
"""
Parse GS or output file for subtask 1 (entity identification)
:param output_path:
:return:
"""
out_fm = set()
out_ob = defaultdict(set)
with open(output_path) as f:
for l in f:
splits = l.strip().split('\t')
if len(splits) == 4:
doc, _, fm, side = splits
out_fm |= set([(doc, fm, side)])
elif len(splits) == 3:
doc, _, ob = splits
out_ob[doc] |= set([ob.lower()])
else:
print("Number of fields should be 3 or 4. Got {} instead: ".format(len(splits)))
print("\"{}\"\t->\t{}".format(l.strip(), splits))
sys.exit(0)
return out_fm, out_ob
def parse_s2_output(output_path):
"""
Parse GS or output file for subtask 2 (family history extraction)
:param output_path:
:return:
"""
out_fm = set()
out_ob = defaultdict(set)
with open(output_path) as f:
for l in f:
splits = l.strip().split('\t')
'''
Example:
doc_1 Son NA LivingStatus 4
doc_1 Cousin Paternal Observation choreic dysphonia
'''
if splits[3] == 'LivingStatus':
doc, fm, side, _ , status = splits
out_fm |= set([(doc, fm, side, status)])
elif splits[3] == 'Observation':
doc, fm, side, _ , ob = splits
out_ob[(doc, fm, side)] |= set([ob.lower()])
return out_fm, out_ob
def get_pr_f1(tp, fp, fn):
print("TP\t\tFP\t\tFN\t\t")
print("{}\t{} \t{}".format(tp, fp, fn))
precision = tp / float(tp + fp)
recall = tp / float(tp + fn)
f1 = 2 * precision * recall / (precision + recall)
print("Prevision\tRecall\tF1")
print("{:.4f}\t{:.4f} \t{:.4f}".format(precision, recall, f1))
print()
def calculate_s1(gs_tsv, pred_tsv, verbose):
"""
Calculate system performance of subtask 1
:param gs_tsv:
:param pred_tsv:
:param verbose:
:return: None. The performance are printed in console output
"""
gs_fm, gs_ob = parse_s1_output(gs_tsv)
pred_fm, pred_ob = parse_s1_output(pred_tsv)
tp_fm = 0
fp_fm = 0
for elm in pred_fm:
if elm in gs_fm:
tp_fm += 1
else:
fp_fm += 1
fn_fm = len(gs_fm) - tp_fm
if verbose:
print("FM: ")
get_pr_f1(tp_fm, fp_fm, fn_fm)
tp_ob = 0
total_pred_ob = 0
for doc in pred_ob:
pred_ob_set = pred_ob[doc]
gs_ob_set = gs_ob[doc]
for gob in gs_ob_set:
for pob in pred_ob_set:
if set(pob.split()) & set(gob.split()):
tp_ob += 1
break
total_pred_ob += len(pred_ob_set)
total_gs_ob = 0
for doc in gs_ob:
total_gs_ob += len(gs_ob[doc])
fp_ob = total_pred_ob - tp_ob
fn_ob = total_gs_ob - tp_ob
if verbose:
print("OB: ")
get_pr_f1(tp_ob, fp_ob, fn_ob)
tp = tp_ob + tp_fm
fp = fp_ob + fp_fm
fn = fn_ob + fn_fm
print("Overall:")
get_pr_f1(tp, fp, fn)
def calculate_s2(gs_tsv, pred_tsv, verbose):
"""
Calculate system performance of subtask 2
:param gs_tsv:
:param pred_tsv:
:param verbose:
:return: None. The performance are printed in console output
"""
gs_fm, gs_ob = parse_s2_output(gs_tsv)
pred_fm, pred_ob = parse_s2_output(pred_tsv)
tp_fm = 0
fp_fm = 0
for elm in pred_fm:
if elm in gs_fm:
tp_fm += 1
else:
fp_fm += 1
fn_fm = len(gs_fm) - tp_fm
if verbose:
print("FM: ")
get_pr_f1(tp_fm, fp_fm, fn_fm)
tp_ob = 0
total_pred_ob = 0
for key in pred_ob:
pred_ob_set = pred_ob[key]
gs_ob_set = gs_ob[key]
for gob in gs_ob_set:
for pob in pred_ob_set:
if set(pob.split()) & set(gob.split()):
tp_ob += 1
break
total_pred_ob += len(pred_ob_set)
total_gs_ob = 0
for key in gs_ob:
total_gs_ob += len(gs_ob[key])
fp_ob = total_pred_ob - tp_ob
fn_ob = total_gs_ob - tp_ob
if verbose:
print("OB: ")
get_pr_f1(tp_ob, fp_ob, fn_ob)
tp = tp_ob + tp_fm
fp = fp_ob + fp_fm
fn = fn_ob + fn_fm
print("Overall:")
get_pr_f1(tp, fp, fn)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("subtask", type=int, help="selection of subtasks: \"1\" or \"2\"")
parser.add_argument("-v", "--verbose",
help="includes the performance of " +
"family members and observations in the output",
action="store_true")
parser.add_argument("gs", help="Gold Standard file of the selected subtask")
parser.add_argument("pred", help="Prediction file of the selected subtask")
args = parser.parse_args()
if args.subtask == 1:
calculate_s1(args.gs, args.pred, args.verbose)
elif args.subtask == 2:
calculate_s2(args.gs, args.pred, args.verbose)
else:
print("Please select the subtask. ")
parser.print_help()