Skip to content

Commit 7c472e4

Browse files
committed
fix error afer rebase
Original commit before rebase was 3ae237f
1 parent 3f27122 commit 7c472e4

File tree

8 files changed

+275
-157
lines changed

8 files changed

+275
-157
lines changed

docs/release.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Release notes
22
=============
33

4-
54
2.6.0
65
-----
76

@@ -41,7 +40,6 @@ merged PR tagged with the 2.6 milestone.
4140
chunks and is stored contiguously (last dimensions for C layout, firsts for
4241
F). Set ``partial_decompress=True`` when creating an ``Array``
4342

44-
4543
2.5.0
4644
-----
4745

zarr/core.py

Lines changed: 92 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,36 @@
1212
from zarr.attrs import Attributes
1313
from zarr.codecs import AsType, get_codec
1414
from zarr.errors import ArrayNotFoundError, ReadOnlyError, ArrayIndexError
15-
from zarr.indexing import (BasicIndexer, CoordinateIndexer, MaskIndexer,
16-
OIndex, OrthogonalIndexer, VIndex, PartialChunkIterator,
17-
check_fields,
18-
check_no_multi_fields, ensure_tuple,
19-
err_too_many_indices, is_contiguous_selection,
20-
is_scalar, pop_fields)
15+
from zarr.indexing import (
16+
BasicIndexer,
17+
CoordinateIndexer,
18+
MaskIndexer,
19+
OIndex,
20+
OrthogonalIndexer,
21+
VIndex,
22+
PartialChunkIterator,
23+
check_fields,
24+
check_no_multi_fields,
25+
ensure_tuple,
26+
err_too_many_indices,
27+
is_contiguous_selection,
28+
is_scalar,
29+
pop_fields,
30+
)
2131
from zarr.meta import decode_array_metadata, encode_array_metadata
2232
from zarr.storage import array_meta_key, attrs_key, getsize, listdir
23-
from zarr.util import (InfoReporter, check_array_shape, human_readable_size,
24-
is_total_slice, nolock, normalize_chunks,
25-
normalize_resize_args, normalize_shape,
26-
normalize_storage_path, PartialReadBuffer)
33+
from zarr.util import (
34+
InfoReporter,
35+
check_array_shape,
36+
human_readable_size,
37+
is_total_slice,
38+
nolock,
39+
normalize_chunks,
40+
normalize_resize_args,
41+
normalize_shape,
42+
normalize_storage_path,
43+
PartialReadBuffer,
44+
)
2745

2846

2947
# noinspection PyUnresolvedReferences
@@ -107,9 +125,17 @@ class Array(object):
107125
108126
"""
109127

110-
def __init__(self, store, path=None, read_only=False, chunk_store=None,
111-
synchronizer=None, cache_metadata=True, cache_attrs=True,
112-
partial_decompress=True):
128+
def __init__(
129+
self,
130+
store,
131+
path=None,
132+
read_only=False,
133+
chunk_store=None,
134+
synchronizer=None,
135+
cache_metadata=True,
136+
cache_attrs=True,
137+
partial_decompress=True,
138+
):
113139
# N.B., expect at this point store is fully initialized with all
114140
# configuration metadata fully specified and normalized
115141

@@ -1026,8 +1052,9 @@ def _get_selection(self, indexer, out=None, fields=None):
10261052
else:
10271053
check_array_shape('out', out, out_shape)
10281054

1029-
# iterate over chunks, not self.size becuase then indexer none and cant be zipped, will raise error
1030-
if not hasattr(self.chunk_store, "getitems") or not self.size:
1055+
# iterate over chunks
1056+
if not hasattr(self.chunk_store, "getitems") or \
1057+
any(map(lambda x: x == 0, self.shape)):
10311058
# sequentially get one key at a time from storage
10321059
for chunk_coords, chunk_selection, out_selection in indexer:
10331060

@@ -1586,9 +1613,17 @@ def _set_selection(self, indexer, value, fields=None):
15861613
self._chunk_setitems(lchunk_coords, lchunk_selection, chunk_values,
15871614
fields=fields)
15881615

1589-
def _process_chunk(self, out, cdata, chunk_selection, drop_axes,
1590-
out_is_ndarray, fields, out_selection,
1591-
partial_read_decode=False):
1616+
def _process_chunk(
1617+
self,
1618+
out,
1619+
cdata,
1620+
chunk_selection,
1621+
drop_axes,
1622+
out_is_ndarray,
1623+
fields,
1624+
out_selection,
1625+
partial_read_decode=False,
1626+
):
15921627
"""Take binary data from storage and fill output array"""
15931628
if (out_is_ndarray and
15941629
not fields and
@@ -1630,13 +1665,20 @@ def _process_chunk(self, out, cdata, chunk_selection, drop_axes,
16301665
index_selection = PartialChunkIterator(chunk_selection, self.chunks)
16311666
for start, nitems, partial_out_selection in index_selection:
16321667
expected_shape = [
1633-
len(range(*partial_out_selection[i].indices(self.chunks[0]+1)))
1634-
if i < len(partial_out_selection) else dim
1635-
for i, dim in enumerate(self.chunks)]
1668+
len(
1669+
range(*partial_out_selection[i].indices(self.chunks[0] + 1))
1670+
)
1671+
if i < len(partial_out_selection)
1672+
else dim
1673+
for i, dim in enumerate(self.chunks)
1674+
]
16361675
cdata.read_part(start, nitems)
16371676
chunk_partial = self._decode_chunk(
1638-
cdata.buff, start=start, nitems=nitems,
1639-
expected_shape=expected_shape)
1677+
cdata.buff,
1678+
start=start,
1679+
nitems=nitems,
1680+
expected_shape=expected_shape,
1681+
)
16401682
tmp[partial_out_selection] = chunk_partial
16411683
out[out_selection] = tmp[chunk_selection]
16421684
return
@@ -1716,20 +1758,35 @@ def _chunk_getitems(self, lchunk_coords, lchunk_selection, out, lout_selection,
17161758
out_is_ndarray = False
17171759

17181760
ckeys = [self._chunk_key(ch) for ch in lchunk_coords]
1719-
if self._partial_decompress and self._compressor \
1720-
and self._compressor.codec_id == 'blosc' and not fields \
1721-
and self.dtype != object and hasattr(self.chunk_store, "getitems"):
1761+
if (
1762+
self._partial_decompress
1763+
and self._compressor
1764+
and self._compressor.codec_id == "blosc"
1765+
and not fields
1766+
and self.dtype != object
1767+
and hasattr(self.chunk_store, "getitems")
1768+
):
17221769
partial_read_decode = True
1723-
cdatas = {ckey: PartialReadBuffer(ckey, self.chunk_store)
1724-
for ckey in ckeys if ckey in self.chunk_store}
1770+
cdatas = {
1771+
ckey: PartialReadBuffer(ckey, self.chunk_store)
1772+
for ckey in ckeys
1773+
if ckey in self.chunk_store
1774+
}
17251775
else:
17261776
partial_read_decode = False
17271777
cdatas = self.chunk_store.getitems(ckeys, on_error="omit")
17281778
for ckey, chunk_select, out_select in zip(ckeys, lchunk_selection, lout_selection):
17291779
if ckey in cdatas:
1730-
self._process_chunk(out, cdatas[ckey], chunk_select, drop_axes,
1731-
out_is_ndarray, fields, out_select,
1732-
partial_read_decode=partial_read_decode)
1780+
self._process_chunk(
1781+
out,
1782+
cdatas[ckey],
1783+
chunk_select,
1784+
drop_axes,
1785+
out_is_ndarray,
1786+
fields,
1787+
out_select,
1788+
partial_read_decode=partial_read_decode,
1789+
)
17331790
else:
17341791
# check exception type
17351792
if self._fill_value is not None:
@@ -1842,9 +1899,10 @@ def _decode_chunk(self, cdata, start=None, nitems=None, expected_shape=None):
18421899
# decompress
18431900
if self._compressor:
18441901
# only decode requested items
1845-
if ((all([x is not None for x in [start, nitems]])
1846-
and self._compressor.codec_id == 'blosc')
1847-
and hasattr(self._compressor, 'decode_partial')):
1902+
if (
1903+
all([x is not None for x in [start, nitems]])
1904+
and self._compressor.codec_id == "blosc"
1905+
) and hasattr(self._compressor, "decode_partial"):
18481906
chunk = self._compressor.decode_partial(cdata, start, nitems)
18491907
else:
18501908
chunk = self._compressor.decode(cdata)

zarr/errors.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ class CopyError(RuntimeError):
1111
class _BaseZarrError(ValueError):
1212
_msg = ""
1313

14-
class ArrayIndexError(IndexError):
15-
1614
def __init__(self, *args):
1715
super().__init__(self._msg.format(*args))
1816

1917

18+
class ArrayIndexError(IndexError):
19+
pass
20+
21+
2022
class _BaseZarrIndexError(IndexError):
2123
_msg = ""
2224

zarr/indexing.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
from zarr.errors import (
99
NegativeStepError,
10+
err_too_many_indices,
1011
VindexInvalidSelectionError,
1112
BoundsCheckError,
12-
ArrayIndexError
13+
ArrayIndexError,
1314
)
1415

1516

@@ -828,7 +829,7 @@ def pop_fields(selection):
828829

829830

830831
def int_to_slice(dim_selection):
831-
return slice(dim_selection, dim_selection+1, 1)
832+
return slice(dim_selection, dim_selection + 1, 1)
832833

833834

834835
def make_slice_selection(selection):
@@ -868,19 +869,27 @@ def __init__(self, selection, arr_shape):
868869

869870
# number of selection dimensions can't be greater than the number of chunk dimensions
870871
if len(self.selection) > len(self.arr_shape):
871-
raise ValueError('Selection has more dimensions then the array:\n'
872-
'selection dimensions = {len(self.selection)\n'
873-
'array dimensions = {len(self.arr_shape)}')
872+
raise ValueError(
873+
"Selection has more dimensions then the array:\n"
874+
"selection dimensions = {len(self.selection)\n"
875+
"array dimensions = {len(self.arr_shape)}"
876+
)
874877

875878
# any selection can not be out of the range of the chunk
876879
self.selection_shape = np.empty(self.arr_shape)[self.selection].shape
877-
if any([selection_dim < 0 or selection_dim > arr_dim for selection_dim, arr_dim
878-
in zip(self.selection_shape, self.arr_shape)]):
879-
raise IndexError('a selection index is out of range for the dimension') # pragma: no cover
880+
if any(
881+
[
882+
selection_dim < 0 or selection_dim > arr_dim
883+
for selection_dim, arr_dim in zip(self.selection_shape, self.arr_shape)
884+
]
885+
):
886+
raise IndexError(
887+
"a selection index is out of range for the dimension"
888+
) # pragma: no cover
880889

881890
for i, dim_size in enumerate(self.arr_shape[::-1]):
882-
index = len(self.arr_shape) - (i+1)
883-
if index <= len(self.selection)-1:
891+
index = len(self.arr_shape) - (i + 1)
892+
if index <= len(self.selection) - 1:
884893
slice_size = self.selection_shape[index]
885894
if slice_size == dim_size and index > 0:
886895
self.selection.pop()
@@ -892,17 +901,19 @@ def __init__(self, selection, arr_shape):
892901
for i, sl in enumerate(self.selection):
893902
dim_chunk_loc_slices = []
894903
for i, x in enumerate(slice_to_range(sl, arr_shape[i])):
895-
dim_chunk_loc_slices.append(slice(x, x+1, 1))
904+
dim_chunk_loc_slices.append(slice(x, x + 1, 1))
896905
chunk_loc_slices.append(dim_chunk_loc_slices)
897906
if last_dim_slice:
898907
chunk_loc_slices.append([last_dim_slice])
899908
self.chunk_loc_slices = list(itertools.product(*chunk_loc_slices))
900909

901910
def __iter__(self):
902911
chunk1 = self.chunk_loc_slices[0]
903-
nitems = (chunk1[-1].stop - chunk1[-1].start) * np.prod(self.arr_shape[len(chunk1):])
912+
nitems = (chunk1[-1].stop - chunk1[-1].start) * np.prod(
913+
self.arr_shape[len(chunk1) :]
914+
)
904915
for chunk_selection in self.chunk_loc_slices:
905916
start = 0
906917
for i, sl in enumerate(chunk_selection):
907-
start += sl.start * np.prod(self.arr_shape[i+1:])
918+
start += sl.start * np.prod(self.arr_shape[i + 1 :])
908919
yield int(start), int(nitems), chunk_selection

0 commit comments

Comments
 (0)