def test_getitem(shape, chunkshape, blockshape, slices, dtype): size = int(np.prod(shape)) nparray = np.arange(size, dtype=dtype).reshape(shape) a = cat.from_buffer(bytes(nparray), nparray.shape, itemsize=nparray.itemsize, chunkshape=chunkshape, blockshape=blockshape) nparray_slice = nparray[slices] buffer_slice = a[slices] a_slice = np.frombuffer(buffer_slice, dtype=dtype).reshape(nparray_slice.shape) np.testing.assert_almost_equal(a_slice, nparray_slice)
def test_buffer(shape, chunkshape, blockshape, itemsize): size = int(np.prod(shape)) buffer = bytes(size * itemsize) a = cat.from_buffer(buffer, shape, itemsize, chunkshape=chunkshape, blockshape=blockshape) buffer2 = a.to_buffer() assert buffer == buffer2
def test_iters(shape, chunkshape1, blockshape1, chunkshape2, blockshape2, dtype): dtype = np.dtype(dtype) size = int(np.prod(shape)) nparray = np.ones(size, dtype=dtype).reshape(shape) a = cat.asarray(nparray, chunkshape=chunkshape1, blockshape=blockshape1) itemsize = dtype.itemsize b = cat.empty(shape, itemsize, chunkshape=chunkshape2, blockshape=blockshape2) itershape = chunkshape2 if chunkshape2 is not None else b.shape for (block_r, info_r), (block_w, info_w) in lzip(a.iter_read(itershape), b.iter_write()): block_w[:] = bytearray(np.asarray(block_r)) nparray2 = np.asarray(cat.from_buffer(b.to_buffer(), b.shape, b.itemsize, str(dtype))) np.testing.assert_equal(nparray, nparray2)
def test_copy(shape, chunkshape1, blockshape1, chunkshape2, blockshape2, itemsize): size = int(np.prod(shape)) buffer = bytes(size * itemsize) a = cat.from_buffer(buffer, shape, chunkshape=chunkshape1, blockshape=blockshape1, itemsize=itemsize, complevel=2) b = a.copy(chunkshape=chunkshape2, blockshape=blockshape2, itemsize=itemsize, complevel=5, filters=[2]) buffer2 = b.to_buffer() assert buffer == buffer2
def test_frame(shape, chunkshape, blockshape, itemsize, enforceframe, filename, copy_sframe): if filename is not None and os.path.exists(filename): os.remove(filename) size = int(np.prod(shape)) buffer = bytes(size * itemsize) a = cat.from_buffer(buffer, shape, chunkshape=chunkshape, blockshape=blockshape, itemsize=itemsize, enforceframe=enforceframe, filename=filename) sframe1 = a.to_sframe() buffer1 = a.to_buffer() # Size of a compressed frame should be less than the plain buffer for the cases here assert len(sframe1) < len(buffer1) b = cat.from_sframe(sframe1, copy=copy_sframe) sframe2 = b.to_sframe() # For some reason, the size of sframe1 and sframe2 are not equal when copies are made, # but the important thing is that the length of the frame should be stable in multiple # round-trips after the first one. # assert len(sframe2) == len(sframe1) sframe3 = sframe2 c = b for i in range(1): c = cat.from_sframe(sframe2, copy=copy_sframe) sframe3 = c.to_sframe() if not copy_sframe: # When the frame is not copied, we *need* a copy for the next iteration sframe3 = bytes(sframe3) assert len(sframe3) == len(sframe2) buffer2 = b.to_buffer() assert buffer2 == buffer1 buffer3 = c.to_buffer() assert buffer3 == buffer1 if filename is not None and os.path.exists(filename): os.remove(filename)
import cat4py as cat import numpy as np np.random.seed(123) shape = (50, 50) chunkshape = (49, 49) blockshape = (48, 48) itemsize = 8 # Create a buffer buffer = bytes(np.random.normal(0, 1, np.prod(shape)) * itemsize) # Create a caterva array from a buffer a = cat.from_buffer(buffer, shape, chunkshape=chunkshape, blockshape=blockshape, itemsize=itemsize) print(a.cratio) # Convert a caterva array to a buffer buffer2 = a.to_buffer() assert buffer == buffer2
shape = (128, 128) chunkshape = (32, 32) blockshape = (8, 8) filename = "ex_persistency.cat" if os.path.exists(filename): # Remove file on disk os.remove(filename) dtype = np.dtype(np.complex128) itemsize = dtype.itemsize # Create a numpy array nparray = np.arange(int(np.prod(shape)), dtype=dtype).reshape(shape) # Create a caterva array from a numpy array (on disk) a = cat.from_buffer(bytes(nparray), nparray.shape, chunkshape=chunkshape, blockshape=blockshape, filename=filename, itemsize=itemsize) # Read a caterva array from disk b = cat.from_file(filename) # Convert a caterva array to a numpy array nparray2 = np.asarray(cat.from_buffer(b.to_buffer(), b.shape, b.itemsize, str(dtype))) np.testing.assert_almost_equal(nparray, nparray2) # Remove file on disk os.remove(filename)
filename = "ex_persistency.cat" if os.path.exists(filename): # Remove file on disk os.remove(filename) dtype = np.complex128 itemsize = np.dtype(dtype).itemsize # Create a numpy array nparray = np.arange(int(np.prod(shape)), dtype=dtype).reshape(shape) # Create a caterva array from a numpy array (on disk) a = cat.from_buffer(bytes(nparray), nparray.shape, chunkshape=chunkshape, blockshape=blockshape, filename=filename, itemsize=itemsize) # Read a caterva array from disk b = cat.from_file(filename) # Convert a caterva array to a numpy array nparray2 = b.to_numpy(dtype=dtype) np.testing.assert_almost_equal(nparray, nparray2) # Remove file on disk os.remove(filename)
import cat4py as cat import numpy as np shape = (10, 10) chunkshape = (10, 10) blockshape = (10, 10) dtype = np.dtype(np.float64) # Create a buffer buffer = bytes(np.arange(int(np.prod(shape)), dtype=dtype).reshape(shape)) # Create a caterva array from a buffer a = cat.from_buffer(buffer, shape, dtype.itemsize, dtype=str(dtype), chunkshape=chunkshape, blockshape=blockshape) # Get a copy of a caterva array (plainbuffer) b = cat.copy(a) d = b.copy() aux = np.asarray(b) aux[1, 2] = 0 aux2 = cat.asarray(aux) print(np.asarray(aux2)) c = np.asarray(b) c[3:5, 2:7] = 0 print(c)