-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathevaluate.py
345 lines (295 loc) · 13 KB
/
evaluate.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""Official evaluation script for CJRC.
The code is based partially on CoQA evaluation script.
"""
import argparse
import json
import re
import string
import sys
from collections import Counter, OrderedDict
import logging
OPTS = None
logger = logging.getLogger(__name__)
class CJRCEvaluator():
def __init__(self, gold_file):
self.gold_data, self.id_to_domain = CJRCEvaluator.gold_answers_to_dict(gold_file)
@staticmethod
def gold_answers_to_dict(gold_file):
dataset = json.load(open(gold_file))
gold_dict = {}
id_to_domain = {}
for story in dataset['data']:
qas = story["paragraphs"][0]["qas"]
for qa in qas:
qid = qa['id']
gold_answers = []
if not qa["answers"]:
gold_answers = ['']
for answer in qa["answers"]:
gold_answers.append(answer["text"])
if qid in gold_dict:
sys.stderr.write("Gold file has duplicate stories: {}".format(qid))
gold_dict[qid] = gold_answers
id_to_domain[qid] = story["domain"]
return gold_dict, id_to_domain
@staticmethod
def preds_to_dict(pred_file):
preds = json.load(open(pred_file))
pred_dict = {}
for pred in preds:
pred_dict[pred['id']] = pred['answer']
return pred_dict
@staticmethod
def normalize_answer(s):
"""Lower text and remove punctuation, storys and extra whitespace."""
def remove_punc(text):
return "".join(ch for ch in text if ch.isdigit() or ch.isalpha())
def lower(text):
return text.lower()
return remove_punc(lower(s))
@staticmethod
def get_tokens(s):
if not s: return []
return list(CJRCEvaluator.normalize_answer(s))
@staticmethod
def compute_exact(a_gold, a_pred):
return int(CJRCEvaluator.normalize_answer(a_gold) == CJRCEvaluator.normalize_answer(a_pred))
@staticmethod
def compute_f1(a_gold, a_pred):
gold_toks = CJRCEvaluator.get_tokens(a_gold)
pred_toks = CJRCEvaluator.get_tokens(a_pred)
common = Counter(gold_toks) & Counter(pred_toks)
num_same = sum(common.values())
if len(gold_toks) == 0 or len(pred_toks) == 0:
# If either is no-answer, then F1 is 1 if they agree, 0 otherwise
# if len(gold_toks) == 0:
# print("---"*10)
# print('gold: ', a_gold)
# print('pred: ', a_pred)
return int(gold_toks == pred_toks)
if num_same == 0:
return 0
precision = 1.0 * num_same / len(pred_toks)
recall = 1.0 * num_same / len(gold_toks)
f1 = (2 * precision * recall) / (precision + recall)
# if f1 < 0.5:
# print("---"*10)
# print(f1)
# print('gold', a_gold)
# print('pred', a_pred)
return f1
@staticmethod
def _compute_turn_score(a_gold_list, a_pred):
f1_sum = 0.0
em_sum = 0.0
if len(a_gold_list) > 1:
for i in range(len(a_gold_list)):
# exclude the current answer
gold_answers = a_gold_list[0:i] + a_gold_list[i + 1:]
em_sum += max(CJRCEvaluator.compute_exact(a, a_pred) for a in gold_answers)
f1_sum += max(CJRCEvaluator.compute_f1(a, a_pred) for a in gold_answers)
else:
em_sum += max(CJRCEvaluator.compute_exact(a, a_pred) for a in a_gold_list)
f1_sum += max(CJRCEvaluator.compute_f1(a, a_pred) for a in a_gold_list)
return {'em': em_sum / max(1, len(a_gold_list)), 'f1': f1_sum / max(1, len(a_gold_list))}
def compute_turn_score(self, qid, a_pred):
''' This is the function what you are probably looking for. a_pred is the answer string your model predicted. '''
a_gold_list = self.gold_data[qid]
return CJRCEvaluator._compute_turn_score(a_gold_list, a_pred)
def get_raw_scores(self, pred_data):
''''Returns a dict with score'''
exact_scores = {}
f1_scores = {}
civil_yes_all = 0
civil_yes_right = 0
civil_no_all = 0
civil_no_right = 0
civil_null_all = 0
civil_null_right = 0
civil_wrong_cls = 0
yes_all = 0
yes_right = 0
no_all = 0
no_right = 0
null_all = 0
null_right = 0
wrong_cls = 0
for qid in self.gold_data:
domain = self.id_to_domain[qid]
if qid not in pred_data:
sys.stderr.write('Missing prediction for {}\n'.format(qid))
continue
a_pred = pred_data[qid]
scores = self.compute_turn_score(qid, a_pred)
# Take max over all gold answers
exact_scores[qid] = scores['em']
f1_scores[qid] = scores['f1']
if domain == 'civil':
if self.gold_data[qid][0] == 'YES':
civil_yes_all += 1
if self.gold_data[qid][0] == a_pred:
civil_yes_right += 1
# else:
# print(qid)
# print('YES vs '+a_pred)
if self.gold_data[qid][0] == 'NO':
civil_no_all += 1
if self.gold_data[qid][0] == a_pred:
civil_no_right += 1
# else:
# print(qid)
# print('NO vs '+a_pred)
if self.gold_data[qid][0] == '':
civil_null_all += 1
if a_pred == '':
civil_null_right += 1
if self.gold_data[qid][0] not in ['YES','NO',''] and a_pred in ['YES','NO','']:
civil_wrong_cls += 1
else:
if self.gold_data[qid][0] == 'YES':
yes_all += 1
if self.gold_data[qid][0] == a_pred:
yes_right += 1
# else:
# print(qid)
# print('YES vs '+a_pred)
if self.gold_data[qid][0] == 'NO':
no_all += 1
if self.gold_data[qid][0] == a_pred:
no_right += 1
# else:
# print(qid)
# print('NO vs '+a_pred)
if self.gold_data[qid][0] == '':
null_all += 1
if a_pred == '':
null_right += 1
if self.gold_data[qid][0] not in ['YES', 'NO', ''] and a_pred in ['YES', 'NO', '']:
wrong_cls += 1
print('t')
logger.info("civil...")
# logger.info('yes-right:{}, yes-all:{}'.format(civil_yes_right, civil_yes_all))
logger.info('yes-right:{:0.2f}'.format(civil_yes_right/civil_yes_all))
# logger.info('no-right:{}, no-all:{}'.format(civil_no_right, civil_no_all))
logger.info('no-right:{:0.2f}'.format(civil_no_right/civil_no_all))
# logger.info('null-right:{}, null-all:{}'.format(civil_null_right, civil_null_all))
logger.info('null-right:{:0.2f}'.format(civil_null_right/civil_null_all))
logger.info('wrong_cls:{}'.format(civil_wrong_cls))
logger.info('criminal...')
# logger.info('yes-right:{}, yes-all:{}'.format(yes_right, yes_all))
logger.info('yes-right:{:0.2f}'.format(yes_right/yes_all))
# logger.info('no-right:{}, no-all:{}'.format(no_right, no_all))
logger.info('no-right:{:0.2f}'.format(no_right/no_all))
# logger.info('null-right:{}, null-all:{}'.format(null_right, null_all))
logger.info('null-right:{:0.2f}'.format(null_right/null_all))
logger.info('wrong_cls:{}'.format(wrong_cls))
return exact_scores, f1_scores
def get_raw_scores_human(self):
''''Returns a dict with score'''
exact_scores = {}
f1_scores = {}
for qid in self.gold_data:
f1_sum = 0.0
em_sum = 0.0
if len(self.gold_data[qid]) > 1:
for i in range(len(self.gold_data[qid])):
# exclude the current answer
gold_answers = self.gold_data[qid][0:i] + self.gold_data[qid][i + 1:]
em_sum += max(CJRCEvaluator.compute_exact(a, self.gold_data[qid][i]) for a in gold_answers)
f1_sum += max(CJRCEvaluator.compute_f1(a, self.gold_data[qid][i]) for a in gold_answers)
else:
exit("Gold answers should be multiple: {}={}".format(qid, self.gold_data[qid]))
exact_scores[qid] = em_sum / len(self.gold_data[qid])
f1_scores[qid] = f1_sum / len(self.gold_data[qid])
return exact_scores, f1_scores
def human_performance(self):
exact_scores, f1_scores = self.get_raw_scores_human()
return self.get_domain_scores(exact_scores, f1_scores)
def model_performance(self, pred_data):
exact_scores, f1_scores = self.get_raw_scores(pred_data)
return self.get_domain_scores(exact_scores, f1_scores)
def get_domain_scores(self, exact_scores, f1_scores):
domains = {"civil": Counter(), "criminal": Counter()}
for qid in self.gold_data:
domain = self.id_to_domain[qid]
domains[domain]['em_total'] += exact_scores.get(qid, 0)
domains[domain]['f1_total'] += f1_scores.get(qid, 0)
domains[domain]['qa_count'] += 1
scores = OrderedDict()
civil_em_total = domains["civil"]["em_total"]
civil_f1_total = domains["civil"]["f1_total"]
civil_turn_count = domains["civil"]["qa_count"]
criminal_em_total = domains["criminal"]["em_total"]
criminal_f1_total = domains["criminal"]["f1_total"]
criminal_turn_count = domains["criminal"]["qa_count"]
em_total = civil_em_total + criminal_em_total
f1_total = civil_f1_total + criminal_f1_total
turn_count = civil_turn_count + criminal_turn_count
scores["civil"] = {'em': round(civil_em_total / max(1, civil_turn_count) * 100, 1),
'f1': round(civil_f1_total / max(1, civil_turn_count) * 100, 1),
'qas': civil_turn_count}
scores["criminal"] = {'em': round(criminal_em_total / max(1, criminal_turn_count) * 100, 1),
'f1': round(criminal_f1_total / max(1, criminal_turn_count) * 100, 1),
'qas': criminal_turn_count}
scores["overall"] = {'em': round(em_total / max(1, turn_count) * 100, 1),
'f1': round(f1_total / max(1, turn_count) * 100, 1),
'qas': turn_count}
return scores
def parse_args():
parser = argparse.ArgumentParser('Official evaluation script for CJRC based on CoQA.')
parser.add_argument('--data-file', dest="data_file", help='Input data JSON file.')
parser.add_argument('--pred-file', dest="pred_file", help='Model predictions.')
parser.add_argument('--out-file', '-o', metavar='eval.json',
help='Write accuracy metrics to file (default is stdout).')
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--human', dest="human", action='store_true')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
return parser.parse_args()
def find_best_thresh(preds, scores, na_probs, qid_to_has_ans):
num_no_ans = sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k])
cur_score = num_no_ans
best_score = cur_score
best_thresh = 0.0
qid_list = sorted(na_probs, key=lambda k: na_probs[k])
for i, qid in enumerate(qid_list):
if qid not in scores: continue
if qid_to_has_ans[qid]:
diff = scores[qid]
else:
if preds[qid]:
diff = -1
else:
diff = 0
cur_score += diff
if cur_score > best_score:
best_score = cur_score
best_thresh = na_probs[qid]
return 100.0 * best_score / len(scores), best_thresh
def find_all_best_thresh(main_eval, preds, exact_raw, f1_raw, na_probs, qid_to_has_ans):
best_exact, exact_thresh = find_best_thresh(preds, exact_raw, na_probs, qid_to_has_ans)
best_f1, f1_thresh = find_best_thresh(preds, f1_raw, na_probs, qid_to_has_ans)
main_eval['best_exact'] = best_exact
main_eval['best_exact_thresh'] = exact_thresh
main_eval['best_f1'] = best_f1
main_eval['best_f1_thresh'] = f1_thresh
def main():
evaluator = CJRCEvaluator(OPTS.data_file)
if OPTS.human:
res = evaluator.human_performance()
print(res)
if OPTS.pred_file:
with open(OPTS.pred_file) as f:
pred_data = CJRCEvaluator.preds_to_dict(OPTS.pred_file)
res = evaluator.model_performance(pred_data)
print(res)
if OPTS.out_file:
with open(OPTS.out_file, 'w') as f:
json.dump(res, f)
return res
# if OPTS.na_prob_file:
# find_all_best_thresh(out_eval, preds, exact_raw, f1_raw, na_probs, qid_to_has_ans)
if __name__ == '__main__':
OPTS = parse_args()
main()