diff --git a/CHANGELOG.md b/CHANGELOG.md index 116a9e18..893a163c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.5.3] + +### Fix +- Fix bug in AnnData backend where "raw" entry with encoding-type "null" is written by default in newer anndata versions, which causes compatibility issues with older anndata versions. Now the "raw" entry is removed after writing if it has encoding-type "null". + ## [0.5.2] ### Fix diff --git a/src/ngio/tables/backends/_anndata.py b/src/ngio/tables/backends/_anndata.py index 40a092f1..4d1d4a64 100644 --- a/src/ngio/tables/backends/_anndata.py +++ b/src/ngio/tables/backends/_anndata.py @@ -81,6 +81,17 @@ def _write_to_memory_store( suppress_warnings=True, ) + def _cleanup_after_write(self) -> None: + """Clean up any temporary data after writing.""" + group = self._group_handler._group + try: + raw_group = group["raw"] + except KeyError: + return + # Remove "raw" entry (encoding-type "null") that anndata <0.11 can't read + if dict(raw_group.attrs).get("encoding-type") == "null": + del group["raw"] + def write_from_anndata(self, table: AnnData) -> None: """Serialize the table from an AnnData object.""" # Make sure to use the correct zarr format @@ -112,6 +123,7 @@ def write_from_anndata(self, table: AnnData) -> None: "Please make sure to use a compatible " "store like a LocalStore, or FsspecStore." ) + self._cleanup_after_write() def write_from_pandas(self, table: DataFrame) -> None: """Serialize the table from a pandas DataFrame.""" diff --git a/tests/unit/tables/test_backends.py b/tests/unit/tables/test_backends.py index ff54c8f7..9e7e29a2 100644 --- a/tests/unit/tables/test_backends.py +++ b/tests/unit/tables/test_backends.py @@ -185,6 +185,11 @@ def test_anndata_backend(tmp_path: Path): lf_data = backend.load_as_polars_lf() backend.write(lf_data, metadata={"test": "test"}) + # Test that the "raw" entry with encoding-type "null" is + # removed after writing for compatibility with older anndata versions + with pytest.raises(KeyError): + backend._group_handler._group["raw"] + @pytest.mark.parametrize( "index_label, index_type",