tacco.tools.affinity

affinity(adata, sigma, distance_key=None, result_key=None)[source]

Calculates an affinity from a distance matrix.

The affinity is calculated according to

aff(i,j) = exp(-dist(i,j)**2 / (2 * sigma**2))

where sigma gives the half width of the Gaussian weight. This can be used e.g. for spectral clustering.

Parameters:
  • adata – A AnnData with distance under .obsp[distance_key] or a (sparse) distance matrix.

  • sigma – The width parameter of the Gaussian.

  • distance_key – The .obsp key with the distances. Ignored if adata is the distance matrix.

  • result_key – The .obsp key to contain the affinities. If None, a csr_matrix containing the affinities is returned. Ignored if adata is the distance matrix.

Returns:

Depending on result_key returns either the (sparse) affinity matrix or an updated input adata contining the affinity matrix under adata.obsp[result_key].