pertpy.tools.DistanceTest#

class DistanceTest(metric, *, n_perms=1000, layer_key=None, obsm_key=None, padj_threshold=0.05, correction='holm-sidak', cell_wise_metric=None, alpha=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 Literal['edistance', 'euclidean', 'root_mean_squared_error', 'mse', 'mean_absolute_error', 'pearson_distance', 'spearman_distance', 'kendalltau_distance', 'cosine_distance', 'r2_distance', 'mean_pairwise', 'mmd', 'wasserstein', 'sym_kldiv', 't_test', 'ks_test', 'nb_ll', 'classifier_proba', 'classifier_cp', 'mean_var_distribution', 'mahalanobis']

Distance metric to use between groups of cells.

n_perms int, default: 1000

Number of permutations to run.

layer_key str | None, default: None

Name of the counts layer containing raw counts to calculate distances for. Mutually exclusive with ‘obsm_key’. If equal to None the parameter is ignored.

obsm_key str | None, default: None

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.

padj_threshold float, default: 0.05

Significance level, applied to the raw p-values and to the p-values after multiple testing correction.

correction str, default: 'holm-sidak'

Multiple testing correction method.

cell_wise_metric default: None

Metric to use between single cells.

alpha float | None, default: None

Deprecated and will be removed in a future release. Use padj_threshold.

Examples

>>> import pertpy as pt
>>> adata = pt.ds.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(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, default: True

Whether to print progress.

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:

DataFrame

Examples

>>> import pertpy as pt
>>> adata = pt.ds.distance_example()
>>> distance_test = pt.tl.DistanceTest("edistance", n_perms=1000)
>>> test_results = distance_test.test_precomputed(adata, groupby="perturbation", contrast="control")
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, default: True

Whether to print progress.

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:

DataFrame

Examples

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