-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_finqa.py
515 lines (393 loc) · 13.8 KB
/
evaluate_finqa.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Script for evaluate results
Usage:
python evaluate.py predicted_file_name test_file_name
"""
import time
import os
import sys
import shutil
import io
import re
import json
import copy
import random
import collections
import math
import numpy as np
from tqdm import tqdm
from sympy import simplify
all_ops = ["add", "subtract", "multiply", "divide", "exp", "greater", "table_max", \
"table_min", "table_sum", "table_average"]
def str_to_num(text):
text = text.replace(",", "")
try:
num = float(text)
except ValueError:
if "%" in text:
text = text.replace("%", "")
try:
num = float(text)
num = num / 100.0
except ValueError:
num = "n/a"
elif "const" in text:
text = text.replace("const_", "")
if text == "m1":
text = "-1"
num = float(text)
else:
num = "n/a"
return num
def process_row(row_in):
row_out = []
invalid_flag = 0
for num in row_in:
num = num.replace("$", "").strip()
num = num.split("(")[0].strip()
num = str_to_num(num)
if num == "n/a":
invalid_flag = 1
break
row_out.append(num)
if invalid_flag:
return "n/a"
return row_out
def eval_program(program, table):
'''
calculate the numerical results of the program
'''
invalid_flag = 0
this_res = "n/a"
try:
program = program[:-1] # remove EOF
# check structure
for ind, token in enumerate(program):
if ind % 4 == 0:
if token.strip("(") not in all_ops:
return 1, "n/a"
if (ind + 1) % 4 == 0:
if token != ")":
return 1, "n/a"
program = "|".join(program)
steps = program.split(")")[:-1]
res_dict = {}
for ind, step in enumerate(steps):
step = step.strip()
if len(step.split("(")) > 2:
invalid_flag = 1
break
op = step.split("(")[0].strip("|").strip()
args = step.split("(")[1].strip("|").strip()
arg1 = args.split("|")[0].strip()
arg2 = args.split("|")[1].strip()
if op == "add" or op == "subtract" or op == "multiply" or op == "divide" or op == "exp" or op == "greater":
if "#" in arg1:
arg1 = res_dict[int(arg1.replace("#", ""))]
else:
arg1 = str_to_num(arg1)
if arg1 == "n/a":
invalid_flag = 1
break
if "#" in arg2:
arg2 = res_dict[int(arg2.replace("#", ""))]
else:
arg2 = str_to_num(arg2)
if arg2 == "n/a":
invalid_flag = 1
break
if op == "add":
this_res = arg1 + arg2
elif op == "subtract":
this_res = arg1 - arg2
elif op == "multiply":
this_res = arg1 * arg2
elif op == "divide":
this_res = arg1 / arg2
elif op == "exp":
this_res = arg1 ** arg2
elif op == "greater":
this_res = "yes" if arg1 > arg2 else "no"
res_dict[ind] = this_res
elif "table" in op:
table_dict = {}
for row in table:
table_dict[row[0]] = row[1:]
if "#" in arg1:
arg1 = res_dict[int(arg1.replace("#", ""))]
else:
if arg1 not in table_dict:
invalid_flag = 1
break
cal_row = table_dict[arg1]
num_row = process_row(cal_row)
if num_row == "n/a":
invalid_flag = 1
break
if op == "table_max":
this_res = max(num_row)
elif op == "table_min":
this_res = min(num_row)
elif op == "table_sum":
this_res = sum(num_row)
elif op == "table_average":
this_res = sum(num_row) / len(num_row)
# this_res = round(this_res, 5)
res_dict[ind] = this_res
# print(this_res)
if this_res != "yes" and this_res != "no" and this_res != "n/a":
# print(this_res)
this_res = round(this_res, 5)
except:
invalid_flag = 1
return invalid_flag, this_res
def equal_program(program1, program2):
'''
symbolic program if equal
program1: gold
program2: pred
'''
sym_map = {}
program1 = program1[:-1] # remove EOF
program1 = "|".join(program1)
steps = program1.split(")")[:-1]
invalid_flag = 0
sym_ind = 0
step_dict_1 = {}
# symbolic map
for ind, step in enumerate(steps):
step = step.strip()
assert len(step.split("(")) <= 2
op = step.split("(")[0].strip("|").strip()
args = step.split("(")[1].strip("|").strip()
arg1 = args.split("|")[0].strip()
arg2 = args.split("|")[1].strip()
step_dict_1[ind] = step
if "table" in op:
if step not in sym_map:
sym_map[step] = "a" + str(sym_ind)
sym_ind += 1
else:
if "#" not in arg1:
if arg1 not in sym_map:
sym_map[arg1] = "a" + str(sym_ind)
sym_ind += 1
if "#" not in arg2:
if arg2 not in sym_map:
sym_map[arg2] = "a" + str(sym_ind)
sym_ind += 1
# check program 2
step_dict_2 = {}
try:
program2 = program2[:-1] # remove EOF
# check structure
for ind, token in enumerate(program2):
if ind % 4 == 0:
if token.strip("(") not in all_ops:
print("structure error")
return False
if (ind + 1) % 4 == 0:
if token != ")":
print("structure error")
return False
program2 = "|".join(program2)
steps = program2.split(")")[:-1]
for ind, step in enumerate(steps):
step = step.strip()
if len(step.split("(")) > 2:
return False
op = step.split("(")[0].strip("|").strip()
args = step.split("(")[1].strip("|").strip()
# print(args)
# print(op)
arg1 = args.split("|")[0].strip()
arg2 = args.split("|")[1].strip()
step_dict_2[ind] = step
if "table" in op:
if step not in sym_map:
return False
else:
if "#" not in arg1:
if arg1 not in sym_map:
return False
else:
if int(arg1.strip("#")) >= ind:
return False
if "#" not in arg2:
if arg2 not in sym_map:
return False
else:
if int(arg2.strip("#")) >= ind:
return False
except:
return False
def symbol_recur(step, step_dict):
step = step.strip()
op = step.split("(")[0].strip("|").strip()
args = step.split("(")[1].strip("|").strip()
arg1 = args.split("|")[0].strip()
arg2 = args.split("|")[1].strip()
# print(op)
# print(arg1)
# print(arg2)
if "table" in op:
# as var
return sym_map[step]
if "#" in arg1:
arg1_ind = int(arg1.replace("#", ""))
arg1_part = symbol_recur(step_dict[arg1_ind], step_dict)
else:
arg1_part = sym_map[arg1]
if "#" in arg2:
arg2_ind = int(arg2.replace("#", ""))
arg2_part = symbol_recur(step_dict[arg2_ind], step_dict)
else:
arg2_part = sym_map[arg2]
if op == "add":
return "( " + arg1_part + " + " + arg2_part + " )"
elif op == "subtract":
return "( " + arg1_part + " - " + arg2_part + " )"
elif op == "multiply":
return "( " + arg1_part + " * " + arg2_part + " )"
elif op == "divide":
return "( " + arg1_part + " / " + arg2_part + " )"
elif op == "exp":
return "( " + arg1_part + " ** " + arg2_part + " )"
elif op == "greater":
return "( " + arg1_part + " > " + arg2_part + " )"
# # derive symbolic program 1
# print(program1)
steps = program1.split(")")[:-1]
# print(steps)
# print(steps)
# print(sym_map)
sym_prog1 = symbol_recur(steps[-1], step_dict_1)
sym_prog1 = simplify(sym_prog1, evaluate=False)
# print("########")
# print(sym_prog1)
try:
# derive symbolic program 2
steps = program2.split(")")[:-1]
sym_prog2 = symbol_recur(steps[-1], step_dict_2)
sym_prog2 = simplify(sym_prog2, evaluate=False)
# print(sym_prog2)
except:
return False
return sym_prog1 == sym_prog2
def program_tokenization(original_program):
original_program = original_program.split(', ')
program = []
for tok in original_program:
cur_tok = ''
for c in tok:
if c == ')':
if cur_tok != '':
program.append(cur_tok)
cur_tok = ''
cur_tok += c
if c in ['(', ')']:
program.append(cur_tok)
cur_tok = ''
if cur_tok != '':
program.append(cur_tok)
program.append('EOF')
return program
def evaluate_result(json_in, json_ori):
'''
execution acc
program acc
'''
correct = 0
with open(json_in) as f_in:
data = json.load(f_in)
with open(json_ori) as f_in:
data_ori = json.load(f_in)
data_dict = {}
for each_data in data_ori:
assert each_data["id"] not in data_dict
data_dict[each_data["id"]] = each_data
exe_correct = 0
prog_correct = 0
res_list = []
all_res_list = []
for each_data in data:
each_id = each_data["id"]
each_ori_data = data_dict[each_id]
table = each_ori_data["table"]
gold_res = each_ori_data["qa"]["exe_ans"]
pred = each_data["predicted"]
gold = program_tokenization(each_ori_data["qa"]["program"])
# print("#########")
# print(pred)
# print(gold)
# if program_mode == "nest":
# if pred[-1] == "EOF":
# pred = pred[:-1]
# pred = reprog_to_seq(pred, is_gold=False)
# pred += ["EOF"]
# gold = gold[:-1]
# gold = reprog_to_seq(gold, is_gold=True)
# gold += ["EOF"]
# print("\n")
# print("########")
invalid_flag, exe_res = eval_program(pred, table)
# print(invalid_flag)
# print(exe_res)
if invalid_flag == 0:
if exe_res == gold_res:
exe_correct += 1
# else:
# if "".join(gold) == "".join(pred):
# print(each_id)
# print(gold)
# print(pred)
# print(gold_res)
# print(exe_res)
# print(each_ori_data["id"])
if equal_program(gold, pred):
# assert exe_res == gold_res
if exe_res != gold_res:
print(each_id)
print(gold)
print(pred)
print(gold_res)
print(exe_res)
print(each_ori_data["id"])
assert exe_res == gold_res
prog_correct += 1
if "".join(gold) != "".join(pred):
print(each_id)
print(gold)
print(pred)
print(gold_res)
print(exe_res)
print(each_ori_data["id"])
# if "".join(gold) == "".join(pred):
# if not equal_program(gold, pred):
# print(each_id)
# print(gold)
# print(pred)
# print(gold_res)
# print(exe_res)
# print(each_ori_data["id"])
# prog_correct += 1
each_ori_data["qa"]["predicted"] = pred
if exe_res != gold_res:
res_list.append(each_ori_data)
all_res_list.append(each_ori_data)
exe_acc = float(exe_correct) / len(data)
prog_acc = float(prog_correct) / len(data)
print("All: ", len(data))
print("Exe acc: ", exe_acc)
print("Prog acc: ", prog_acc)
# with open(error_file, "w") as f:
# json.dump(res_list, f, indent=4)
# with open(all_res_file, "w") as f:
# json.dump(all_res_list, f, indent=4)
return exe_acc, prog_acc
if __name__ == '__main__':
json_in = sys.argv[1]
json_ori = sys.argv[2]
evaluate_result(json_in, json_ori)