-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
148 lines (140 loc) · 6.57 KB
/
Copy patheval.py
File metadata and controls
148 lines (140 loc) · 6.57 KB
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
import json
import logging
from collections import Counter
import hydra
import tqdm
from omegaconf import DictConfig
def generate_output(classes, class_string, tp, fn, fp, tn):
output_strings = []
for a_c in classes:
output_strings.append(f'{class_string} {a_c}')
if (tp[a_c] + tn[a_c] + fp[a_c] + fn[a_c]) == 0:
recall = 'n/a'
precision = 'n/a'
f1 = 'n/a'
accuracy = 'n/a'
elif tp[a_c] == 0:
recall = 0
precision = 0
f1 = 0
accuracy = (tp[a_c] + tn[a_c]) / (tp[a_c] + tn[a_c] + fp[a_c] + fn[a_c])
else:
recall = tp[a_c] / (tp[a_c] + fn[a_c])
precision = tp[a_c] / (tp[a_c] + fp[a_c])
f1 = 2 * (precision * recall) / (precision + recall)
accuracy = (tp[a_c] + tn[a_c]) / (tp[a_c] + tn[a_c] + fp[a_c] + fn[a_c])
output_strings.append(f'Recall: {recall}')
output_strings.append(f'Precision: {precision}')
output_strings.append(f'F1: {f1}')
output_strings.append(f'Acc: {accuracy}')
output_strings.append(f'Total actual: {tp[a_c] + fn[a_c]}')
output_strings.append(f'Total prediction: {tp[a_c] + fp[a_c]}\n')
return output_strings
@hydra.main(version_base=None, config_path='config', config_name='config')
def main(cfg: DictConfig) -> None:
logging.info('Starting eval')
true_answers = []
question_classes = []
logging.info('Reading true answers:')
with open(cfg.eval.dataset.predict_path) as f:
for line in tqdm.tqdm(f):
jsonline = json.loads(line.strip())
answer = jsonline['answer'].strip().lower()
if cfg.eval.two_classes:
if 'yes' in answer:
answer = 'yes'
if 'unknown' in answer:
answer = 'no'
true_answers.append(answer.strip())
if cfg.eval.collapse:
if 'actively involved in the study? context' in jsonline['question']:
question_classes.append('active')
elif 'passively involved in the study? context' in jsonline['question']:
question_classes.append('passive')
else:
if 'perform analysis in the study? context' in jsonline['question']:
question_classes.append('analysis')
elif 'collect data in the study? context' in jsonline['question']:
question_classes.append('collect data')
elif 'coordinate the study? context' in jsonline['question']:
question_classes.append('coordinate')
elif 'design the study? context' in jsonline['question']:
question_classes.append('design')
elif 'fund the study? context' in jsonline['question']:
question_classes.append('fund')
elif 'participate in the study? context' in jsonline['question']:
question_classes.append('participate')
elif 'review the study? context' in jsonline['question']:
question_classes.append('review')
elif 'supply data to the study? context' in jsonline['question']:
question_classes.append('supply data')
elif 'supply the study? context' in jsonline['question']:
question_classes.append('supply')
elif 'write the study? context' in jsonline['question']:
question_classes.append('write')
elif 'support the study? context' in jsonline['question']:
question_classes.append('support')
if cfg.eval.no_unknown:
answer_a_classes = list(set(true_answers) - {'unknown'})
else:
answer_a_classes = list(set(true_answers))
question_a_classes = list(set(question_classes))
print(Counter(question_classes))
predict_answers = []
logging.info('Reading predict answers:')
with open(cfg.eval.dataset.prediction_path) as f:
for line in tqdm.tqdm(f):
answer = line.strip().lower()
if cfg.eval.two_classes:
if 'yes' in answer:
answer = 'yes'
elif 'unknown' in answer or 'no' in answer:
answer = 'no'
else:
answer = 'no'
predict_answers.append(answer.strip())
correct = 0
tp = {c: 0 for c in answer_a_classes + question_a_classes + ['all'] +
[f'{x} {y}' for x in answer_a_classes for y in question_a_classes]}
fn = {c: 0 for c in answer_a_classes + question_a_classes + ['all'] +
[f'{x} {y}' for x in answer_a_classes for y in question_a_classes]}
fp = {c: 0 for c in answer_a_classes + question_a_classes + ['all'] +
[f'{x} {y}' for x in answer_a_classes for y in question_a_classes]}
tn = {c: 0 for c in answer_a_classes + question_a_classes + ['all'] +
[f'{x} {y}' for x in answer_a_classes for y in question_a_classes]}
logging.info('Calculating metrics:')
assert len(true_answers) == len(predict_answers)
assert len(true_answers) == len(question_classes), f"{len(true_answers)} & {len(question_classes)}"
for t, y, q_c in tqdm.tqdm(zip(true_answers, predict_answers, question_classes)):
if t == y:
correct += 1
for a_c in answer_a_classes:
if y == a_c and t == a_c:
tp[a_c] += 1
tp[q_c] += 1
tp[f'{a_c} {q_c}'] += 1
tp['all'] += 1
if y != a_c and t == a_c:
fn[a_c] += 1
fn[q_c] += 1
fn[f'{a_c} {q_c}'] += 1
fn['all'] += 1
if y == a_c and t != a_c:
fp[a_c] += 1
fp[q_c] += 1
fp[f'{a_c} {q_c}'] += 1
fp['all'] += 1
if y != a_c and t != a_c:
tn[a_c] += 1
tn[q_c] += 1
tn[f'{a_c} {q_c}'] += 1
tn['all'] += 1
output_strings = []
output_strings.extend(generate_output(answer_a_classes, 'Answer Class', tp, fn, fp, tn))
output_strings.extend(generate_output(question_a_classes, 'Question Class', tp, fn, fp, tn))
output_strings.extend(generate_output([f'{x} {y}' for x in answer_a_classes for y in question_a_classes], 'Product QxA Class', tp, fn, fp, tn))
output_strings.extend(generate_output(['all'], 'All', tp, fn, fp, tn))
print('\n'.join(output_strings))
open(cfg.eval.dataset.eval_path, 'w').write('\n'.join(output_strings))
if __name__ == '__main__':
main()