diff --git a/experiments/examples/consolidate_experiments.py b/experiments/examples/consolidate_experiments.py index 9ccb2df05d..b512d681e0 100644 --- a/experiments/examples/consolidate_experiments.py +++ b/experiments/examples/consolidate_experiments.py @@ -1,13 +1,14 @@ import os # set these variables according to your experiments # -dirpath = '../' +dirpath = 'data' experiments_type = [ 'default_experiment' ] runs = 1 # set these variables according to your experiments # + def build_headers(path): print(path + "/all_measures.txt") @@ -36,11 +37,12 @@ def build_headers(path): return behavior_headers, phenotype_headers + for exp in experiments_type: for run in range(1, runs+1): print(exp, run) - path = dirpath + str(exp) + '_' + str(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") diff --git a/experiments/karines_experiments/consolidate_experiments.py b/experiments/karines_experiments/consolidate_experiments.py index fd60993d9a..e7d713d137 100644 --- a/experiments/karines_experiments/consolidate_experiments.py +++ b/experiments/karines_experiments/consolidate_experiments.py @@ -2,7 +2,7 @@ # set these variables according to your experiments # -dirpath = 'data/' +dirpath = 'data' experiments_type = [ 'plane', 'lava' @@ -10,6 +10,7 @@ runs = 10 # set these variables according to your experiments # + def build_headers(path): print(path + "/all_measures.txt") @@ -38,11 +39,12 @@ def build_headers(path): return behavior_headers, phenotype_headers + for exp in experiments_type: for run in range(1, runs+1): print(exp, run) - path = dirpath + str(exp) + '_' + str(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") diff --git a/experiments/karines_experiments/stuck-experiments_watchman.py b/experiments/karines_experiments/stuck-experiments_watchman.py index d65be96d26..272d59a67a 100644 --- a/experiments/karines_experiments/stuck-experiments_watchman.py +++ b/experiments/karines_experiments/stuck-experiments_watchman.py @@ -21,7 +21,7 @@ for exp in experiments_names: for run in range(0, runs): - path = dir_path + "/" + exp +'_'+str(run+1) + "/data_fullevolution/fitness" + path = os.path.join(dir_path, exp, str(run+1), 'data_fullevolution', 'fitness') time_now = datetime.now() time_ago = time_now - timedelta(minutes=limit_of_minutes) diff --git a/pyrevolve/evolution/population.py b/pyrevolve/evolution/population.py index d83d408bad..45ad959032 100644 --- a/pyrevolve/evolution/population.py +++ b/pyrevolve/evolution/population.py @@ -89,26 +89,26 @@ def _new_individual(self, genotype): individual.develop() self.conf.experiment_management.export_genotype(individual) self.conf.experiment_management.export_phenotype(individual) - self.conf.experiment_management.export_phenotype_images('data_fullevolution/phenotype_images', individual) - individual.phenotype.measure_phenotype(self.conf.experiment_name) - individual.phenotype.export_phenotype_measurements(self.conf.experiment_name) + self.conf.experiment_management.export_phenotype_images(os.path.join('data_fullevolution', 'phenotype_images'), individual) + individual.phenotype.measure_phenotype() + individual.phenotype.export_phenotype_measurements(self.conf.experiment_management.data_folder) return individual async def load_individual(self, id): - path = 'experiments/'+self.conf.experiment_name + data_path = self.conf.experiment_management.data_folder genotype = self.conf.genotype_constructor(self.conf.genotype_conf, id) - genotype.load_genotype(f'{path}/data_fullevolution/genotypes/genotype_{id}.txt') + genotype.load_genotype(os.path.join(data_path, 'genotypes', f'genotype_{id}.txt')) individual = Individual(genotype) individual.develop() - individual.phenotype.measure_phenotype(self.conf.experiment_name) + individual.phenotype.measure_phenotype() - with open(os.path.join(path, 'data_fullevolution', 'fitness', f'fitness_{id}.txt')) as f: + with open(os.path.join(data_path, 'fitness', f'fitness_{id}.txt')) as f: data = f.readlines()[0] individual.fitness = None if data == 'None' else float(data) - with open(os.path.join(path, 'data_fullevolution', 'descriptors', f'behavior_desc_{id}.txt')) as f: + with open(os.path.join(data_path, 'descriptors', f'behavior_desc_{id}.txt')) as f: lines = f.readlines() if lines[0] == 'None': individual.phenotype._behavioural_measurements = None @@ -135,8 +135,8 @@ async def load_snapshot(self, gen_num): Recovers all genotypes and fitnesses of robots in the lastest selected population :param gen_num: number of the generation snapshot to recover """ - path = 'experiments/'+self.conf.experiment_name - for r, d, f in os.walk(path +'/selectedpop_'+str(gen_num)): + data_path = self.conf.experiment_management.experiment_folder + for r, d, f in os.walk(data_path +'/selectedpop_'+str(gen_num)): for file in f: if 'body' in file: id = file.split('.')[0].split('_')[-2]+'_'+file.split('.')[0].split('_')[-1] diff --git a/pyrevolve/experiment_management.py b/pyrevolve/experiment_management.py index 72072e8d4e..af9f38207c 100644 --- a/pyrevolve/experiment_management.py +++ b/pyrevolve/experiment_management.py @@ -10,45 +10,49 @@ class ExperimentManagement: def __init__(self, settings): self.settings = settings - self.dirpath = os.path.join('experiments', self.settings.experiment_name) + manager_folder = os.path.dirname(self.settings.manager) + self._experiment_folder = os.path.join(manager_folder, 'data', self.settings.experiment_name, self.settings.run) + self._data_folder = os.path.join(self._experiment_folder, 'data_fullevolution') def create_exp_folders(self): - if os.path.exists(self.dirpath): - shutil.rmtree(self.dirpath) - os.mkdir(self.dirpath) - os.mkdir(self.dirpath+'/data_fullevolution') - os.mkdir(self.dirpath+'/data_fullevolution/genotypes') - os.mkdir(self.dirpath+'/data_fullevolution/phenotypes') - os.mkdir(self.dirpath+'/data_fullevolution/descriptors') - os.mkdir(self.dirpath+'/data_fullevolution/fitness') - os.mkdir(self.dirpath+'/data_fullevolution/phenotype_images') - os.mkdir(self.dirpath+'/data_fullevolution/failed_eval_robots') - - def _experiment_folder(self): - return self.dirpath - - def _data_folder(self): - return os.path.join(self.dirpath, 'data_fullevolution') + if os.path.exists(self.experiment_folder): + shutil.rmtree(self.experiment_folder) + os.makedirs(self.experiment_folder) + os.mkdir(self.data_folder) + os.mkdir(os.path.join(self.data_folder, 'genotypes')) + os.mkdir(os.path.join(self.data_folder, 'phenotypes')) + os.mkdir(os.path.join(self.data_folder, 'descriptors')) + os.mkdir(os.path.join(self.data_folder, 'fitness')) + os.mkdir(os.path.join(self.data_folder, 'phenotype_images')) + os.mkdir(os.path.join(self.data_folder, 'failed_eval_robots')) + + @property + def experiment_folder(self): + return self._experiment_folder + + @property + def data_folder(self): + return self._data_folder def export_genotype(self, individual): if self.settings.recovery_enabled: - individual.export_genotype(self._data_folder()) + individual.export_genotype(self.data_folder) def export_phenotype(self, individual): if self.settings.export_phenotype: - individual.export_phenotype(self._data_folder()) + individual.export_phenotype(self.data_folder) def export_fitnesses(self, individuals): - folder = self._data_folder() + folder = self.data_folder for individual in individuals: individual.export_fitness(folder) def export_fitness(self, individual): - folder = os.path.join(self._data_folder(),'fitness') + folder = os.path.join(self.data_folder, 'fitness') individual.export_fitness(folder) def export_behavior_measures(self, _id, measures): - filename = os.path.join(self._data_folder(), 'descriptors', f'behavior_desc_{_id}.txt') + filename = os.path.join(self.data_folder, 'descriptors', f'behavior_desc_{_id}.txt') with open(filename, "w") as f: if measures is None: f.write(str(None)) @@ -57,17 +61,17 @@ def export_behavior_measures(self, _id, measures): f.write(f"{key} {val}\n") def export_phenotype_images(self, dirpath, individual): - individual.phenotype.render_body(self._experiment_folder() +'/'+dirpath+f'/body_{individual.phenotype.id}.png') - individual.phenotype.render_brain(self._experiment_folder() +'/'+dirpath+f'/brain_{individual.phenotype.id}') + individual.phenotype.render_body(os.path.join(self.experiment_folder, dirpath, f'body_{individual.phenotype.id}.png')) + individual.phenotype.render_brain(os.path.join(self.experiment_folder, dirpath, f'brain_{individual.phenotype.id}.png')) def export_failed_eval_robot(self, individual): - individual.genotype.export_genotype(f'{self._data_folder()}/failed_eval_robots/genotype_{str(individual.phenotype.id)}.txt') - individual.phenotype.save_file(f'{self._data_folder()}/failed_eval_robots/phenotype_{str(individual.phenotype.id)}.yaml') - individual.phenotype.save_file(f'{self._data_folder()}/failed_eval_robots/phenotype_{str(individual.phenotype.id)}.sdf', conf_type='sdf') + individual.genotype.export_genotype(os.path.join(self.data_folder, 'failed_eval_robots', f'genotype_{individual.phenotype.id}.txt')) + individual.phenotype.save_file(os.path.join(self.data_folder, 'failed_eval_robots', f'phenotype_{individual.phenotype.id}.yaml')) + individual.phenotype.save_file(os.path.join(self.data_folder, 'failed_eval_robots', f'phenotype_{individual.phenotype.id}.sdf'), conf_type='sdf') def export_snapshots(self, individuals, gen_num): if self.settings.recovery_enabled: - path = os.path.join(self._experiment_folder(), f'selectedpop_{gen_num}') + path = os.path.join(self.experiment_folder, f'selectedpop_{gen_num}') if os.path.exists(path): shutil.rmtree(path) os.mkdir(path) @@ -76,9 +80,9 @@ def export_snapshots(self, individuals, gen_num): logger.info(f'Exported snapshot {str(gen_num)} with {str(len(individuals))} individuals') def experiment_is_new(self): - if not os.path.exists(self._experiment_folder()): + if not os.path.exists(self.experiment_folder): return True - path, dirs, files = next(os.walk(os.path.join(self._data_folder(), 'fitness'))) + path, dirs, files = next(os.walk(os.path.join(self.data_folder, 'fitness'))) if len(files) == 0: return True else: @@ -87,10 +91,10 @@ def experiment_is_new(self): def read_recovery_state(self, population_size, offspring_size): snapshots = [] - for r, d, f in os.walk(self._experiment_folder()): + for r, d, f in os.walk(self.experiment_folder): for dir in d: if 'selectedpop' in dir: - exported_files = len([name for name in os.listdir(os.path.join(self._experiment_folder(), dir)) if os.path.isfile(os.path.join(self._experiment_folder(), dir, name))]) + exported_files = len([name for name in os.listdir(os.path.join(self.experiment_folder, dir)) if os.path.isfile(os.path.join(self.experiment_folder, dir, name))]) if exported_files == (population_size * 2): # body and brain files snapshots.append(int(dir.split('_')[1])) @@ -104,7 +108,7 @@ def read_recovery_state(self, population_size, offspring_size): n_robots = 0 robot_ids = [] - for r, d, f in os.walk(os.path.join(self._data_folder(), 'fitness')): + for r, d, f in os.walk(os.path.join(self.data_folder, 'fitness')): for file in f: robot_ids.append(int(file.split('.')[0].split('_')[-1])) last_id = np.sort(robot_ids)[-1] diff --git a/pyrevolve/revolve_bot/revolve_bot.py b/pyrevolve/revolve_bot/revolve_bot.py index 848884858c..f9b59456dc 100644 --- a/pyrevolve/revolve_bot/revolve_bot.py +++ b/pyrevolve/revolve_bot/revolve_bot.py @@ -68,7 +68,7 @@ def measure_behaviour(self): """ pass - def measure_phenotype(self, experiment_name): + def measure_phenotype(self): self._morphological_measurements = self.measure_body() self._brain_measurements = self.measure_brain() logger.info('Robot ' + str(self.id) + ' was measured.') @@ -86,14 +86,13 @@ def measure_body(self): except Exception as e: logger.exception('Failed measuring body') - def export_phenotype_measurements(self, path): - with open('experiments/' + path + '/data_fullevolution/descriptors/' - + 'phenotype_desc_' + str(self.id) + '.txt', 'w+') as file: - # TODO this crashes + def export_phenotype_measurements(self, data_path): + filepath = os.path.join(data_path, 'descriptors', f'phenotype_desc_{self.id}.txt') + with open(filepath, 'w+') as file: for key, value in self._morphological_measurements.measurements_to_dict().items(): - file.write('{} {}\n'.format(key, value)) + file.write(f'{key} {value}\n') for key, value in self._brain_measurements.measurements_to_dict().items(): - file.write('{} {}\n'.format(key, value)) + file.write(f'{key} {value}\n') def measure_brain(self): """