def test_getitem(shape, chunks, blocks, slices, dtype): size = int(np.prod(shape)) nparray = np.arange(size, dtype=dtype).reshape(shape) a = cat.from_buffer(bytes(nparray), nparray.shape, nparray.itemsize, chunks=chunks, blocks=blocks) nparray_slice = nparray[slices] buffer_slice = np.asarray(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_copy(shape, chunks1, blocks1, chunks2, blocks2, itemsize): size = int(np.prod(shape)) buffer = bytes(size * itemsize) a = cat.from_buffer(buffer, shape, itemsize, chunks=chunks1, blocks=blocks1, complevel=2) b = a.copy(chunks=chunks2, blocks=blocks2, itemsize=itemsize, complevel=5, filters=[cat.Filter.BITSHUFFLE]) buffer2 = b.to_buffer() assert buffer == buffer2
import caterva as cat import numpy as np shape = (10, 10) chunks = (10, 10) blocks = (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), chunks=chunks, blocks=blocks) # Get a copy of a caterva array 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)
####################################################################### import caterva as cat import numpy as np np.random.seed(123) shape = (50, 50) chunks = (49, 49) blocks = (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, chunks=chunks, blocks=blocks, itemsize=itemsize) print(a.filters) print(a.codec) print(a.cratio) # Convert a caterva array to a buffer buffer2 = a.to_buffer() assert buffer == buffer2
urlpath = "ex_persistency.cat" if os.path.exists(urlpath): cat.remove(urlpath) 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, itemsize, chunks=chunks, blocks=blocks, urlpath=urlpath, sequencial=False) # Read a caterva array from disk b = cat.open(urlpath) # Convert a caterva array to a numpy array nparray2 = np.asarray(cat.from_buffer(b.to_buffer(), b.shape, b.itemsize)).view(dtype) np.testing.assert_almost_equal(nparray, nparray2) # Remove file on disk if os.path.exists(urlpath):
blocks = (8, 8) urlpath = "ex_meta.cat" if os.path.exists(urlpath): # Remove file on disk os.remove(urlpath) dtype = np.dtype(np.complex128) itemsize = dtype.itemsize # Create a numpy array nparray = np.arange(int(np.prod(shape)), dtype=dtype).reshape(shape) meta = { "m1": b"1111", "m2": b"2222", } # Create a caterva array from a numpy array (on disk) a = cat.from_buffer(bytes(nparray), nparray.shape, chunks=chunks, blocks=blocks, urlpath=urlpath, itemsize=itemsize, meta=meta) # Read a caterva array from disk b = cat.open(urlpath) # Deal with meta m1 = b.meta.get("m5", b"0000") m2 = b.meta["m2"] # Remove file on disk os.remove(urlpath)
def test_buffer(shape, chunks, blocks, itemsize): size = int(np.prod(shape)) buffer = bytes(size * itemsize) a = cat.from_buffer(buffer, shape, itemsize, chunks=chunks, blocks=blocks) buffer2 = a.to_buffer() assert buffer == buffer2