Repository navigation
Expand file tree
/
Copy pathcode_select_humaneval.py
More file actions
76 lines (72 loc) · 2.35 KB
/
Copy pathcode_select_humaneval.py
File metadata and controls
76 lines (72 loc) · 2.35 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
import json
import os
import io
import sys
import tqdm
import multiprocessing as mp
def splitCode(problem,code,k,K):
head = code[:code.find(problem)+len(problem)]
code = code[code.find(problem)+len(problem):]
code = code.replace('`','')
if code.find('if __name__') != -1:
code = code[:code.find('if __name__')]
lines = code.split('\n')
L = (len(lines)-1)//K
if L == 0:
return -1
chunk = ""
if k == 0:
chunk = lines[1:L]
elif k == K-1:
chunk = lines[k*L:]
else:
chunk = lines[k*L:(k+1)*L]
return head+'\n'+"\n".join(chunk)
with open('code_3_1e.json','r') as file:
evaluated_code = json.load(file)
n_steps = 3
n_branches = 3
selected_code = {}
n_selected = 0
n_selected0 = 0
overall = [0,0]
for key,problem in tqdm.tqdm(evaluated_code.items()):
print(key)
selected = []
for code in problem['code']:
step_acc = []
for k in range(n_steps-1):
#print(problem['problem'])
partial_code = splitCode(problem['problem'],code,k,n_steps)
if partial_code == -1:
continue
accuracies = []
for i,new_codes in enumerate(problem['beams']):
if new_codes[0].find(partial_code) != -1:
acc = len([a for a in problem['result'][i] if a.find('Accepted') != -1])/len(problem['result'][i])
accuracies.append(acc)
if len(accuracies) == 0:
# print('000')
continue
mean_acc = sum(accuracies)/len(accuracies)
step_acc.append((partial_code,mean_acc))
for k in range(len(step_acc)-1):
if step_acc[k][1] <= step_acc[k+1][1] and step_acc[k+1][1]>0:
selected.append(step_acc[k+1][0])
print(step_acc[k+1][1])
if step_acc[k][1] <= step_acc[k+1][1]:
n_selected0 += 1
for res in problem['result']:
for r in res:
if 'Accepted' in r:
overall[1] += 1
else:
overall[0] += 1
selected_code[key] = problem.copy()
selected_code['selected'] = selected
n_selected += len(selected)
print(f'====={n_selected}=====')
print(f'====={n_selected0}=====')
print(overall,overall[1]/(overall[0]+overall[1]))
# with open('code_3_1s.json','w') as file:
# json.dump(selected_code,file)