-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDBSCANclusterer_test.go
136 lines (116 loc) · 2.94 KB
/
DBSCANclusterer_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
package dbscan
import (
// "bufio"
// "io"
// "os"
// "path/filepath"
// "strconv"
// "strings"
"testing"
)
func Test_DBSCAN(t *testing.T) {
t.Parallel()
var (
data = []ClusterablePoint{
&NamedPoint{"0", []float64{2, 4}},
&NamedPoint{"1", []float64{7, 3}},
&NamedPoint{"2", []float64{3, 5}},
&NamedPoint{"3", []float64{5, 3}},
&NamedPoint{"4", []float64{7, 4}},
// Noise point
&NamedPoint{"5", []float64{6, 8}},
&NamedPoint{"6", []float64{6, 5}},
&NamedPoint{"7", []float64{8, 4}},
&NamedPoint{"8", []float64{2, 5}},
&NamedPoint{"9", []float64{3, 7}},
}
clusterer = NewDBSCANClusterer(2.0, 2)
result [][]ClusterablePoint
expectedClusters = map[ClusterablePoint]int{
// Cluster 0
data[0]: 4,
data[2]: 4,
data[8]: 4,
data[9]: 4,
// Cluster 1
data[1]: 5,
data[3]: 5,
data[4]: 5,
data[6]: 5,
data[7]: 5,
}
compare = func(result [][]ClusterablePoint, expectedClusters map[ClusterablePoint]int) {
for _, cluster := range result {
size := len(cluster)
for _, p := range cluster {
if expectedClusters[p] != size {
t.Fatalf("Expected point %v to be in cluster %v, got %v", p, expectedClusters[p], size)
}
}
}
}
)
result = clusterer.Cluster(data)
compare(result, expectedClusters)
clusterer.AutoSelectDimension = false
clusterer.SetEps(7.0)
expectedClusters = make(map[ClusterablePoint]int)
for _, v := range data {
expectedClusters[v] = len(data)
}
result = clusterer.Cluster(data)
compare(result, expectedClusters)
}
/*var (
blockSize = 64 * 1024 * 1024
LineDelim = byte('\n')
)
func Test_DBSCAN2(t *testing.T) {
var (
filename = "intermediate-92.txt"
data []ClusterablePoint = NamedPointToClusterablePoint(transformToPoints(t, filename))
result [][]ClusterablePoint
clusterer = NewDBSCANClusterer(1.465, 10)
printCluster = func(result [][]ClusterablePoint) {
for i, cluster := range result {
t.Logf("Cluster %d: %d items", i, len(cluster))
}
}
)
t.Logf("Eps = %f", clusterer.GetEps())
result = clusterer.Cluster(data)
printCluster(result)
}
func transformToPoints(t *testing.T, filename string) []*NamedPoint {
var (
fileHandle, err = os.Open(filepath.Clean(filename))
reader = bufio.NewReaderSize(fileHandle, blockSize)
line string
records = make([]*NamedPoint, 0, 10000)
)
defer fileHandle.Close()
if err != nil {
t.Fatalf(err.Error())
return nil
}
line, err = reader.ReadString(LineDelim)
line = strings.TrimSpace(line)
for _, name := range strings.Split(line, ",")[2:] {
records = append(records, NewNamedPoint(
name,
make([]float64, 0, 10000),
))
}
for err != io.EOF {
line, err = reader.ReadString(LineDelim)
line = strings.TrimSpace(line)
if line == "" {
continue
}
for i, v := range strings.Split(line, ",")[2:] {
floatValue, _ := strconv.ParseFloat(v, 64)
records[i].Point = append(records[i].Point, floatValue)
}
}
return records
}*/