rapids_singlecell.pp.pca#

rapids_singlecell.pp.pca(data, n_comps=None, *, use=None, zero_center=True, svd_solver=None, rng=None, mask=adata.var.get('highly_variable'), dtype='float32', chunked=False, chunk_size=None, return_info=False, out=False, key_added=None, layer=None, copy=False, mask_var=None, **kwargs)[source]#

Principal component analysis using GPU acceleration [HMT09, TQOA24].

Uses the following implementations based on data type (defaults for svd_solver in parentheses):

Dense

Sparse

Dask

zero_center=True

cuML PCA ('full')

Custom ('lanczos' if n_vars > 8k, else 'covariance_eigh')

Custom ('covariance_eigh')

zero_center=False

cuML TruncatedSVD ('full')

Custom ('lanczos' if n_vars > 8k, else 'covariance_eigh')

Custom ('covariance_eigh')

chunked=True

cuML IncrementalPCA

cuML IncrementalPCA

Not supported

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. If a matrix is passed instead of an AnnData object, the PCA representation is returned.

n_comps int | None (default: None)

Number of principal components to compute. Defaults to 50, or 1 - minimum dimension size of selected representation.

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

Which matrix to run PCA on, e.g. A.X, A.layers['counts'] or A.obsm['x']. Strings are resolved by anndata.acc.AdAcc.resolve(), e.g. 'layers.counts'.

zero_center bool (default: True)

If True, compute standard PCA from covariance matrix. If False, omit zero-centering variables (truncated SVD).

svd_solver str | None (default: None)

SVD solver to use. See table above for which implementation is used based on data type, as well as the default solver when svd_solver=None.

None

Choose automatically based on data type (see table above).

'covariance_eigh'

Eigendecomposition of the covariance matrix. Fast for sparse matrices with fewer than ~8,000 features. Works with Dask arrays.

'lanczos'

Lanczos bidiagonalization with implicit restarts. Memory efficient for large sparse matrices (>8,000 features). Best singular value accuracy. Does not support Dask arrays.

'randomized'

Randomized SVD (Halko et al. 2009) with CholeskyQR2 orthogonalization (Tomás et al. 2024). Faster than Lanczos but approximate. Does not support Dask arrays.

'full'

cuML: Full eigendecomposition of covariance matrix. For dense arrays only.

'jacobi'

cuML: Jacobi iterative solver. Faster but less accurate. For dense arrays only.

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

Random seed or Generator for initialization. The superseded random_state argument is still accepted.

mask NDArray[bool] | AdRef | str | Default | None (default: adata.var.get('highly_variable'))

To run only on a certain set of genes, given by a boolean array or a reference like A.var['selected']. Strings are resolved by anndata.acc.AdAcc.resolve(), e.g. 'var.selected'. By default, uses .var['highly_variable'] if available, else everything.

dtype str (default: 'float32')

Numpy data type string to which to convert the result.

chunked bool (default: False)

If True, perform an incremental PCA on segments of chunk_size. The incremental PCA automatically zero centers and ignores settings of rng and svd_solver. If False, perform a full PCA.

chunk_size int (default: None)

Number of observations to include in each chunk. Required if chunked=True was passed.

return_info bool (default: False)

Only relevant when passing a matrix instead of an AnnData: see “Returns”.

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

Where to write the outputs. A single accessor writes embedding to it and names the others after its key k, e.g. out=A.obsm['k'] writes .varm['k'] and .uns['k']. A mapping (PcaOut) writes only the outputs it names, e.g. {'embedding': A.obsm['pca']}; it has to include embedding, after which the .uns entry is named. If True, they are returned as a PcaResult (without the .uns entry). If False, they are written to .obsm['X_pca'] and .varm['PCs'] ('pca' under the v2 preset). If use points to .obsm, the loadings are stored in the .uns entry instead.

**kwargs

Additional arguments for specific SVD solvers. For svd_solver='randomized':

  • n_oversamples: Extra random vectors for better approximation. Higher values improve accuracy. Default is 10.

  • n_iter: Number of power iterations. Higher values improve accuracy for matrices with slowly decaying singular values. Default is 2.

Return type:

None | AnnData | PcaResult | ndarray | csc_matrix | csr_matrix | Array

Returns:

If a matrix is passed and return_info=False, the PCA representation is returned. If a matrix is passed and return_info=True, a tuple of (X_pca, components, variance_ratio, variance) is returned.

If an AnnData object is passed, returns a PcaResult if out=True, else None. By default sets (see out for others):

.obsm['X_pca']

PCA representation of data.

.varm['PCs']

The principal components containing the loadings.

.uns['pca']['variance_ratio']

Ratio of explained variance.

.uns['pca']['variance']

Explained variance, equivalent to the eigenvalues of the covariance matrix.