Skip to content

Commit bde89ae

Browse files
committed
deal with a.chunks is None; resolves #64
1 parent d084cb6 commit bde89ae

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

zarr/creation.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,26 @@ def full(shape, fill_value, **kwargs):
244244
return create(shape=shape, fill_value=fill_value, **kwargs)
245245

246246

247+
def _get_shape_chunks(a):
248+
shape = None
249+
chunks = None
250+
251+
if hasattr(a, 'shape') and \
252+
isinstance(a.shape, tuple):
253+
shape = a.shape
254+
255+
if hasattr(a, 'chunks') and \
256+
isinstance(a.chunks, tuple) and \
257+
(len(a.chunks) == len(a.shape)):
258+
chunks = a.chunks
259+
260+
elif hasattr(a, 'chunklen'):
261+
# bcolz carray
262+
chunks = (a.chunklen,) + a.shape[1:]
263+
264+
return shape, chunks
265+
266+
247267
def array(data, **kwargs):
248268
"""Create an array filled with `data`.
249269
@@ -279,13 +299,7 @@ def array(data, **kwargs):
279299
# setup chunks
280300
chunks = kwargs.pop('chunks', None)
281301
if chunks is None:
282-
# try to use same chunks as data
283-
if hasattr(data, 'chunklen'):
284-
# bcolz carray
285-
chunks = (data.chunklen,) + shape[1:]
286-
elif hasattr(data, 'chunks') and len(data.chunks) == len(data.shape):
287-
# h5py dataset or zarr array
288-
chunks = data.chunks
302+
_, chunks = _get_shape_chunks(data)
289303

290304
# instantiate array
291305
z = create(shape=shape, chunks=chunks, dtype=dtype, **kwargs)
@@ -428,11 +442,11 @@ def open_array(store=None, mode='a', shape=None, chunks=None, dtype=None,
428442

429443
def _like_args(a, kwargs):
430444

431-
if hasattr(a, 'shape'):
432-
kwargs.setdefault('shape', a.shape)
433-
434-
if hasattr(a, 'chunks'):
435-
kwargs.setdefault('chunks', a.chunks)
445+
shape, chunks = _get_shape_chunks(a)
446+
if shape is not None:
447+
kwargs.setdefault('shape', shape)
448+
if chunks is not None:
449+
kwargs.setdefault('chunks', chunks)
436450

437451
if hasattr(a, 'dtype'):
438452
kwargs.setdefault('dtype', a.dtype)

zarr/tests/test_creation.py

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@
2121
from zarr.codecs import Zlib
2222

2323

24+
# something bcolz-like
25+
class MockBcolzArray(object):
26+
27+
def __init__(self, data, chunklen):
28+
self.data = data
29+
self.chunklen = chunklen
30+
31+
def __getattr__(self, item):
32+
return getattr(self.data, item)
33+
34+
def __getitem__(self, item):
35+
return self.data[item]
36+
37+
38+
# something h5py-like
39+
class MockH5pyDataset(object):
40+
41+
def __init__(self, data, chunks):
42+
self.data = data
43+
self.chunks = chunks
44+
45+
def __getattr__(self, item):
46+
return getattr(self.data, item)
47+
48+
def __getitem__(self, item):
49+
return self.data[item]
50+
51+
2452
def test_array():
2553

2654
# with numpy array
@@ -44,25 +72,23 @@ def test_array():
4472
eq(z.dtype, z2.dtype)
4573
assert_array_equal(z[:], z2[:])
4674

47-
# with something bcolz-like
48-
class MockBcolzArray(object):
49-
50-
def __init__(self, data, chunklen):
51-
self.data = data
52-
self.chunklen = chunklen
53-
54-
def __getattr__(self, item):
55-
return getattr(self.data, item)
56-
57-
def __getitem__(self, item):
58-
return self.data[item]
59-
6075
b = np.arange(1000).reshape(100, 10)
6176
c = MockBcolzArray(b, 10)
6277
z3 = array(c)
6378
eq(c.shape, z3.shape)
6479
eq((10, 10), z3.chunks)
6580

81+
b = np.arange(1000).reshape(100, 10)
82+
c = MockH5pyDataset(b, chunks=(10, 2))
83+
z4 = array(c)
84+
eq(c.shape, z4.shape)
85+
eq((10, 2), z4.chunks)
86+
87+
c = MockH5pyDataset(b, chunks=None)
88+
z5 = array(c)
89+
eq(c.shape, z5.shape)
90+
assert_is_instance(z5.chunks, tuple)
91+
6692

6793
def test_empty():
6894
z = empty(100, chunks=10)
@@ -174,6 +200,7 @@ def test_open_array():
174200

175201

176202
def test_empty_like():
203+
177204
# zarr array
178205
z = empty(100, chunks=10, dtype='f4', compressor=Zlib(5),
179206
order='F')
@@ -184,18 +211,35 @@ def test_empty_like():
184211
eq(z.compressor.get_config(), z2.compressor.get_config())
185212
eq(z.fill_value, z2.fill_value)
186213
eq(z.order, z2.order)
214+
187215
# numpy array
188216
a = np.empty(100, dtype='f4')
189217
z3 = empty_like(a)
190218
eq(a.shape, z3.shape)
191219
eq((100,), z3.chunks)
192220
eq(a.dtype, z3.dtype)
193221
assert_is_none(z3.fill_value)
222+
194223
# something slightly silly
195224
a = [0] * 100
196225
z3 = empty_like(a, shape=200)
197226
eq((200,), z3.shape)
198227

228+
# other array-likes
229+
b = np.arange(1000).reshape(100, 10)
230+
c = MockBcolzArray(b, 10)
231+
z = empty_like(c)
232+
eq(b.shape, z.shape)
233+
eq((10, 10), z.chunks)
234+
c = MockH5pyDataset(b, chunks=(10, 2))
235+
z = empty_like(c)
236+
eq(b.shape, z.shape)
237+
eq((10, 2), z.chunks)
238+
c = MockH5pyDataset(b, chunks=None)
239+
z = empty_like(c)
240+
eq(b.shape, z.shape)
241+
assert_is_instance(z.chunks, tuple)
242+
199243

200244
def test_zeros_like():
201245
# zarr array

0 commit comments

Comments
 (0)