pertpy.tools.DistanceTest

class pertpy.tools.DistanceTest(metric, n_perms=1000, layer_key=None, obsm_key=None, alpha=0.05, correction='holm-sidak', cell_wise_metric=None)[source]

Run permutation tests using a distance of choice between groups of cells.

Performs Monte-Carlo permutation testing using a distance metric of choice as test statistic. Tests all groups of cells against a specified contrast group (which normally would be your “control” cells).

Parameters:
  • metric (str) – Distance metric to use between groups of cells.

  • n_perms (int) – Number of permutations to run. Defaults to 1000.

  • layer_key (str) – Name of the counts layer containing raw counts to calculate distances for. Mutually exclusive with ‘obsm_key’. Defaults to None and is then not used.

  • obsm_key (str) – Name of embedding in adata.obsm to use. Mutually exclusive with ‘counts_layer_key’. Defaults to None, but is set to “X_pca” if not set explicitly internally.

  • alpha (float) – Significance level. Defaults to 0.05.

  • correction (str) – Multiple testing correction method. Defaults to ‘holm-sidak’.

  • cell_wise_metric – Metric to use between single cells. Defaults to “euclidean”.

Examples

>>> import pertpy as pt
>>> adata = pt.dt.distance_example()
>>> distance_test = pt.tl.DistanceTest("edistance", n_perms=1000)
>>> tab = distance_test(adata, groupby="perturbation", contrast="control")

Methods table

test_precomputed(adata, groupby, contrast[, ...])

Run permutation test for metrics that take precomputed distances.

test_xy(adata, groupby, contrast[, ...])

Run permutation test for metric not supporting precomputed distances.

Methods

test_precomputed

DistanceTest.test_precomputed(adata, groupby, contrast, verbose=True)[source]

Run permutation test for metrics that take precomputed distances.

Parameters:
  • adata (AnnData) – Annotated data matrix.

  • groupby (str) – Key in adata.obs for grouping cells.

  • contrast (str) – Name of the contrast group.

  • cell_wise_metric – Metric to use for pairwise distances.

  • verbose (bool) – Whether to print progress. Defaults to True.

Returns:

Results of the permutation test, with columns:
  • distance: distance between the contrast group and the group

  • pvalue: p-value of the permutation test

  • significant: whether the group is significantly different from the contrast group

  • pvalue_adj: p-value after multiple testing correction

  • significant_adj: whether the group is significantly different from the contrast group after multiple testing correction

Return type:

pandas.DataFrame

Examples

>>> import pertpy as pt
>>> adata = pt.dt.distance_example()
>>> distance_test = pt.tl.DistanceTest("edistance", n_perms=1000)
>>> test_results = distance_test.test_precomputed(adata, groupby="perturbation", contrast="control")

test_xy

DistanceTest.test_xy(adata, groupby, contrast, show_progressbar=True)[source]

Run permutation test for metric not supporting precomputed distances.

Runs a permutation test for a metric that can not be computed using precomputed pairwise distances, but need the actual data points. This is generally slower than test_precomputed.

Parameters:
  • adata (AnnData) – Annotated data matrix.

  • groupby (str) – Key in adata.obs for grouping cells.

  • contrast (str) – Name of the contrast group.

  • show_progressbar (bool) – Whether to print progress. Defaults to True.

Returns:

Results of the permutation test, with columns:
  • distance: distance between the contrast group and the group

  • pvalue: p-value of the permutation test

  • significant: whether the group is significantly different from the contrast group

  • pvalue_adj: p-value after multiple testing correction

  • significant_adj: whether the group is significantly different from the contrast group after multiple testing correction

Return type:

pandas.DataFrame

Examples

>>> import pertpy as pt
>>> adata = pt.dt.distance_example()
>>> distance_test = pt.tl.DistanceTest("edistance", n_perms=1000)
>>> test_results = distance_test.test_xy(adata, groupby="perturbation", contrast="control")