ifujisawa commited on
Commit
86d1691
·
1 Parent(s): 45fa08d

add evaluator

Browse files
Files changed (2) hide show
  1. evaluate_sample.py +25 -0
  2. evaluator.py +155 -0
evaluate_sample.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from evaluator import prefix_accuracy, exact_match
3
+
4
+ if __name__ == '__main__':
5
+ path = './dataset/combined_dataset.json'
6
+ with open(path, 'r') as f:
7
+ data = [json.loads(line) for line in f]
8
+
9
+ fm_list, sm_list, pa_list = [], [], []
10
+ for item in data:
11
+ gt = item['label']
12
+ states_true = gt['intermediate'] + [gt['final']]
13
+ ## dummy
14
+ states_pred = gt['intermediate'] + [gt['final']]
15
+
16
+ pa = prefix_accuracy(states_true, states_pred)
17
+ sm = pa == 1.0
18
+ fm = exact_match(states_true[-1], states_pred[-1])
19
+ pa_list.append(pa)
20
+ sm_list.append(sm)
21
+ fm_list.append(fm)
22
+
23
+ print(f'prefix accuracy: {sum(pa_list) / len(pa_list)}')
24
+ print(f'final match: {sum(fm_list) / len(fm_list)}')
25
+ print(f'sequential match: {sum(sm_list) / len(sm_list)}')
evaluator.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ def match_int(y_true, y_pred):
4
+ y_true_int = int(y_true)
5
+ try:
6
+ y_pred_int = int(y_pred)
7
+ except:
8
+ return 0.0
9
+
10
+ return float(y_true_int == y_pred_int)
11
+
12
+ def match_str(y_true, y_pred):
13
+ y_true_str = str(y_true)
14
+ try:
15
+ y_pred_str = str(y_pred)
16
+ except:
17
+ return 0.0
18
+
19
+ return float(y_true_str == y_pred_str)
20
+
21
+ def match_list(seq_true, seq_pred, match=match_str):
22
+ assert isinstance(seq_true, list)
23
+
24
+ if len(seq_true) == 0:
25
+ return 0.0
26
+
27
+ try:
28
+ seq_pred == list(seq_pred)
29
+ except:
30
+ return 0.0
31
+
32
+ if len(seq_true) != len(seq_pred):
33
+ return 0.0
34
+
35
+ arr = np.array([match(st, sp) for st, sp in zip(seq_true, seq_pred)])
36
+ return float(arr.all())
37
+
38
+
39
+ def reduce_last_intermediate(y_pred):
40
+ y_pred_ret = y_pred.copy()
41
+ if len(y_pred['intermediate']) == 0:
42
+ return y_pred_ret
43
+ if exact_match(y_pred['final'], y_pred['intermediate'][-1]):
44
+ y_pred_ret['intermediate'] = y_pred['intermediate'][:-1]
45
+ return y_pred_ret
46
+
47
+ def exact_match(y_true, y_pred):
48
+ type_y_true = type(y_true)
49
+ if type_y_true == str:
50
+ return match_str(y_true, y_pred)
51
+ elif type_y_true == int:
52
+ return match_int(y_true, y_pred)
53
+ elif type_y_true == list:
54
+ return match_list(y_true, y_pred)
55
+ else:
56
+ raise TypeError(f'type of ground truth is {type_y_true} and not supported')
57
+
58
+ def match_hit(seq_true, seq_pred):
59
+ assert type(seq_true) == list
60
+ if len(seq_true) == 0:
61
+ return np.nan
62
+
63
+ hit = 0.0
64
+ for st, sp in zip(seq_true, seq_pred):
65
+ hit += exact_match(st, sp)
66
+ return hit
67
+
68
+ def prefix_match_length(seq_true, seq_pred):
69
+ assert type(seq_true) == list
70
+ if len(seq_true) == 0:
71
+ return 0.0
72
+
73
+ hit = 0.0
74
+ for st, sp in zip(seq_true, seq_pred):
75
+ if exact_match(st, sp):
76
+ hit += 1
77
+ else:
78
+ break
79
+ return hit
80
+
81
+ def match_ratio(seq_true, seq_pred):
82
+ assert type(seq_true) == list
83
+ len_true = len(seq_true)
84
+ len_pred = len(seq_pred)
85
+ len_max = max(len_true, len_pred)
86
+ if len_true == 0:
87
+ return 0.0
88
+
89
+ hit = 0.0
90
+ for st, sp in zip(seq_true, seq_pred):
91
+ hit += exact_match(st, sp)
92
+ return hit / len_max
93
+
94
+ def prefix_accuracy(seq_true, seq_pred):
95
+ assert type(seq_true) == list
96
+ len_true = len(seq_true)
97
+ len_pred = len(seq_pred)
98
+ len_max = max(len_true, len_pred)
99
+ if len_true == 0:
100
+ return 0.0
101
+
102
+ hit = 0.0
103
+ for st, sp in zip(seq_true, seq_pred):
104
+ if exact_match(st, sp):
105
+ hit += 1
106
+ else:
107
+ break
108
+ return hit / len_max
109
+
110
+
111
+ if __name__ == '__main__':
112
+ assert match_int(5, '5') == 1.0
113
+ assert match_int(32, '32') == 1.0
114
+ assert match_int('512', 512) == 1.0
115
+ assert match_int(234, (234)) == 1.0
116
+
117
+ assert match_int(5, '55') == 0.0
118
+ assert match_int(512, 'a') == 0.0
119
+ assert match_int('512', 'a') == 0.0
120
+ assert match_int(231, [231]) == 0.0
121
+ assert match_int(231, [231, 66]) == 0.0
122
+ assert match_int(65, None) == 0.0
123
+ assert match_int(73, []) == 0.0
124
+
125
+
126
+ assert match_str('abc', 'abc') == 1.0
127
+ assert match_str('3', '3') == 1.0
128
+ assert match_str('21', 21) == 1.0
129
+ assert match_str('fe a t', 'fe a t') == 1.0
130
+
131
+ assert match_str('abc', 'ac') == 0.0
132
+ assert match_str('fe a t', 'feat') == 0.0
133
+ assert match_str('abc', ['abc']) == 0.0
134
+ assert match_str('abc', None) == 0.0
135
+ assert match_str('abc', []) == 0.0
136
+
137
+
138
+ assert match_list([1], [1]) == 1.0
139
+ assert match_list([1, 3, 5], [1, 3, 5]) == 1.0
140
+ assert match_list(['abc'], ['abc']) == 1.0
141
+ assert match_list(['abc', 'de'], ['abc', 'de']) == 1.0
142
+
143
+ assert match_list([], []) == 0.0
144
+ assert match_list([], [1]) == 0.0
145
+ assert match_list([2], [3]) == 0.0
146
+ assert match_list([1], [1, 1]) == 0.0
147
+ assert match_list([1], 1) == 0.0
148
+ assert match_list(['abc', 'de', 'f'], ['abc', 'de']) == 0.0
149
+ assert match_list(['abc', 'de'], ['abc', 'de', 'f']) == 0.0
150
+ assert match_list(['abc', 'de'], ['de', 'abc']) == 0.0
151
+ assert match_list([1], []) == 0.0
152
+ assert match_list([1], None) == 0.0
153
+ assert match_list(['abc', 'de'], [None, 'de']) == 0.0
154
+
155
+ print('All tests passed!')