[docs]defget_best_checkpoint(trial_dir:str,metric:Optional[str]='episode_reward_mean',mode:Optional[str]='max')->str:""" Gets best persistent checkpoint path of provided trial. Args: trial: The log directory of a trial instance which contains all the checkpoints metric: The metric by which the best checkpoint would be chosen, like 'episode_reward_mean' mode: One of [min, max]. Returns: string (path to checkpoint) """trial_dir=os.path.abspath(trial_dir)df_metrics=pd.read_json(os.path.join(trial_dir,'result.json'),lines=True)df_metrics=pd.json_normalize(df_metrics["sampler_results"])defget_path_and_metric(checkpoint,metric):checkpoint_nr=int(checkpoint[-6:])-1return(checkpoint,df_metrics.iloc[checkpoint_nr][metric])checkpoint_paths=[get_path_and_metric(cp,metric)forcpinnext(os.walk(trial_dir))[1]]checkpoint_paths=[(cp,metric)for(cp,metric)incheckpoint_pathsifnotis_nan(metric)]ifnotcheckpoint_paths:logging.Logger(name='No Checkpoint').error("No checkpoints have been found for trial: "+trial_dir)returna=-1ifmode=="max"else1best_path_metrics=sorted(checkpoint_paths,key=lambdax:a*x[1])returnos.path.join(trial_dir,best_path_metrics[0][0])