mantispy.metrics.pc_regression

Contents

mantispy.metrics.pc_regression#

mantispy.metrics.pc_regression(adata, key, use_rep='X_pca', n_comps=None)[source]#

Variance-weighted R^2 of the principal components on key.

Each component is regressed on key on its own, a categorical key through a one-way ANOVA R^2 and a numeric key through the squared Pearson correlation, and the per-component R^2 is weighted by that component’s share of the total variance. The value is the share of total variance the covariate explains, so for a batch key lower is better. The per-component loop runs in a numba kernel and imports no scib.

Parameters:
  • adata (AnnData) – Object with the embedding to measure in.

  • key (str) – obs column the components are regressed on.

  • use_rep (str (default: 'X_pca')) – obsm key of the embedding.

  • n_comps (int | None (default: None)) – Use only the leading components, or None for every component the embedding holds.

Return type:

DataFrame

Returns:

A one-row tidy frame holding pc_regression, whose value is NaN when key is constant (a single batch), where the share of variance it explains is undefined.

Raises:
  • KeyError – obsm holds nothing under use_rep.

  • ValueError – key is numeric and has missing values, which cannot be regressed.