liupei101 / TFDeepSurv

COX Proportional risk model and survival analysis implemented by tensorflow.
MIT License
99 stars 27 forks source link

TypeError: Object of type int32 is not JSON serializable #7

Closed hmMed22 closed 4 years ago

hmMed22 commented 4 years ago

Hello, Thank you for your great packages. I run hpopt.py but at the end of the process, nothing has been written in JSON output file due to bellow error Any help would be greatly appreciated

raise TypeError(f'Object of type {o.__class__.__name__} '

TypeError: Object of type int32 is not JSON serializable`

hmMed22 commented 4 years ago

The problem was solved by adding this code class NumpyEncoder(json.JSONEncoder): """ Special json encoder for numpy types """ def default(self, obj): if isinstance(obj, np.integer): return int(obj) elif isinstance(obj, np.floating): return float(obj) elif isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) and json.dump(Logval, f,cls=NumpyEncoder)