How should I calculate the knn entropy using the leiden clusters?

Hello:

How should I calculate the knn entropy using the leiden clusters?
https://arxiv.org/pdf/1506.06501v1

Thanks a lot

you will need to measure how much the points in each leiden cluster differ from the surrounding points in the KNN graph.

given you are working with adata and scvi latent space:
knn graph can be computed with: sc.pp.neighbors(adata, use_rep=SCVI_LATENT_KEY)
leiden clusters can be made with: sc.tl.leiden(adata, key_added="leiden_scVI")

then use this information to perform:

  1. For each data point in the dataset, find its K-nearest neighbors from the KNN graph.
  2. For each cluster, calculate how many of its K-nearest neighbors belong to the same cluster and how many belong to other clusters.
  3. Calculate Entropy per cluster by −∑p_i*log(p_i), where p_i is the probability of a point being in a specific cluster (based on its KNN neighbours).