diff --git a/CHANGELOG.md b/CHANGELOG.md index a0a7602f..19dc83aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixes - `fractal_fsspec_store` accepts OME-Zarr 0.5 (zarr v3) stores. Its reachability probe only fetched the zarr v2 keys `.zgroup` and `.zarray`, so any v3 store, which has a `zarr.json` instead, raised `NgioValueError("Store ... can not be read")` even though the data was reachable. The probe now tries `zarr.json` first; the missing/invalid `fractal_token` errors apply to v3 stores too ([#289](https://github.com/BioVisionCenter/ngio/issues/289)). +- The zarr-python fallback of `copy_group` (used when source or destination is not a local/fsspec store, e.g. deriving into a memory store) no longer goes through pydantic-zarr. pydantic-zarr 0.11.0 deprecated `ArraySpec.from_zarr`/`to_zarr` for removal in 0.12, so every such copy emitted a `DeprecationWarning` and would have broken on 0.12. Each array is now created with `zarr.from_array`; the copied metadata is unchanged. The weekly pip CI was red on both jobs ([#287](https://github.com/BioVisionCenter/ngio/issues/287), [#288](https://github.com/BioVisionCenter/ngio/issues/288)). ## [v1.1.4] diff --git a/src/ngio/utils/_zarr_utils.py b/src/ngio/utils/_zarr_utils.py index bd679711..a73285f1 100644 --- a/src/ngio/utils/_zarr_utils.py +++ b/src/ngio/utils/_zarr_utils.py @@ -13,8 +13,6 @@ import fsspec import zarr from filelock import BaseFileLock, FileLock -from pydantic_zarr.v2 import ArraySpec as AnyArraySpecV2 -from pydantic_zarr.v3 import ArraySpec as AnyArraySpecV3 from zarr.abc.store import Store from zarr.errors import ContainsGroupError from zarr.storage import FsspecStore, LocalStore, MemoryStore, ZipStore @@ -709,13 +707,15 @@ def _zarr_python_copy(src_group: zarr.Group, dest_group: zarr.Group): dest_group.attrs.put(src_group.attrs.asdict()) # Copy arrays for name, array in src_group.arrays(): - if array.metadata.zarr_format == 2: - spec = AnyArraySpecV2.from_zarr(array) - else: - spec = AnyArraySpecV3.from_zarr(array) - dst = spec.to_zarr( - store=dest_group.store, - path=f"{dest_group.path}/{name}", + # Metadata only. `from_array` keeps the chunk layout and codecs of + # `array` but not its fill value or attributes. + dst = zarr.from_array( + dest_group.store, + data=array, + write_data=False, + name=f"{dest_group.path}/{name}", + fill_value=array.fill_value, + attributes=array.attrs.asdict(), overwrite=True, ) if array.ndim > 0: diff --git a/tests/unit/utils/test_zarr_utils.py b/tests/unit/utils/test_zarr_utils.py index e847d08c..d528b3d0 100644 --- a/tests/unit/utils/test_zarr_utils.py +++ b/tests/unit/utils/test_zarr_utils.py @@ -20,6 +20,7 @@ NgioValueError, ZarrGroupHandler, _retry, + copy_group, open_group_wrapper, ) @@ -535,6 +536,47 @@ def test_copy_group_sharded_data_survives_a_small_block_budget(tmp_path: Path): np.testing.assert_array_equal(copied[...], data) +@pytest.mark.parametrize("zarr_format", [2, 3]) +def test_copy_group_fallback_keeps_array_metadata(tmp_path: Path, zarr_format): + """The zarr-python copy fallback reproduces each array's metadata and data.""" + src_group = zarr.create_group( + store=tmp_path / "src.zarr", zarr_format=zarr_format, overwrite=True + ) + plain = src_group.create_array( + "plain", + shape=(8, 8), + chunks=(4, 4), + dtype="uint16", + fill_value=7, + attributes={"a": 1}, + chunk_key_encoding={"name": "v2", "separator": "."}, + ) + plain[:4] = 3 + src_group.create_array("uncompressed", shape=(4,), dtype="f4", compressors=None) + src_group.create_array("scalar", shape=(), dtype="int64") + strings = src_group.create_array("strings", shape=(3,), dtype=str) + strings[:] = np.array(["a", "bb", "ccc"]) + if zarr_format == 3: + src_group.create_array( + "sharded", + shape=(16, 16), + chunks=(4, 4), + shards=(8, 8), + dtype="uint8", + dimension_names=("y", "x"), + ) + + # A MemoryStore destination forces the `_zarr_python_copy` fallback. + dest_group = zarr.group(store={}, zarr_format=zarr_format, overwrite=True) + copy_group(src_group, dest_group, suppress_warnings=True) + + for name, src_array in src_group.arrays(): + copied = dest_group[name] + assert isinstance(copied, zarr.Array) + assert copied.metadata.to_dict() == src_array.metadata.to_dict(), name + np.testing.assert_array_equal(copied[...], src_array[...]) + + def test_writes_are_visible_through_a_stale_consolidated_metadata(tmp_path: Path): """A store carrying `.zmetadata` must not shadow ngio's own writes.