scanpy.pp.downsample_counts#

scanpy.pp.downsample_counts(adata, counts_per_cell=None, total_counts=None, *, rng=None, replace=False, use=None, out=False, copy=False)[source]#

Downsample counts from count matrix.

If counts_per_cell is specified, each cell will downsampled. If total_counts is specified, expression matrix will be downsampled to contain at most total_counts.

Array type support#

Array type

supported

… experimentally in dask Array

numpy.ndarray

✅

❌

scipy.sparse.csr_array / csr_matrix

✅

❌

scipy.sparse.csc_array / csc_matrix

❌

❌

Parameters:
adata AnnData

Annotated data matrix.

counts_per_cell int | Collection[int] | None (default: None)

Target total counts per cell. If a cell has more than ‘counts_per_cell’, it will be downsampled to this number. Resulting counts can be specified on a per cell basis by passing an array.Should be an integer or integer ndarray with same length as number of obs.

total_counts int | None (default: None)

Target total counts. If the count matrix has more than total_counts it will be downsampled to have this number.

rng int | integer | Sequence[int] | SeedSequence | Generator | BitGenerator | None (default: None)

Random number generation to control stochasticity.

If a type:SeedLike value, it’s used to seed a new random number generator; If a numpy.random.Generator, rng’s state will be directly advanced; If None, a non-reproducible random number generator is used. See numpy.random.default_rng() for more details.

The default value matches legacy scanpy behavior and will change to None in scanpy 2.0.

replace bool (default: False)

Whether to sample the counts with replacement.

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

Which count matrix to downsample. Given by an accessor, e.g. A.X, A.layers['counts'] or A.obsm['pca']. strs are anndata.acc.AdAcc.resolve()d, e.g. 'layers.counts'.

out LayerAcc | MultiAcc | str | bool (default: False)

Where to write the result, e.g. A.layers['scaled']. strs are anndata.acc.AdAcc.resolve()d, e.g. 'layers.scaled'. If True, the result is returned instead of written. If False, it is written to the place use reads from.

copy bool (default: False)

Deprecated since version 1.13.0: Copy adata before calling this function instead.

Whether to return a modified copy instead of modifying adata in place.

Return type:

AnnData | ndarray | csr_array | csc_array | csr_matrix | csc_matrix | None

Returns:

Returns None if the result was written, else the downsampled count matrix.