Skip to content

Commit

Permalink
Fix saving of AwkwardArrayView (#1736)
Browse files Browse the repository at this point in the history
* Add testcase

* Make copy of view before saving

* Add release notes

* (chore): use `obsm_types`

---------

Co-authored-by: ilan-gold <[email protected]>
  • Loading branch information
grst and ilan-gold authored Nov 6, 2024
1 parent 57258d0 commit e63cb0a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes/1736.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Ensure that views of AwkwardArrays have their "view" attributes removed on saving an {class}`~anndata.AnnData` object
to disk. {user}`grst`
4 changes: 4 additions & 0 deletions src/anndata/_io/specs/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import warnings
from collections.abc import Mapping
from copy import copy
from functools import partial
from itertools import product
from types import MappingProxyType
Expand Down Expand Up @@ -792,6 +793,9 @@ def write_awkward(
from anndata.compat import awkward as ak

group = f.require_group(k)
if isinstance(v, views.AwkwardArrayView):
# copy to remove the view attributes
v = copy(v)
form, length, container = ak.to_buffers(ak.to_packed(v))
group.attrs["length"] = length
group.attrs["form"] = form.to_json()
Expand Down
17 changes: 17 additions & 0 deletions tests/test_awkward.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ImplicitModificationWarning,
read_h5ad,
)
from anndata.compat import AwkArray
from anndata.compat import awkward as ak
from anndata.tests.helpers import assert_equal, gen_adata, gen_awkward
from anndata.utils import axis_len
Expand Down Expand Up @@ -249,6 +250,22 @@ def test_awkward_io(tmp_path, array):
assert_equal(adata.uns["awk"], adata2.uns["awk"], exact=True)


def test_awkward_io_view(tmp_path):
"""Check that views are converted to actual arrays on save, i.e. the _view_args and __list__ parameters are removed"""
adata = gen_adata((3, 3), varm_types=(), obsm_types=(AwkArray,), layers_types=())

v = adata[1:]
adata_path = tmp_path / "adata.h5ad"
v.write_h5ad(adata_path)

adata2 = read_h5ad(adata_path)
# parameters are not fully removed, but set to None
assert ak.parameters(adata2.obsm["awk_2d_ragged"]) == {
"__list__": None,
"_view_args": None,
}


# @pytest.mark.parametrize("join", ["outer", "inner"])
@pytest.mark.parametrize(
("arrays", "join", "expected"),
Expand Down

0 comments on commit e63cb0a

Please sign in to comment.