-
Notifications
You must be signed in to change notification settings - Fork 2
/
metric.py
272 lines (220 loc) · 10.2 KB
/
metric.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
import json
from collections import defaultdict
import click
from utils import DOMAINS
class MetricTracker:
'''Dialog Metric Tracker
- Fields: restaurant, hotel, attraction, train, taxi, domain, dialog
- Metrics: inform, success, book, combine
'''
METICS = ['inform', 'success', 'book']
def __init__(self):
self.raw = defaultdict(dict)
self.cost = defaultdict(float)
self.cost_total = 0.0
self.domain_scores = {} # domain -> metric -> score (restaurant -> inform -> score)
for domain in DOMAINS:
self.domain_scores[domain] = {}
for m in self.METICS:
self.domain_scores[domain][m] = {'score': 0.0, 'hit': 0, 'total': 0}
self.fuse_domain_scores = {m: {'score': 0.0, 'hit': 0, 'total': 0} for m in self.METICS}
self.dialog_scores = {m: {'score': 0.0, 'hit': 0, 'total': 0} for m in self.METICS}
self.combine_scores = {d: {'score': 0.0, 'accum': 0.0, 'total': 0} for d in DOMAINS + ['domain', 'dialog']}
def add_domain_eval_result(self, dialog_id, domain, eval_result):
assert domain in DOMAINS
assert domain not in self.raw[dialog_id]
if status := eval_result.get('status'):
assert status == 'succeed'
self.raw[dialog_id][domain] = eval_result
# Inform, Success, Book Score
for k in self.METICS:
v = eval_result[k]
if v['complete'] is not None:
## each domain
self.domain_scores[domain][k]['hit'] += v['complete']
self.domain_scores[domain][k]['total'] += 1
# domain level
self.fuse_domain_scores[k]['hit'] += v['complete']
self.fuse_domain_scores[k]['total'] += 1
# Combine Score
combine_score = self.calc_combine_score(eval_result['inform']['complete'],
eval_result['success']['complete'],
eval_result['book']['complete'],)
## each domain
self.combine_scores[domain]['accum'] += combine_score
self.combine_scores[domain]['total'] += 1
## domain level
self.combine_scores['domain']['accum'] += combine_score
self.combine_scores['domain']['total'] += 1
def add_dialog_eval_results(self, dialog_id, eval_results):
assert dialog_id not in self.raw
# Each Domain & Domain Level
for domain, eval_result in eval_results.items():
self.add_domain_eval_result(dialog_id, domain, eval_result)
# Dialog level
## inform, success, book
scores = {}
for m in self.METICS:
complete_list = [result[m]['complete'] for result in eval_results.values() if result[m]['complete'] is not None]
if len(complete_list) > 0:
scores[m] = int(all(complete_list))
self.dialog_scores[m]['hit'] += scores[m]
self.dialog_scores[m]['total'] += 1
else:
scores[m] = None
## combine
combine_score = self.calc_combine_score(scores['inform'], scores['success'], scores['book'])
self.combine_scores['dialog']['accum'] += combine_score
self.combine_scores['dialog']['total'] += 1
@staticmethod
def calc_combine_score(inform, success, book):
'''Formula
score = 0.5 * inform + 0.5 * success_book
success_book = (success + book) / (success_f + book_f)
'''
assert inform is not None
if success is None and book is None:
score = 1.0 * inform
elif success is not None and book is None:
score = 0.5 * inform + 0.5 * success
elif success is None and book is not None:
score = 0.5 * inform + 0.5 * book
else:
score = 0.5 * inform + 0.25 * success + 0.25 * book
return score
def add_cost(self, dialog_id, cost):
self.cost[dialog_id] += cost
self.cost_total += cost
def get_cost(self):
n_dialog = len(self.raw)
avg = self.cost_total / n_dialog if n_dialog > 0 else 0.0
return {
'total': self.cost_total,
'n_dialog': n_dialog,
'average': avg,
}
def generate_postfix_str(self, fields=['cost', 'inform', 'success', 'book'], prefixes=[]):
postfix_str = [s for s in prefixes]
if 'cost' in fields:
postfix_str.append(f'cost: {self.cost_total:.6f}')
fields = [f for f in fields if f != 'cost']
for m in fields:
hit, total = 0, 0
for d in DOMAINS:
hit += self.domain_scores[d][m]['hit']
total += self.domain_scores[d][m]['total']
s = hit / total if total > 0 else 0.0
postfix_str.append(f'{m}: {s * 100:.1f} ({hit}/{total})')
postfix_str = ', '.join(postfix_str)
return postfix_str
def generate_detail_table(self, domains=['restaurant', 'hotel', 'attraction', 'train', 'taxi'],
metrics=['inform', 'success', 'book'],
invalid_colums=[('attraction', 'book'), ('taxi', 'success')],
fix_taxi=True):
DOMAIN_ABBR = {'restaurant': 'Rest', 'hotel': 'Hotel', 'attraction': 'Attra', 'train': 'Train', 'taxi': 'Taxi'}
METRIC_ABBR = {'inform': 'I', 'success': 'S', 'book': 'B'}
scores = {}
for domain in domains:
for metric in metrics:
dd = self.domain_scores[domain][metric]
score = dd['hit'] / dd['total'] if dd['total'] > 0.0 else 0.0
scores[(domain, metric)] = score
if fix_taxi:
scores['taxi', 'book'], scores['taxi', 'success'] = scores['taxi', 'success'], scores['taxi', 'book']
if invalid_colums:
for domain in domains:
for metric in metrics:
if (domain, metric) in invalid_colums:
score = '--'
else:
score = scores[domain, metric]
score = f'{score * 100:.1f}'
scores[domain, metric] = score
head, body = [], []
for domain in domains:
for metric in metrics:
head.append(f'{DOMAIN_ABBR[domain]}-{METRIC_ABBR[metric]}')
body.append(scores[domain, metric])
table = []
table.append('| ' + ' | '.join(head) + ' |')
table.append('| ' + ' | '.join([':---:'] * len(head)) + ' |')
table.append('| ' + ' | '.join(body) + ' |')
table = '\n'.join(table)
return table
def generate_fuse_table(self, fields=['domain', 'dialog'],
metrics=['inform', 'success', 'book', 'combine']):
FIELDS_MAP = {'domain': 'Domain', 'dialog': 'Dialog'}
METRIC_ABBR = {'inform': 'I', 'success': 'S', 'book': 'B', 'combine': 'C'}
head, body = [], []
basic_metrics = [m for m in metrics if m in self.METICS]
if 'domain' in fields:
for m in basic_metrics:
dd = self.fuse_domain_scores[m]
dd['score'] = dd['hit'] / dd['total'] if dd['total'] > 0.0 else 0.0
head.append(f"{FIELDS_MAP['domain']}-{METRIC_ABBR[m]}")
body.append(f"{dd['score'] * 100:.1f}")
if 'combine' in metrics:
dd = self.combine_scores['domain']
dd['score'] = dd['accum'] / dd['total'] if dd['total'] > 0.0 else 0.0
head.append(f"{FIELDS_MAP['domain']}-{METRIC_ABBR['combine']}")
body.append(f"{dd['score'] * 100:.1f}")
if 'dialog' in fields:
for m in basic_metrics:
dd = self.dialog_scores[m]
dd['score'] = dd['hit'] / dd['total'] if dd['total'] > 0.0 else 0.0
head.append(f"{FIELDS_MAP['dialog']}-{METRIC_ABBR[m]}")
body.append(f"{dd['score'] * 100:.1f}")
if 'combine' in metrics:
dd = self.combine_scores['dialog']
dd['score'] = dd['accum'] / dd['total'] if dd['total'] > 0.0 else 0.0
head.append(f"{FIELDS_MAP['dialog']}-{METRIC_ABBR['combine']}")
body.append(f"{dd['score'] * 100:.1f}")
table = []
table.append('| ' + ' | '.join(head) + ' |')
table.append('| ' + ' | '.join([':---:'] * len(head)) + ' |')
table.append('| ' + ' | '.join(body) + ' |')
table = '\n'.join(table)
return table
def generate_cost_table(self):
# #dialogs Total($) Average($)
# n x y
d = self.get_cost()
total, n_dialog, average = d['total'], d['n_dialog'], d['average']
head = ['#dialogs', 'Total ($)', 'Average ($)']
body = [str(n_dialog), f'{total:.4f}', f'{average:.4f}']
table = []
table.append('| ' + ' | '.join(head) + ' |')
table.append('| ' + ' | '.join([':---:'] * len(head)) + ' |')
table.append('| ' + ' | '.join(body) + ' |')
table = '\n'.join(table)
return table
def generate_summary_tables(self):
summary = []
summary.append('## Comprehensive Metrics')
summary.append(self.generate_fuse_table())
summary.append('')
summary.append('## Domain Metrics')
summary.append(self.generate_detail_table())
summary.append('')
summary.append('## Cost')
summary.append(self.generate_cost_table())
summary = '\n'.join(summary)
return summary
@click.command()
@click.option('--log_file')
@click.option('--score_table_file')
def metric(log_file, score_table_file):
with open(log_file) as f:
data = [json.loads(s) for s in f.read().splitlines()]
metric_tracker = MetricTracker()
for item in data[1:]:
if item['status'] != 'succeed':
print(f'Skip dialog: {item["dialog_id"]}, status: {item["status"]}')
continue
metric_tracker.add_dialog_eval_results(item['dialog_id'], item['eval_results'])
metric_tracker.add_cost(item['dialog_id'], item['cost'])
summary = metric_tracker.generate_summary_tables()
with open(score_table_file, 'w') as f:
f.write(summary + '\n')
if __name__ == '__main__':
metric()