-
Notifications
You must be signed in to change notification settings - Fork 4
/
eval_mc.py
147 lines (136 loc) · 5.46 KB
/
eval_mc.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
import os.path as osp
from utils import load_file
map_name = {'D': 'Description', 'E': 'Explaination', 'PA': 'Predictive-Answer', 'CA': 'Counterfactual-Answer', 'PR': 'Predictive-Reason', 'CR': 'Counterfactual-Reason', 'P':'Predictive', 'C': 'Counterfactual'}
def accuracy_metric(result_file, qtype):
if qtype == -1:
accuracy_metric_all(result_file)
if qtype == 0:
accuracy_metric_q0(result_file)
if qtype == 1:
accuracy_metric_q1(result_file)
if qtype == 2:
accuracy_metric_q2(result_file)
if qtype == 3:
accuracy_metric_q3(result_file)
def accuracy_metric_q0(result_file):
preds = list(load_file(result_file).items())
group_acc = {'D': 0}
group_cnt = {'D': 0}
all_acc = 0
all_cnt = 0
for idx in range(len(preds)):
id_qtypes = preds[idx]
answer = id_qtypes[1]['answer']
pred = id_qtypes[1]['prediction']
group_cnt['D'] += 1
all_cnt += 1
if answer == pred:
group_acc['D'] += 1
all_acc += 1
for qtype, acc in group_acc.items(): #
print('{0:21} ==> {1:6.2f}%'.format(map_name[qtype], acc*100.0/group_cnt[qtype]))
print('{0:21} ==> {1:6.2f}%'.format('Acc', all_acc*100.0/all_cnt))
def accuracy_metric_q1(result_file):
preds = list(load_file(result_file).items())
group_acc = {'E': 0}
group_cnt = {'E': 0}
all_acc = 0
all_cnt = 0
for idx in range(len(preds)):
id_qtypes = preds[idx]
answer = id_qtypes[1]['answer']
pred = id_qtypes[1]['prediction']
group_cnt['E'] += 1
all_cnt += 1
if answer == pred:
group_acc['E'] += 1
all_acc += 1
for qtype, acc in group_acc.items(): #
print('{0:21} ==> {1:6.2f}%'.format(map_name[qtype], acc*100.0/group_cnt[qtype]))
print('{0:21} ==> {1:6.2f}%'.format('Acc', all_acc*100.0/all_cnt))
def accuracy_metric_q2(result_file):
preds = list(load_file(result_file).items())
qtype2short = ['PA', 'PR', 'P']
group_acc = {'PA': 0, 'PR': 0, 'P': 0}
group_cnt = {'PA': 0, 'PR': 0, 'P': 0}
all_acc = 0
all_cnt = 0
for idx in range(len(preds)//2):
id_qtypes = preds[idx*2:(idx+1)*2]
qtypes = [0, 1]
answer = [ans_pre[1]['answer'] for ans_pre in id_qtypes]
pred = [ans_pre[1]['prediction'] for ans_pre in id_qtypes]
for i in range(2):
group_cnt[qtype2short[qtypes[i]]] += 1
if answer[i] == pred[i]:
group_acc[qtype2short[qtypes[i]]] += 1
group_cnt['P'] += 1
all_cnt += 1
if answer[0] == pred[0] and answer[1] == pred[1]:
group_acc['P'] += 1
all_acc += 1
for qtype, acc in group_acc.items(): #
print('{0:21} ==> {1:6.2f}%'.format(map_name[qtype], acc*100.0/group_cnt[qtype]))
print('{0:21} ==> {1:6.2f}%'.format('Acc', all_acc*100.0/all_cnt))
def accuracy_metric_q3(result_file):
preds = list(load_file(result_file).items())
qtype2short = ['CA', 'CR', 'C']
group_acc = {'CA': 0, 'CR': 0, 'C': 0}
group_cnt = {'CA': 0, 'CR': 0, 'C': 0}
all_acc = 0
all_cnt = 0
for idx in range(len(preds)//2):
id_qtypes = preds[idx*2:(idx+1)*2]
qtypes = [0, 1]
answer = [ans_pre[1]['answer'] for ans_pre in id_qtypes]
pred = [ans_pre[1]['prediction'] for ans_pre in id_qtypes]
for i in range(2):
group_cnt[qtype2short[qtypes[i]]] += 1
if answer[i] == pred[i]:
group_acc[qtype2short[qtypes[i]]] += 1
group_cnt['C'] += 1
all_cnt += 1
if answer[0] == pred[0] and answer[1] == pred[1]:
group_acc['C'] += 1
all_acc += 1
for qtype, acc in group_acc.items(): #
print('{0:21} ==> {1:6.2f}%'.format(map_name[qtype], acc*100.0/group_cnt[qtype]))
print('{0:21} ==> {1:6.2f}%'.format('Acc', all_acc*100.0/all_cnt))
def accuracy_metric_all(result_file):
preds = list(load_file(result_file).items())
qtype2short = ['D', 'E', 'PA', 'PR', 'CA', 'CR', 'P', 'C']
group_acc = {'D': 0, 'E': 0, 'PA': 0, 'PR': 0, 'CA': 0, 'CR': 0, 'P': 0, 'C': 0}
group_cnt = {'D': 0, 'E': 0, 'PA': 0, 'PR': 0, 'CA': 0, 'CR': 0, 'P': 0, 'C': 0}
all_acc = 0
all_cnt = 0
for idx in range(len(preds)//6):
id_qtypes = preds[idx*6:(idx+1)*6]
qtypes = [int(id_qtype[0].split('_')[-1]) for id_qtype in id_qtypes]
answer = [ans_pre[1]['answer'] for ans_pre in id_qtypes]
pred = [ans_pre[1]['prediction'] for ans_pre in id_qtypes]
for i in range(6):
group_cnt[qtype2short[qtypes[i]]] += 1
if answer[i] == pred[i]:
group_acc[qtype2short[qtypes[i]]] += 1
group_cnt['C'] += 1
group_cnt['P'] += 1
all_cnt += 4
if answer[0] == pred[0]:
all_acc += 1
if answer[1] == pred[1]:
all_acc += 1
if answer[2] == pred[2] and answer[3] == pred[3]:
group_acc['P'] += 1
all_acc += 1
if answer[4] == pred[4] and answer[5] == pred[5]:
group_acc['C'] += 1
all_acc += 1
for qtype, acc in group_acc.items(): #
print('{0:21} ==> {1:6.2f}%'.format(map_name[qtype], acc*100.0/group_cnt[qtype]))
print('{0:21} ==> {1:6.2f}%'.format('Acc', all_acc*100.0/all_cnt))
def main(result_file, qtype=-1):
print('Evaluating {}'.format(result_file))
accuracy_metric(result_file, qtype)
if __name__ == "__main__":
result_file = 'path to results json'
main(result_file, -1)