scviva.tools.vision.apply_permutation_wpca#

scviva.tools.vision.apply_permutation_wpca(X, max_components=50, p_threshold=0.05, n_repeats=20)[source]#

PCA with permutation significance test for automatic PC selection.

Based on the method of Buja & Eyuboglu (1992). Each gene’s expression is independently permuted across cells n_repeats times; the resulting variance-explained values form a null distribution. A z-score is computed for each real PC against the null, and PCs are retained while their p-value (from the normal survival function) is below p_threshold. A minimum of 5 PCs is always kept.

Parameters:
X ndarray | spmatrix

Log-transformed expression matrix, cells × genes.

max_components int

Maximum number of PCs to evaluate, by default 50.

p_threshold float

P-value cut-off for PC retention, by default 0.05.

n_repeats int

Number of permutation replicates, by default 20.

Return type:

tuple[ndarray, ndarray, ndarray]

Returns:

pca_coordsndarray of shape (n_cells, n_sig)

Cell scores for the retained PCs.

variance_explainedndarray of shape (n_sig,)

Proportion of variance explained by each retained PC.

gene_loadingsndarray of shape (n_genes, n_sig)

Gene loadings for each retained PC.

Notes

The R implementation slices evec[1:k, ] (rows) when it should slice evec[, 1:k] (columns) to subset PCs from the loading matrix. This is a bug in the R source; the Python implementation applies the correct column slice on gene_loadings.