def decode(filedata, encoding, shape=None, dtype=None, block_size=None): if (shape is None or dtype is None) and encoding not in ('npz', 'fpzip', 'kempressed'): raise ValueError( "Only npz encoding can omit shape and dtype arguments. {}".format( encoding)) if filedata is None or len(filedata) == 0: return np.zeros(shape=shape, dtype=dtype) elif encoding == "raw": return decode_raw(filedata, shape=shape, dtype=dtype) elif encoding == "kempressed": return decode_kempressed(filedata) elif encoding == "fpzip": return fpzip.decompress(filedata, order='F') elif encoding == "compressed_segmentation": return decode_compressed_segmentation(filedata, shape=shape, dtype=dtype, block_size=block_size) elif encoding == "jpeg": return decode_jpeg(filedata, shape=shape, dtype=dtype) elif encoding == "npz": return decode_npz(filedata) else: raise NotImplementedError(encoding)
def decode(filedata: bytes, encoding: str, shape: Optional[Sequence[int]] = None, dtype: Any = None, block_size: Optional[Sequence[int]] = None, background_color: int = 0) -> np.ndarray: if (shape is None or dtype is None) and encoding not in ('npz', 'fpzip', 'kempressed'): raise ValueError( "Only npz encoding can omit shape and dtype arguments. {}".format( encoding)) if filedata is None or len(filedata) == 0: return np.full(shape=shape, fill_value=background_color, dtype=dtype) elif encoding == "raw": return decode_raw(filedata, shape=shape, dtype=dtype) elif encoding == "kempressed": return decode_kempressed(filedata) elif encoding == "fpzip": return fpzip.decompress(filedata, order='F') elif encoding == "compressed_segmentation": return decode_compressed_segmentation(filedata, shape=shape, dtype=dtype, block_size=block_size) elif encoding == "compresso": return compresso.decompress(filedata).reshape(shape) elif encoding == "jpeg": return decode_jpeg(filedata, shape=shape, dtype=dtype) elif encoding == "png": return decode_png(filedata, shape=shape, dtype=dtype) elif encoding == "npz": return decode_npz(filedata) else: raise NotImplementedError(encoding)
def test_noncontiguous_memory(): x = np.random.random_sample(size=(15, 15, 15, 1)).astype(np.float32) x_broken = x[2::2, ::3, ::2] for order in ('C', 'F'): y = fpzip.compress(x_broken, order=order) z = fpzip.decompress(y, order=order) assert np.all(x_broken == z)
def load_compressed_inf(file): COR_SIZE = 4 FUR_SIZE = 4 POS = 0 with open(file, 'rb') as f: compressed_bytes = f.read() data_again = fpzip.decompress(compressed_bytes, order='C') data_again = np.squeeze(data_again) return data_again
def fpzip_decompress(c_bitstring): """ uses fpzip's decompress method to decompress a numpy array and return only the given array. INPUT c_bitstring: compressed bitstring OUTPUT np_arry = numpy array of data (converted to 1D list) """ np_arr = fpzip.decompress(c_bitstring) return np_arr.tolist()[0][0][0]
def test_basic_conformation(): # corresponds to the base64 encoded compression of # np.array([[[1]]], dtype=np.float32).tobytes() # Was compressed by fpzip C program. one_fpz = b'ZnB5KYcO8R7gAP8AAAD/AAAA/wAAAP8A8zvT3FAAAAA=\n' one_fpz = base64.decodestring(one_fpz) # decodebytes is the modern name, but py27 doesn't have it one_array = np.array([[[1]]], dtype=np.float32) compressed = fpzip.compress(one_array) assert len(one_fpz) == len(compressed) assert np.all( fpzip.decompress(compressed) == fpzip.decompress(one_fpz) ) # encoded np.array([[[1,2,3], [4,5,6]]], dtype=np.float32) # with open("six.raw", 'wb') as f: # f.write(six_array.tobytes('C')) # # ./fpzip -i six.raw -3 3 2 1 -o six.fpz # >> outbytes=44 ratio=0.55 # # with open('six.fpz', 'rb') as f: # encoded = f.read() # six_fpz = base64.encodestring(encoded) six_fpz = b'ZnB5KYcO8R7gAv0AAAH+AAAA/wAAAP8A8zvT3GsIJgDRE0yNUZgAHeZbgAA=\n' # 3 2 1 six_fpz = base64.decodestring(six_fpz) six_array = np.array([[[1,2,3], [4,5,6]]], dtype=np.float32) compressed = fpzip.compress(six_array) assert len(six_fpz) == len(compressed) assert np.all( fpzip.decompress(six_fpz) == fpzip.decompress(compressed)[0,:,:,:] )
def test_doubles(): size = (128, 127, 126, 2) for dims in range(4): print(dims) x = np.random.random_sample(size=size[:dims]).astype(np.float64) x = np.ascontiguousarray(x) y = fpzip.compress(x, order='C') z = fpzip.decompress(y, order='C') assert np.all(x == z) x = np.asfortranarray(x) y = fpzip.compress(x, order='F') z = fpzip.decompress(y, order='F') z = np.squeeze(z) assert np.all(x == z) x = np.random.random_sample(size=[0] * dims).astype(np.float64) y = fpzip.compress(x) z = fpzip.decompress(y) assert np.all(x == z)
def test_floats(): size = (128, 127, 126, 2) for dims in range(4): print(dims) x = np.random.random_sample(size=size[:dims]).astype(np.float64) x = np.ascontiguousarray(x) y = fpzip.compress(x) z = fpzip.decompress(y, order='C') assert np.all(x == z) x = np.asfortranarray(x) y = fpzip.compress(x, order='F') z = fpzip.decompress(y, order='F') z = np.squeeze(z) assert np.all(x == z) x = np.random.random_sample(size=(128, 128, 128)).astype(np.float32) x = np.ascontiguousarray(x) y = fpzip.compress(x) z = fpzip.decompress(y) z = np.squeeze(z, axis=0) # is3d logic assert np.all(x == z) x = np.asfortranarray(x) y = fpzip.compress(x, order='F') z = fpzip.decompress(y, order='F') z = np.squeeze(z, axis=3) assert np.all(x == z) x = np.random.random_sample(size=(0, 0, 0, 0)).astype(np.float32) x = np.ascontiguousarray(x) y = fpzip.compress(x, order='C') z = fpzip.decompress(y, order='C') assert np.all(x == z) x = np.asfortranarray(x) y = fpzip.compress(x, order='F') z = fpzip.decompress(y, order='F') assert np.all(x == z)
def decode_kempressed(bytestring): """subvol not bytestring since numpy conversion is done inside fpzip extension.""" subvol = fpzip.decompress(bytestring, order='F') return np.swapaxes(subvol, 3, 2) - 2.0