def save_env_info(env, env_info, episode, env_hist_path): """ Saves the environment info dictionary to a csv args env (energ_py environment) env_info (dict) the info dict returned from env.step() episode (int) """ if hasattr(env.observation_space, 'info') and hasattr( env.state_space, 'info'): logging.debug('saving env history') state_info = env.state_space.info observation_info = env.observation_space.info output = [] for key, info in env_info.items(): if isinstance(info[0], np.ndarray): df = pd.DataFrame(np.array(info).reshape(len(info), -1)) # there must be a better way! TODO if key == 'observation' and observation_info: df.columns = [ '{}_{}'.format(key, o) for o in observation_info ] elif key == 'next_observation' and observation_info: df.columns = [ '{}_{}'.format(key, o) for o in observation_info ] elif key == 'state' and state_info: df.columns = ['{}_{}'.format(key, s) for s in state_info] elif key == 'next_state' and state_info: df.columns = ['{}_{}'.format(key, s) for s in state_info] else: df.columns = [ '{}_{}'.format(key, n) for n in range(df.shape[1]) ] else: df = pd.DataFrame(info, columns=[key]) output.append(df) output = pd.concat(output, axis=1) output.index = env.state_space.episode.index[:-1] csv_path = os.path.join(env_hist_path, 'ep_{}'.format(episode), 'info.csv') ensure_dir(csv_path) output.to_csv(csv_path) else: logging.debug('Not saving env history')
def make_paths(experiments_dir, expt_name, run_name): """ Creates a dictionary of paths for use with experiments args experiments_dir (str) usually energy_py/energy_py/experiments expt_name (str) run_name (str) returns paths (dict) {name: path} Folder structure experiments/configs/expt_name/expt.ini runs.ini experiments/results/expt_name/run_name/tensorboard/run_name/rl /act /learn env_histories/ep_1/info.csv ep_2/info.csv e.. expt.ini runs.ini agent_args.txt env_args.txt info.log debug.log """ # rename the join function to make code below eaiser to read join = os.path.join config_dir = join(experiments_dir, 'configs', expt_name) results_dir = join(experiments_dir, 'results', expt_name) paths = { # config files 'expt_config': join(config_dir, 'expt.ini'), 'run_configs': join(config_dir, 'runs.ini'), # tensorboard runs are all in the tensoboard folder # this is for easy comparision of runs 'tb_rl': join(results_dir, 'tensorboard', run_name, 'rl'), 'tb_act': join(results_dir, 'tensorboard', run_name, 'act'), 'tb_learn': join(results_dir, 'tensorboard', run_name, 'learn'), # run specific folders are in another folder 'env_histories': join(results_dir, run_name, 'env_histories'), 'debug_log': join(results_dir, run_name, 'debug.log'), 'info_log': join(results_dir, run_name, 'info.log'), 'env_args': join(results_dir, run_name, 'env_args.txt'), 'agent_args': join(results_dir, run_name, 'agent_args.txt'), 'ep_rewards': join(results_dir, run_name, 'ep_rewards.csv') } # check that all our paths exist for key, path in paths.items(): ensure_dir(path) # copy config files into results directory copyfile(paths['expt_config'], join(results_dir, run_name, 'expt.ini')) copyfile(paths['run_configs'], join(results_dir, run_name, 'runs.ini')) return paths
def make_paths(expt_path, run_name=None): """ Creates a dictionary of paths for use with experiments args expt_path (str) run_name (str) optional name for run. Timestamp used if not given returns paths (dict) {name: path} Folder structure experiments/results/expt_name/run_name/tensoboard/run_name/rl /act /learn env_histories/ep_1/hist.csv ep_2/hist.csv e.. common.ini run_configs.ini agent_args.txt env_args.txt info.log debug.log """ # use a timestamp if no run_name is supplied if run_name is None: run_name = str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) # rename the join function to make code below eaiser to read join = os.path.join # run_path is the folder where output from this run will be saved in run_path = join(expt_path, run_name) paths = { 'run_path': run_path, # config files 'common_config': join(expt_path, 'common.ini'), 'run_configs': join(expt_path, 'run_configs.ini'), # tensorboard runs are all in the tensoboard folder # this is for easy comparision of run 'tb_rl': join(expt_path, 'tensorboard', run_name, 'rl'), 'tb_act': join(expt_path, 'tensorboard', run_name, 'act'), 'tb_learn': join(expt_path, 'tensorboard', run_name, 'learn'), 'env_histories': join(run_path, 'env_histories'), # run specific folders are in another folder 'debug_log': join(run_path, 'debug.log'), 'info_log': join(run_path, 'info.log'), 'env_args': join(run_path, 'env_args.txt'), 'agent_args': join(run_path, 'agent_args.txt'), 'ep_rewards': join(run_path, 'ep_rewards.csv') } # check that all our paths exist for key, path in paths.items(): ensure_dir(path) return paths