Skip to content

Commit 6d4179e

Browse files
committed
test and fix for uninitialised structured array
1 parent ba7b2a6 commit 6d4179e

File tree

2 files changed

+40
-66
lines changed

2 files changed

+40
-66
lines changed

zarr/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,11 @@ def _chunk_getitem(self, chunk_coords, chunk_selection, out, out_selection,
15681568
except KeyError:
15691569
# chunk not initialized
15701570
if self._fill_value is not None:
1571-
out[out_selection] = self._fill_value
1571+
if fields:
1572+
fill_value = self._fill_value[fields]
1573+
else:
1574+
fill_value = self._fill_value
1575+
out[out_selection] = fill_value
15721576

15731577
else:
15741578

zarr/tests/test_core.py

Lines changed: 35 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -867,99 +867,69 @@ def test_array_dtype_shape(self):
867867
z[...] = a
868868
assert_array_equal(a, z[...])
869869

870-
def test_structured_array(self):
871-
872-
# setup some data
873-
d = np.array([(b'aaa', 1, 4.2),
874-
(b'bbb', 2, 8.4),
875-
(b'ccc', 3, 12.6)],
876-
dtype=[('foo', 'S3'), ('bar', 'i4'), ('baz', 'f8')])
870+
def check_structured_array(self, d, fill_values):
877871
for a in (d, d[:0]):
878-
for fill_value in None, b'', (b'zzz', 42, 16.8):
872+
for fill_value in fill_values:
879873
z = self.create_array(shape=a.shape, chunks=2, dtype=a.dtype, fill_value=fill_value)
880874
assert len(a) == len(z)
875+
assert a.shape == z.shape
876+
assert a.dtype == z.dtype
877+
878+
# check use of fill value before array is initialised with data
881879
if fill_value is not None:
882880
if fill_value == b'':
883881
# numpy 1.14 compatibility
884882
np_fill_value = np.array(fill_value, dtype=a.dtype.str).view(a.dtype)[()]
885883
else:
886884
np_fill_value = np.array(fill_value, dtype=a.dtype)[()]
887885
assert np_fill_value == z.fill_value
888-
if len(z):
886+
if len(a):
889887
assert np_fill_value == z[0]
890888
assert np_fill_value == z[-1]
889+
empty = np.empty_like(a)
890+
empty[:] = np_fill_value
891+
assert empty[0] == z[0]
892+
assert_array_equal(empty[0:2], z[0:2])
893+
assert_array_equal(empty, z[...])
894+
for f in a.dtype.names:
895+
assert_array_equal(empty[f], z[f])
896+
897+
# store data in array
891898
z[...] = a
899+
900+
# check stored data
892901
if len(a):
893902
assert a[0] == z[0]
894-
assert_array_equal(a, z[...])
895-
assert_array_equal(a['foo'], z['foo'])
896-
assert_array_equal(a['bar'], z['bar'])
897-
assert_array_equal(a['baz'], z['baz'])
903+
assert a[-1] == z[-1]
904+
assert_array_equal(a[0:2], z[0:2])
905+
assert_array_equal(a, z[...])
906+
for f in a.dtype.names:
907+
assert_array_equal(a[f], z[f])
898908

899-
def test_structured_array_subshapes(self):
909+
def test_structured_array(self):
910+
d = np.array([(b'aaa', 1, 4.2),
911+
(b'bbb', 2, 8.4),
912+
(b'ccc', 3, 12.6)],
913+
dtype=[('foo', 'S3'), ('bar', 'i4'), ('baz', 'f8')])
914+
fill_values = None, b'', (b'zzz', 42, 16.8)
915+
self.check_structured_array(d, fill_values)
900916

901-
# setup some data
917+
def test_structured_array_subshapes(self):
902918
d = np.array([(0, ((0, 1, 2), (1, 2, 3)), b'aaa'),
903919
(1, ((1, 2, 3), (2, 3, 4)), b'bbb'),
904920
(2, ((2, 3, 4), (3, 4, 5)), b'ccc')],
905921
dtype=[('foo', 'i8'), ('bar', '(2, 3)f4'), ('baz', 'S3')])
906-
for a in (d, d[:0]):
907-
for fill_value in None, b'', (0, ((0, 0, 0), (1, 1, 1)), b'zzz'):
908-
z = self.create_array(shape=a.shape, chunks=2, dtype=a.dtype, fill_value=fill_value)
909-
assert len(a) == len(z)
910-
if fill_value is not None:
911-
if fill_value == b'':
912-
# numpy 1.14 compatibility
913-
np_fill_value = np.array(fill_value, dtype=a.dtype.str).view(a.dtype)[()]
914-
else:
915-
np_fill_value = np.array(fill_value, dtype=a.dtype)[()]
916-
assert np_fill_value == z.fill_value
917-
if len(z):
918-
assert np_fill_value == z[0]
919-
assert np_fill_value == z[-1]
920-
z[...] = a
921-
if len(a):
922-
assert a[0] == z[0]
923-
assert_array_equal(a, z[...])
924-
assert_array_equal(a['foo'], z['foo'])
925-
assert_array_equal(a['bar'], z['bar'])
926-
assert_array_equal(a['baz'], z['baz'])
927-
else:
928-
# workaround for numpy bug https://www.github.com/numpy/numpy/issues/11946
929-
assert a.tobytes() == z[...].tobytes()
922+
fill_values = None, b'', (0, ((0, 0, 0), (1, 1, 1)), b'zzz')
923+
self.check_structured_array(d, fill_values)
930924

931925
def test_structured_array_nested(self):
932-
933-
# setup some data
934926
d = np.array([(0, (0, ((0, 1), (1, 2), (2, 3)), 0), b'aaa'),
935927
(1, (1, ((1, 2), (2, 3), (3, 4)), 1), b'bbb'),
936928
(2, (2, ((2, 3), (3, 4), (4, 5)), 2), b'ccc')],
937929
dtype=[('foo', 'i8'), ('bar', [('foo', 'i4'), ('bar', '(3, 2)f4'),
938930
('baz', 'u1')]), ('baz', 'S3')])
939-
for a in (d, d[:0]):
940-
for fill_value in None, b'', (0, (0, ((0, 0), (1, 1), (2, 2)), 0), b'zzz'):
941-
z = self.create_array(shape=a.shape, chunks=2, dtype=a.dtype, fill_value=fill_value)
942-
assert len(a) == len(z)
943-
if fill_value is not None:
944-
if fill_value == b'':
945-
# numpy 1.14 compatibility
946-
np_fill_value = np.array(fill_value, dtype=a.dtype.str).view(a.dtype)[()]
947-
else:
948-
np_fill_value = np.array(fill_value, dtype=a.dtype)[()]
949-
assert np_fill_value == z.fill_value
950-
if len(z):
951-
assert np_fill_value == z[0]
952-
assert np_fill_value == z[-1]
953-
z[...] = a
954-
if len(a):
955-
assert a[0] == z[0]
956-
assert_array_equal(a, z[...])
957-
assert_array_equal(a['foo'], z['foo'])
958-
assert_array_equal(a['bar'], z['bar'])
959-
assert_array_equal(a['baz'], z['baz'])
960-
else:
961-
# workaround for numpy bug https://www.github.com/numpy/numpy/issues/11946
962-
assert a.tobytes() == z[...].tobytes()
931+
fill_values = None, b'', (0, (0, ((0, 0), (1, 1), (2, 2)), 0), b'zzz')
932+
self.check_structured_array(d, fill_values)
963933

964934
def test_dtypes(self):
965935

0 commit comments

Comments
 (0)