Skip to content

Commit

Permalink
Modification to dbscan to add test cases and support for the case whe…
Browse files Browse the repository at this point in the history
…re number of points <= num dimensions
  • Loading branch information
Leon Wabeke committed Jan 3, 2025
1 parent 7a675b9 commit 56cd07f
Show file tree
Hide file tree
Showing 2 changed files with 29 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
29 changes: 29 additions & 0 deletions test/dbscan.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ include("test_helpers.jl")
@test @inferred(dbscan(randn(2, 2), 0.5, metric=nothing, min_neighbors=1)) isa DbscanResult
end

@testset "Simple 2D test case" begin
X = [10.0 0.0 10.5 ;
0.0 10.0 0.1]
R = dbscan(X, 20)
@test nclusters(R) == 1
R = dbscan(X, 1.0)
@test nclusters(R) == 2
R = dbscan(X, 0.1)
@test nclusters(R) == 3
end

@testset "Simple 2D test case with limited points" begin
X = [10.0 0.0 ;
0.0 10.0 ]
R = dbscan(X, 20)
@test nclusters(R) == 1
R = dbscan(X, 1.0)
@test nclusters(R) == 2
end
@testset "Simple 2D test case with limited points trivial case" begin
X = zeros(2,1) # At minimum dbscan needs a matrix and will not work with just a vector, but matrix could be Nx1
X[:,1] = [10.0 ;
0.0 ]
R = dbscan(X, 20)
@test nclusters(R) == 1
R = dbscan(X, 1.0)
@test nclusters(R) == 1
end

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

Expand Down

0 comments on commit 56cd07f

Please sign in to comment.