biotapy.tl.beta

Contents

biotapy.tl.beta#

biotapy.tl.beta(adata, *, metric='braycurtis', inplace=False)#

Distances between every pair of samples.

Parameters:
  • adata (AnnData) – Samples x features.

  • metric (Literal['braycurtis', 'jaccard'] (default: 'braycurtis')) – "braycurtis", or "jaccard" on presence/absence.

  • inplace (bool (default: False)) – Write the matrix to obsp[metric] and return None.

Return type:

DataFrame | None

Returns:

pandas.DataFrame or None Symmetric samples x samples distances with a zero diagonal, indexed by obs_names. Two all-zero samples are NaN apart under Bray-Curtis and 0 apart under Jaccard (scikit-bio’s convention; vegan’s binary Jaccard gives NaN).

Raises:

ValueError – metric is not "braycurtis" or "jaccard".

Notes

R equivalent: phyloseq::distance Guide: Diversity

"jaccard" matches phyloseq::distance(physeq, "jaccard", binary = TRUE): without binary = TRUE, vegan computes a quantitative Jaccard instead. scikit-bio needs dense input, so X is densified once (8 bytes x samples x features) and the result takes 8 bytes x samples x samples. scikit-bio’s working copies add to that: measured with tracemalloc on scikit-bio 0.7.4, peak memory is the dense copy plus 1.5 results (its condensed and square matrices), or two results while the matrix becomes a DataFrame, whichever is larger, and "jaccard" adds a 1-byte presence/absence copy of X.

References

Bray JR, Curtis JT (1957) An ordination of the upland forest communities of southern Wisconsin. Ecological Monographs 27:325-349.

Jaccard P (1912) The distribution of the flora in the alpine zone. New Phytologist 11:37-50.

Examples

>>> import biotapy as bt
>>> round(float(bt.tl.beta(bt.datasets.toy()).loc["s1", "s4"]), 3)
0.708