Skip to content

Commit b716e47

Browse files
Chunkwise iteration over arrays.
Closes #398.
1 parent 43f7fae commit b716e47

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

zarr/core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,18 @@ def __array__(self, *args):
424424
a = a.astype(args[0])
425425
return a
426426

427+
def __iter__(self):
428+
if len(self.shape) == 0:
429+
# Same error as numpy
430+
raise TypeError("iteration over a 0-d array")
431+
# Avoid repeatedly decompressing chunks by iterating over the chunks
432+
# in the first dimension.
433+
chunk_size = self.chunks[0]
434+
for j in range(self.shape[0]):
435+
if j % chunk_size == 0:
436+
chunk = self[j: j + chunk_size][:]
437+
yield chunk[j % chunk_size]
438+
427439
def __len__(self):
428440
if self.shape:
429441
return self.shape[0]

zarr/tests/test_core.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import pickle
88
import os
99
import warnings
10+
import itertools
1011

1112

1213
import numpy as np
@@ -1155,6 +1156,40 @@ def test_object_codec_warnings(self):
11551156
# provide object_codec, but not object dtype
11561157
self.create_array(shape=10, chunks=5, dtype='i4', object_codec=JSON())
11571158

1159+
def test_zero_d_iter(self):
1160+
a = np.array(1, dtype=int)
1161+
z = self.create_array(shape=a.shape, dtype=int)
1162+
z[...] = a
1163+
with pytest.raises(TypeError):
1164+
# noinspection PyStatementEffect
1165+
list(a)
1166+
with pytest.raises(TypeError):
1167+
# noinspection PyStatementEffect
1168+
list(z)
1169+
1170+
def test_iter(self):
1171+
params = (
1172+
((1,), (1,)),
1173+
((2,), (1,)),
1174+
((1,), (2,)),
1175+
((3,), (3,)),
1176+
((1000,), (100,)),
1177+
((100,), (1000,)),
1178+
((1, 100), (1, 1)),
1179+
((1, 0), (1, 1)),
1180+
((0, 1), (1, 1)),
1181+
((0, 1), (2, 1)),
1182+
((100, 1), (3, 1)),
1183+
((100, 100), (10, 10)),
1184+
((10, 10, 10), (3, 3, 3)),
1185+
)
1186+
for shape, chunks in params:
1187+
z = self.create_array(shape=shape, chunks=chunks, dtype=int)
1188+
a = np.arange(np.product(shape)).reshape(shape)
1189+
z[:] = a
1190+
for expect, actual in itertools.zip_longest(a, z):
1191+
assert_array_equal(expect, actual)
1192+
11581193

11591194
class TestArrayWithPath(TestArray):
11601195

0 commit comments

Comments
 (0)