def json_numpy_obj_hook(dct): """ Replace any numpy arrays previously encoded by NumpyEncoder to their proper shape, data type and data. :param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray """ if isinstance(dct, dict) and '__ndarray__' in dct: try: from numpy import asarray import numpy as nptypes except ImportError: raise NoNumpyException( 'Trying to decode a map which appears to represent a numpy ' 'array, but numpy appears not to be installed.') order = 'A' if 'Corder' in dct: order = 'C' if dct['Corder'] else 'F' if dct['shape']: return asarray(dct['__ndarray__'], dtype=dct['dtype'], order=order) else: dtype = getattr(nptypes, dct['dtype']) return dtype(dct['__ndarray__']) return dct
def json_numpy_obj_hook(dct): """ Replace any numpy arrays previously encoded by NumpyEncoder to their proper shape, data type and data. :param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray """ if not isinstance(dct, dict): return dct if not '__ndarray__' in dct: return dct try: from numpy import asarray, empty, ndindex import numpy as nptypes except ImportError: raise NoNumpyException( 'Trying to decode a map which appears to represent a numpy ' 'array, but numpy appears not to be installed.') order = 'A' if 'Corder' in dct: order = 'C' if dct['Corder'] else 'F' if dct['shape']: if dct['dtype'] == 'object': dec_data = dct['__ndarray__'] arr = empty(dct['shape'], dtype=dct['dtype'], order=order) for indx in ndindex(arr.shape): arr[indx] = nested_index(dec_data, indx) return arr return asarray(dct['__ndarray__'], dtype=dct['dtype'], order=order) else: dtype = getattr(nptypes, dct['dtype']) return dtype(dct['__ndarray__'])
def json_numpy_obj_hook(dct): """ Replace any numpy arrays previously encoded by NumpyEncoder to their proper shape, data type and data. :param dct: (dict) json encoded ndarray :return: (ndarray) if input was an encoded ndarray """ if not isinstance(dct, dict): return dct if not '__ndarray__' in dct: return dct try: import numpy except ImportError: raise NoNumpyException('Trying to decode a map which appears to represent a numpy ' 'array, but numpy appears not to be installed.') order = None if 'Corder' in dct: order = 'C' if dct['Corder'] else 'F' data_json = dct['__ndarray__'] shape = tuple(dct['shape']) nptype = dct['dtype'] if shape: if nptype == 'object': return _lists_of_obj_to_ndarray(data_json, order, shape, nptype) if isinstance(data_json, str_type): return _bin_str_to_ndarray(data_json, order, shape, nptype) else: return _lists_of_numbers_to_ndarray(data_json, order, shape, nptype) else: return _scalar_to_numpy(data_json, nptype)
def json_nonumpy_obj_hook(dct): """ This hook has no effect except to check if you're trying to decode numpy arrays without support, and give you a useful message. """ if isinstance(dct, dict) and '__ndarray__' in dct: raise NoNumpyException(('Trying to decode a map which appears to represent a numpy array, ' 'but numpy support is not enabled, perhaps it is not installed.')) return dct