def processResultPath(new_base, resultAndPath): result, path = resultAndPath mean, stderr, count = result # sampled_mean = windowAverage(mean) # sampled_stderr = windowAverage(stderr) sampled_mean = everyN(mean, EVERY) sampled_stderr = everyN(stderr, EVERY) sampled = [sampled_mean, sampled_stderr, count] new_path = new_base + '/' + rest(path) os.makedirs(up(new_path), exist_ok=True) np.save(new_path, sampled)
bounds.append(b) if __name__ == "__main__": f, axes = plt.subplots(len(algorithms), len(problems)) for i, alg in enumerate(algorithms): for j, problem in enumerate(problems): bounds = [] exp_paths = glob.glob(f'experiments/stepsizes/{problem}/{alg}/*.json') if len(exp_paths) == 0: continue exp = loadExperiment(exp_paths[0]) path = up(up(first(exp_paths))) + '/lstd.json' lstd_exp = loadExperiment(path) LSTD_res = loadResults(lstd_exp, errorfile) LSTD_best = getBest(LSTD_res) b = plotBest(LSTD_best, axes[i, j], color=colors['LSTD'], label='LSTD', alphaMain=0.5) bounds.append(b) if '_h' in alg or alg == 'td': generatePlotSSA(axes[i, j], exp_paths, bounds) else: generatePlotTTA(axes[i, j], exp_paths, bounds) lower = min(map(lambda x: x[0], bounds)) * 0.9 upper = max(map(lambda x: x[1], bounds)) * 1.05
import sys import json import glob import os sys.path.append(os.getcwd()) from src.utils.path import fileName, up old_agent_path = sys.argv[1] problems = sys.argv[2:] problems = filter( lambda p: '.' not in p and 'plots' not in p and 'LeftZero' not in p and 'Random' not in p and '5050' not in p, problems) with open(old_agent_path, 'r') as f: old_agent = f.read() old_agent_base_name = fileName(up(old_agent_path)) old_agent_name = fileName(old_agent_path).replace('.json', '') old_problem = fileName(up(up(old_agent_path))) for problem in problems: path = f'{problem}/{old_agent_base_name}/{old_agent_name}.json' new = old_agent.replace(old_problem, fileName(problem)) os.makedirs(up(path), exist_ok=True) with open(path, 'w') as f: f.write(new)
import os import sys import json import numpy as np sys.path.append(os.getcwd()) from src.analysis.results import loadResults, getBest from src.utils.model import loadExperiment from src.utils.path import up, fileName exp_paths = sys.argv[1:] for exp_path in exp_paths: exp = loadExperiment(exp_path) results = loadResults(exp, 'rmspbe_summary.npy') best = getBest(results) print('---------------------') print('agent:', exp.agent) print(best.params) f = fileName(exp_path) new_path = up(exp_path) + '/best_rmspbe_auc/' + f d = exp._d d['metaParameters'] = best.params os.makedirs(up(new_path), exist_ok=True) with open(new_path, 'w') as f: json.dump(d, f, indent=4)
import sys import json import glob import os sys.path.append(os.getcwd()) from src.utils.path import fileName, up prob_folder = sys.argv[1] new_prob = sys.argv[2] jsons = glob.glob(f'{prob_folder}/**/*.json', recursive=True) old_prob = fileName(prob_folder) for json in jsons: with open(json, 'r') as f: d = f.read() new = d.replace(old_prob, new_prob) new_path = json.replace(old_prob, new_prob) os.makedirs(up(new_path), exist_ok=True) with open(new_path, 'w') as f: f.write(new)
def generatePlot(exp_paths): f, axes = plt.subplots(2, 2) # get LSTD solution path = up(up(first(exp_paths))) + '/lstd.json' exp = loadExperiment(path) LSTD_rmsve_results = loadResults(exp, 'errors_summary.npy') LSTD_rmspbe_results = loadResults(exp, 'rmspbe_summary.npy') LSTD_rmsve = metric(LSTD_rmsve_results) LSTD_rmspbe = metric(LSTD_rmspbe_results) rmspbe_bounds = [] rmsve_bounds = [] bounds = plotBest(LSTD_rmspbe, axes[0, 0], color=colors['LSTD'], label='LSTD', alphaMain=0.5) rmspbe_bounds.append(bounds) bounds = plotBest(LSTD_rmspbe, axes[0, 1], color=colors['LSTD'], label='LSTD', alphaMain=0.5) rmspbe_bounds.append(bounds) bounds = plotBest(LSTD_rmsve, axes[1, 0], color=colors['LSTD'], label='LSTD', alphaMain=0.5) rmsve_bounds.append(bounds) bounds = plotBest(LSTD_rmsve, axes[1, 1], color=colors['LSTD'], label='LSTD', alphaMain=0.5) rmsve_bounds.append(bounds) # RMSPBE plots ax = axes[0, 0] for exp_path in exp_paths: exp = loadExperiment(exp_path) results = loadResults(exp, 'rmspbe_summary.npy') const, unconst = tee(results) const = whereParameterGreaterEq(const, 'ratio', 1) use_ideal_h = exp._d['metaParameters'].get('use_ideal_h', False) agent = exp.agent if 'SmoothTDC' in agent: average = exp._d['metaParameters']['averageType'] agent += '_' + average color = colors[agent] label = agent.replace('adagrad', '') if not (exp.agent in ['TDadagrad', 'TDschedule', 'TD', 'TDamsgrad'] or use_ideal_h): if UNCONSTRAINED: bounds = plot(unconst, ax, label=label + '_unc', color=color, dashed=True, bestBy=bestBy) rmspbe_bounds.append(bounds) bounds = plot(const, ax, label=label, color=color, dashed=False, bestBy=bestBy) rmspbe_bounds.append(bounds) else: bounds = plot(unconst, ax, label=label, color=color, dashed=False, bestBy=bestBy) rmspbe_bounds.append(bounds) ax.set_ylabel("MSPBE") ax.set_title("MSPBE") ax = axes[0, 1] for exp_path in exp_paths: exp = loadExperiment(exp_path) rmsve = loadResults(exp, 'errors_summary.npy') results = loadResults(exp, 'rmspbe_summary.npy') const, unconst = tee(rmsve) const_res, unconst_res = tee(results) const = whereParameterGreaterEq(const, 'ratio', 1) use_ideal_h = exp._d['metaParameters'].get('use_ideal_h', False) agent = exp.agent if 'SmoothTDC' in agent: average = exp._d['metaParameters']['averageType'] agent += '_' + average color = colors[agent] label = agent.replace('adagrad', '') if not (exp.agent in ['TDadagrad', 'TDschedule', 'TD', 'TDamsgrad'] or use_ideal_h): best = metric(const) best_unc = metric(unconst) best_rmspbe = find(const_res, best) best_rmspbe_unc = find(unconst_res, best_unc) bounds = plotBest(best_rmspbe, ax, label=label, color=color, dashed=False) rmspbe_bounds.append(bounds) if UNCONSTRAINED: bounds = plotBest(best_rmspbe_unc, ax, label=label + '_unc', color=color, dashed=True) rmspbe_bounds.append(bounds) else: best = metric(unconst) best_rmspbe = find(unconst_res, best) bounds = plotBest(best_rmspbe, ax, label=label, color=color, dashed=False) rmspbe_bounds.append(bounds) ax.set_title("MSVE") # RMSVE plots ax = axes[1, 0] for exp_path in exp_paths: exp = loadExperiment(exp_path) rmsve = loadResults(exp, 'errors_summary.npy') rmspbe = loadResults(exp, 'rmspbe_summary.npy') const, unconst = tee(rmspbe) const_res, unconst_res = tee(rmsve) const = whereParameterGreaterEq(const, 'ratio', 1) use_ideal_h = exp._d['metaParameters'].get('use_ideal_h', False) agent = exp.agent if 'SmoothTDC' in agent: average = exp._d['metaParameters']['averageType'] agent += '_' + average color = colors[agent] label = agent.replace('adagrad', '') if not (exp.agent in ['TDadagrad', 'TDschedule', 'TD', 'TDamsgrad'] or use_ideal_h): # best PBE using AUC best = metric(const) best_unc = metric(unconst) best_rmsve = find(const_res, best) best_rmsve_unc = find(unconst_res, best_unc) print('rmsve_over_rmspbe') print(label, best_rmsve.params) print(label, best_rmsve_unc.params) bounds = plotBest(best_rmsve, ax, label=label, color=color, dashed=False) rmsve_bounds.append(bounds) if UNCONSTRAINED: bounds = plotBest(best_rmsve_unc, ax, label=label + '_unc', color=color, dashed=True) rmsve_bounds.append(bounds) else: best = metric(unconst) best_rmsve = find(unconst_res, best) bounds = plotBest(best_rmsve, ax, label=label, color=color, dashed=False) rmsve_bounds.append(bounds) ax.set_ylabel("MSVE") ax = axes[1, 1] for exp_path in exp_paths: exp = loadExperiment(exp_path) results = loadResults(exp) const, unconst = tee(results) const = whereParameterGreaterEq(const, 'ratio', 1) use_ideal_h = exp._d['metaParameters'].get('use_ideal_h', False) agent = exp.agent if 'SmoothTDC' in agent: average = exp._d['metaParameters']['averageType'] agent += '_' + average color = colors[agent] label = agent.replace('adagrad', '') if not (exp.agent in ['TDadagrad', 'TDschedule', 'TD', 'TDamsgrad'] or use_ideal_h): bounds = plot(const, ax, label=label, color=color, dashed=False, bestBy=bestBy) rmsve_bounds.append(bounds) if UNCONSTRAINED: bounds = plot(unconst, ax, label=label + '_unc', color=color, dashed=True, bestBy=bestBy) rmsve_bounds.append(bounds) else: bounds = plot(unconst, ax, label=label, color=color, dashed=False, bestBy=bestBy) rmsve_bounds.append(bounds) # rmspbe rmspbe_lower = min(map(lambda x: x[0], rmspbe_bounds)) * 0.9 rmspbe_upper = max(map(lambda x: x[1], rmspbe_bounds)) * 1.05 if rmspbe_lower < 0.01: rmspbe_lower = -0.01 axes[0, 0].set_ylim([rmspbe_lower, rmspbe_upper]) axes[0, 1].set_ylim([rmspbe_lower, rmspbe_upper]) # rmsve rmsve_lower = min(map(lambda x: x[0], rmsve_bounds)) * 0.9 rmsve_upper = max(map(lambda x: x[1], rmsve_bounds)) * 1.05 if rmsve_lower < 0.01: rmsve_lower = -0.01 axes[1, 0].set_ylim([0, 20]) axes[1, 1].set_ylim([0, 20])
if 'lstd' in exp_path or 'htd' in exp_path or ( 'tdc_ema' in exp_path and not 'tdc_ema_x' in exp_path): continue if stepsize != 'constant' and stepsize not in exp_path: continue if stepsize == 'constant' and ('amsgrad' in exp_path or 'adagrad' in exp_path or 'schedule' in exp_path): continue exp = loadExperiment(exp_path) use_ideal_h = exp._d['metaParameters'].get('use_ideal_h', False) if use_ideal_h: continue paths.append(exp_path) generatePlot(paths) plt.show() exit() exp_name = fileName(up(exp.getExperimentName())) save_path = f'experiments/stepsizes/plots/2x2/{bestBy}' os.makedirs(save_path, exist_ok=True) fig = plt.gcf() fig.set_size_inches((13, 12), forward=False) plt.savefig(f'{save_path}/{exp_name}_square_{stepsize}.png', dpi=125)