Skip to content

Commit 495bcf3

Browse files
Handle kwargs better in store (#14)
* Handle kwargs better in store * Add a test * Only run with processes executor on Python 3.11 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0b35a57 commit 495bcf3

File tree

3 files changed

+70
-12
lines changed

3 files changed

+70
-12
lines changed

cubed_xarray/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from importlib.metadata import version
22

3-
43
try:
54
__version__ = version("cubed-xarray")
65
except Exception:

cubed_xarray/cubedmanager.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
from __future__ import annotations
22

33
from collections.abc import Sequence
4-
from typing import TYPE_CHECKING, Any, Callable, Union
4+
from typing import TYPE_CHECKING, Any, Callable, Iterable, Union
55

66
import numpy as np
7-
87
from tlz import partition
9-
108
from xarray.namedarray.parallelcompat import ChunkManagerEntrypoint
119

12-
1310
if TYPE_CHECKING:
14-
from xarray.core.types import T_Chunks, T_NormalizedChunks
1511
from cubed import Array as CubedArray
12+
from xarray.core.types import T_Chunks, T_NormalizedChunks
1613

1714

1815
class CubedManager(ChunkManagerEntrypoint["CubedArray"]):
@@ -204,6 +201,27 @@ def store(
204201
"""Used when writing to any backend."""
205202
from cubed.core.ops import store
206203

204+
compute = kwargs.pop("compute", True)
205+
if not compute:
206+
raise NotImplementedError("Delayed compute is not supported.")
207+
208+
lock = kwargs.pop("lock", None)
209+
if lock:
210+
raise NotImplementedError("Locking is not supported.")
211+
212+
regions = kwargs.pop("regions", None)
213+
if regions:
214+
# regions is either a tuple of slices or a collection of tuples of slices
215+
if isinstance(regions, tuple):
216+
regions = [regions]
217+
for t in regions:
218+
if not all(r == slice(None) for r in t):
219+
raise NotImplementedError(
220+
"Only whole slices are supported for regions."
221+
)
222+
223+
kwargs.pop("flush", None) # not used
224+
207225
return store(
208226
sources,
209227
targets,

cubed_xarray/tests/test_wrapping.py

+47-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
1+
import sys
2+
3+
import cubed
4+
import pytest
15
import xarray as xr
6+
from cubed.runtime.create import create_executor
27
from xarray.namedarray.parallelcompat import list_chunkmanagers
3-
import cubed
8+
from xarray.tests import assert_allclose, create_test_data
49

510
from cubed_xarray.cubedmanager import CubedManager
611

12+
EXECUTORS = [create_executor("single-threaded")]
13+
14+
if sys.version_info >= (3, 11):
15+
EXECUTORS.append(create_executor("processes"))
16+
17+
18+
@pytest.fixture(
19+
scope="module",
20+
params=EXECUTORS,
21+
ids=[executor.name for executor in EXECUTORS],
22+
)
23+
def executor(request):
24+
return request.param
25+
726

827
class TestDiscoverCubedManager:
928
def test_list_cubedmanager(self):
1029
chunkmanagers = list_chunkmanagers()
11-
assert 'cubed' in chunkmanagers
12-
assert isinstance(chunkmanagers['cubed'], CubedManager)
30+
assert "cubed" in chunkmanagers
31+
assert isinstance(chunkmanagers["cubed"], CubedManager)
1332

1433
def test_chunk(self):
15-
da = xr.DataArray([1, 2], dims='x')
16-
chunked = da.chunk(x=1, chunked_array_type='cubed')
34+
da = xr.DataArray([1, 2], dims="x")
35+
chunked = da.chunk(x=1, chunked_array_type="cubed")
1736
assert isinstance(chunked.data, cubed.Array)
18-
assert chunked.chunksizes == {'x': (1, 1)}
37+
assert chunked.chunksizes == {"x": (1, 1)}
1938

2039
# TODO test cubed is default when dask not installed
2140

2241
# TODO test dask is default over cubed when both installed
42+
43+
44+
def test_to_zarr(tmpdir, executor):
45+
spec = cubed.Spec(allowed_mem="200MB", executor=executor)
46+
47+
original = create_test_data().chunk(
48+
chunked_array_type="cubed", from_array_kwargs={"spec": spec}
49+
)
50+
51+
filename = tmpdir / "out.zarr"
52+
original.to_zarr(filename)
53+
54+
with xr.open_dataset(
55+
filename,
56+
chunks="auto",
57+
engine="zarr",
58+
chunked_array_type="cubed",
59+
from_array_kwargs={"spec": spec},
60+
) as restored:
61+
assert isinstance(restored.var1.data, cubed.Array)
62+
computed = restored.compute()
63+
assert_allclose(original, computed)

0 commit comments

Comments
 (0)