forked from bobhancock/goxmeans
/
km_test.go
273 lines (225 loc) · 6.53 KB
/
km_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package goxmeans
import (
"bufio"
// "code.google.com/p/gomatrix/matrix"
"fmt"
"os"
"testing"
"github.com/bobhancock/gomatrix/matrix"
"goxmeans/matutil"
)
func TestAtof64Invalid(t *testing.T) {
s := "xyz"
if _, err := Atof64(s); err == nil {
t.Errorf("err == nil with invalid input %s.", s)
}
}
func TestAtof64Valid(t *testing.T) {
s := "1234.5678"
if f64, err := Atof64(s); err != nil {
t.Errorf("err != nil with input %s. Returned f64=%f,err= %v.", s, f64, err)
}
}
func TestFileNotExistsLoad(t *testing.T) {
f := "filedoesnotexist"
if _, err := Load(f); err == nil {
t.Errorf("err == nil with file that does not exist. err=%v.", err)
}
}
func createtestfile(fname, record string) (int, error) {
fp, err := os.Create(fname)
if err != nil {
return 0, err
}
defer fp.Close()
w := bufio.NewWriter(fp)
i, err := w.WriteString(record)
if err != nil {
return i, err
}
w.Flush()
return i, err
}
// Does the input line contain < 2 elements
func TestInputInvalid(t *testing.T) {
fname := "inputinvalid"
_, err := createtestfile(fname, "123\n")
if err != nil {
t.Errorf("Could not create test file. err=%v", err)
}
defer os.Remove(fname)
if _, err := Load(fname); err == nil {
t.Errorf("err: %v", err)
}
}
func TestValidReturnLoad(t *testing.T) {
fname := "inputvalid"
record := fmt.Sprintf("123\t456\n789\t101")
_, err := createtestfile(fname, record)
if err != nil {
t.Errorf("Could not create test file %s err=%v", fname, err)
}
defer os.Remove(fname)
if _, err := Load(fname); err != nil {
t.Errorf("Load(%s) err=%v", fname, err)
}
}
/*func TestRandCentroids(t *testing.T) {
rows := 3
cols := 3
k := 2
data := []float64{1, 2.0, 3, -4.945, 5, -6.1, 7, 8, 9}
mat := matrix.MakeDenseMatrix(data, rows, cols)
choosers := []CentroidChooser{RandCentroids{}, DataCentroids{}, EllipseCentroids{0.5}}
for _, cc := range choosers{
centroids := cc.ChooseCentroids(mat, k)
r, c := centroids.GetSize()
if r != k || c != cols {
t.Errorf("Returned centroid was %dx%d instead of %dx%d", r, c, rows, cols)
}
}
}*/
func TestComputeCentroid(t *testing.T) {
empty := matrix.Zeros(0, 0)
_, err := ComputeCentroid(empty)
if err == nil {
t.Errorf("Did not raise error on empty matrix")
}
twoByTwo := matrix.Ones(2, 2)
centr, err := ComputeCentroid(twoByTwo)
if err != nil {
t.Errorf("Could not compute centroid, err=%v", err)
}
expected := matrix.MakeDenseMatrix([]float64{1.0, 1.0}, 1, 2)
if !matrix.Equals(centr, expected) {
t.Errorf("Incorrect centroid: was %v, should have been %v", expected, centr)
}
twoByTwo.Set(0, 0, 3.0)
expected.Set(0, 0, 2.0)
centr, err = ComputeCentroid(twoByTwo)
if err != nil {
t.Errorf("Could not compute centroid, err=%v", err)
}
if !matrix.Equals(centr, expected) {
t.Errorf("Incorrect centroid: was %v, should have been %v", expected, centr)
}
}
func TestKmeansp(t *testing.T) {
dataPoints, err := Load("./testSetSmall.txt")
if err != nil {
t.Errorf("Load returned: %v", err)
return
}
var ed matutil.EuclidDist
var cc RandCentroids
//centroidsdata := []float64{1.5,1.5,2,2,3,3,0.9,0,9}
//centroids := matrix.MakeDenseMatrix(centroidsdata, 4,2)
centroidMeans, centroidSqDist, err := Kmeansp(dataPoints, 4, cc, ed)
if err != nil {
t.Errorf("Kmeans returned: %v", err)
return
}
if a, b := centroidMeans.GetSize(); a == 0 || b == 0 {
t.Errorf("Kmeans centroidMeans is of size %d, %d.", a,b)
}
if c, d := centroidSqDist.GetSize(); c == 0 || d == 0 {
t.Errorf("Kmeans centroidSqDist is of size %d, %d.", c,d)
}
}
func TestAddPairPointToCentroidJob(t *testing.T) {
r := 4
c := 2
jobs := make(chan PairPointCentroidJob, r)
results := make(chan PairPointCentroidResult, minimum(1024, r))
dataPoints := matrix.Zeros(r, c)
centroidSqDist := matrix.Zeros(r, c)
centroids := matrix.Zeros(r, c)
var ed matutil.EuclidDist
go addPairPointCentroidJobs(jobs, dataPoints, centroids, centroidSqDist,ed ,results)
i := 0
for ; i < r; i++ {
<-jobs
//fmt.Printf("Drained %d\n", i)
}
if i != r {
t.Errorf("addPairPointToCentroidJobs number of jobs=%d. Should be %d", i, r)
}
}
func TestDoPairPointCentroidJobs(t *testing.T) {
r := 4
c := 2
dataPoints := matrix.Zeros(r, c)
centroidSqDist := matrix.Zeros(r, c)
centroids := matrix.Zeros(r, c)
done := make(chan int)
jobs := make(chan PairPointCentroidJob, r)
results := make(chan PairPointCentroidResult, minimum(1024, r))
var md matutil.ManhattanDist
go addPairPointCentroidJobs(jobs, dataPoints, centroids, centroidSqDist, md, results)
for i := 0; i < r; i++ {
go doPairPointCentroidJobs(done, jobs)
}
j := 0
for ; j < r; j++ {
<- done
}
if j != r {
t.Errorf("doPairPointToCentroidJobs jobs processed=%d. Should be %d", j, r)
}
}
func TestProcessPairPointToCentroidResults(t *testing.T) {
r := 4
c := 2
dataPoints := matrix.Zeros(r, c)
centroidSqDist := matrix.Zeros(r, c)
centroids := matrix.Zeros(r, c)
done := make(chan int)
jobs := make(chan PairPointCentroidJob, r)
results := make(chan PairPointCentroidResult, minimum(1024, r))
var md matutil.ManhattanDist
go addPairPointCentroidJobs(jobs, dataPoints, centroids, centroidSqDist, md, results)
for i := 0; i < r; i++ {
go doPairPointCentroidJobs(done, jobs)
}
go awaitPairPointCentroidCompletion(done, results)
//TODO check deterministic results of centroidDistSq
processPairPointToCentroidResults(centroidSqDist, results)
}
func TestKmeansbi(t *testing.T) {
dataPoints, err := Load("./testSetSmall.txt")
if err != nil {
t.Errorf("Load returned: %v", err)
return
}
var ed matutil.EuclidDist
var cc RandCentroids
matCentroidlist, clusterAssignment, err := Kmeansp(dataPoints, 4, cc, ed)
if err != nil {
t.Errorf("Kmeans returned: %v", err)
return
}
if a, b := matCentroidlist.GetSize(); a == 0 || b == 0 {
t.Errorf("Kmeans centroidMeans is of size %d, %d.", a,b)
}
if c, d := clusterAssignment.GetSize(); c == 0 || d == 0 {
t.Errorf("Kmeans centroidSqDist is of size %d, %d.", c,d)
}
}
func TestVariance(t *testing.T) {
points := matrix.MakeDenseMatrix([]float64{1.2, 2.1,
3.3, 2.2,
4.1, 3.2,
5.3, 4.1},
4,2)
centroid := matrix.MakeDenseMatrix([]float64{6.5, 4},
1,2)
var ed matutil.EuclidDist
v, err := variance(points, centroid, ed)
if err != nil {
t.Errorf("TestVaricance:variance returned error %v.", err)
}
//fmt.Printf("v=%f\n",v)
if v != 8.452500 {
t.Errorf("TestVariance: variance should be 8.452500 but received %f", v)
}
}