From f3f295ca8f1c210510aefcadf41308d9c44b4226 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Mon, 25 Mar 2024 20:45:22 +0000 Subject: [PATCH 1/4] Fix scalar chunking --- ome_zarr/writer.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ome_zarr/writer.py b/ome_zarr/writer.py index 69efb4d0..34964579 100644 --- a/ome_zarr/writer.py +++ b/ome_zarr/writer.py @@ -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) From b46828fd46ab4635d7261bf44b89cb07784fa236 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 26 Mar 2024 09:50:55 +0000 Subject: [PATCH 2/4] Add test for scalar chunks --- tests/test_writer.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_writer.py b/tests/test_writer.py index 6927ba2e..9abb182c 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -216,6 +216,24 @@ 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) From 407024fd985c413efad60abddc05aeb9d5173de7 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 19 May 2024 18:21:44 +0100 Subject: [PATCH 3/4] Add a changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71806120..61d97373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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": }`` 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) From 192744419c7b817753de177f29d1048a5b97073d Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 19 May 2024 18:23:04 +0100 Subject: [PATCH 4/4] pre-commit fixes --- tests/test_writer.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/test_writer.py b/tests/test_writer.py index 9abb182c..3923faa2 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -224,16 +224,12 @@ def test_write_image_scalar_chunks(self): shape = (64, 64, 64) data = np.array(self.create_data(shape)) write_image( - image=data, - group=self.group, - axes="xyz", - storage_options={"chunks": 32} + 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)