code: def save_data(self):
          
          if len(self.data.keys()) == 0:
              return
  
          # Sort everything by episode.
          assert 'episode' in self.data
          sorted_indexes = np.argsort(self.data['episode'])
          sorted_data = {}
          for key, values in self.data.items():
              assert len(self.data[key]) == len(sorted_indexes)
              # We convert to np.array() and then to list to convert from np datatypes to native datatypes.
              # This is necessary because json.dump cannot handle np.float32, for example.
              sorted_data[key] = np.array([self.data[key][idx] for idx in sorted_indexes]).tolist()
  
          # Overwrite already open file. We can simply seek to the beginning since the file will
          # grow strictly monotonously.
          with open(self.filepath, 'w') as f:
              json.dump(sorted_data, f)
docs: Save metrics in a json file