From 539e223d04d83a0bf52575c7f1876c2561721bc4 Mon Sep 17 00:00:00 2001 From: Leon Wabeke Date: Sun, 5 Jan 2025 14:13:30 -0800 Subject: [PATCH 1/2] dbscan(): drop number of points <= num dims test add unit tests for n=3,2,1,0 points --- src/dbscan.jl | 1 - test/dbscan.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/dbscan.jl b/src/dbscan.jl index b34e580e..bfa04bc3 100644 --- a/src/dbscan.jl +++ b/src/dbscan.jl @@ -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 diff --git a/test/dbscan.jl b/test/dbscan.jl index e35195a4..492d4755 100644 --- a/test/dbscan.jl +++ b/test/dbscan.jl @@ -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) From 27983b8d6835ba7a412d73c678e470153a9a63de Mon Sep 17 00:00:00 2001 From: Leon Wabeke Date: Sun, 5 Jan 2025 14:05:53 -0800 Subject: [PATCH 2/2] tests: include test_helpers.jl in runtests.jl to avoid redefining test helper functions --- test/affprop.jl | 1 - test/dbscan.jl | 1 - test/kmedoids.jl | 1 - test/runtests.jl | 2 ++ 4 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/affprop.jl b/test/affprop.jl index d51c3382..f218f50c 100644 --- a/test/affprop.jl +++ b/test/affprop.jl @@ -6,7 +6,6 @@ using Clustering using LinearAlgebra using Random, StableRNGs using Statistics -include("test_helpers.jl") @testset "affinityprop() (affinity propagation)" begin diff --git a/test/dbscan.jl b/test/dbscan.jl index 492d4755..07a89ce9 100644 --- a/test/dbscan.jl +++ b/test/dbscan.jl @@ -1,7 +1,6 @@ using Test using Clustering using Distances -include("test_helpers.jl") @testset "dbscan() (DBSCAN clustering)" begin diff --git a/test/kmedoids.jl b/test/kmedoids.jl index a9f50d5f..4a649fc9 100644 --- a/test/kmedoids.jl +++ b/test/kmedoids.jl @@ -1,7 +1,6 @@ using Test using Distances using Clustering -include("test_helpers.jl") @testset "kmedoids() (k-medoids)" begin diff --git a/test/runtests.jl b/test/runtests.jl index 42301653..2e6a7894 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,6 +6,8 @@ using SparseArrays using StableRNGs using Statistics +include("test_helpers.jl") + tests = ["seeding", "kmeans", "kmedoids",