Skip to content

Commit

Permalink
dbscan(): drop number of points <= num dims test
Browse files Browse the repository at this point in the history
add unit tests for n=3,2,1,0 points
  • Loading branch information
Leon Wabeke authored and alyst committed Jan 5, 2025
1 parent 7a675b9 commit 539e223
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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
45 changes: 45 additions & 0 deletions test/dbscan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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

0 comments on commit 539e223

Please sign in to comment.