-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheval_utils.py
152 lines (147 loc) · 5.65 KB
/
eval_utils.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
import numpy as np
import operator
import re
from pprint import pprint
import pint
import math
from copy import deepcopy
ureg = pint.UnitRegistry(system='mks', autoconvert_offset_to_baseunit=True)
ureg.load_definitions('./units.txt')
def compile_fp(context, p):
var = {
'num_fact_score': []
}
answer_to_fact = {}
match_number = re.compile('-?\ *[0-9]+\.*[0-9]*(?:[Ee]\ *[-+]?\ *[0-9]+)*')
context = context.split('=')[1:]
for fact in context:
if fact == '':
continue
try:
var[fact[:fact.index(':')]] = float(re.findall(match_number, fact[fact.index(':')+1: ])[0])
except:
try:
var[fact[:fact.index(':')]] = None
except:
pass
p = p.split('=')[1:]
funcs = {'Mul':operator.mul, 'Div':operator.truediv, 'Add':operator.add, 'Sub': operator.sub, 'Pow': operator.pow, 'Min': lambda *a: min(*a), 'Log': lambda *a: math.log(*a), 'Fac': lambda a: math.factorial(a)}
paren_match = re.compile('\(([^()]+)\)')
for line in p:
# print(line)
if line[0] == 'Q' or line[0] == 'P':
try:
lhs, rhs = line.split('->')
lhs = lhs.strip()
rhs = rhs.strip()
new = False
except:
try:
lhs, rhs = line.split('—>')
lhs = lhs.strip()
rhs = rhs.strip()
new = False
except:
lhs, rhs = line.split(':')
lhs = lhs.strip()
rhs = rhs.strip()
new = True
if (lhs not in var and not new) or (lhs in var and new):
raise ValueError('Improper assignment notation')
if '-> A' in line and line[0] == 'Q' and new:
raise ValueError('Question must be defined first')
if '-> A' in line and line[0] == 'P' and new:
raise ValueError('Output must be in terms of question identifiers')
if '|' in rhs:
answer, fact = rhs.split('|')
answer = answer.strip()
fact = fact.strip()
try:
var['num_fact_score'].append(accuracy_metric(var[answer], var[fact]))
except:
var['num_fact_score'].append(0)
answer_to_fact[answer] = fact[1]
var[lhs] = var[answer]
elif any(re.search(r'\b' + func + r'\b', line) for func in funcs):
for func in funcs:
if func in line:
break
parens = [i.strip() for i in re.findall(paren_match, line)[0].split(',')]
in_parens = []
for i in parens:
if i in var and 'Q' not in i:
raise ValueError('Only question identifiers allowed in functions')
if i in var:
in_parens.append(var[i])
else:
in_parens.append(float(i))
var[lhs] = funcs[func](*in_parens)
else:
var[lhs] = rhs
elif line[0] == 'A':
# var[line[:line.index(':')]] = float(re.findall(match_number, line[line.index(':')+1: ])[0])
ureg_conv = ureg(line[line.index(':')+1: ])
# if type(ureg_conv) not in [float, int, np.float64] and 'kelvin' in str(ureg_conv.units):
# print(str(ureg_conv.units), ',', str(ureg_conv.units).replace('kelvin', 'celsius'))
# print(ureg_conv)
# ureg_conv = ureg_conv.to(str(ureg_conv.units).replace('kelvin', 'celsius'))
# print(ureg_conv)
# print()
var[line[:line.index(':')]] = ureg_conv
else:
raise ValueError('Line must begin with question identifier, answer identifier, or final output identifier')
var['answer_to_fact'] = answer_to_fact
var['num_fact_score'] = np.mean(var['num_fact_score'])
# pprint(var)
return var
def parse_program(cur):
if cur[0] == 'answer':
cur = cur[1]
if len(cur) == 1:
cur = cur[0]
if type(cur) == str and cur.isdigit():
return float(cur)
if cur[0] == '':
return float(cur[1][0])
elif cur[0] == '.':
return kb[kb.name == cur[1][0]][cur[1][1]].values[0]
elif cur[0] in MATH_OPS:
cur_left = parse_program(cur[1][0])
cur_right = parse_program(cur[1][1])
if cur[0] == '/' and cur_right == 0:
return np.inf
return MATH_OPS[cur[0]](cur_left, cur_right)
def accuracy_metric(y, y_hat):
if type(y) not in [int, float, np.float64] or type(y_hat) not in [int, float, np.float64]:
return 0
if y is None or y_hat is None:
return 0
if y < 0 or y_hat < 0:
return 0
if y == 0 and y_hat == 0:
return 1
elif y == 0 or y_hat == 0:
return max(0, 1-np.abs(np.log10(np.abs(y - y_hat))))
# elif y/y_hat == 0:
# return 0
try:
return max(0, 3-np.abs(np.log10(y/y_hat)))/3
except:
return 0
def convert_units(answer):
if type(answer) == str:
original_pint = ureg(answer)
else:
original_pint = answer
if original_pint is None:
return None, None
if type(original_pint) not in [float, int]:
original_unit = original_pint.units
try:
converted_pint = original_pint.to_base_units()
except:
converted_pint = deepcopy(original_pint)
standard_unit = converted_pint.units
return converted_pint.magnitude, converted_pint.units
else:
return original_pint, None