Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cubed/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,16 @@ def _store_array(
shape = target.shape
chunks = target.chunks
for i, (sl, cs) in enumerate(zip(region, chunks)):
if sl.start % cs != 0 or (sl.stop % cs != 0 and sl.stop != shape[i]):
if (sl.start is not None and sl.start % cs != 0) or (
sl.stop is not None and sl.stop % cs != 0 and sl.stop != shape[i]
):
raise ValueError(
f"Region {region} does not align with target chunks {chunks}"
)
block_offsets = [sl.start // cs for sl, cs in zip(region, chunks)]
block_offsets = [
(0 if sl.start is None else sl.start // cs)
for sl, cs in zip(region, chunks)
]

def key_function(out_key):
out_coords = out_key[1:]
Expand Down
24 changes: 23 additions & 1 deletion cubed/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ def test_to_zarr_array(tmp_path, spec, executor):

def test_to_zarr_region(tmp_path, spec, executor):
a = xp.asarray(
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]],
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20],
],
chunks=(2, 2),
spec=spec,
)
Expand Down Expand Up @@ -272,6 +278,22 @@ def test_to_zarr_region(tmp_path, spec, executor):
),
)

region = (slice(None), slice(4, 5))
cubed.to_zarr(a[:, 0:1], z, region=region, executor=executor)
res = open_storage_array(store, mode="r")
assert_array_equal(
res[:],
np.array(
[
[1, 2, 0, 0, 1],
[5, 6, 0, 0, 5],
[1, 2, 1, 2, 9],
[5, 6, 5, 6, 13],
[0, 0, 9, 10, 17],
]
),
)


def test_to_zarr_region_fails(tmp_path):
a = xp.ones((4, 4), chunks=(2, 2))
Expand Down
Loading