def on_episode_end(self, episode, logs):
duration = timeit.default_timer() - self.starts[episode]<EOL>metrics = self.metrics[episode]<EOL>if np.isnan(metrics).all():<EOL><INDENT>mean_metrics = np.array([np.nan for _ in self.metrics_names])<EOL><DEDENT>else:<EOL><INDENT>mean_metrics = np.nanmean(metrics, axis=<NUM_LIT:0>)<EOL><DEDENT>assert len(mean_metrics) == len(self.metrics_names)<EOL>data = list(zip(self.metrics_names, mean_metrics))<EOL>data += list(logs.items())<EOL>data += [('<STR_LIT>', episode), ('<STR_LIT>', duration)]<EOL>for key, value in data:<EOL><INDENT>if key not in self.data:<EOL><INDENT>self.data[key] = []<EOL><DEDENT>self.data[key].append(value)<EOL><DEDENT>if self.interval is not None and episode % self.interval == <NUM_LIT:0>:<EOL><INDENT>self.save_data()<EOL><DEDENT>del self.metrics[episode]<EOL>del self.starts[episode]<EOL>
Compute and print metrics at the end of each episode