rapids_singlecell.get.aggregate#

rapids_singlecell.get.aggregate(adata, by, func, *, use=None, axis=None, mask=None, dof=1, return_sparse=False, acc=None, layer=None, obsm=None, varm=None, **kwargs)[source]#

Aggregate data matrix based on some categorical grouping.

This function is useful for pseudobulking as well as plotting.

Aggregation to perform is specified by func, which can be a single metric or a list of metrics. Each metric is computed over the group and results in a new layer in the output AnnData object.

If use is not passed, X will be used for aggregation data. If func only has length 1 or is just an AggType, then aggregation data is written to X. Otherwise, it is written to layers or xxxm as appropriate for the dimensions of the aggregation data.

Parameters:
adata AnnData

AnnData to be aggregated.

by str | Collection[str | AdRef[Idx2D | int, AnnData]] | AdRef[Idx2D | int, AnnData]

References to the vectors to be grouped by. Bare strings retain the legacy column-name behavior; accessor strings such as "obs.group" and anndata.acc references are also supported.

func Literal['count_nonzero', 'mean', 'sum', 'var', 'sq_sum'] | Iterable[Literal['count_nonzero', 'mean', 'sum', 'var', 'sq_sum']]

How to aggregate.

use LayerAcc | MultiAcc | GraphAcc | str | None (default: None)

Accessor for the matrix to aggregate, such as A.X, A.layers["counts"], A.obsm["pca"], or an observation/variable graph. Strings are resolved by anndata.acc.AdAcc.resolve(), e.g. 'layers.counts'.

axis Literal['obs', 0, 'var', 1] | None (default: None)

Axis on which to find the grouping column. Inferred from by when accessor references are used.

mask NDArray[bool] | AdRef[Idx2D | int, AnnData] | str | None (default: None)

Boolean mask (or key to column containing mask) to apply along the axis.

dof int (default: 1)

Degrees of freedom for variance. Defaults to 1.

acc LayerAcc | MultiAcc | GraphAcc | str | None (default: None)

Deprecated since version 0.19.0: Use use instead.

Former name of use.

layer str | None (default: None)

Deprecated since version 0.19.0: Use use instead.

obsm str | None (default: None)

Deprecated since version 0.19.0: Use use instead.

varm str | None (default: None)

Deprecated since version 0.19.0: Use use instead.

If not None, key for aggregation data.

return_sparse bool (default: False)

Whether to return a sparse matrix. Only works for device-resident sparse input. In-memory host input is not streamed for sparse output; it is copied to the GPU first, so it must fit in GPU memory.

Return type:

AnnData

Returns:

Aggregated AnnData.

Examples

Calculating mean expression and number of nonzero entries per cluster:

>>> import scanpy as sc, pandas as pd
>>> import rapids_singlecell as rsc
>>> pbmc = sc.datasets.pbmc3k_processed().raw.to_adata()
>>> rsc.get.anndata_to_GPU(pbmc)
>>> pbmc.shape
(2638, 13714)
>>> aggregated = rsc.get.aggregate(pbmc, by="louvain", func=["mean", "count_nonzero"])
>>> aggregated
AnnData object with n_obs × n_vars = 8 × 13714
    obs: 'louvain'
    var: 'n_cells'
    layers: 'mean', 'count_nonzero'

We can group over multiple columns:

>>> pbmc.obs["percent_mito_binned"] = pd.cut(pbmc.obs["percent_mito"], bins=5)
>>> rsc.get.aggregate(pbmc, by=["louvain", "percent_mito_binned"], func=["mean", "count_nonzero"])
AnnData object with n_obs × n_vars = 40 × 13714
    obs: 'louvain', 'percent_mito_binned'
    var: 'n_cells'
    layers: 'mean', 'count_nonzero'

Note that this filters out any combination of groups that wasn’t present in the original data.