Skip to content

Commit 7cd6d69

Browse files
committed
fix: allow creating objects with array-api
1 parent 21d5882 commit 7cd6d69

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/anndata/_core/index.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pandas as pd
1111
from scipy.sparse import issparse
1212

13-
from ..compat import AwkArray, CSArray, CSMatrix, DaskArray, XDataArray
13+
from ..compat import AwkArray, CSArray, CSMatrix, DaskArray, XDataArray, has_xp
1414
from .xarray import Dataset2D
1515

1616
if TYPE_CHECKING:
@@ -115,6 +115,9 @@ def name_idx(i):
115115
if isinstance(indexer.data, DaskArray):
116116
return indexer.data.compute()
117117
return indexer.data
118+
elif has_xp(indexer):
119+
msg = "Need to implement array api-based indexing"
120+
raise NotImplementedError(msg)
118121
msg = f"Unknown indexer {indexer!r} of type {type(indexer)}"
119122
raise IndexError(msg)
120123

src/anndata/_core/storage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from anndata.compat import CSArray, CSMatrix
1111

1212
from .._warnings import ImplicitModificationWarning
13-
from ..compat import XDataset
13+
from ..compat import XDataset, has_xp
1414
from ..utils import (
1515
ensure_df_homogeneous,
1616
join_english,
@@ -67,6 +67,8 @@ def coerce_array(
6767
return np.array(value)
6868
except (ValueError, TypeError) as _e:
6969
e = _e
70+
if has_xp(value):
71+
return value
7072
# if value isn’t the right type or convertible, raise an error
7173
msg = f"{name} needs to be of one of {join_english(map(str, array_data_structure_types))}, not {type(value)}."
7274
if e is not None:

src/anndata/compat/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import numpy as np
1313
import pandas as pd
1414
import scipy
15+
from array_api_compat import get_namespace as array_api_get_namespace
1516
from packaging.version import Version
1617
from zarr import Array as ZarrArray # noqa: F401
1718
from zarr import Group as ZarrGroup
@@ -415,3 +416,11 @@ def _map_cat_to_str(cat: pd.Categorical) -> pd.Categorical:
415416
return cat.map(str, na_action="ignore")
416417
else:
417418
return cat.map(str)
419+
420+
421+
def has_xp(mod):
422+
try:
423+
array_api_get_namespace(mod)
424+
return True
425+
except TypeError:
426+
return False

0 commit comments

Comments
 (0)