scviva.tools.vision.apply_pca#

scviva.tools.vision.apply_pca(X, max_components=200)[source]#

PCA via randomized truncated SVD with gene-mean centering.

Mirrors R VISION’s applyPCA which runs IRLBA on t(exprData) with center = rowMeans(exprData). The Python equivalent works on the (cells × genes) layout and centers by subtracting per-gene means before the SVD.

Parameters:
X ndarray | spmatrix

Expression matrix, cells × genes. Sparse matrices are densified internally because centering destroys sparsity.

max_components int

Maximum number of PCs to compute, by default 200.

Return type:

tuple[ndarray, ndarray, ndarray]

Returns:

pca_coordsndarray of shape (n_cells, n_components)

Cell coordinates in PC space (scores).

variance_explainedndarray of shape (n_components,)

Proportion of total variance captured by each PC.

gene_loadingsndarray of shape (n_genes, n_components)

Right singular vectors (variable loadings) for each PC.

Notes

total_var is computed as sum((X - mu)²) which equals R’s sum(exprData²) - ncol(exprData) * sum(rM²) — algebraically identical expressions for the variance of the centered matrix.