Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scalar chunking #365

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
- Correctly specify maximum compatible fsspec version. ([#338](https://github.com/ome/ome-zarr-py/pull/338))
- Add tests on Python 3.12. ([#338](https://github.com/ome/ome-zarr-py/pull/338))
- Write OMERO metadata. ([#261](https://github.com/ome/ome-zarr-py/pull/261))
- Fixed chunking when a scalar value for chunks is given. Previously
passing ``storage_options={"chunks": <int>}`` only set the chunk
size of the final dimension. Now the chunk size of all dimensions is
set to this value, which is identical behaviour to ``zarr-python``.
([#365](https://github.com/ome/ome-zarr-py/pull/365))

# 0.8.3 (November 2023)

Expand Down
13 changes: 6 additions & 7 deletions ome_zarr/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,13 @@ def _retuple(

E.g. if chunks is (64, 64) and shape is (3, 4, 5, 1028, 1028)
return (3, 4, 5, 64, 64)

If chunks is an integer, it is applied to all dimensions, to match
the behaviour of zarr-python.
"""

_chunks: Tuple[Any, ...]
if isinstance(chunks, int):
_chunks = (chunks,)
else:
_chunks = chunks

dims_to_add = len(shape) - len(_chunks)
return tuple([chunks] * len(shape))

return (*shape[:dims_to_add], *_chunks)
dims_to_add = len(shape) - len(chunks)
return (*shape[:dims_to_add], *chunks)
14 changes: 14 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ def test_write_image_dask(self, read_from_zarr, compute):
shallow=False,
)

def test_write_image_scalar_chunks(self):
"""
Make sure a scalar chunks value is applied to all dimensions,
matching the behaviour of zarr-python.
"""
shape = (64, 64, 64)
data = np.array(self.create_data(shape))
write_image(
image=data, group=self.group, axes="xyz", storage_options={"chunks": 32}
)
for data in self.group.values():
print(data)
assert data.chunks == (32, 32, 32)

@pytest.mark.parametrize("array_constructor", [np.array, da.from_array])
def test_write_image_compressed(self, array_constructor):
shape = (64, 64, 64)
Expand Down
Loading