Esempio n. 1
0
def test_persistency(shape, chunkshape, blockshape, filename, dtype, copy):
    if os.path.exists(filename):
        os.remove(filename)

    size = int(np.prod(shape))
    nparray = np.arange(size, dtype=dtype).reshape(shape)
    cat.from_numpy(nparray,
                   chunkshape=chunkshape,
                   blockshape=blockshape,
                   filename=filename)
    b = cat.from_file(filename, copy)
    nparray2 = b.to_numpy()
    np.testing.assert_almost_equal(nparray, nparray2)

    os.remove(filename)
Esempio n. 2
0
def test_getitem_numpy(shape, chunkshape, blockshape, slices, dtype):
    size = int(np.prod(shape))
    nparray = np.arange(size, dtype=dtype).reshape(shape)
    a = cat.from_numpy(nparray, chunkshape=chunkshape, blockshape=blockshape)
    nparray_slice = nparray[slices]
    a_slice = a[slices]
    np.testing.assert_almost_equal(a_slice, nparray_slice)
Esempio n. 3
0
def test_copy_numpy(shape, chunkshape1, blockshape1, chunkshape2, blockshape2,
                    dtype):
    size = int(np.prod(shape))
    nparray = np.arange(size, dtype=dtype).reshape(shape)
    a = cat.from_numpy(nparray,
                       chunkshape1=chunkshape1,
                       blockshape=blockshape1)
    b = a.copy(chunkshape=chunkshape2,
               blockshape=blockshape2,
               complevel=5,
               filters=[2])
    if chunkshape2 is not None:
        nparray2 = b.to_numpy()
    else:
        nparray2 = b
    np.testing.assert_almost_equal(nparray, nparray2)
Esempio n. 4
0
def test_iters_numpy(shape, chunkshape1, blockshape1, chunkshape2, blockshape2,
                     dtype):
    size = int(np.prod(shape))
    nparray = np.ones(size, dtype=dtype).reshape(shape)
    a = cat.from_numpy(nparray, chunkshape=chunkshape1,
                       blockshape=blockshape1)  # creates a NPArray

    b = cat.empty(shape, dtype, 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[:] = block_r

    nparray2 = b.to_numpy()
    np.testing.assert_equal(nparray, nparray2)
Esempio n. 5
0
def test_frame_numpy(shape, chunkshape, blockshape, dtype, enforceframe,
                     filename, copy_sframe):
    if filename is not None and os.path.exists(filename):
        os.remove(filename)

    size = int(np.prod(shape))
    nparray = np.arange(size, dtype=dtype).reshape(shape)
    a = cat.from_numpy(nparray,
                       chunkshape=chunkshape,
                       blockshape=blockshape,
                       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(10):
        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)
Esempio n. 6
0
blockshape = (12, 7)

filename = "ex_metalayers.cat"
if os.path.exists(filename):
    # Remove file on disk
    os.remove(filename)

dtype = np.int32
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
a = cat.from_numpy(nparray,
                   chunkshape=chunkshape,
                   blockshape=blockshape,
                   enforceframe=True)

# Create an empty caterva array (on disk)
metalayers = {
    "numpy": {
        b"dtype": str(np.dtype(dtype))
    },
    "test": {
        b"lorem": 1234
    }
}
b = cat.empty(shape,
              chunkshape=chunkshape,
              blockshape=blockshape,
              filename=filename,
Esempio n. 7
0
import cat4py as cat
import numpy as np


shape = (1234, 23)
chunkshape = (253, 23)
blockshape = (10, 23)

dtype = np.bool

# Create a buffer
nparray = np.random.choice(a=[True, False], size=np.prod(shape)).reshape(shape)

# Create a caterva array from a numpy array
a = cat.from_numpy(nparray, chunkshape=chunkshape, blockshape=blockshape)

# Convert a caterva array to a numpy array
nparray2 = a.to_numpy()

np.testing.assert_almost_equal(nparray, nparray2)
Esempio n. 8
0
chunkshape = (22, 32)
blockshape = (12, 7)

filename = "ex_iters.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
a = cat.from_numpy(nparray)

# Create an empty caterva array (on disk)
b = cat.empty(shape,
              chunkshape=chunkshape,
              blockshape=blockshape,
              filename=filename,
              itemsize=itemsize)

# Fill an empty caterva array using a block iterator
for block, info in b.iter_write():
    block[:] = bytes(nparray[info.slice])

# Load file
c = cat.from_file(filename)
Esempio n. 9
0
def test_numpy(shape, chunkshape, blockshape, dtype):
    size = int(np.prod(shape))
    nparray = np.arange(size, dtype=dtype).reshape(shape)
    a = cat.from_numpy(nparray, chunkshape=chunkshape, blockshape=blockshape)
    nparray2 = a.to_numpy()
    np.testing.assert_almost_equal(nparray, nparray2)