anndata.acc.AdAcc#
- class anndata.acc.AdAcc[R: AdRef](ref_class=<class 'anndata.acc.AdRef'>, layer_cls=<class 'anndata.acc.LayerAcc'>, meta_cls=<class 'anndata.acc.MetaAcc'>, multi_cls=<class 'anndata.acc.MultiAcc'>, graph_cls=<class 'anndata.acc.GraphAcc'>, item_cls=<class 'anndata.acc.ItemAcc'>, *, scope=None)[source]#
Bases:
Accessor to create
AdRefs (A).See examples below and in
anndata.acc.Attributes
- ATTRS: ClassVar = frozenset({'layers', 'obs', 'obsm', 'obsp', 'uns', 'var', 'varm', 'varp'})[source]#
- scope: ScopeAcc | None = None[source]#
Accessor for the element this object is a cast of, e.g.
A.uns['a']forA.uns['a'].AD.Nonefor a top-level accessor likeA.
- X: LayerAcc[R]#
Access data matrix (shortcut for
A.layers[None]).>>> A.X[:, :] >>> A.X["cell-1", :] >>> A.X[:, "gene-5"]
- layers: LayerMapAcc[R]#
Access complete layers or 1D vectors across observations or variables.
>>> A.layers["counts"][:, :] >>> A.layers["counts"]["cell-1", :] >>> A.layers["counts"][:, "gene-5"]
- obsm: MultiMapAcc[R]#
Access 1D vectors along observations.
>>> A.obsm["pca"][:, 0].idx 0
- varm: MultiMapAcc[R]#
Access 1D vectors along variables.
>>> A.varm["loadings"][:, 0].idx 0
- obsp: GraphMapAcc[R]#
Access 1D or 2D vectors along observations.
>>> A.layers["x"][:, :].dims ('obs', 'obs') >>> A.layers["x"]["cell-1", :].dims {'obs'} >>> A.layers["x"][:, "cell-1"].dims {'obs'}
- varp: GraphMapAcc[R]#
Access 1D or 2D vectors along variables.
>>> A.layers["x"][:, :].dims ('var', 'var') >>> A.layers["x"]["gene-1", :].dims {'var'} >>> A.layers["x"][:, "gene-1"].dims {'var'}
- uns: UnsMapAcc[R]#
Access (nested) items in unstructured metadata.
>>> A.uns["neighbors"]["params"] A.uns['neighbors']['params'] >>> A.uns["nested"].AD.obs["x"] A.uns['nested'].AD.obs['x']
Methods
- container(data, /)[source]#
Get the AnnData object this accessor indexes into.
That is
dataitself, or the nested objectscoperefers to indata.>>> from anndata import AnnData >>> adata = AnnData(shape=(3, 2), uns={"n": AnnData(shape=(5, 4))}) >>> A.container(adata) is adata True >>> A.uns["n"].AD.container(adata) is adata.uns["n"] True
- from_json(data, *, vec=None)[source]#
Create an
AdRefor a whole container accessor from a JSON sequence.- Overloads:
vecworks like inresolve(): ifTrue,datamust refer to a vector (AdRef), ifFalse, to a whole container (LayerAcc/MultiAcc/GraphAcc/ItemAcc/DFAcc/AdAcc), and if unset, both are accepted.Schema: acc-schema-v1.json (
#/$defs/refmatches vectors,#/$defs/accwhole containers)Examples
>>> A.from_json(["obsm", "pca", 0]) A.obsm['pca'][:, 0] >>> A.from_json(["obsm", "pca"]) A.obsm['pca']
- Raises:
ValueError – If parsing fails.
- isin(data, /)[source]#
Check if the nested AnnData object this accessor refers to is in
data.Truefor a top-level accessor likeA, otherwise whetherscopeis indataand an AnnData object.- Return type:
- resolve(spec, *, strict=True, vec=None)[source]#
Create
AdReffrom a simplified string.- Overloads:
spec (
str), *, strict (Literal[True]) =True, vec (Literal[True]) →Rspec (
str), *, strict (Literal[False]), vec (Literal[True]) →R|Nonespec (
str), *, strict (Literal[True]) =True, vec (Literal[False]) →LayerAcc[R] |MultiAcc[R] |GraphAcc[R] |ItemAcc[R]spec (
str), *, strict (Literal[False]), vec (Literal[False]) →LayerAcc[R] |MultiAcc[R] |GraphAcc[R] |ItemAcc[R] |Nonespec (
str), *, strict (Literal[True]) =True, vec (None) =None→R|LayerAcc[R] |MultiAcc[R] |GraphAcc[R] |ItemAcc[R]spec (
str), *, strict (Literal[False]), vec (None) =None→R|LayerAcc[R] |MultiAcc[R] |GraphAcc[R] |ItemAcc[R] |None
If
vecisTrue,specmust be an indexed form yielding anAdRef, e.g."X[:,:]","obs.a", or"obsm.c.0"(the current default behavior). IfvecisFalse,specmust refer to a whole container instead ("X","layers.<k>","obsm.<k>","varm.<k>","obsp.<k>","varp.<k>", or"uns.<k1>.….<kn>"), and aLayerAcc/MultiAcc/GraphAcc/ItemAccis returned instead of anAdRef. Ifvecis unset, both forms are accepted.Examples
Indexed, yielding an
AdRef:>>> A.resolve("X[:,:]") A.X[:, :] >>> A.resolve("layers.y[c,:]") A.layers['y']['c', :] >>> A.resolve("layers.y[:,g]") A.layers['y'][:, 'g'] >>> A.resolve("obs.a") A.obs['a'] >>> A.resolve("var.b") A.var['b'] >>> A.resolve("obsm.c.0") A.obsm['c'][:, 0] >>> A.resolve("varm.d.1") A.varm['d'][:, 1] >>> A.resolve("obsp.g[c1,:]") A.obsp['g']['c1', :] >>> A.resolve("obsp.g[:,c2]") A.obsp['g'][:, 'c2']
Whole containers, yielding a
LayerAcc/MultiAcc/GraphAcc:>>> A.resolve("X", vec=False) A.X >>> A.resolve("layers.y", vec=False) A.layers['y'] >>> A.resolve("obsm.c", vec=False) A.obsm['c'] >>> A.resolve("obsp.g", vec=False) A.obsp['g']
Items in
.uns, yielding anItemAcc. Like for"obsm.<k>.<i>", the path is split at periods.>>> A.resolve("uns.neighbors", vec=False) A.uns['neighbors'] >>> A.resolve("uns.a.b.0", vec=False) A.uns['a']['b']['0']
- to_json(ref)[source]#
Serialize an
AdRefor a whole container accessor to a JSON-compatible list.Schema: acc-schema-v1.json (
#/$defs/refmatches vectors,#/$defs/accwhole containers)Examples
>>> A.to_json(A.obsm["pca"][0]) ['obsm', 'pca', 0] >>> A.to_json(A.obsm["pca"]) ['obsm', 'pca'] >>> A.to_json(A.X) ['layers', None]
Items in
.unshave their path in a list, and casts (.AD,.DF) add the cast name and its payload:>>> A.to_json(A.uns["a"]["b"]) ['uns', ['a', 'b']] >>> A.to_json(A.obsm["df"].DF["c"]) ['obsm', 'df', 'DF', 'c'] >>> A.to_json(A.uns["n"].AD.obs["x"]) ['uns', ['n'], 'AD', ['obs', 'x']]