Using a custom metric for scanpy.pp.neighbors?

For scanpy.pp.neighbors, I would like to use Spearman’s correlation for the metric. Since it’s not one of the listed options, I assume that I would have to create a callable per the API docs. Also since Spearman’s is the Pearson of the rankings, the function call should be compact.

However, I’m not clear from the docs how to do so. Does anyone have a working example/code?

1 Like

This is how it’s done in pynndescent

@numba.njit(fastmath=True)
def spearmanr(x, y):
    a = np.column_stack((x, y))

    n_vars = a.shape[1]

    for i in range(n_vars):
        a[:, i] = rankdata(a[:, i])
    rs = np.corrcoef(a, rowvar=0)

    return rs[1, 0]

For some reason this metric isn’t available through umap though.

1 Like

Oh, looking a bit more carefully, I think you should just be able to call:

sc.pp.neighbors(adata, metric="spearmanr")

as long as you have over 4096 observations. This limit may be solved by this PR.

@braymp You are the genius bro. I was solving this problem from last week but i failed in this. You told me the mistake. Thank man for this solution. :slightly_smiling_face: