Skip to content

Commit 4a05a5c

Browse files
committed
Handle overwrite for existing pyramid levels & add test
1 parent afffd1b commit 4a05a5c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

fractal_tasks_core/pyramids.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import dask.array as da
2424
import numpy as np
25-
import zarr
25+
import zarr.errors
2626

2727
logger = logging.getLogger(__name__)
2828

@@ -107,6 +107,18 @@ def build_pyramid(
107107
if open_array_kwargs is None:
108108
open_array_kwargs = {}
109109

110+
# If overwrite is false, check that the array doesn't exist yet
111+
if not overwrite:
112+
try:
113+
zarr.open(f"{zarrurl}/{ind_level}", mode="r")
114+
raise ValueError(
115+
f"While building the pyramids, pyramid level {ind_level} "
116+
"already existed, but `build_pyramid` was called with "
117+
f"{overwrite=}."
118+
)
119+
except zarr.errors.PathNotFoundError:
120+
pass
121+
110122
zarrarr = zarr.open(
111123
f"{zarrurl}/{ind_level}",
112124
shape=newlevel_rechunked.shape,

tests/test_unit_pyramid_creation.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,19 @@ def test_build_pyramid(tmp_path):
8181
# check that the empty chunks are not written to disk
8282
assert not (tmp_path / "F.zarr/1/0.0").exists()
8383
assert not (tmp_path / "F.zarr/2/0.0").exists()
84+
85+
86+
def test_build_pyramid_overwrite(tmp_path):
87+
# Succeed
88+
zarrurl = str(tmp_path / "D.zarr")
89+
da.ones(shape=(8, 8)).to_zarr(f"{zarrurl}/0")
90+
build_pyramid(zarrurl=zarrurl, coarsening_xy=2, num_levels=3)
91+
# Should fail because overwrite is not set
92+
with pytest.raises(ValueError):
93+
build_pyramid(
94+
zarrurl=zarrurl, coarsening_xy=2, num_levels=3, overwrite=False
95+
)
96+
# Should work
97+
build_pyramid(
98+
zarrurl=zarrurl, coarsening_xy=2, num_levels=3, overwrite=True
99+
)

0 commit comments

Comments
 (0)