Skip to content

Commit bb15e09

Browse files
committed
kmedoids(): cleanup tests
1 parent 03e77a9 commit bb15e09

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

test/kmedoids.jl

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ include("test_helpers.jl")
1010
@test_throws ArgumentError kmedoids(randn(2, 3), 1)
1111
@test_throws ArgumentError kmedoids(randn(2, 3), 4)
1212
dist = inv.(max.(pairwise(Euclidean(), randn(2, 3), dims=2), 0.1))
13-
@test kmedoids(dist, 2) isa KmedoidsResult
13+
@test @inferred(kmedoids(dist, 2)) isa KmedoidsResult
1414
@test_throws ArgumentError kmedoids(dist, 2, display=:mylog)
1515
for disp in keys(Clustering.DisplayLevels)
16-
@test kmedoids(dist, 2, display=disp) isa KmedoidsResult
16+
@test @inferred(kmedoids(dist, 2, display=disp)) isa KmedoidsResult
1717
end
1818
end
1919

@@ -28,7 +28,7 @@ dist = pairwise(SqEuclidean(), X, dims=2)
2828
@assert size(dist) == (n, n)
2929

3030
Random.seed!(34568) # reset seed again to known state
31-
R = kmedoids(dist, k)
31+
R = @inferred(kmedoids(dist, k))
3232
@test isa(R, KmedoidsResult)
3333
@test nclusters(R) == k
3434
@test length(R.medoids) == length(unique(R.medoids))
@@ -48,36 +48,45 @@ R = kmedoids(dist, k)
4848
end
4949
end
5050

51-
# k=1 and k=n cases
52-
x = pairwise(SqEuclidean(), [1 2 3; .1 .2 .3; 4 5.6 7], dims=2)
53-
kmed1 = kmedoids(x, 1)
54-
@test nclusters(kmed1) == 1
55-
@test assignments(kmed1) == [1, 1, 1]
56-
@test kmed1.medoids == [2]
57-
kmed3 = kmedoids(x, 3)
58-
@test nclusters(kmed3) == 3
59-
@test sort(assignments(kmed3)) == [1, 2, 3]
60-
@test sort(kmed3.medoids) == [1, 2, 3]
51+
@testset "Toy example #1" begin
52+
pts = [1 2 3; .1 .2 .3; 4 5.6 7]
53+
# k=1 and k=n cases
54+
dists = pairwise(SqEuclidean(), pts, dims=2)
6155

56+
@testset "k=1" begin
57+
kmed1 = @inferred(kmedoids(dists, 1))
58+
@test nclusters(kmed1) == 1
59+
@test assignments(kmed1) == [1, 1, 1]
60+
@test kmed1.medoids == [2]
61+
end
6262

63-
# this data set has three obvious groups:
64-
# group 1: [1, 3, 4], values: [1, 2, 3]
65-
# group 2: [2, 5, 7], values: [6, 7, 8]
66-
# group 3: [6, 8, 9], values: [21, 20, 22]
67-
#
63+
@testset "k=3" begin
64+
kmed3 = @inferred(kmedoids(dists, 3))
65+
@test nclusters(kmed3) == 3
66+
@test sort(assignments(kmed3)) == [1, 2, 3]
67+
@test sort(kmed3.medoids) == [1, 2, 3]
68+
end
69+
end
6870

69-
X = reshape(map(Float64, [1, 6, 2, 3, 7, 21, 8, 20, 22]), 1, 9)
70-
dist = pairwise(SqEuclidean(), X, dims=2)
71+
@testset "Toy example #2" begin
72+
pts = reshape(map(Float64, [1, 6, 2, 3, 7, 21, 8, 20, 22]), 1, 9)
73+
# this data set has three obvious groups:
74+
# group 1: [1, 3, 4], values: [1, 2, 3]
75+
# group 2: [2, 5, 7], values: [6, 7, 8]
76+
# group 3: [6, 8, 9], values: [21, 20, 22]
7177

72-
R = kmedoids!(dist, [1, 2, 6])
73-
@test isa(R, KmedoidsResult)
74-
@test nclusters(R) == 3
75-
@test R.medoids == [3, 5, 6]
76-
@test R.assignments == [1, 2, 1, 1, 2, 3, 2, 3, 3]
77-
@test counts(R) == [3, 3, 3]
78-
@test wcounts(R) == counts(R)
79-
@test R.costs [1, 1, 0, 1, 0, 0, 1, 1, 1]
80-
@test R.totalcost 6.0
81-
@test R.converged
78+
dists = pairwise(SqEuclidean(), pts, dims=2)
79+
80+
R = @inferred(kmedoids!(dists, [1, 2, 6]))
81+
@test isa(R, KmedoidsResult)
82+
@test nclusters(R) == 3
83+
@test R.medoids == [3, 5, 6]
84+
@test R.assignments == [1, 2, 1, 1, 2, 3, 2, 3, 3]
85+
@test counts(R) == [3, 3, 3]
86+
@test wcounts(R) == counts(R)
87+
@test R.costs [1, 1, 0, 1, 0, 0, 1, 1, 1]
88+
@test R.totalcost 6.0
89+
@test R.converged
90+
end
8291

8392
end

0 commit comments

Comments
 (0)