def json_numpy_obj_hook(dct): """ Decodes a previously encoded numpy ndarray with proper shape and dtype, or an encoded KeywordRegistry. :param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray """ if isinstance(dct, dict) and '__ndarray__' in dct: # data = base64.b64decode(dct['__ndarray__']) data = dct['__ndarray__'] return np.asarray(data, dct['dtype']).reshape(dct['shape']) special_keys = ['level', 'coefficients', 'amplitude', 'kappa', 'base', 'shift', 'mean', 'sd', 'u', 'tau', 'offset'] if isinstance(dct, dict) and any(k in special_keys for k in dct): # print("json_numpy_obj_hook: {0} type {1}".format(dct,type(dct))) for k in dct: if type(dct[k]) is list: dct[k] = np.asarray(dct[k]) if '_KWR_ARGS' in dct: return KeywordRegistry.from_json(dct) if '_KWR_ARGS' in dct: return KeywordRegistry.from_json(dct) return dct
def test_jsonify(model_registry): json = model_registry.to_json() unjson = KeywordRegistry.from_json(json) unjson.keywords == model_registry.keywords