Skip to content

Commit b726b1a

Browse files
authored
Merge pull request #904 from m-albert/fix_build_pyramids_open_array_kwargs_test
Fixed build_pyramid test related to open_array_kwargs
2 parents 1728257 + 65f2ed0 commit b726b1a

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

tests/test_unit_pyramid_creation.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dask.array as da
22
import pytest
3+
import zarr
34
from devtools import debug
45

56
from fractal_tasks_core.pyramids import build_pyramid
@@ -9,31 +10,40 @@ def test_build_pyramid(tmp_path):
910

1011
# Fail because only 2D,3D,4D are supported / A
1112
zarrurl = str(tmp_path / "A.zarr")
12-
da.ones(shape=(16,)).to_zarr(f"{zarrurl}/0")
13+
# Specify the dimension separator as '/'
14+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
15+
# Save the Dask array to the Zarr store
16+
da.ones(shape=(16,)).to_zarr(store)
1317
with pytest.raises(ValueError) as e:
1418
build_pyramid(zarrurl=zarrurl)
1519
debug(e.value)
1620
assert "ndims" in str(e.value)
1721

1822
# Fail because only 2D,3D,4D are supported / B
1923
zarrurl = str(tmp_path / "B.zarr")
20-
da.ones(shape=(2, 2, 2, 2, 2)).to_zarr(f"{zarrurl}/0")
24+
# Specify the dimension separator as '/'
25+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
26+
da.ones(shape=(2, 2, 2, 2, 2)).to_zarr(store)
2127
with pytest.raises(ValueError) as e:
2228
build_pyramid(zarrurl=zarrurl)
2329
debug(e.value)
2430
assert "ndims" in str(e.value)
2531

2632
# Fail because there is not enough data for coarsening
2733
zarrurl = str(tmp_path / "C.zarr")
28-
da.ones(shape=(4, 4)).to_zarr(f"{zarrurl}/0")
34+
# Specify the dimension separator as '/'
35+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
36+
da.ones(shape=(4, 4)).to_zarr(store)
2937
with pytest.raises(ValueError) as e:
3038
build_pyramid(zarrurl=zarrurl, coarsening_xy=10)
3139
debug(e.value)
3240
assert "but previous level has shape" in str(e.value)
3341

3442
# Succeed
3543
zarrurl = str(tmp_path / "D.zarr")
36-
da.ones(shape=(8, 8)).to_zarr(f"{zarrurl}/0")
44+
# Specify the dimension separator as '/'
45+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
46+
da.ones(shape=(8, 8)).to_zarr(store)
3747
build_pyramid(zarrurl=zarrurl, coarsening_xy=2, num_levels=3)
3848
level_1 = da.from_zarr(f"{zarrurl}/1")
3949
level_2 = da.from_zarr(f"{zarrurl}/2")
@@ -44,7 +54,9 @@ def test_build_pyramid(tmp_path):
4454

4555
# Succeed
4656
zarrurl = str(tmp_path / "E.zarr")
47-
da.ones(shape=(243 + 2, 243)).to_zarr(f"{zarrurl}/0")
57+
# Specify the dimension separator as '/'
58+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
59+
da.ones(shape=(243 + 2, 243)).to_zarr(store)
4860
build_pyramid(zarrurl=zarrurl, coarsening_xy=3, num_levels=6, chunksize=9)
4961
level_1 = da.from_zarr(f"{zarrurl}/1")
5062
level_2 = da.from_zarr(f"{zarrurl}/2")
@@ -65,28 +77,42 @@ def test_build_pyramid(tmp_path):
6577
assert level_4.shape == (3, 3)
6678
assert level_5.shape == (1, 1)
6779

68-
# Succeed
69-
zarrurl = str(tmp_path / "F.zarr")
70-
da.zeros(shape=(8, 8)).to_zarr(f"{zarrurl}/0")
80+
# check that open_array_kwargs has an effect
81+
zarrurl = tmp_path / "F.zarr"
82+
# Specify the dimension separator as '/'
83+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
84+
da.ones(shape=(8, 8)).to_zarr(store)
85+
build_pyramid(
86+
zarrurl=zarrurl,
87+
coarsening_xy=2,
88+
num_levels=3,
89+
open_array_kwargs={"write_empty_chunks": True},
90+
)
91+
# check that empty chunks are written to disk
92+
assert (zarrurl / "1/0/0").exists()
93+
assert (zarrurl / "2/0/0").exists()
94+
95+
zarrurl = tmp_path / "G.zarr"
96+
# Specify the dimension separator as '/'
97+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
98+
da.zeros(shape=(8, 8)).to_zarr(store)
7199
build_pyramid(
72100
zarrurl=zarrurl,
73101
coarsening_xy=2,
74102
num_levels=3,
75103
open_array_kwargs={"write_empty_chunks": False, "fill_value": 0},
76104
)
77-
level_1 = da.from_zarr(f"{zarrurl}/1")
78-
level_2 = da.from_zarr(f"{zarrurl}/2")
79-
assert level_1.shape == (4, 4)
80-
assert level_2.shape == (2, 2)
81-
# check that the empty chunks are not written to disk
82-
assert not (tmp_path / "F.zarr/1/0.0").exists()
83-
assert not (tmp_path / "F.zarr/2/0.0").exists()
105+
# check that empty chunks are not written to disk
106+
assert not (zarrurl / "1/0/0").exists()
107+
assert not (zarrurl / "2/0/0").exists()
84108

85109

86110
def test_build_pyramid_overwrite(tmp_path):
87111
# Succeed
88-
zarrurl = str(tmp_path / "D.zarr")
89-
da.ones(shape=(8, 8)).to_zarr(f"{zarrurl}/0")
112+
zarrurl = str(tmp_path / "K.zarr")
113+
# Specify the dimension separator as '/'
114+
store = zarr.DirectoryStore(f"{zarrurl}/0", dimension_separator="/")
115+
da.ones(shape=(8, 8)).to_zarr(store)
90116
build_pyramid(zarrurl=zarrurl, coarsening_xy=2, num_levels=3)
91117
# Should fail because overwrite is not set
92118
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)