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

Modification to dbscan to add test cases and support for the case where num points <= num dimensions #286

Merged
merged 2 commits into from
Jan 5, 2025
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/dbscan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ function dbscan(points::AbstractMatrix, radius::Real;
if metric !== nothing
# points are point coordinates
dim, num_points = size(points)
num_points <= dim && throw(ArgumentError("points has $dim rows and $num_points columns. Must be a D x N matric with D < N"))
kdtree = KDTree(points, metric; nntree_kwargs...)
data = (kdtree, points)
else
Expand Down
1 change: 0 additions & 1 deletion test/affprop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using Clustering
using LinearAlgebra
using Random, StableRNGs
using Statistics
include("test_helpers.jl")

@testset "affinityprop() (affinity propagation)" begin

Expand Down
46 changes: 45 additions & 1 deletion test/dbscan.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Test
using Clustering
using Distances
include("test_helpers.jl")

@testset "dbscan() (DBSCAN clustering)" begin

Expand All @@ -14,6 +13,51 @@ include("test_helpers.jl")
@test @inferred(dbscan(randn(2, 2), 0.5, metric=nothing, min_neighbors=1)) isa DbscanResult
end

@testset "Simple 2D tests" begin
X = [10.0 0.0 10.5
0.0 10.0 0.1]

@testset "n=3 samples" begin
X3 = X

R = dbscan(X3, 20)
@test nclusters(R) == 1

R = dbscan(X3, 1.0)
@test nclusters(R) == 2

R = dbscan(X3, 0.1)
@test nclusters(R) == 3
end

@testset "n=2 samples" begin
X2 = X[:, 1:2]

R = dbscan(X2, 20)
@test nclusters(R) == 1

R = dbscan(X2, 1.0)
@test nclusters(R) == 2
end

@testset "n=1 samples" begin
X1 = X[:, 1:1]

R = dbscan(X1, 20)
@test nclusters(R) == 1

R = dbscan(X1, 1.0)
@test nclusters(R) == 1
end

@testset "n=0 samples" begin
X0 = X[:, 1:0]

R = dbscan(X0, 20)
@test nclusters(R) == 0
end
end

@testset "clustering synthetic data with 3 clusters" begin
Random.seed!(34568)

Expand Down
1 change: 0 additions & 1 deletion test/kmedoids.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Test
using Distances
using Clustering
include("test_helpers.jl")

@testset "kmedoids() (k-medoids)" begin

Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using SparseArrays
using StableRNGs
using Statistics

include("test_helpers.jl")

tests = ["seeding",
"kmeans",
"kmedoids",
Expand Down
Loading