Skip to content

Commit 0cf5c19

Browse files
author
Martin Durant
committed
Writing with setitems
1 parent fc5aef3 commit 0cf5c19

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

zarr/core.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,24 @@ def _set_selection(self, indexer, value, fields=None):
15581558
self._chunk_setitem(chunk_coords, chunk_selection, chunk_value, fields=fields)
15591559
else:
15601560
lchunk_coords, lchunk_selection, lout_selection = zip(*indexer)
1561-
self._chunk_setitems(lchunk_coords, lchunk_selection, out, lout_selection,
1561+
chunk_values = []
1562+
for out_selection in lout_selection:
1563+
if sel_shape == ():
1564+
chunk_values.append(value)
1565+
elif is_scalar(value, self._dtype):
1566+
chunk_values.append(value)
1567+
else:
1568+
cv = value[out_selection]
1569+
# handle missing singleton dimensions
1570+
if indexer.drop_axes:
1571+
item = [slice(None)] * self.ndim
1572+
for a in indexer.drop_axes:
1573+
item[a] = np.newaxis
1574+
item = tuple(item)
1575+
cv = chunk_value[item]
1576+
chunk_values.append(cv)
1577+
1578+
self._chunk_setitems(lchunk_coords, lchunk_selection, chunk_values,
15621579
fields=fields)
15631580

15641581
def _process_chunk(self, out, cdata, chunk_selection, drop_axes,
@@ -1683,6 +1700,12 @@ def _chunk_getitems(self, lchunk_coords, lchunk_selection, out, lout_selection,
16831700
fill_value = self._fill_value
16841701
out[out_select] = fill_value
16851702

1703+
def _chunk_setitems(self, lchunk_coords, lchunk_selection, values, fields=None):
1704+
ckeys = [self._chunk_key(co) for co in lchunk_coords]
1705+
cdatas = [self._process_for_setitem(sel, val, fields=fields)
1706+
for sel, val in zip(lchunk_selection, values)]
1707+
self.chunk_store.setitems({k: v for k, v in zip(ckeys, cdatas)})
1708+
16861709
def _chunk_setitem(self, chunk_coords, chunk_selection, value, fields=None):
16871710
"""Replace part or whole of a chunk.
16881711
@@ -1710,10 +1733,12 @@ def _chunk_setitem(self, chunk_coords, chunk_selection, value, fields=None):
17101733
fields=fields)
17111734

17121735
def _chunk_setitem_nosync(self, chunk_coords, chunk_selection, value, fields=None):
1713-
1714-
# obtain key for chunk storage
17151736
ckey = self._chunk_key(chunk_coords)
1737+
cdata = self._process_for_setitem(chunk_selection, value, fields=fields)
1738+
# store
1739+
self.chunk_store[ckey] = cdata
17161740

1741+
def _process_for_setitem(self, chunk_selection, value, fields=None):
17171742
if is_total_slice(chunk_selection, self._chunks) and not fields:
17181743
# totally replace chunk
17191744

@@ -1768,10 +1793,7 @@ def _chunk_setitem_nosync(self, chunk_coords, chunk_selection, value, fields=Non
17681793
chunk[chunk_selection] = value
17691794

17701795
# encode chunk
1771-
cdata = self._encode_chunk(chunk)
1772-
1773-
# store
1774-
self.chunk_store[ckey] = cdata
1796+
return self._encode_chunk(chunk)
17751797

17761798
def _chunk_key(self, chunk_coords):
17771799
return self._key_prefix + '.'.join(map(str, chunk_coords))

zarr/storage.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,12 @@ def __getitem__(self, key):
10491049
except self.exceptions as e:
10501050
raise KeyError(key) from e
10511051

1052+
def setitems(self, values):
1053+
if self.mode == 'r':
1054+
raise ReadOnlyError()
1055+
values = {self._normalize_key(key): val for key, val in values.items()}
1056+
self.map.setitems(values)
1057+
10521058
def __setitem__(self, key, value):
10531059
if self.mode == 'r':
10541060
raise ReadOnlyError()

0 commit comments

Comments
 (0)