Skip to content

Commit 1b30f4b

Browse files
authored
Optimize store and to_zarr when target store is None (#871)
1 parent d2e9949 commit 1b30f4b

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

cubed/core/ops.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,19 @@ def store(
183183
def _store_array(
184184
source: "Array", target, path=None, region=None, blockwise_kwargs=None
185185
):
186-
if target is not None and not is_storage_array(target):
186+
if target is None:
187+
if region is not None:
188+
raise ValueError("Target store must be specified when setting a region")
189+
else:
190+
return source
191+
if not is_storage_array(target):
187192
target = lazy_zarr_array(
188193
target,
189194
shape=source.shape,
190195
dtype=source.dtype,
191196
chunks=source.chunksize,
192197
path=path,
193198
)
194-
if target is None and region is not None:
195-
raise ValueError("Target store must be specified when setting a region")
196199
identity = lambda a: a
197200
blockwise_kwargs = blockwise_kwargs or {}
198201
if region is None or all(r == slice(None) for r in region):

cubed/tests/test_rechunk.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import cubed
44
import cubed as xp
5+
from cubed.core.ops import _store_array
56

67

78
@pytest.mark.parametrize(
@@ -85,3 +86,24 @@ def test_rechunk_era5_chunk_sizes(spec):
8586
((22528, 41, 109), (22528, 10, 30)),
8687
((350640, 10, 30), (350640, 10, 10)),
8788
]
89+
90+
91+
def test_rechunk_and_store():
92+
# from https://github.com/cubed-dev/cubed/issues/859
93+
shape = (394488, 778, 706)
94+
source_chunks = (24, 778, 706)
95+
target_chunks = (43800, 5, 5)
96+
spec = cubed.Spec(allowed_mem="5GB")
97+
98+
a = xp.empty(shape, dtype=xp.float32, chunks=source_chunks, spec=spec)
99+
b = a.rechunk(target_chunks)
100+
101+
num_tasks = b.plan().num_tasks
102+
103+
# simulate what to_zarr or store does
104+
c = _store_array(b, None)
105+
106+
c.visualize()
107+
108+
# store should not change number of tasks
109+
assert c.plan().num_tasks == num_tasks

0 commit comments

Comments
 (0)