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

fix skipped points #146

Merged
merged 2 commits into from
Jun 15, 2022
Merged
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
1 change: 0 additions & 1 deletion src/brute_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ end
function BruteTree(data::AbstractVecOrMat{T}, metric::Metric = Euclidean();
reorder::Bool=false, leafsize::Int=0, storedata::Bool=true) where {T}
dim = size(data, 1)
npoints = size(data, 2)
BruteTree(copy_svec(T, data, Val(dim)),
metric, reorder = reorder, leafsize = leafsize, storedata = storedata)
end
Expand Down
1 change: 0 additions & 1 deletion src/hyperspheres.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ end
end

function create_bsphere(data::AbstractVector{V}, metric::Metric, indices::Vector{Int}, low, high, ab) where {V}
n_dim = size(data, 1)
n_points = high - low + 1
# First find center of all points
fill!(ab.center, 0.0)
Expand Down
11 changes: 8 additions & 3 deletions src/knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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}
check_input(tree, points)
check_k(tree, k)
n_points = length(points)
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
dists = [Vector{get_T(eltype(V))}(undef, k) for _ in 1:n_points]
idxs = [Vector{Int}(undef, k) for _ in 1:n_points]
for i in 1:n_points
knn_point!(tree, points[i], sortres, dists[i], idxs[i], skip)
end
Expand All @@ -30,6 +30,11 @@ function knn_point!(tree::NNTree{V}, point::AbstractVector{T}, sortres, dist, id
fill!(idx, -1)
fill!(dist, typemax(get_T(eltype(V))))
_knn(tree, point, idx, dist, skip)
if skip !== always_false
skipped_idxs = findall(==(-1), idx)
deleteat!(idx, skipped_idxs)
deleteat!(dist, skipped_idxs)
end
sortres && heap_sort_inplace!(dist, idx)
if tree.reordered
for j in eachindex(idx)
Expand Down
6 changes: 6 additions & 0 deletions test/test_knn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ end
test(data)
test(view(data, :, :))
end

data = [[0.13380863416387367, 0.7845254987714512],[0.1563342025559629, 0.7956456895676077],[0.23320094627474594, 0.9055515160266435]]
tree = KDTree(hcat(map(p -> [p[1], p[2]], data)...))
nearest, distance = knn(tree, [0.15, 0.8], 3, true, x -> x == 2)
@test nearest == [1, 3]
@test distance ≈ [0.02239688629947563, 0.13440059522389006]
end