We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
to_zarr
compute=False
1 parent 476af9d commit 799200dCopy full SHA for 799200d
2 files changed
cubed/core/ops.py
@@ -277,7 +277,9 @@ def __iter__(self):
277
return out
278
279
280
-def to_zarr(x: "Array", store, path=None, region=None, executor=None, **kwargs):
+def to_zarr(
281
+ x: "Array", store, path=None, region=None, compute=True, *, executor=None, **kwargs
282
+):
283
"""Save an array to Zarr storage.
284
285
Note that this operation is eager, and will run the computation
@@ -293,12 +295,17 @@ def to_zarr(x: "Array", store, path=None, region=None, executor=None, **kwargs):
293
295
Group path
294
296
region : tuple of slices, optional
297
The region of data that should be written to in target.
298
+ compute : boolean, optional
299
+ If True compute immediately, return array otherwise.
300
executor : cubed.runtime.types.Executor, optional
301
The executor to use to run the computation.
302
Defaults to using the in-process Python executor.
303
"""
304
out = _store_array(x, store, path=path, region=region)
- out.compute(executor=executor, _return_in_memory_array=False, **kwargs)
305
+ if compute:
306
+ out.compute(executor=executor, _return_in_memory_array=False, **kwargs)
307
+ else:
308
+ return out
309
310
311
def blockwise(
cubed/tests/test_core.py
@@ -224,6 +224,25 @@ def test_to_zarr_array(tmp_path, spec, executor):
224
assert_array_equal(res[:], np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
225
226
227
+def test_to_zarr_lazy_compute(tmp_path, spec):
228
+ a = xp.asarray([[1, 2, 3], [4, 5, 6], [7, 8, 9]], chunks=(2, 2), spec=spec)
229
+ store = tmp_path / "output.zarr"
230
+ z = create_zarr(
231
+ np.zeros(a.shape, dtype=a.dtype),
232
+ chunks=(2, 2),
233
+ store=store,
234
+ )
235
+ b = cubed.to_zarr(a, z, compute=False)
236
+ # target has not been computed yet
237
+ res = open_storage_array(store, mode="r")
238
+ with pytest.raises(AssertionError):
239
+ assert_array_equal(res[:], np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
240
+
241
+ b.compute()
242
243
244
245
246
def test_to_zarr_region(tmp_path, spec, executor):
247
a = xp.asarray(
248
[
0 commit comments