NearestNeighbors.jl icon indicating copy to clipboard operation
NearestNeighbors.jl copied to clipboard

Omitting results to points themselves

Open kskyten opened this issue 7 years ago • 8 comments

I would like to get the k nearest points that are not the querying points when using the same data for building the tree and querying it. Is it possible to do this with the skip function? The skip function seems to take integer arguments corresponding to tree indices, but it is unclear to me what this means.

kskyten avatar Jun 11 '17 07:06 kskyten

Yes it should be documented better (I didn't add that functionality hehe). skip takes one argument which is the index of a point. If the function returns true, the point is skipped. Maybe https://github.com/KristofferC/NearestNeighbors.jl/blob/master/src/tree_ops.jl#L101 can help you see how it is used.

KristofferC avatar Jun 11 '17 08:06 KristofferC

Seems to me like the easiest way would be to pass tree.data[inx] and point to the skip function.

kskyten avatar Jun 11 '17 09:06 kskyten

Yeah, passing the point itself as an argument makes sense. Hmm, not sure it can be done on a backwards compatible way.

KristofferC avatar Jun 11 '17 09:06 KristofferC

How about checking if the distance is zero? Testing equality of the points seems more robust though.

kskyten avatar Jun 11 '17 09:06 kskyten

It would be up to the user to define how equality should be checked by constructing the skip function. The package only need to provide the necessary data to that function.

KristofferC avatar Jun 11 '17 09:06 KristofferC

Adding arguments to the skip function works for omitting the equal points. However, now a wrong number of points are returned and I'm not sure where to fix this.

kskyten avatar Jun 11 '17 14:06 kskyten

What I actually needed was the distances to the k-th nearest neighbours. Here's what I ended up with

k = 3
X = rand(3, 10);
tree = KDTree(X)
ind, dist = knn(tree, X, k+1, true)
kdist = [x[end] for x in dist]

I couldn't figure out how to make a consistent API for the skip function though.

kskyten avatar Jul 11 '17 14:07 kskyten