Skip to content

Commit

Permalink
Use __record__ instead of __list__ to store behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
grst committed Jul 24, 2023
1 parent 6c09341 commit b23eda3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions anndata/_core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from copy import deepcopy
from collections.abc import Sequence, KeysView, Callable, Iterable, Mapping
from functools import reduce, singledispatch, wraps
from typing import Any, Literal
from typing import Any, Literal, Dict, cast
import warnings

import numpy as np
Expand Down Expand Up @@ -295,8 +295,8 @@ def __copy__(self) -> AwkArray:
"""
array = self
# makes a shallow copy and removes the reference to the original AnnData object
array = ak.with_parameter(self, _PARAM_NAME, None)
array = ak.with_parameter(array, "__list__", None)
array = ak.with_parameter(array, _PARAM_NAME, None)
array = ak.with_parameter(array, "__record__", None)
return array

@as_view.register(AwkArray)
Expand All @@ -308,15 +308,16 @@ def as_view_awkarray(array, view_args):
# possible strategies to stack behaviors.
# A better solution might be based on xarray-style "attrs", once this is implemented
# https://github.com/scikit-hep/awkward/issues/1391#issuecomment-1412297114
if type(array).__name__ != "Array":
if "__record__" in cast(Dict, ak.parameters(array)):
raise NotImplementedError(
"Cannot create a view of an awkward array with __array__ parameter. "
"Cannot create a view of an awkward array with __record__ parameter. "
"Please open an issue in the AnnData repo and describe your use-case."
)
array = ak.with_parameter(array, _PARAM_NAME, (parent_key, attrname, keys))
array = ak.with_parameter(array, "__list__", "AwkwardArrayView")
array = ak.with_parameter(array, "__record__", "AwkwardArrayView")
return array

ak.behavior["*", "AwkwardArrayView"] = AwkwardArrayView
ak.behavior["AwkwardArrayView"] = AwkwardArrayView

except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion anndata/tests/test_awkward.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def reversed(self):
ak.behavior[BEHAVIOUR_ID] = ReversibleArray
adata = gen_adata((3, 3), varm_types=(), obsm_types=(), layers_types=())
adata.obsm["awk_string"] = ak.with_parameter(
ak.Array(["AAA", "BBB", "CCC"]), "__list__", BEHAVIOUR_ID
ak.Array(["AAA", "BBB", "CCC"]), "__record__", BEHAVIOUR_ID
)
adata_view = adata[:2]

Expand Down

0 comments on commit b23eda3

Please sign in to comment.