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

Initialize indices with 1, allows for Infs and NaNs in the data #126

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ end
Performs a lookup of the `k` nearest neigbours to the `points` from the data
in the `tree`. If `sortres = true` the result is sorted such that the results are
in the order of increasing distance to the point. `skip` is an optional predicate
to determine if a point that would be returned should be skipped based on its
to determine if a point that would be returned should be skipped based on its
index.
"""
function knn(tree::NNTree{V}, points::Vector{T}, k::Int, sortres=false, skip::F=always_false) where {V, T <: AbstractVector, F<:Function}
Expand All @@ -27,7 +27,7 @@ function knn(tree::NNTree{V}, points::Vector{T}, k::Int, sortres=false, skip::F=
end

function knn_point!(tree::NNTree{V}, point::AbstractVector{T}, sortres, dist, idx, skip::F) where {V, T <: Number, F}
fill!(idx, -1)
fill!(idx, 1)
fill!(dist, typemax(get_T(eltype(V))))
_knn(tree, point, idx, dist, skip)
sortres && heap_sort_inplace!(dist, idx)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include("test_knn.jl")
include("test_inrange.jl")
include("test_monkey.jl")
include("test_datafreetree.jl")
include("test_specialfloats.jl")

@testset "periodic euclidean" begin
pred = PeriodicEuclidean([Inf, 2.5])
Expand Down
53 changes: 53 additions & 0 deletions test/test_specialfloats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using NearestNeighbors
using Test

# Test for issue #125
@testset "nan on query" begin
for _ in 1:111
Ndim = 35
Npt = 408

data = randn(Ndim, Npt)
tree = KDTree(data)

pointnan = repeat([NaN], Ndim)
indnan,distnan = nn(tree, pointnan)
@test 1 <= indnan <= Npt
end
end

# # Test for issue #78
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you commented this out,
but if its merely because the test fails, a @test_broken might be appropriate so we know its broken :)

# @testset "infs on data" begin
# for _ in 1:11
# coords = [
# 29882.5 25974.3 Inf Inf 17821.8 Inf Inf Inf Inf Inf 16322.0;
# 9279.86 9286.35 Inf Inf 10320.4 Inf Inf Inf Inf Inf 11459.0;
# 0.0 0.0 Inf Inf 0.0 Inf Inf Inf Inf Inf 0.0]
# point = [17889.55, 2094.45, 0.0]

# tree = BallTree(coords)
# idx, _ = knn(tree, point, 1)
# @test idx[1] == 5
# end
# end

# @testset "nan on data" begin
# for _ in 1:11
# # Ndim = 35
# # Npt = 408

# # data = randn(Ndim, Npt)
# # tree = KDTree(data)

# # datanan = copy(data)
# # datanan[rand(1:Ndim),rand(1:Npt)] = NaN
# # treenan = KDTree(datanan)

# # pointrand = randn(Ndim)

# # @show indnan2, distnan2 = nn(tree, pointrand)
# # @show indnan2, distnan2 = nn(treenan, pointrand)
# # @test 1 <= indnan2 <= Npt

# end
# end