def test_empty_like(): z = empty(100, 10) z2 = empty_like(z) eq(z.shape, z2.shape) eq(z.chunks, z2.chunks) eq(z.dtype, z2.dtype) eq(z.cname, z2.cname) eq(z.clevel, z2.clevel) eq(z.shuffle, z2.shuffle) eq(z.fill_value, z2.fill_value)
def test_set_selections_with_fields(): v = [('aaa', 1, 4.2), ('bbb', 2, 8.4), ('ccc', 3, 12.6)] v = np.array(v, dtype=[('foo', 'S3'), ('bar', 'i4'), ('baz', 'f8')]) a = np.empty_like(v) z = zarr.empty_like(v, chunks=2) fields_fixture = [ 'foo', [], ['foo'], ['foo', 'bar'], ['foo', 'baz'], ['bar', 'baz'], ['foo', 'bar', 'baz'], ['bar', 'foo'], ['baz', 'bar', 'foo'], ] for fields in fields_fixture: # currently multi-field assignment is not supported in numpy, so we won't support # it either if isinstance(fields, list) and len(fields) > 1: with pytest.raises(IndexError): z.set_basic_selection(Ellipsis, v, fields=fields) with pytest.raises(IndexError): z.set_orthogonal_selection([0, 2], v, fields=fields) with pytest.raises(IndexError): z.set_coordinate_selection([0, 2], v, fields=fields) with pytest.raises(IndexError): z.set_mask_selection([True, False, True], v, fields=fields) else: if isinstance(fields, list) and len(fields) == 1: # work around numpy does not support multi-field assignment even if there # is only one field key = fields[0] elif isinstance(fields, list) and len(fields) == 0: # work around numpy ambiguity about what is a field selection key = Ellipsis else: key = fields # setup expectation a[:] = ('', 0, 0) z[:] = ('', 0, 0) assert_array_equal(a, z[:]) a[key] = v[key] # total selection z.set_basic_selection(Ellipsis, v[key], fields=fields) assert_array_equal(a, z[:]) # basic selection with slice a[:] = ('', 0, 0) z[:] = ('', 0, 0) a[key][0:2] = v[key][0:2] z.set_basic_selection(slice(0, 2), v[key][0:2], fields=fields) assert_array_equal(a, z[:]) # orthogonal selection a[:] = ('', 0, 0) z[:] = ('', 0, 0) ix = [0, 2] a[key][ix] = v[key][ix] z.set_orthogonal_selection(ix, v[key][ix], fields=fields) assert_array_equal(a, z[:]) # coordinate selection a[:] = ('', 0, 0) z[:] = ('', 0, 0) ix = [0, 2] a[key][ix] = v[key][ix] z.set_coordinate_selection(ix, v[key][ix], fields=fields) assert_array_equal(a, z[:]) # mask selection a[:] = ('', 0, 0) z[:] = ('', 0, 0) ix = [True, False, True] a[key][ix] = v[key][ix] z.set_mask_selection(ix, v[key][ix], fields=fields) assert_array_equal(a, z[:])
import sys import timeit import numpy as np import line_profiler import zarr from zarr import blosc sys.path.insert(0, '..') # setup a = np.random.normal(2000, 1000, size=200000000).astype('u2') z = zarr.empty_like(a, chunks=1000000, compression='blosc', compression_opts=dict(cname='lz4', clevel=5, shuffle=2)) print(z) print('*' * 79) # time t = timeit.repeat('z[:] = a', repeat=10, number=1, globals=globals()) print(t) print(min(t)) print(z) # profile profile = line_profiler.LineProfiler(blosc.compress) profile.run('z[:] = a') profile.print_stats()