Skip to content

Commit

Permalink
[tune] Fix _SafeFallbackEncoder type checks (ray-project#4238)
Browse files Browse the repository at this point in the history
* Fix numpy type checks for _SafeFallbackEncoder

* Format changes

* Fix usage of nan_str in _SafeFallbackEncoder
  • Loading branch information
hartikainen authored and richardliaw committed Mar 18, 2019
1 parent 27cd6ea commit 2a04611
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions python/ray/tune/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import csv
import json
import logging
import numpy as np
import os
import yaml
import distutils.version
import numbers

import numpy as np

import ray.cloudpickle as cloudpickle
from ray.tune.log_sync import get_syncer
Expand Down Expand Up @@ -255,11 +257,19 @@ def __init__(self, nan_str="null", **kwargs):
def default(self, value):
try:
if np.isnan(value):
return None
if np.issubdtype(value, float):
return float(value)
if np.issubdtype(value, int):
return self.nan_str

if (type(value).__module__ == np.__name__
and isinstance(value, np.ndarray)):
return value.tolist()

if issubclass(type(value), numbers.Integral):
return int(value)
if issubclass(type(value), numbers.Number):
return float(value)

return super(_SafeFallbackEncoder, self).default(value)

except Exception:
return str(value) # give up, just stringify it (ok for logs)

Expand Down

0 comments on commit 2a04611

Please sign in to comment.