Teichlab / bbknn

Batch balanced KNN

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

save_knn

1edv opened this issue · comments

commented

Hi @ktpolanski , I find your save_knn function extremely useful (in fact, it was the primary reason I was using bbknn in the first place) ! I am currently reverting to the 1.3.0 version of bbknn so I can use save_knn , but it would be nice to have the save_knn option in future versions of bbknn, if possible.

Huh, interesting - I literally just put that there for some visualisation in an early demonstration notebook. I kicked it out because it can be reverse engineered quite easily from one of the graphs, and I presumed nobody was using it until you just proved me wrong. Still, as mentioned, it can be reverse engineered quite easily from the distances graph. Not sure if you use the scanpy or PCA matrix version, so here you go:

distances = adata.uns['neighbors']['distances']
n_neighbors = len(distances[0,:].data)+1

knn_inds = np.zeros((distances.shape[0],n_neighbors), dtype='int')
knn_inds[:,0] = np.arange(distances.shape[0], dtype='int')
for i in np.arange(distances.shape[0]):
    knn_inds[i,1:] = distances[i,:].indices[np.argsort(distances[i,:].data)]