Skip to content

Commit

Permalink
Handle numpy types in monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
tlbtlbtlb committed Dec 30, 2016
1 parent 4cec141 commit 1059ccd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion gym/monitoring/monitor_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _flush(self, force=False):
'videos': [(os.path.basename(v), os.path.basename(m))
for v, m in self.videos],
'env_info': self._env_info(),
}, f)
}, f, default=json_encode)

def close(self):
"""Flush all monitor data to disk and close any open rending windows."""
Expand Down Expand Up @@ -408,3 +408,21 @@ def collapse_env_infos(env_infos, training_dir):
if key not in first:
raise error.Error("env_info {} from training directory {} is missing expected key {}. This is unexpected and likely indicates a bug in gym.".format(first, training_dir, key))
return first


def json_encode(obj):
"""
JSON can't serialize numpy types, so convert to pure python
"""
if isinstance(obj, np.ndarray):
return list(obj)
elif isinstance(obj, np.float32):
return float(obj)
elif isinstance(obj, np.float64):
return float(obj)
elif isinstance(obj, np.int32):
return int(obj)
elif isinstance(obj, np.int64):
return int(obj)
else:
return obj
2 changes: 1 addition & 1 deletion gym/monitoring/stats_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def after_reset(self, observation):
def save_complete(self):
if self.steps is not None:
self.episode_lengths.append(self.steps)
self.episode_rewards.append(self.rewards)
self.episode_rewards.append(float(self.rewards))
self.timestamps.append(time.time())

def close(self):
Expand Down

0 comments on commit 1059ccd

Please sign in to comment.