-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdemo_morpheus_enumerator.py
371 lines (318 loc) · 12.2 KB
/
demo_morpheus_enumerator.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
#!/usr/bin/env python
import argparse
import tyrell.spec as S
from tyrell.interpreter import PostOrderInterpreter, GeneralError
from tyrell.enumerator import SmtEnumerator
from tyrell.decider import Example, ExampleConstraintPruningDecider
from tyrell.synthesizer import Synthesizer
from tyrell.logger import get_logger
import rpy2.robjects as robjects
logger = get_logger('tyrell')
counter_ = 1
robjects.r('''
library(compare)
library(dplyr)
library(tidyr)
''')
## Common utils.
def get_collist(sel):
sel_str = ",".join(sel)
return "c(" + sel_str + ")"
def get_fresh_name():
global counter_
counter_ = counter_ + 1
fresh_str = 'RET_DF' + str(counter_)
return fresh_str
def get_fresh_col():
global counter_
counter_ = counter_ + 1
fresh_str = 'COL' + str(counter_)
return fresh_str
def get_type(df, index):
_rscript = 'sapply({df_name}, class)[{pos}]'.format(df_name=df, pos=index)
ret_val = robjects.r(_rscript)
return ret_val[0]
def eq_r(actual, expect):
_rscript = '''
tmp1 <- sapply({lhs}, as.character)
tmp2 <- sapply({rhs}, as.character)
compare(tmp1, tmp2, ignoreOrder = TRUE)
'''.format(lhs=actual, rhs=expect)
# logger.info(robjects.r(actual))
# logger.info(robjects.r(expect))
ret_val = robjects.r(_rscript)
return True == ret_val[0][0]
def get_head(df):
head = set()
for h in df.colnames:
head.add(h)
return head
def get_content(df):
content = set()
for vec in df:
for elem in vec:
e_val = str(elem)
content.add(e_val)
return content
class MorpheusInterpreter(PostOrderInterpreter):
## Concrete interpreter
def eval_ColInt(self, v):
return int(v)
def eval_ColList(self, v):
return v
def eval_const(self, node, args):
return args[0]
def eval_select(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=1,
cond=lambda x: max(list(map(lambda y: int(y), x))) <= n_cols,
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- select({table}, {cols})'.format(
ret_df=ret_df_name, table=args[0], cols=get_collist(args[1]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting select...')
raise GeneralError()
def eval_unite(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
first_idx = int(args[1])
self.assertArg(node, args,
index=1,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=2,
cond=lambda x: x <= n_cols and x != first_idx,
capture_indices=[0, 1])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- unite({table}, {TMP}, {col1}, {col2})'.format(
ret_df=ret_df_name, table=args[0], TMP=get_fresh_col(), col1=str(args[1]), col2=str(args[2]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting unite...')
raise GeneralError()
def eval_filter(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=2,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=2,
cond=lambda x: get_type(args[0], str(x)) != 'factor',
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- {table} %>% filter(.[[{col}]] {op} {const})'.format(
ret_df=ret_df_name, table=args[0], op=args[1], col=str(args[2]), const=str(args[3]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting filter...')
raise GeneralError()
def eval_separate(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=1,
cond=lambda x: x <= n_cols,
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- separate({table}, {col1}, c("{TMP1}", "{TMP2}"))'.format(
ret_df=ret_df_name, table=args[0], col1=str(args[1]), TMP1=get_fresh_col(), TMP2=get_fresh_col())
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting separate...')
raise GeneralError()
def eval_spread(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
first_idx = int(args[1])
self.assertArg(node, args,
index=1,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=2,
cond=lambda x: x <= n_cols and x > first_idx,
capture_indices=[0, 1])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- spread({table}, {col1}, {col2})'.format(
ret_df=ret_df_name, table=args[0], col1=str(args[1]), col2=str(args[2]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting spread...')
raise GeneralError()
def eval_gather(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=1,
cond=lambda x: max(list(map(lambda y: int(y), x))) <= n_cols,
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- gather({table}, KEY, VALUE, {cols})'.format(
ret_df=ret_df_name, table=args[0], cols=get_collist(args[1]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting gather...')
raise GeneralError()
def eval_group_by(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=1,
cond=lambda x: max(list(map(lambda y: int(y), x))) <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=1,
cond=lambda x: len(x) == 1,
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- group_by({table}, {cols})'.format(
ret_df=ret_df_name, table=args[0], cols=get_collist(args[1]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting group_by...')
raise GeneralError()
def eval_summarise(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=2,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=2,
cond=lambda x: get_type(args[0], str(x)) == 'integer' or get_type(args[0], str(x)) == 'numeric',
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- {table} %>% summarise({TMP} = {aggr} (.[[{col}]]))'.format(
ret_df=ret_df_name, table=args[0], TMP=get_fresh_col(), aggr=str(args[1]), col=str(args[2]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting summarise...')
raise GeneralError()
def eval_mutate(self, node, args):
n_cols = robjects.r('ncol(' + args[0] + ')')[0]
self.assertArg(node, args,
index=2,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=3,
cond=lambda x: x <= n_cols,
capture_indices=[0])
self.assertArg(node, args,
index=2,
cond=lambda x: get_type(args[0], str(x)) == 'numeric',
capture_indices=[0])
self.assertArg(node, args,
index=3,
cond=lambda x: get_type(args[0], str(x)) == 'numeric',
capture_indices=[0])
ret_df_name = get_fresh_name()
_script = '{ret_df} <- {table} %>% mutate({TMP}=.[[{col1}]] {op} .[[{col2}]])'.format(
ret_df=ret_df_name, table=args[0], TMP=get_fresh_col(), op=args[1], col1=str(args[2]), col2=str(args[3]))
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting mutate...')
raise GeneralError()
def eval_inner_join(self, node, args):
ret_df_name = get_fresh_name()
_script = '{ret_df} <- inner_join({t1}, {t2})'.format(
ret_df=ret_df_name, t1=args[0], t2=args[1])
try:
ret_val = robjects.r(_script)
return ret_df_name
except:
logger.error('Error in interpreting innerjoin...')
raise GeneralError()
## Abstract interpreter
def apply_row(self, val):
df = robjects.r(val)
return df.nrow
def apply_col(self, val):
df = robjects.r(val)
return df.ncol
def apply_head(self, val):
input_df = robjects.r('input0')
curr_df = robjects.r(val)
head_input = get_head(input_df)
content_input = get_content(input_df)
head_curr = get_head(curr_df)
return len(head_curr - head_input - content_input)
def apply_content(self, val):
input_df = robjects.r('input0')
curr_df = robjects.r(val)
content_input = get_content(input_df)
content_curr = get_content(curr_df)
return len(content_curr - content_input)
def init_tbl(df_name, csv_loc):
cmd = '''
tbl_name <- read.csv(csv_location, check.names = FALSE)
fctr.cols <- sapply(tbl_name, is.factor)
int.cols <- sapply(tbl_name, is.integer)
tbl_name[, fctr.cols] <- sapply(tbl_name[, fctr.cols], as.character)
tbl_name[, int.cols] <- sapply(tbl_name[, int.cols], as.numeric)
'''
cmd = cmd.replace('tbl_name', df_name).replace('csv_location', '"'+ csv_loc + '"')
robjects.r(cmd)
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i0', '--input0', type=str)
parser.add_argument('-i1', '--input1', type=str)
parser.add_argument('-o', '--output', type=str)
parser.add_argument('-l', '--length', type=int)
args = parser.parse_args()
loc_val = args.length
# Input and Output must be in CSV format.
input0 = args.input0
input1 = args.input1
output = args.output
# This is required by Ruben.
depth_val = loc_val + 1
print(input0, input1, output, loc_val)
init_tbl('input0', input0)
#FIXME: ignore the second input table for now.
init_tbl('output', output)
logger.info('Parsing Spec...')
spec = S.parse_file('example/morpheus.tyrell')
logger.info('Parsing succeeded')
logger.info('Building synthesizer...')
synthesizer = Synthesizer(
#loc: # of function productions
enumerator=SmtEnumerator(spec, depth=depth_val, loc=loc_val),
decider=ExampleConstraintPruningDecider(
spec=spec,
interpreter=MorpheusInterpreter(),
examples=[
# Example(input=[DataFrame2(benchmark1_input)], output=benchmark1_output),
Example(input=['input0'], output='output'),
],
equal_output=eq_r
)
)
logger.info('Synthesizing programs...')
prog = synthesizer.synthesize()
if prog is not None:
logger.info('Solution found: {}'.format(prog))
else:
logger.info('Solution not found!')
if __name__ == '__main__':
logger.setLevel('DEBUG')
main()