def eval_damping(): """ Plot joint trajectories for different joint damping parameters """ # Load experiment and remove possible randomization wrappers ex_dir = ask_for_experiment() env, policy, _ = load_experiment(ex_dir) env = inner_env(env) env.domain_param = WAMBallInCupSim.get_nominal_domain_param() data = [] t = [] dampings = [0., 1e-2, 1e-1, 1e0] print_cbt(f'Run policy for damping coefficients: {dampings}') for d in dampings: env.reset(domain_param=dict(joint_damping=d)) ro = rollout(env, policy, render_mode=RenderMode(video=False), eval=True) t.append(ro.env_infos['t']) data.append(ro.env_infos['qpos']) fig, ax = plt.subplots(3, sharex='all') ls = ['k-', 'b--', 'g-.', 'r:'] # line style setting for better visibility for i, idx in enumerate([1, 3, 5]): for j in range(len(dampings)): ax[i].plot(t[j], data[j][:, idx], ls[j], label=f'damping: {dampings[j]}') if i == 0: ax[i].legend() ax[i].set_ylabel(f'joint {idx} pos [rad]') ax[2].set_xlabel('time [s]') plt.suptitle('Evaluation of joint damping coefficient') plt.show()
def load_teachers(self): """Recursively load all teachers that can be found in the current experiment's directory.""" # Get the experiment's directory to load from ex_dir = ask_for_experiment(max_display=10, env_name=self.env_real.name, perma=False) self.load_teacher_experiment(ex_dir) if len(self.teacher_policies) < self.num_teachers: print( f"You have loaded {len(self.teacher_policies)} teachers - load at least {self.num_teachers - len(self.teacher_policies)} more!" ) self.load_teachers()
import torch as to from matplotlib import pyplot as plt import pyrado from pyrado.logger.experiment import ask_for_experiment from pyrado.plotting.gaussian_process import render_singletask_gp from pyrado.utils.argparser import get_argparser if __name__ == '__main__': # Parse command line arguments args = get_argparser().parse_args() plt.rc('text', usetex=args.use_tex) # Get the experiment's directory to load from ex_dir = ask_for_experiment() if args.ex_dir is None else args.ex_dir cands = to.load(osp.join(ex_dir, 'candidates.pt')) cands_values = to.load(osp.join(ex_dir, 'candidates_values.pt')).unsqueeze(1) dim_cand = cands.shape[1] # number of domain distribution parameters if dim_cand%2 != 0: raise pyrado.ShapeErr(msg='The dimension of domain distribution parameters must be a multiple of 2!') # Select dimensions to plot (ignored for 1D mode) if len(args.idcs) != 2: raise pyrado.ShapeErr(msg='Select exactly 2 indices!') fig_size = (12, 10) # Plot
import pyrado from pyrado.environment_wrappers.domain_randomization import remove_all_dr_wrappers from pyrado.logger.experiment import ask_for_experiment from pyrado.sampling.rollout import rollout from pyrado.utils.argparser import get_argparser from pyrado.utils.data_types import RenderMode from pyrado.utils.experiments import load_experiment from pyrado.utils.input_output import print_cbt if __name__ == "__main__": # Parse command line arguments args = get_argparser().parse_args() # Get the experiment's directory to load from if not given as command line argument ex_dir = ask_for_experiment( hparam_list=args.show_hparams) if args.dir is None else args.dir # Load real trajectories mode = "" real_data_exists = True try: while mode not in ["ep", "sb"]: mode = input( "Pass ep for episodic and sb for step-based control mode: " ).lower() qpos_real = np.load(osp.join(ex_dir, f"qpos_real_{mode}.npy")) qvel_real = np.load(osp.join(ex_dir, f"qvel_real_{mode}.npy")) except FileNotFoundError: real_data_exists = False print_cbt( f"Did not find a recorded real trajectory (qpos_real_{mode} and qvel_real_{mode}) for this policy. "
import pyrado from pyrado.environment_wrappers.domain_randomization import MetaDomainRandWrapper from pyrado.domain_randomization.utils import print_domain_params from pyrado.logger.experiment import ask_for_experiment, load_dict_from_yaml from pyrado.sampling.rollout import rollout, after_rollout_query from pyrado.utils.argparser import get_argparser from pyrado.utils.input_output import print_cbt from pyrado.utils.data_types import RenderMode if __name__ == '__main__': # Parse command line arguments args = get_argparser().parse_args() # Get the experiment's directory to load from ex_dir = ask_for_experiment() if not osp.isdir(ex_dir): raise pyrado.PathErr(given=ex_dir) # Load the environment randomizer env_sim = joblib.load(osp.join(ex_dir, 'env_sim.pkl')) hparam = load_dict_from_yaml(osp.join(ex_dir, 'hyperparams.yaml')) # Override the time step size if specified if args.dt is not None: env_sim.dt = args.dt # Crawl through the given directory and check how many init policies and candidates there are for root, dirs, files in os.walk(ex_dir): if args.load_all: found_policies = [p for p in files if p.endswith('_policy.pt')]
{"num_rpp": args.num_rollouts_per_config, "seed": args.seed}, {"metrics": dict_arraylike_to_float(metrics)}, save_dir=save_dir, file_name="summary", ) pyrado.save(df, f"df_sp_grid_{len(param_spec) if param_spec_dim is None else param_spec_dim}d.pkl", save_dir) if __name__ == "__main__": # Parse command line arguments g_args = get_argparser().parse_args() if g_args.load_all: if not g_args.dir: raise pyrado.PathErr(msg="load_all was set but no dir was given") if not os.path.isdir(g_args.dir): raise pyrado.PathErr(given=g_args.dir) g_ex_dirs = [tmp[0] for tmp in os.walk(g_args.dir, followlinks=True) if "policy.pt" in tmp[2]] elif g_args.dir is None: g_ex_dirs = [ask_for_experiment(hparam_list=g_args.show_hyperparameters, max_display=50)] else: g_ex_dirs = [g_args.dir] print(f"Evaluating all of {g_ex_dirs}.") for g_ex_dir in g_ex_dirs: print(f"Evaluating {g_ex_dir}.") evaluate_policy(g_args, g_ex_dir)
def _main(): # Parse command line arguments argparser = get_argparser() argparser.add_argument( "--average", action="store_true", help= "average over all loaded policies (default: False); create only a single heatmap", ) argparser.add_argument( "--save_dir", help="if --average is set, the directory to save the plot to") args = argparser.parse_args() # Get the experiment's directory to load from if args.dir is None: ex_dirs = [] while True: ex_dirs.append( ask_for_experiment( show_hyper_parameters=args.show_hyperparameters, max_display=50)) if input("Ask for more (Y/n)? ") == "n": break else: ex_dirs = [d.strip() for d in args.dir.split(",")] eval_parent_dirs = [] for ex_dir in ex_dirs: eval_parent_dir = osp.join(ex_dir, "eval_domain_grid") if not osp.isdir(eval_parent_dir): raise pyrado.PathErr(given=eval_parent_dir) eval_parent_dirs.append(eval_parent_dir) if args.load_all: list_eval_dirs = [] for eval_parent_dir in eval_parent_dirs: list_eval_dirs += [tmp[0] for tmp in os.walk(eval_parent_dir)][1:] else: list_eval_dirs = [ osp.join(eval_parent_dir, "ENV_NAME", "ALGO_NAME") for eval_parent_dir in eval_parent_dirs ] dataframes, eval_dirs = [], [] for eval_dir in list_eval_dirs: assert osp.isdir(eval_dir) # Load the data pickle_file = osp.join(eval_dir, "df_sp_grid_2d.pkl") if not osp.isfile(pickle_file): print(f"{pickle_file} is not a file! Skipping...") continue df = pd.read_pickle(pickle_file) dataframes.append(df) eval_dirs.append(eval_dir) if args.average: _plot([sum(dataframes) / len(dataframes)], [args.save_dir], True) else: _plot(dataframes, eval_dirs, args.save)
save_figure=args.save, save_dir=eval_dir, ) plt.show() if __name__ == "__main__": # Parse command line arguments g_args = get_argparser().parse_args() if g_args.load_all and g_args.dir: if not os.path.isdir(g_args.dir): raise pyrado.PathErr(given=g_args.dir) g_ex_dirs = [ tmp[0] for tmp in os.walk(g_args.dir) if "policy.pt" in tmp[2] ] elif g_args.dir is None: g_ex_dirs = [ ask_for_experiment( show_hyper_parameters=g_args.show_hyperparameters, max_display=50) ] else: g_ex_dirs = [g_args.dir] print(f"Plotting all of {g_ex_dirs}.") for g_ex_dir in g_ex_dirs: print(f"Plotting {g_ex_dir}.") plot_policy(g_args, g_ex_dir)