Skip to content

Commit faeb43f

Browse files
committed
Test support for regions in to_zarr
1 parent df937c8 commit faeb43f

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

cubed_xarray/cubedmanager.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,12 @@ def store(
210210
if lock:
211211
raise NotImplementedError("Locking is not supported.")
212212

213-
regions = kwargs.pop("regions", None)
214-
if regions:
215-
# regions is either a tuple of slices or a collection of tuples of slices
216-
if isinstance(regions, tuple):
217-
regions = [regions]
218-
for t in regions:
219-
if not all(r == slice(None) for r in t):
220-
raise NotImplementedError(
221-
"Only whole slices are supported for regions."
222-
)
223-
224213
kwargs.pop("flush", None) # not used
225214

226215
return store(
227216
sources,
228217
targets,
229-
**kwargs,
218+
**kwargs, # regions passed through here
230219
)
231220

232221

cubed_xarray/tests/test_wrapping.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22

33
import cubed
4+
import numpy as np
45
import pytest
56
import xarray as xr
67
from cubed.runtime.create import create_executor
@@ -75,6 +76,29 @@ def test_to_zarr(tmpdir, executor):
7576
assert_allclose(original, computed)
7677

7778

79+
# based on test_write_region
80+
def test_write_region(tmpdir):
81+
zeros = xr.Dataset({"u": (("x",), np.zeros(10))}).chunk(
82+
2, chunked_array_type="cubed"
83+
)
84+
nonzeros = xr.Dataset({"u": (("x",), np.arange(1, 11))}).chunk(
85+
2, chunked_array_type="cubed"
86+
)
87+
88+
store = tmpdir / "out.zarr"
89+
zeros.to_zarr(
90+
store,
91+
encoding={"u": dict(chunks=2)},
92+
)
93+
with xr.open_zarr(store) as actual:
94+
assert_identical(actual, zeros)
95+
for i in range(0, 10, 2):
96+
region = {"x": slice(i, i + 2)}
97+
nonzeros.isel(region).to_zarr(store, region=region)
98+
with xr.open_zarr(store) as actual:
99+
assert_identical(actual, nonzeros)
100+
101+
78102
def test_dataset_accessor_visualize(tmp_path):
79103
spec = cubed.Spec(allowed_mem="200MB")
80104

0 commit comments

Comments
 (0)