From 0036a304821583fec0a3399422eaf58d18ce6367 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Fri, 14 Jul 2023 15:31:28 +0200 Subject: [PATCH] Fix #1056 --- anndata/_io/zarr.py | 5 ++++- anndata/tests/test_io_elementwise.py | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/anndata/_io/zarr.py b/anndata/_io/zarr.py index d65379e63..45f885302 100644 --- a/anndata/_io/zarr.py +++ b/anndata/_io/zarr.py @@ -62,7 +62,10 @@ def read_zarr(store: Union[str, Path, MutableMapping, zarr.Group]) -> AnnData: if isinstance(store, Path): store = str(store) - f = zarr.open(store, mode="r") + if isinstance(store, zarr.Group): + f = store + else: + f = zarr.open(store, mode="r") # Read with handling for backwards compat def callback(func, elem_name: str, elem, iospec): diff --git a/anndata/tests/test_io_elementwise.py b/anndata/tests/test_io_elementwise.py index 7fa80526a..8eaa916c8 100644 --- a/anndata/tests/test_io_elementwise.py +++ b/anndata/tests/test_io_elementwise.py @@ -180,3 +180,24 @@ def test_override_specification(): ) def _(store, key, adata): pass + + +@pytest.mark.parametrize("consolidated", [True, False]) +def test_read_zarr_from_group(tmp_path, consolidated): + # https://github.com/scverse/anndata/issues/1056 + pth = tmp_path / "test.zarr" + adata = gen_adata((3, 2)) + + with zarr.open(pth, mode="w") as z: + write_elem(z, "table/table", adata) + + if consolidated: + zarr.convenience.consolidate_metadata(z.store) + + if consolidated: + read_func = zarr.open_consolidated + else: + read_func = zarr.open + + with read_func(pth) as z: + assert_equal(read_elem(z["table/table"]), adata)