.calculate_cv

proteopy.pp.calculate_cv(adata, group_by=None, layer=None, zero_to_na=True, min_samples=2, force=False, key_added=None, inplace=True)[source]

Compute the coefficient of variation (CV = std / mean) for each variable.

Performed within group_by groups optionally. CV is calculated ignoring NaNs.

Parameters:
  • adata (AnnData) – AnnData object containing data.

  • group_by (str or None, optional) – Column in adata.obs defining groups. If None, computes CV across all samples without grouping.

  • layer (str or None, optional) – Layer to use for data. If None, uses adata.X.

  • zero_to_na (bool, optional) – Treat zeros as missing values (NaN).

  • min_samples (int, optional) – Minimum number of non-NaN samples (obs) required to compute a CV.

  • force (bool, optional) – If True, bypass the log-transform check. Use when you are certain the data is on the appropriate scale for CV calculation.

  • key_added (str or None, optional) – Key under which to store results. When group_by is provided, defaults to 'cv_by_<group_by>_<layer>' and stores in adata.varm. When group_by is None, defaults to 'cv_<layer>' and stores in adata.var.

  • inplace (bool, optional) – If True, modify adata in place. If False, return a copy of adata with the added key.

Returns:

Returns the modified AnnData object if inplace=False, otherwise returns None.

Return type:

AnnData or None

Raises:
  • ValueError – If the data appears to be log-transformed. CVs should be computed on raw (linear-scale) data.

  • KeyError – If group_by is provided but does not exist in adata.obs.