From f7774975aca1c69051eaf7bdce56e8926a3f1929 Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Fri, 1 Oct 2021 22:07:48 +0200 Subject: [PATCH 1/5] Initialize indices with , allows for Infs and NaNs in the data --- src/knn.jl | 4 ++-- test/runtests.jl | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/knn.jl b/src/knn.jl index 6fb4bb1..0da7a50 100644 --- a/src/knn.jl +++ b/src/knn.jl @@ -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} @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index a257562..6e893ce 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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]) From c8e03ad7cccddf3e58ebd5d492f329395351c294 Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Fri, 1 Oct 2021 22:29:37 +0200 Subject: [PATCH 2/5] add missing file --- test/test_specialfloats.jl | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/test_specialfloats.jl diff --git a/test/test_specialfloats.jl b/test/test_specialfloats.jl new file mode 100644 index 0000000..4dac711 --- /dev/null +++ b/test/test_specialfloats.jl @@ -0,0 +1,41 @@ +using NearestNeighbors +using Test + +# Test for issue #78 +@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) + @show idx, _ = knn(tree, point, 1) + @test 1 <= idx[1] <= 11 + end +end + +# Test for issue #125 +@testset "infs on data" begin + for _ in 1:111 + + Ndim = 35 + Npt = 408 + + data = randn(Ndim, Npt) + + data[:,1] .= Inf + # tree = KDTree(data) + tree = BallTree(data) + + pointnan = repeat([NaN], Ndim) + indnan,distnan = nn(tree, pointnan) + # @test 1 <= indnan <= Npt + + pointinf = repeat([Inf], Ndim) + indinf, distinf = nn(tree, pointinf) + @test 1 <= indinf <= Npt + end +end From 8b3ec5ce27e894d056051f6999005b18c656c77b Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Fri, 1 Oct 2021 22:32:13 +0200 Subject: [PATCH 3/5] fixing old test code to proper patch --- test/test_specialfloats.jl | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/test/test_specialfloats.jl b/test/test_specialfloats.jl index 4dac711..6d193ab 100644 --- a/test/test_specialfloats.jl +++ b/test/test_specialfloats.jl @@ -4,7 +4,6 @@ using Test # Test for issue #78 @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; @@ -20,22 +19,14 @@ end # Test for issue #125 @testset "infs on data" begin for _ in 1:111 - Ndim = 35 Npt = 408 data = randn(Ndim, Npt) - - data[:,1] .= Inf - # tree = KDTree(data) - tree = BallTree(data) + tree = KDTree(data) pointnan = repeat([NaN], Ndim) indnan,distnan = nn(tree, pointnan) - # @test 1 <= indnan <= Npt - - pointinf = repeat([Inf], Ndim) - indinf, distinf = nn(tree, pointinf) - @test 1 <= indinf <= Npt + @test 1 <= indnan <= Npt end end From 58bddcc82d891542543b4370cc59f7a2f97f05a0 Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Fri, 1 Oct 2021 22:42:46 +0200 Subject: [PATCH 4/5] trying out nan on data, not as easy --- test/test_specialfloats.jl | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/test_specialfloats.jl b/test/test_specialfloats.jl index 6d193ab..e84bbb6 100644 --- a/test/test_specialfloats.jl +++ b/test/test_specialfloats.jl @@ -11,13 +11,13 @@ using Test point = [17889.55, 2094.45, 0.0] tree = BallTree(coords) - @show idx, _ = knn(tree, point, 1) + idx, _ = knn(tree, point, 1) @test 1 <= idx[1] <= 11 end end # Test for issue #125 -@testset "infs on data" begin +@testset "nan on query" begin for _ in 1:111 Ndim = 35 Npt = 408 @@ -30,3 +30,24 @@ end @test 1 <= indnan <= Npt 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 From 90d8dcc72516dc159dc73bbca4fd847a19e38c10 Mon Sep 17 00:00:00 2001 From: Nicolau Leal Werneck Date: Fri, 1 Oct 2021 22:50:42 +0200 Subject: [PATCH 5/5] inf on data actually still not correct, patch only makes result valid --- test/test_specialfloats.jl | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/test/test_specialfloats.jl b/test/test_specialfloats.jl index e84bbb6..e2a7d98 100644 --- a/test/test_specialfloats.jl +++ b/test/test_specialfloats.jl @@ -1,21 +1,6 @@ using NearestNeighbors using Test -# Test for issue #78 -@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 1 <= idx[1] <= 11 - end -end - # Test for issue #125 @testset "nan on query" begin for _ in 1:111 @@ -31,6 +16,21 @@ end end end +# # Test for issue #78 +# @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