anndata.acc.RefAcc#

class anndata.acc.RefAcc[R: AdRef, I: Hashable, D: AnnDataLikeMetadataOnly, E = object, V = object](*, ref_class, scope=None)[source]#

Bases: ABC

Abstract base class for reference accessors.

See reference accessors for all existing subclasses.

Attributes

scope: ScopeAcc | None = None[source]#

Accessor for the element whose .AD cast this accessor is in, e.g. A.uns['a'] for A.uns['a'].AD.obsm['x'].DF.

If None, this accessor refers into the object passed to get()/set()/isin() itself.

ref_class: type[R]#

Methods

__getitem__(idx, /)[source]#

Get a reference to an array.

Parameters:
idx Any

Index into the referenced element. Subclasses define which indices are valid.

Return type:

R

abstractmethod __repr__()[source]#

Get a display representation of the accessor.

This is not a simplified representation supported by AdAcc.resolve(), but instead the code used to create the accessor, e.g. A.layers['counts'].

Return type:

str

container(data, /)[source]#

Get the AnnData object this accessor indexes into.

That is data itself, or the element scope refers to in data. Call this in get(), set(), and isin() so your subclass works when nested.

Examples

>>> from anndata import AnnData
>>> from anndata.acc import A
>>> adata = AnnData(shape=(3, 2), uns={"n": AnnData(shape=(5, 4))})
>>> A.obs.container(adata) is adata
True
>>> A.uns["n"].AD.obs.container(adata) is adata.uns["n"]
True
Raises:

TypeError – If the element scope refers to isn’t an AnnData object.

Return type:

AnnDataLikeMetadataOnly

abstractmethod dims(idx, /)[source]#

Get along which dimensions the referenced array is.

Return type:

Collection[Literal['obs', 'var']]

abstractmethod get(data, idx=NO_IDX, /)[source]#

Get the indexed array or element from the AnnData object at idx.

Overloads:
  • data (D), / → E

  • data (D), idx (I), / → V

When idx is omitted (i.e., idx is NO_IDX), return the full array one level up instead. This has the same semantics as the AdRef path but one level up: adata[A.obs] returns the full DataFrame, adata[A.obsm["pca"]] the full numpy.ndarray, and adata[A.layers] the full Mapping. The first two both have defined shape-like properties (or awkward.Array), but layers or similar do not.

abstractmethod idx_repr(idx, /)[source]#

Get a string representation of the index.

Return type:

str

abstractmethod isin(data, idx=None, /)[source]#

Check if the referenced array is in the AnnData object.

Returns False if scope (or frame_acc) isn’t in data or doesn’t have the type this accessor casts it to (.AD/.DF).

The base implementation only checks scope. Subclasses should return False if super().isin(data, idx) does, so container() won’t fail afterwards.

Return type:

bool

process_idx(idx, /)[source]#
Return type:

I

set(data, idx, value, /)[source]#

Set the indexed array or element in the AnnData object at idx.

When idx is NO_IDX, replace the full element one level up (see get()), otherwise assign into the existing element in-place.

Return type:

None