-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathconsolidate_experiments.py
90 lines (73 loc) · 3.36 KB
/
consolidate_experiments.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
import os
# set these variables according to your experiments #
dirpath = 'data'
experiments_type = [
'default_experiment'
]
runs = 1
# set these variables according to your experiments #
def build_headers(path):
print(path + "/all_measures.txt")
file_summary = open(path + "/all_measures.tsv", "w+")
file_summary.write('robot_id\t')
behavior_headers = []
with open(path + '/data_fullevolution/descriptors/behavior_desc_robot_1.txt') as file:
for line in file:
measure, value = line.strip().split(' ')
behavior_headers.append(measure)
file_summary.write(measure+'\t')
phenotype_headers = []
with open(path + '/data_fullevolution/descriptors/phenotype_desc_robot_1.txt') as file:
for line in file:
measure, value = line.strip().split(' ')
phenotype_headers.append(measure)
file_summary.write(measure+'\t')
file_summary.write('fitness\n')
file_summary.close()
file_summary = open(path + "/snapshots_ids.tsv", "w+")
file_summary.write('generation\trobot_id\n')
file_summary.close()
return behavior_headers, phenotype_headers
for exp in experiments_type:
for run in range(1, runs+1):
print(exp, run)
path = os.path.join(dirpath, str(exp), str(run))
behavior_headers, phenotype_headers = build_headers(path)
file_summary = open(path + "/all_measures.tsv", "a")
for r, d, f in os.walk(path+'/data_fullevolution/fitness'):
for file in f:
robot_id = file.split('.')[0].split('_')[-1]
file_summary.write(robot_id+'\t')
bh_file = path+'/data_fullevolution/descriptors/behavior_desc_robot_'+robot_id+'.txt'
if os.path.isfile(bh_file):
with open(bh_file) as file:
for line in file:
measure, value = line.strip().split(' ')
file_summary.write(value+'\t')
else:
for h in behavior_headers:
file_summary.write('None'+'\t')
pt_file = path+'/data_fullevolution/descriptors/phenotype_desc_robot_'+robot_id+'.txt'
if os.path.isfile(pt_file):
with open(pt_file) as file:
for line in file:
measure, value = line.strip().split(' ')
file_summary.write(value+'\t')
else:
for h in phenotype_headers:
file_summary.write('None'+'\t')
file = open(path+'/data_fullevolution/fitness/fitness_robot_'+robot_id+'.txt', 'r')
fitness = file.read()
file_summary.write(fitness + '\n')
file_summary.close()
file_summary = open(path + "/snapshots_ids.tsv", "a")
for r, d, f in os.walk(path):
for dir in d:
if 'selectedpop' in dir:
gen = dir.split('_')[1]
for r2, d2, f2 in os.walk(path + '/selectedpop_' + str(gen)):
for file in f2:
if 'body' in file:
id = file.split('.')[0].split('_')[-1]
file_summary.write(gen+'\t'+id+'\n')
file_summary.close()