rapids_singlecell.pp.filter_cells#

rapids_singlecell.pp.filter_cells(data, *, min_counts=None, min_genes=None, max_counts=None, max_genes=None, out=False, copy=False, verbose=True, inplace=True)[source]#

Filter cell outliers based on counts and numbers of genes expressed.

For instance, only keep cells with at least min_counts counts or min_genes genes expressed. This is to filter measurement outliers, i.e. “unreliable” observations.

Only provide one of the optional parameters min_counts, min_genes, max_counts, max_genes per call.

Parameters:
data AnnData | ndarray | csc_matrix | csr_matrix | Array

The (annotated) data matrix of shape n_obs × n_vars. Rows correspond to cells and columns to genes.

min_counts int | None (default: None)

Minimum number of counts required for a cell to pass filtering.

min_genes int | None (default: None)

Minimum number of genes expressed required for a cell to pass filtering.

max_counts int | None (default: None)

Maximum number of counts required for a cell to pass filtering.

max_genes int | None (default: None)

Maximum number of genes expressed required for a cell to pass filtering.

out AdRef | str | bool (default: False)

Where to write the counts per cell, e.g. A.obs['n_genes']. If True, return the mask and counts instead of subsetting data and writing them. If False, they are written to .obs['n_counts'] or .obs['n_genes'], depending on what was thresholded.

copy bool (default: False)

Whether to subset and return a copy of data instead of modifying it.

verbose bool (default: True)

Print number of discarded cells

Return type:

AnnData | tuple[ndarray, ndarray] | None

Returns:

With out=True or array input, returns the following arrays, else subsets and annotates the data matrix:

cells_subset

Boolean index mask that does filtering. True means that the cell is kept. False means the cell is removed.

number_per_cell

Depending on what was thresholded (counts or genes), the array stores n_counts or n_cells per gene.

Examples

>>> import scanpy as sc
>>> adata = sc.datasets.krumsiek11()
UserWarning: Observation names are not unique. To make them unique, call `.obs_names_make_unique`.
    utils.warn_names_duplicates("obs")
>>> adata.obs_names_make_unique()
>>> adata.n_obs
640
>>> adata.var_names.tolist()
['Gata2', 'Gata1', 'Fog1', 'EKLF', 'Fli1', 'SCL',
 'Cebpa', 'Pu.1', 'cJun', 'EgrNab', 'Gfi1']
>>> # add some true zeros
>>> adata.X[adata.X < 0.3] = 0
>>> # simply compute the number of genes per cell
>>> sc.pp.filter_cells(adata, min_genes=0)
>>> adata.n_obs
640
>>> int(adata.obs["n_genes"].min())
1
>>> # filter manually
>>> adata_copy = adata[adata.obs["n_genes"] >= 3]
>>> adata_copy.n_obs
554
>>> int(adata_copy.obs["n_genes"].min())
3
>>> # actually do some filtering
>>> sc.pp.filter_cells(adata, min_genes=3)
>>> adata.n_obs
554
>>> int(adata.obs["n_genes"].min())
3