Skip to content

Support Numpy structured arrays containing an object #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements_dev_optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ipytree==0.2.1
azure-storage-blob==12.8.1 # pyup: ignore
redis==3.5.3
types-redis
types-setuptools
pymongo==3.12.0
# optional test requirements
tox==3.24.1
Expand Down
2 changes: 1 addition & 1 deletion zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def _init_array_metadata(
filters_config = []

# deal with object encoding
if dtype == object:
if dtype.hasobject:
if object_codec is None:
if not filters:
# there are no filters so we can be sure there is no object codec
Expand Down
47 changes: 47 additions & 0 deletions zarr/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.tests.common import greetings
from numpy.testing import assert_array_almost_equal, assert_array_equal
from pkg_resources import parse_version

from zarr.core import Array
from zarr.meta import json_loads
Expand Down Expand Up @@ -1362,6 +1363,44 @@ def test_object_codec_warnings(self):
if hasattr(z.store, 'close'):
z.store.close()

@unittest.skipIf(parse_version(np.__version__) < parse_version('1.14.0'),
"unsupported numpy version")
def test_structured_array_contain_object(self):

if "PartialRead" in self.__class__.__name__:
pytest.skip("partial reads of object arrays not supported")

# ----------- creation --------------

structured_dtype = [('c_obj', object), ('c_int', int)]
a = np.array([(b'aaa', 1),
(b'bbb', 2)], dtype=structured_dtype)

# zarr-array with structured dtype require object codec
with pytest.raises(ValueError):
self.create_array(shape=a.shape, dtype=structured_dtype)

# create zarr-array by np-array
za = self.create_array(shape=a.shape, dtype=structured_dtype, object_codec=Pickle())
za[:] = a

# must be equal
assert_array_equal(a, za[:])

# ---------- indexing ---------------

assert za[0] == a[0]

za[0] = (b'ccc', 3)
za[1:2] = np.array([(b'ddd', 4)], dtype=structured_dtype) # ToDo: not work with list
assert_array_equal(za[:], np.array([(b'ccc', 3), (b'ddd', 4)], dtype=structured_dtype))

za['c_obj'] = [b'eee', b'fff']
za['c_obj', 0] = b'ggg'
assert_array_equal(za[:], np.array([(b'ggg', 3), (b'fff', 4)], dtype=structured_dtype))
assert za['c_obj', 0] == b'ggg'
assert za[1, 'c_int'] == 4

def test_iteration_exceptions(self):
# zero d array
a = np.array(1, dtype=int)
Expand Down Expand Up @@ -1893,6 +1932,10 @@ def test_object_arrays_danger(self):
# Cannot hacking out object codec as N5 doesn't allow object codecs
pass

def test_structured_array_contain_object(self):
# skip this one, this data type not supported by N5
pass

def test_attrs_n5_keywords(self):
z = self.create_array(shape=(1050,), chunks=100, dtype='i4')
for k in n5_keywords:
Expand Down Expand Up @@ -2326,6 +2369,10 @@ def test_object_arrays_danger(self):
# skip this one, cannot use delta with objects
pass

def test_structured_array_contain_object(self):
# skip this one, cannot use delta on structured array
pass


# custom store, does not support getsize()
class CustomMapping(object):
Expand Down