|
13 | 13 | from nose.tools import (eq_ as eq, assert_is_instance, assert_raises, assert_true,
|
14 | 14 | assert_false, assert_is, assert_is_none)
|
15 | 15 | from nose import SkipTest
|
| 16 | +import pytest |
16 | 17 |
|
17 | 18 |
|
18 | 19 | from zarr.storage import (DirectoryStore, init_array, init_group, NestedDirectoryStore,
|
|
22 | 23 | from zarr.compat import PY2
|
23 | 24 | from zarr.util import buffer_size
|
24 | 25 | from numcodecs import (Delta, FixedScaleOffset, Zlib, Blosc, BZ2, MsgPack, Pickle,
|
25 |
| - Categorize) |
| 26 | + Categorize, JSON) |
26 | 27 |
|
27 | 28 |
|
28 | 29 | # noinspection PyMethodMayBeStatic
|
@@ -901,6 +902,43 @@ def test_object_arrays(self):
|
901 | 902 | a = z[:]
|
902 | 903 | assert a.dtype == object
|
903 | 904 |
|
| 905 | + # create an object array using JSON |
| 906 | + z = self.create_array(shape=10, chunks=3, dtype=object, object_codec=JSON()) |
| 907 | + z[0] = 'foo' |
| 908 | + assert z[0] == 'foo' |
| 909 | + # z[1] = b'bar' |
| 910 | + # assert z[1] == b'bar' # not supported for JSON |
| 911 | + z[2] = 1 |
| 912 | + assert z[2] == 1 |
| 913 | + z[3] = [2, 4, 6, 'baz'] |
| 914 | + assert z[3] == [2, 4, 6, 'baz'] |
| 915 | + z[4] = {'a': 'b', 'c': 'd'} |
| 916 | + assert z[4] == {'a': 'b', 'c': 'd'} |
| 917 | + a = z[:] |
| 918 | + assert a.dtype == object |
| 919 | + |
| 920 | + def test_object_arrays_text(self): |
| 921 | + |
| 922 | + from numcodecs.tests.common import greetings |
| 923 | + data = np.array(greetings * 1000, dtype=object) |
| 924 | + |
| 925 | + z = self.create_array(shape=data.shape, dtype=object, object_codec=MsgPack()) |
| 926 | + z[:] = data |
| 927 | + assert_array_equal(data, z[:]) |
| 928 | + |
| 929 | + z = self.create_array(shape=data.shape, dtype=object, object_codec=JSON()) |
| 930 | + z[:] = data |
| 931 | + assert_array_equal(data, z[:]) |
| 932 | + |
| 933 | + z = self.create_array(shape=data.shape, dtype=object, object_codec=Pickle()) |
| 934 | + z[:] = data |
| 935 | + assert_array_equal(data, z[:]) |
| 936 | + |
| 937 | + z = self.create_array(shape=data.shape, dtype=object, |
| 938 | + object_codec=Categorize(greetings, dtype=object)) |
| 939 | + z[:] = data |
| 940 | + assert_array_equal(data, z[:]) |
| 941 | + |
904 | 942 | def test_object_arrays_danger(self):
|
905 | 943 |
|
906 | 944 | # do something dangerous - manually force an object array with no object codec
|
@@ -938,6 +976,12 @@ def test_object_arrays_danger(self):
|
938 | 976 | # noinspection PyStatementEffect
|
939 | 977 | v[:]
|
940 | 978 |
|
| 979 | + def test_object_codec_warnings(self): |
| 980 | + |
| 981 | + with pytest.warns(UserWarning): |
| 982 | + # provide object_codec, but not object dtype |
| 983 | + self.create_array(shape=10, chunks=5, dtype='i4', object_codec=JSON()) |
| 984 | + |
941 | 985 |
|
942 | 986 | class TestArrayWithPath(TestArray):
|
943 | 987 |
|
@@ -1367,6 +1411,10 @@ def test_object_arrays(self):
|
1367 | 1411 | # skip this one, cannot use delta with objects
|
1368 | 1412 | pass
|
1369 | 1413 |
|
| 1414 | + def test_object_arrays_text(self): |
| 1415 | + # skip this one, cannot use delta with objects |
| 1416 | + pass |
| 1417 | + |
1370 | 1418 | def test_object_arrays_danger(self):
|
1371 | 1419 | # skip this one, cannot use delta with objects
|
1372 | 1420 | pass
|
|
0 commit comments