Fraser-Greenlee
commited on
Commit
·
eb3e3a6
1
Parent(s):
5fd46fe
del
Browse files- make_variations.py +0 -183
make_variations.py
DELETED
@@ -1,183 +0,0 @@
|
|
1 |
-
import json
|
2 |
-
from typing import List
|
3 |
-
from tqdm import tqdm
|
4 |
-
from random import randint, random, sample
|
5 |
-
from astpretty import pprint
|
6 |
-
from copy import deepcopy
|
7 |
-
from string import ascii_letters
|
8 |
-
import ast
|
9 |
-
|
10 |
-
|
11 |
-
def write_rows(rows):
|
12 |
-
# TODO write this as a gzip for much better compression
|
13 |
-
with open('data.10_simple_variations.jsonl', 'a') as f:
|
14 |
-
f.writelines(rows)
|
15 |
-
|
16 |
-
|
17 |
-
class AlternativeList(ast.NodeTransformer):
|
18 |
-
def __init__(self):
|
19 |
-
self.n_changes = 0
|
20 |
-
super().__init__()
|
21 |
-
|
22 |
-
def visit_List(self, node: List):
|
23 |
-
code = ast.unparse(node)
|
24 |
-
try:
|
25 |
-
list_val = eval(code)
|
26 |
-
except Exception:
|
27 |
-
return node
|
28 |
-
|
29 |
-
for _ in range(randint(0, 3)):
|
30 |
-
list_val.append(randint(-999, 999))
|
31 |
-
|
32 |
-
if not list_val:
|
33 |
-
list_val.append(randint(-999, 999))
|
34 |
-
|
35 |
-
list_val = sample(list_val, randint(1, len(list_val)))
|
36 |
-
|
37 |
-
self.n_changes += 1
|
38 |
-
return ast.parse(str(list_val)).body[0].value
|
39 |
-
|
40 |
-
|
41 |
-
class AlternativeConstant(ast.NodeTransformer):
|
42 |
-
def __init__(self):
|
43 |
-
self.n_changes = 0
|
44 |
-
super().__init__()
|
45 |
-
|
46 |
-
def visit_Constant(self, node):
|
47 |
-
'''
|
48 |
-
Switch constant values to simple variations
|
49 |
-
'''
|
50 |
-
if type(node.value) is int:
|
51 |
-
if randint(0, 1) == 1:
|
52 |
-
node.value = randint(-9, 9)
|
53 |
-
else:
|
54 |
-
node.value = randint(-999, 999)
|
55 |
-
elif type(node.value) is str:
|
56 |
-
if randint(0, 1) == 1 and node.value:
|
57 |
-
if node.value:
|
58 |
-
node.value = ''.join(sample(node.value, randint(1, len(node.value))))
|
59 |
-
else:
|
60 |
-
self.n_changes -= 1
|
61 |
-
else:
|
62 |
-
node.value = ''.join(sample(ascii_letters, randint(1, 4)))
|
63 |
-
elif type(node.value) is float:
|
64 |
-
if randint(0, 1) == 1:
|
65 |
-
node.value = random()
|
66 |
-
else:
|
67 |
-
node.value = random() * randint(-999, 999)
|
68 |
-
elif type(node.value) is bool:
|
69 |
-
node.value = bool(randint(0, 1))
|
70 |
-
else:
|
71 |
-
self.n_changes -= 1
|
72 |
-
self.n_changes += 1
|
73 |
-
return super().visit_Constant(node)
|
74 |
-
|
75 |
-
|
76 |
-
class AlternativeNames(ast.NodeTransformer):
|
77 |
-
|
78 |
-
def visit_Name(self, node):
|
79 |
-
return ast.copy_location(ast.Subscript(
|
80 |
-
value=ast.Name(id='data', ctx=ast.Load()),
|
81 |
-
slice=ast.Index(value=ast.Str(s=node.id)),
|
82 |
-
ctx=node.ctx
|
83 |
-
), node)
|
84 |
-
|
85 |
-
|
86 |
-
def state_dict_to_str(state):
|
87 |
-
vals = []
|
88 |
-
for k, v in state.items():
|
89 |
-
vals.append(
|
90 |
-
f'{k} = {v}'
|
91 |
-
)
|
92 |
-
vals = sorted(vals)
|
93 |
-
return ';'.join(vals)
|
94 |
-
|
95 |
-
|
96 |
-
def trace_code(start_state: str, code: str):
|
97 |
-
state = {}
|
98 |
-
try:
|
99 |
-
exec(start_state, {}, state)
|
100 |
-
except Exception:
|
101 |
-
return
|
102 |
-
start_state = dict(state)
|
103 |
-
try:
|
104 |
-
exec(code, {}, state)
|
105 |
-
except Exception:
|
106 |
-
return
|
107 |
-
return state_dict_to_str(start_state), code, state_dict_to_str(state)
|
108 |
-
|
109 |
-
|
110 |
-
def make_alternative_rows(start, code):
|
111 |
-
variations = {}
|
112 |
-
n_tries = 0
|
113 |
-
state_root = ast.parse(start)
|
114 |
-
|
115 |
-
while len(variations) < 10 and n_tries < 20:
|
116 |
-
|
117 |
-
node_transformer = AlternativeList()
|
118 |
-
alt_state_root = node_transformer.visit(deepcopy(state_root))
|
119 |
-
|
120 |
-
if node_transformer.n_changes < 1:
|
121 |
-
node_transformer = AlternativeConstant()
|
122 |
-
alt_state_root = node_transformer.visit(deepcopy(alt_state_root))
|
123 |
-
if node_transformer.n_changes < 1:
|
124 |
-
n_tries += 10
|
125 |
-
|
126 |
-
alt_start = ast.unparse(alt_state_root)
|
127 |
-
|
128 |
-
alt_start_code_end = trace_code(alt_start, code)
|
129 |
-
if alt_start_code_end:
|
130 |
-
variations[alt_start] = alt_start_code_end
|
131 |
-
n_tries += 1
|
132 |
-
|
133 |
-
# TODO change the names (keep alphabetical order)
|
134 |
-
'''
|
135 |
-
get number of vals in start states (N)
|
136 |
-
get N random lowercase letters in alphabetical order
|
137 |
-
parse start state and shuffle the expr.body
|
138 |
-
make name map old->new using new characters
|
139 |
-
use visitor with name map to swap variable names using map
|
140 |
-
do this for start, code, end seperately
|
141 |
-
'''
|
142 |
-
return [
|
143 |
-
json.dumps({'start': st, 'code': cd, 'end': en}) + '\n' for st, cd, en in variations.values()
|
144 |
-
]
|
145 |
-
|
146 |
-
|
147 |
-
STATS = {
|
148 |
-
'alt_count': 0,
|
149 |
-
'num_rows': 0
|
150 |
-
}
|
151 |
-
|
152 |
-
|
153 |
-
with open('new_all_states.txt', 'r') as f:
|
154 |
-
rows = []
|
155 |
-
prev_lines = []
|
156 |
-
for i, line in tqdm(enumerate(f), total=9_000_000):
|
157 |
-
line = line.strip()
|
158 |
-
|
159 |
-
if line[-1] != ';':
|
160 |
-
prev_lines.append(line)
|
161 |
-
continue
|
162 |
-
elif prev_lines:
|
163 |
-
line = '\n'.join(prev_lines) + '\n' + line
|
164 |
-
prev_lines = []
|
165 |
-
|
166 |
-
start, code_end = line.split('; code: ')
|
167 |
-
start = start.removeprefix('state: ')
|
168 |
-
code, end = code_end.split('; output: ')
|
169 |
-
end = end.removesuffix(';')
|
170 |
-
|
171 |
-
rows.append(json.dumps({
|
172 |
-
'start': start, 'code': code, 'end': end
|
173 |
-
}) + '\n')
|
174 |
-
alt_rows = make_alternative_rows(start, code)
|
175 |
-
rows += alt_rows
|
176 |
-
|
177 |
-
STATS['alt_count'] += len(alt_rows)
|
178 |
-
STATS['num_rows'] += 1
|
179 |
-
|
180 |
-
if len(rows) > 100_000:
|
181 |
-
breakpoint()
|
182 |
-
write_rows(rows)
|
183 |
-
rows = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|