Fraser-Greenlee commited on
Commit ·
eb3e3a6
1
Parent(s): 5fd46fe
del
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183import jsonfrom typing import Listfrom tqdm import tqdmfrom random import randint, random, samplefrom astpretty import pprintfrom copy import deepcopyfrom string import ascii_lettersimport ast
def write_rows(rows): # TODO write this as a gzip for much better compression with open('data.10_simple_variations.jsonl', 'a') as f: f.writelines(rows)
class AlternativeList(ast.NodeTransformer): def __init__(self): self.n_changes = 0 super().__init__()
def visit_List(self, node: List): code = ast.unparse(node) try: list_val = eval(code) except Exception: return node
for _ in range(randint(0, 3)): list_val.append(randint(-999, 999))
if not list_val: list_val.append(randint(-999, 999))
list_val = sample(list_val, randint(1, len(list_val)))
self.n_changes += 1 return ast.parse(str(list_val)).body[0].value
class AlternativeConstant(ast.NodeTransformer): def __init__(self): self.n_changes = 0 super().__init__()
def visit_Constant(self, node): ''' Switch constant values to simple variations ''' if type(node.value) is int: if randint(0, 1) == 1: node.value = randint(-9, 9) else: node.value = randint(-999, 999) elif type(node.value) is str: if randint(0, 1) == 1 and node.value: if node.value: node.value = ''.join(sample(node.value, randint(1, len(node.value)))) else: self.n_changes -= 1 else: node.value = ''.join(sample(ascii_letters, randint(1, 4))) elif type(node.value) is float: if randint(0, 1) == 1: node.value = random() else: node.value = random() * randint(-999, 999) elif type(node.value) is bool: node.value = bool(randint(0, 1)) else: self.n_changes -= 1 self.n_changes += 1 return super().visit_Constant(node)
class AlternativeNames(ast.NodeTransformer):
def visit_Name(self, node): return ast.copy_location(ast.Subscript( value=ast.Name(id='data', ctx=ast.Load()), slice=ast.Index(value=ast.Str(s=node.id)), ctx=node.ctx ), node)
def state_dict_to_str(state): vals = [] for k, v in state.items(): vals.append( f'{k} = {v}' ) vals = sorted(vals) return ';'.join(vals)
def trace_code(start_state: str, code: str): state = {} try: exec(start_state, {}, state) except Exception: return start_state = dict(state) try: exec(code, {}, state) except Exception: return return state_dict_to_str(start_state), code, state_dict_to_str(state)
def make_alternative_rows(start, code): variations = {} n_tries = 0 state_root = ast.parse(start)
while len(variations) < 10 and n_tries < 20:
node_transformer = AlternativeList() alt_state_root = node_transformer.visit(deepcopy(state_root))
if node_transformer.n_changes < 1: node_transformer = AlternativeConstant() alt_state_root = node_transformer.visit(deepcopy(alt_state_root)) if node_transformer.n_changes < 1: n_tries += 10
alt_start = ast.unparse(alt_state_root)
alt_start_code_end = trace_code(alt_start, code) if alt_start_code_end: variations[alt_start] = alt_start_code_end n_tries += 1
# TODO change the names (keep alphabetical order) ''' get number of vals in start states (N) get N random lowercase letters in alphabetical order parse start state and shuffle the expr.body make name map old->new using new characters use visitor with name map to swap variable names using map do this for start, code, end seperately ''' return [ json.dumps({'start': st, 'code': cd, 'end': en}) + '\n' for st, cd, en in variations.values() ]
STATS = { 'alt_count': 0, 'num_rows': 0}
with open('new_all_states.txt', 'r') as f: rows = [] prev_lines = [] for i, line in tqdm(enumerate(f), total=9_000_000): line = line.strip()
if line[-1] != ';': prev_lines.append(line) continue elif prev_lines: line = '\n'.join(prev_lines) + '\n' + line prev_lines = []
start, code_end = line.split('; code: ') start = start.removeprefix('state: ') code, end = code_end.split('; output: ') end = end.removesuffix(';')
rows.append(json.dumps({ 'start': start, 'code': code, 'end': end }) + '\n') alt_rows = make_alternative_rows(start, code) rows += alt_rows
STATS['alt_count'] += len(alt_rows) STATS['num_rows'] += 1
if len(rows) > 100_000: breakpoint() write_rows(rows) rows = []make_variations.pyDeleted-183