-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_analysis.py
More file actions
362 lines (325 loc) · 12.4 KB
/
error_analysis.py
File metadata and controls
362 lines (325 loc) · 12.4 KB
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
import calendar
from collections import defaultdict, OrderedDict
import copy
import numpy as np
import regex
from termcolor import colored
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
from sample import (
load_df,
save_df,
)
from schemas import (
create_date_schema,
sample,
FormPair,
)
from util import flatten, set_seed
DateSchema = create_date_schema()
def get_value_dict(content, tgt_forms: List[Callable]):
t = type(content)
forbidden = 96752 # TODO deal with types, argh # TODO make sure unused
dummy_content = t(*([forbidden] * len(content._fields)))
form_primitives = []
for tgt_form in tgt_forms:
cur_form_primitives = list(set(tgt_form.function(dummy_content).split(str(forbidden))))
cur_form_primitives = list(filter(lambda x: x, cur_form_primitives))
form_primitives.extend(cur_form_primitives)
value_dict = {
**{f: getattr(content, f) for f in content._fields},
**{f'fp{i}': fp for i, fp in enumerate(form_primitives)},
**{'2-digit': r'\d\d',},
}
value_dict = add_neighbors(value_dict)
return value_dict
def get_templates(content, tgt_form):
output_text = tgt_form.function(content)
value_dict = get_value_dict(content, [tgt_form.function])
templates = match_templates(output_text, value_dict)
return templates
def add_neighbors(base_value_dict):
new_value_dict = {}
for k, v in base_value_dict.items():
new_value_dict[k] = v
if isinstance(v, int):
new_value_dict[k] = f'{v:02d}'
if isinstance(v, int) and f'{v:02d}' != str(v):
new_value_dict[f'{k}_not-fmt02d'] = str(v)
if isinstance(v, int) and v > 0: # TODO make 1?
new_value_dict[f'{k}_minus-one'] = f'{v-1:02d}'
if isinstance(v, int) and v > 0 and f'{v-1:02d}' != str(v-1):
new_value_dict[f'{k}_minus-one_not-fmt02d'] = str(v-1)
if isinstance(v, int): # TODO upper bound?
new_value_dict[f'{k}_plus-one'] = f'{v+1:02d}'
if isinstance(v, int) and f'{v+1:02d}' != str(v+1):
new_value_dict[f'{k}_plus-one_not-fmt02d'] = str(v+1)
if k == 'month_name':
new_value_dict['month'] = list(calendar.month_abbr).index(v)
if k == 'month':
new_value_dict['month_name'] = calendar.month_abbr[v]
if k == 'year':
new_value_dict['year_2:4'] = str(v)[2:4]
base_value_dict = new_value_dict
new_value_dict = {}
for k, v in base_value_dict.items():
if v != '':
new_value_dict[k] = v
return new_value_dict
def get_solutions(backtrack, idx, arrs):
if idx == 0:
# return list(map(lambda _: list(reversed(_)), solutions))
return [list(reversed(arrs))]
res = []
els = defaultdict(list)
for name, matched in backtrack[idx]:
# els[len(matched)].append(name)
els[len(matched)].append((name, matched))
for length, lst in els.items():
solutions = get_solutions(backtrack, idx-length, arrs + [lst])
# res.extend(get_solutions(backtrack, idx-len(matched), arrs.append([name])))
res.extend(solutions)
# import pdb; pdb.set_trace()
return list(set(res))
def match_templates(output_text, value_dict):
matches = [{} for _ in range(len(output_text)+1)]
dp = [0 for _ in range(len(output_text)+1)]
backtrack = [[] for _ in range(len(output_text)+1)]
for name, pattern in value_dict.items():
for match in regex.finditer(str(pattern), output_text, overlapped=True):
start_idx = match.span()[0]
matched = match.group()
# if pattern == r'\d\d' and matched in value_dict.values():
if name[0] == '<' and matched in value_dict.values():
continue
matches[start_idx+len(matched)][name] = matched
# print(matches)
for i in range(len(output_text)+1):
dp[i] = dp[max(0, i-1)]
if i > 0:
backtrack[i] = [(output_text[i-1], output_text[i-1])]
for name, matched in matches[i].items():
# begin argmax solution
newval = dp[i-len(matched)] + len(matched)
if newval > dp[i]:
backtrack[i] = []
dp[i] = newval
if newval == dp[i]:
# end argmax solution
backtrack[i].append((name, matched))
# print(backtrack)
# solutions = []
# idx = len(output_text) - 1
# while idx != 0:
# for name, matched in backtrack[i]:
return get_solutions(backtrack, len(output_text), [])
def getitem(lst, index, default_value=None):
try:
return lst[index]
except IndexError:
return default_value
def print_templates(templates, gt_template=None, output_text=None, input_text=None):
if input_text is not None and output_text is not None:
print(f'{input_text} -> {output_text}')
elif output_text is not None:
print(output_text)
for solution in templates:
for i in range(max(map(len, solution))):
line = ''
# width = max(map(len, map(lambda _: getitem(_, i, ''), solution))) + 1
for index in range(len(solution)):
# width = max(map(len, solution[index])) + 1
width = max(map(lambda _: len(_[0]), solution[index])) + 1
val = getitem(solution[index], i, ('', None))[0]
if isinstance(val, tuple):
val = '_'.join(val)
# val = '{0: <{width}}'.format(val, width=width)
# line += val + colored('|', 'magenta')
line += f' {val:{width}s}' + colored('|', 'magenta')
print(line)
exemplar = list(map(lambda x: x[0][1], solution))
if output_text is not None:
assert ''.join(exemplar) == output_text
print('')
if gt_template is not None:
predicted_templates = list(map(lambda s: list(map(lambda x: x[0][0], s)), templates))
print(gt_template in predicted_templates)
print('')
def test_match_templates():
test_examples = [
('7-Mar-1', '!01!1!7-Mar-1', ['!', ['day', 'fmt_02d'], '!', ['day'], '!', ['input_text']]),
('7-Mar-1', '!7!Mar!1!7!Mar', ['!', 'year', '!', 'month', '!', 'day', '!', 'year', '!', 'month']),
('7-Mar-1', '!03!01!1970!', ['!', ['month', 'number', 'fmt_02d'], '!', ['day', 'fmt_02d'], '!', ['year', 'projected'], '!']),
('7-Mar-1', '!Mar!1!7!', ['!', 'month', '!', 'day', '!', 'year', '!']),
('7-Mar-1', '!05!1!7!', ['!', 'month', '!', 'day', '!', 'year', '!']),
]
gt_template = ['!', 'month', '!', 'day', '!', 'year', '!']
# TODO add scores
value_dict = {
'year': '7',
'month': 'Mar',
'day': '1',
'day_fmt02d': '01',
'month_number': '3',
'month_fmt02d': '03',
# 'month_number_fmt02d': '03',
'input_text': '7-Mar-1',
# 'extra': '1',
'year_proj': '1970',
# ('year', 'proj'): '1970',
# 'dummy': '!1!',
# '*0': '0',
'2-digit': r'\d\d',
}
for input_text, output_text, expected_template in test_examples:
print(f'{input_text} -> {output_text}')
solutions = match_templates(output_text, value_dict)
# import pdb; pdb.set_trace()
for solution in solutions:
for i in range(max(map(len, solution))):
line = ''
# width = max(map(len, map(lambda _: getitem(_, i, ''), solution))) + 1
for index in range(len(solution)):
# width = max(map(len, solution[index])) + 1
width = max(map(lambda _: len(_[0]), solution[index])) + 1
val = getitem(solution[index], i, ('', None))[0]
if isinstance(val, tuple):
val = '_'.join(val)
# val = '{0: <{width}}'.format(val, width=width)
# line += val + colored('|', 'magenta')
line += f' {val:{width}s}' + colored('|', 'magenta')
print(line)
exemplar = list(map(lambda x: x[0][1], solution))
assert ''.join(exemplar) == output_text
# print('')
predicted_template = list(map(lambda x: x[0][0], solutions[0]))
print(predicted_template)
predicted_templates = list(map(lambda s: list(map(lambda x: x[0][0], s)), solutions))
print(gt_template in predicted_templates)
print('')
def test_end_to_end():
date_schema = create_date_schema()
schema_type = date_schema
# poss_fc = [
# (FormPair(0, 4), schema_type(*([0] * len(schema_type._fields))))
# ]
set_seed()
sm = sample(schema_type, 0, 4, schema_type(*([0] * len(schema_type._fields))))
print(sm.src_form, sm.tgt_form)
content = sm.content
# input_text = sm.src_form
# output_text = sm.tgt_form
tgt_form_func = schema_type.forms['tgt_form'][4]
# templates = get_templates(content, tgt_form_func)
# # gt_template = ['!', 'month', '!', 'day', '!', 'year', '!']
# gt_template = ['fp0', 'month_fmt02d', 'fp0', 'day_fmt02d', 'fp0', 'year', 'fp0']
# print_templates(templates, gt_template, output_text)
value_dict = get_value_dict(content, [tgt_form_func.function])
for idx, form in schema_type.forms['tgt_form'].items():
if idx != 4:
continue
# form = schema_type.forms['tgt_form'][2]
value_dict = get_value_dict(content, [form.function])
text = form.function(content)
templates = match_templates(text, value_dict)
templates = filter_templates(templates, 'fp0')
# template = sorted(templates, key=len)[0]
# print(template)
# print_templates([template], None, text)
print_templates(templates, None, text)
def filter_templates(templates, fp0):
def alternating_fp0(template):
# lst1 = list(map(lambda x: x[0][0], template[::2]))
# lst2 = [fp0] * ((len(template) + 1) // 2)
# print(template)
# print(template[::2])
# print(lst1)
# print(lst2)
return list(map(lambda x: x[0][0], template[::2])) == [fp0] * ((len(template) + 1) // 2)
templates = list(filter(alternating_fp0, templates))
return templates
def analyze_errors(df, filename):
date_schema = create_date_schema()
schema_type = date_schema
errors = []
for row in df.itertuples():
content = eval(row.content)
tgt_form_func = schema_type.forms['tgt_form'][4]
value_dict = get_value_dict(content, [tgt_form_func.function])
# text = form.function(content)
x = row.x
text = row.pred
# print(content)
# print(value_dict)
# print(text)
templates = match_templates(text, value_dict)
templates = filter_templates(templates, 'fp0')
print_templates(templates, None, text, x)
# templates_by_name = list(map(lambda s: list(map(lambda x: x[0][0], s)), templates))
templates_by_name = list(map(lambda x1: list(map(lambda x2: list(map(tuple, x2)), x1)), templates))
templates_by_name = list(map(lambda x1: list(map(lambda x2: list(map(lambda x3: x3[0], x2)), x1)), templates))
# if text == '!12!13!2017!':
# import pdb; pdb.set_trace()
errors.append(templates_by_name)
df = df.assign(templates=errors)
df = save_df(filename, df)
def run_error_analysis():
df = load_df()
analyze_errors(df[(df.engine == 'davinci') & (df.num_examples == 5) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'ada') & (df.num_examples == 15) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'davinci') & (df.num_examples == 3) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'curie') & (df.num_examples == 15) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'curie') & (df.num_examples == 10) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'curie') & (df.num_examples == 5) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
analyze_errors(df[(df.engine == 'babbage') & (df.num_examples == 15) & (df.rel != 'EQUALS')], filename='results_error_analysis_templates.csv')
def test_match_templates_addition():
from sequence_manipulation import add_neighbors_numeric
test_examples = [
[28, 67],
]
for _content in test_examples:
summand1, summand2 = _content
_sum = summand1 + summand2
_diff = summand1 - summand2
value_dict = OrderedDict({
**{
'summand1': summand1,
'summand2': summand2,
'sum': _sum,
'diff': _diff,
'neg': '-',
'\$': '\$',
'zero': '0',
},
**{'<other>': r'\d+',},
**{'Input': 'Input'},
**{'Output': 'Output'},
})
input_text = '{summand1} - {summand2}'
output_text = str(_sum)
_x = input_text
_pred = output_text
value_dict = add_neighbors_numeric(value_dict)
templates = match_templates(_pred, value_dict)
min_length = min(map(len, templates))
templates = list(filter(lambda _: len(_) == min_length, templates))
print_templates(templates, None, _pred, _x)
if __name__ == '__main__':
# test_match_templates()
# test_end_to_end()
# run_error_analysis()
test_match_templates_addition()
# extend to log probs?
# - beam search the top predictions when variance is high, and apply template search to the predictions
# for remaining unmatched, apply Levenshtein distance
# prefer unmatched in input
# # dp[i] = max(dp[i], dp[i-len(matched)] + len(matched))
# # matches = list(regex.finditer(pattern, output_text, overlapped=True))
# # for i in range(len(output_text)):
# # for v, neighbors in value_dict.items():
# template:
# constant
# value
# transformation applied to value
# numerical: plus 1, close
# copy
# string: take substring (more generally, levenshtein distance edits)