Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutual nearest neighbors #141

Open
RainerHeintzmann opened this issue Mar 9, 2022 · 3 comments
Open

mutual nearest neighbors #141

RainerHeintzmann opened this issue Mar 9, 2022 · 3 comments

Comments

@RainerHeintzmann
Copy link

Thanks for the great toolbox you contributed!
I was trying to use it for the problem of picking particles in an image that need to have a minimum distance to their neighbors.
I have a list of coordinates and converted it to the required matrix form. But then I am stuck.
I can see that your toolbox answers the question to find the neighbor in dataset A, given the probe coordinates of dataset B.
But in my case these datasets are the same and I want to exclude each vector from the search.

Of course I could build a seperate KDTree()each time, leaving one vector out, but this sounds like a really bad idea to me for performance reasons.
Can your toolbox handle the one-dataset nearest neighbor question? If not, which algorithm do I need for this and where can I find it?
Maybe the easiest is to simply do a brute-force search. Or maybe the KDTree already encodes the answer?

@RainerHeintzmann
Copy link
Author

I looked a bit more at the trees and found a solution that sort of works:

balltree = BallTree(points)
maxdist = 10.0
idxs = inrange(balltree, points, maxdist, true)

and then you need to see if a point has more than one result.
But is this the best way of solving this problem?

@RainerHeintzmann
Copy link
Author

RainerHeintzmann commented Mar 9, 2022

Maybe this is a good solution?:

"""
    nn_mutual(points)
    returns the mutual nearest neighbor distances and the index of the neares neighbor, both in matrix form.
"""
function nn_mutual(points)
    kdtree = KDTree(points)
    nns, nndist = knn(kdtree, points, 2)
    nns = [mynn[1] for mynn in nns]
    nndist = [myd[1] for myd in nndist]
    return nndist, nns
end

If so, should it maybe be part of the toolbox?

@BioTurboNick
Copy link
Contributor

You might be looking for the skip argument, which allows you to specify a function to decide which points in the tree to not match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants