-
Notifications
You must be signed in to change notification settings - Fork 3
/
distancemap_test.go
160 lines (140 loc) · 3.62 KB
/
distancemap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package clustering
import "testing"
func TestDistanceMapClustering1(t *testing.T) {
d := NewDistanceMapClusterSet(nil)
if d == nil {
t.Errorf("could not create empty DistanceMapClusterSet")
}
d = NewDistanceMapClusterSet(DistanceMap{"a": {"b": 0.0}})
if d == nil {
t.Errorf("could not create 2-node DistanceMapClusterSet")
}
if d.Count() != 2 {
t.Errorf("2-node DistanceMapClusterSet doesn't start with 2 clusters")
}
n1, n2 := 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 2 || n2 != 2 {
t.Errorf("2-node DistanceMapClusterSet didn't enumerate 2 clusters w/start=-1")
}
n1, n2 = 0, 0
d.EachCluster(0, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 1 || n2 != 1 {
t.Errorf("2-node DistanceMapClusterSet didn't enumerate 1 clusters w/start=0")
}
n1 = 0
d.EachCluster(1, func(cluster int) {
n1++
})
if n1 != 0 {
t.Errorf("2-node DistanceMapClusterSet didn't enumerate 0 clusters w/start=1")
}
if d.Distance(0, 1, "a", "b") != 0.0 {
t.Errorf("2-node DistanceMapClusterSet gave wrong distance")
}
d.Merge(0, 1)
if d.Count() != 1 {
t.Errorf("after Merge(0,1), 2-node DistanceMapClusterSet isn't 1 cluster")
}
n1, n2 = 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 1 || n2 != 2 {
t.Errorf("after Merge(0,1), 2-node DistanceMapClusterSet isn't 1 cluster with 2 items")
}
}
func TestDistanceMapClustering2(t *testing.T) {
d := NewDistanceMapClusterSet(DistanceMap{"a": {"b": 0.0, "c": 0.0}})
if d == nil {
t.Errorf("could not create 3-node DistanceMapClusterSet")
}
if d.Count() != 3 {
t.Errorf("3-node DistanceMapClusterSet doesn't start with 3 clusters")
}
n1, n2 := 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 3 || n2 != 3 {
t.Errorf("3-node DistanceMapClusterSet didn't enumerate 3 clusters w/start=-1")
}
Cluster(d, Threshold(1.0), CompleteLinkage())
if d.Count() != 1 {
t.Errorf("after clustering, 3-node DistanceMapClusterSet isn't 1 cluster")
}
n1, n2 = 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 1 || n2 != 3 {
t.Errorf("after clustering, 3-node DistanceMapClusterSet isn't 1 cluster with 3 items")
}
}
func TestDistanceMapClustering3(t *testing.T) {
d := NewDistanceMapClusterSet(DistanceMap{
"a": {"b": 0.0, "c": 0.0, "d": 1.0, "e": 0.4},
"b": {"c": 0.1, "d": 0.9, "e": 0.4},
"c": {"d": 0.9, "e": 0.2},
"d": {"e": 0.1},
})
if d == nil {
t.Errorf("could not create 5-node DistanceMapClusterSet")
}
if d.Count() != 5 {
t.Errorf("5-node DistanceMapClusterSet doesn't start with 5 clusters")
}
n1, n2 := 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
})
})
if n1 != 5 || n2 != 5 {
t.Errorf("5-node DistanceMapClusterSet didn't enumerate 5 clusters w/start=-1")
}
Cluster(d, Threshold(0.4), CompleteLinkage())
if d.Count() != 2 {
t.Errorf("after clustering, 5-node DistanceMapClusterSet isn't 2 clusters")
}
n1, n2 = 0, 0
c0, c1 := 0, 0
d.EachCluster(-1, func(cluster int) {
n1++
d.EachItem(cluster, func(x ClusterItem) {
n2++
if cluster == 0 {
c0++
}
if cluster == 1 {
c1++
}
})
})
if n1 != 2 || n2 != 5 {
t.Errorf("after clustering, 5-node DistanceMapClusterSet isn't 2 clusters with 5 items")
}
if (c0 < 2 || c0 > 3) || (c1 < 2 || c1 > 3) || (c0+c1) != 5 {
t.Errorf("after clustering, 5-node DistanceMapClusterSet should be 2,3")
}
}