Skip to content

Commit 01e7b20

Browse files
committed
fix is_initialised bug
1 parent f066cdc commit 01e7b20

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

zarr/ext.pyx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,14 @@ cdef class BaseArray:
815815
def __get__(self):
816816
return self.size * self.itemsize
817817

818+
property chunk_size:
819+
def __get__(self):
820+
return reduce(operator.mul, self._chunks)
821+
822+
property chunk_nbytes:
823+
def __get__(self):
824+
return self.chunk_size * self.itemsize
825+
818826
def __getitem__(self, item):
819827
cdef ndarray dest
820828
cdef BaseChunk chunk
@@ -928,6 +936,8 @@ cdef class BaseArray:
928936
r += '; cbytes: %s' % _util.human_readable_size(self.cbytes)
929937
if self.cbytes > 0:
930938
r += '; ratio: %.1f' % (self.nbytes / self.cbytes)
939+
# r += '; chunk_nbytes: %s' % \
940+
# _util.human_readable_size(self.chunk_nbytes)
931941
n_chunks = reduce(operator.mul, self._cdata_shape)
932942
r += '; initialized: %s/%s' % (np.count_nonzero(self.is_initialized),
933943
n_chunks)
@@ -1140,7 +1150,7 @@ cdef class PersistentArray(BaseArray):
11401150
meta_path = os.path.join(path, defaults.metapath)
11411151

11421152
if mode in ['r', 'r+']:
1143-
self._open(path)
1153+
self._open(path, **kwargs)
11441154

11451155
elif mode == 'w':
11461156
if os.path.exists(path):
@@ -1154,7 +1164,7 @@ cdef class PersistentArray(BaseArray):
11541164

11551165
elif mode == 'a':
11561166
if os.path.exists(meta_path):
1157-
self._open(path)
1167+
self._open(path, **kwargs)
11581168
else:
11591169
self._create(path, **kwargs)
11601170

@@ -1207,19 +1217,32 @@ cdef class PersistentArray(BaseArray):
12071217
'fill_value': self._fill_value}
12081218
_write_array_metadata(path, metadata)
12091219

1210-
def _open(self, path):
1220+
def _open(self, path, shape=None, chunks=None, dtype=None, cname=None,
1221+
clevel=None, shuffle=None, fill_value=None):
12111222

12121223
# read metadata
12131224
metadata = _read_array_metadata(path)
12141225

12151226
# set attributes
12161227
self._shape = metadata['shape']
1217-
self._dtype = metadata['dtype']
12181228
self._chunks = metadata['chunks']
1229+
self._dtype = metadata['dtype']
12191230
self._cname = metadata['cname']
12201231
self._clevel = metadata['clevel']
12211232
self._shuffle = metadata['shuffle']
12221233
self._fill_value = metadata['fill_value']
1234+
1235+
# check consistency with user arguments
1236+
if shape is not None and _normalize_shape(shape) != self._shape:
1237+
raise ValueError('shape %r not consistent with existing %r' %
1238+
(shape, self._shape))
1239+
if chunks is not None and \
1240+
_normalize_chunks(chunks, self._shape) != self._chunks:
1241+
raise ValueError('chunks %r not consistent with existing %r' %
1242+
(chunks, self._chunks))
1243+
if dtype is not None and np.dtype(dtype) != self._dtype:
1244+
raise ValueError('dtype %r not consistent with existing %r' %
1245+
(dtype, self._dtype))
12231246

12241247
property cbytes:
12251248
def __get__(self):
@@ -1455,6 +1478,7 @@ cdef class LazyPersistentArray(PersistentArray):
14551478
bn = os.path.basename(fn)[:-len(defaults.datasuffix)]
14561479
cidx = tuple(map(int, bn.split('.')))
14571480
a[cidx] = True
1481+
return a
14581482

14591483
cdef BaseChunk get_chunk(self, tuple cidx):
14601484
return _lazy_get_chunk(self, cidx)
@@ -1486,6 +1510,8 @@ cdef class LazyPersistentArray(PersistentArray):
14861510
yield cidx, chunk
14871511

14881512
def resize(self, *args):
1513+
if self._mode == 'r':
1514+
raise ValueError('array is read-only')
14891515

14901516
# do resize
14911517
_lazy_resize(self, *args)

zarr/tests/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ def test_1d(self):
3636
eq(defaults.shuffle, z.shuffle)
3737
eq(a.nbytes, z.nbytes)
3838
eq(0, z.cbytes)
39+
eq(0, np.count_nonzero(z.is_initialized))
3940

4041
# set data
4142
z[:] = a
4243

4344
# check properties
4445
eq(a.nbytes, z.nbytes)
4546
eq(sum(c.cbytes for c in z.iter_chunks()), z.cbytes)
47+
eq(11, np.count_nonzero(z.is_initialized))
4648

4749
# check round-trip
4850
assert_array_equal(a, z[:])
@@ -108,13 +110,15 @@ def test_array_2d(self):
108110
eq(defaults.cname, z.cname)
109111
eq(defaults.clevel, z.clevel)
110112
eq(defaults.shuffle, z.shuffle)
113+
eq(0, np.count_nonzero(z.is_initialized))
111114

112115
# set data
113116
z[:] = a
114117

115118
# check properties
116119
eq(a.nbytes, z.nbytes)
117120
eq(sum(c.cbytes for c in z.iter_chunks()), z.cbytes)
121+
eq(50, np.count_nonzero(z.is_initialized))
118122

119123
# check round-trip
120124
assert_array_equal(a, z[:])
@@ -329,11 +333,14 @@ def _test_persistence(self, a, chunks):
329333
eq(a.nbytes, z2.nbytes)
330334
eq(z.cbytes, z2.cbytes)
331335
assert_array_equal(z.is_initialized, z2.is_initialized)
336+
assert_true(np.count_nonzero(z2.is_initialized) > 0)
332337
assert_array_equal(a, z2[:])
333338

334339
# check read-only
335340
with assert_raises(ValueError):
336341
z2[:] = 0
342+
with assert_raises(ValueError):
343+
z2.resize(100)
337344

338345
# open for read/write if exists
339346
z3 = self.create_array(path=path, mode='r+')
@@ -346,6 +353,7 @@ def _test_persistence(self, a, chunks):
346353
eq(a.nbytes, z3.nbytes)
347354
eq(z.cbytes, z3.cbytes)
348355
assert_array_equal(z.is_initialized, z3.is_initialized)
356+
assert_true(np.count_nonzero(z3.is_initialized) > 0)
349357
assert_array_equal(a, z3[:])
350358

351359
# check can write

0 commit comments

Comments
 (0)