Skip to content

Commit

Permalink
adds farthest first traversal fft
Browse files Browse the repository at this point in the history
  • Loading branch information
sadit committed Mar 10, 2024
1 parent 8314348 commit 305e4a3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SimilaritySearch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ include("deprecated.jl")

include("allknn.jl")
include("neardup.jl")
include("fft.jl")
include("closestpair.jl")
include("hsp.jl")

Expand Down
49 changes: 49 additions & 0 deletions src/fft.jl
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

1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ include("testadj.jl")
include("testallknn.jl")
include("testhsp.jl")
include("testneardup.jl")
include("testfft.jl")
include("testclosestpair.jl")
include("testsearchgraph.jl")
12 changes: 12 additions & 0 deletions test/testfft.jl
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

2 comments on commit 305e4a3

@sadit
Copy link
Owner Author

@sadit sadit commented on 305e4a3 Mar 10, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

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.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

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:

git tag -a v0.11.7 -m "<description of version>" 305e4a3473b4bffa8e0ed48625a801c7a585ae94
git push origin v0.11.7

Please sign in to comment.