Skip to content

Commit c0e5594

Browse files
committed
Added optional parameter open_array_kwargs to build_pyramid, to be passed as kwargs to zarr.open when creating the pyramid level arrays.
1 parent d27e8f5 commit c0e5594

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

fractal_tasks_core/pyramids.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"""
1515
import logging
1616
import pathlib
17+
import zarr
1718
from typing import Callable
19+
from typing import Mapping
1820
from typing import Optional
1921
from typing import Sequence
2022
from typing import Union
@@ -33,6 +35,7 @@ def build_pyramid(
3335
coarsening_xy: int = 2,
3436
chunksize: Optional[Sequence[int]] = None,
3537
aggregation_function: Optional[Callable] = None,
38+
open_array_kwargs: Optional[Mapping] = None,
3639
) -> None:
3740

3841
"""
@@ -48,6 +51,7 @@ def build_pyramid(
4851
coarsening_xy: Linear coarsening factor between subsequent levels.
4952
chunksize: Shape of a single chunk.
5053
aggregation_function: Function to be used when downsampling.
54+
open_array_kwargs: Additional arguments for zarr.open.
5155
"""
5256

5357
# Clean up zarrurl
@@ -100,10 +104,21 @@ def build_pyramid(
100104
f"{str(newlevel_rechunked)}"
101105
)
102106

107+
if open_array_kwargs is None:
108+
open_array_kwargs = {}
109+
110+
zarrarr = zarr.open(
111+
f"{zarrurl}/{ind_level}",
112+
shape=newlevel_rechunked.shape,
113+
chunks=newlevel_rechunked.chunksize,
114+
dtype=newlevel_rechunked.dtype,
115+
mode="w",
116+
**open_array_kwargs,
117+
)
118+
103119
# Write zarr and store output (useful to construct next level)
104120
previous_level = newlevel_rechunked.to_zarr(
105-
zarrurl,
106-
component=f"{ind_level}",
121+
zarrarr,
107122
overwrite=overwrite,
108123
compute=True,
109124
return_stored=True,

tests/test_unit_pyramid_creation.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ def test_build_pyramid(tmp_path):
6464
assert level_3.chunksize == (9, 9)
6565
assert level_4.shape == (3, 3)
6666
assert level_5.shape == (1, 1)
67+
68+
# Succeed
69+
zarrurl = str(tmp_path / "F.zarr")
70+
da.zeros(shape=(8, 8)).to_zarr(f"{zarrurl}/0")
71+
build_pyramid(zarrurl=zarrurl, coarsening_xy=2, num_levels=3,
72+
open_array_kwargs={"write_empty_chunks": False, "fill_value": 0})
73+
level_1 = da.from_zarr(f"{zarrurl}/1")
74+
level_2 = da.from_zarr(f"{zarrurl}/2")
75+
assert level_1.shape == (4, 4)
76+
assert level_2.shape == (2, 2)
77+
# check that the empty chunks are not written to disk
78+
assert not (tmp_path / "F.zarr/1/0.0").exists()
79+
assert not (tmp_path / "F.zarr/2/0.0").exists()

0 commit comments

Comments
 (0)