-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This file is a part of SimilaritySearch.jl | ||
|
||
export fft | ||
|
||
""" | ||
fft(dist::SemiMetric, X::AbstractDatabase, k; verbose=true) | ||
Selects `k` items far from each other based on Farthest First Traversal algorithm. | ||
Returns a named tuple with the following fields: | ||
- `centers` contains the list of centers (indexes to ``X``) | ||
- `nn` the id of the nearest center (in ``X`` order, identifiers between 1 to `length(X)) | ||
- `nndists` the distance from each object in the database to its nearest centers (in ``X`` order) | ||
- `dmax` smallest distance among centers | ||
Based on `enet.jl` from `KCenters.jl` | ||
""" | ||
function fft(dist::SemiMetric, X::AbstractDatabase, k::Integer; verbose=true) | ||
N = length(X) | ||
centers = Int32[] | ||
dmaxlist = Float32[] | ||
nndists = Vector{Float32}(undef, N) | ||
fill!(nndists, typemax(Float32)) | ||
nn = zeros(UInt32, N) | ||
imax::Int = rand(1:N) | ||
dmax::Float32 = typemax(Float32) | ||
N == 0 && return (; centers, nn, dists=nndists, dmax) | ||
|
||
@inbounds for i in 1:N | ||
push!(dmaxlist, dmax) | ||
push!(centers, imax) | ||
verbose && println(stderr, "computing fartest point $(length(centers)), dmax: $dmax, imax: $imax, n: $(length(X))") | ||
|
||
pivot = X[imax] | ||
@batch minbatch=getminbatch(0, N) for i in 1:N | ||
d = evaluate(dist, X[i], pivot) | ||
if d < nndists[i] | ||
nndists[i] = d | ||
nn[i] = imax | ||
end | ||
end | ||
|
||
dmax, imax = findmax(nndists) | ||
length(dmaxlist) < k || break | ||
end | ||
|
||
(; centers, nn, dists=nndists, dmax) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is a part of SimilaritySearch.jl | ||
|
||
using Test, SimilaritySearch, LinearAlgebra | ||
|
||
@testset "farthest first traversal" begin | ||
dist = L2Distance() | ||
X = rand(Float32, 4, 300) | ||
res = fft(dist, MatrixDatabase(X), 30) | ||
@test Set(res.centers) == Set(res.nn) | ||
@test all(res.dmax .>= res.dists) | ||
end | ||
|
305e4a3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
305e4a3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/102608
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: