-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_test.go
336 lines (309 loc) · 12 KB
/
dataset_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package sticker
import (
"bytes"
"encoding/gob"
"fmt"
"math/rand"
"sort"
"strings"
"testing"
"github.com/hiro4bbh/go-assert"
)
func TestFloat32Slice(t *testing.T) {
x := []float32{5.0, 3.0, 1.0, 4.0, 2.0}
sort.Sort(Float32Slice(x))
goassert.New(t, []float32{1.0, 2.0, 3.0, 4.0, 5.0}).Equal(x)
}
func TestSummarizeFloat32Slice(t *testing.T) {
min0, q25_0, med0, q75_0, max0, mean0 := SummarizeFloat32Slice([]float32{})
goassert.New(t, "NaN,NaN,NaN,NaN,NaN,NaN").Equal(fmt.Sprintf("%f,%f,%f,%f,%f,%f", min0, q25_0, med0, q75_0, max0, mean0))
goassert.New(t, float32(1.0), float32(1.0), float32(1.0), float32(1.0), float32(1.0), float32(1.0)).Equal(SummarizeFloat32Slice([]float32{1.0}))
goassert.New(t, float32(1.0), float32(1.25), float32(1.5), float32(1.75), float32(2.0), float32(1.5)).Equal(SummarizeFloat32Slice([]float32{1.0, 2.0}))
goassert.New(t, float32(1.0), float32(2.0), float32(3.0), float32(4.0), float32(5.0), float32(3.0)).Equal(SummarizeFloat32Slice([]float32{5.0, 3.0, 1.0, 4.0, 2.0}))
}
func TestAvgTotalVariationAmongSparseVectors(t *testing.T) {
goassert.New(t, float32(0.0)).Equal(AvgTotalVariationAmongSparseVectors(SparseVectors{}))
goassert.New(t, "NaN").Equal(fmt.Sprintf("%g", AvgTotalVariationAmongSparseVectors(SparseVectors{
SparseVector{0: 1.0, 1: 1.0, 2: 1.0},
SparseVector{},
})))
goassert.New(t, float32(0.0)).Equal(AvgTotalVariationAmongSparseVectors(SparseVectors{
SparseVector{0: 1.0, 1: 1.0, 2: 1.0},
}))
goassert.New(t, float32(0.0)).Equal(AvgTotalVariationAmongSparseVectors(SparseVectors{
SparseVector{0: 1.0, 1: 1.0, 2: 1.0},
SparseVector{0: 1.0, 1: 1.0, 2: 1.0},
}))
goassert.New(t, float32(0.25)).Equal(AvgTotalVariationAmongSparseVectors(SparseVectors{
SparseVector{0: 1.0, 1: 1.0, 2: 1.0, 4: 1.0},
SparseVector{0: 1.0, 1: 1.0, 2: 1.0, 3: 1.0},
}))
goassert.New(t, float32(0.0)).Equal(AvgTotalVariationAmongSparseVectors(createBenchmarkAvgTotalVariationAmongSparseVectors()))
}
func TestSparsifyVector(t *testing.T) {
goassert.New(t, SparseVector{
1: 1.0, 3: -1.0,
}).Equal(SparsifyVector([]float32{0.0, 1.0, 0.0, -1.0, 0.0}))
}
func TestKeyCountMap32(t *testing.T) {
m := NewKeyCountMap32(27)
goassert.New(t, 32).Equal(len(m))
goassert.New(t, map[uint32]uint32{}).Equal(m.Map())
m.Inc(1)
m.Inc(2)
m.Inc(1)
m.Inc(31)
goassert.New(t, map[uint32]uint32{1: 2, 2: 1, 31: 1}).Equal(m.Map())
goassert.New(t, KeyCount32{1, 2}).Equal(m.Get(1))
goassert.New(t, KeyCount32{2, 1}).Equal(m.Get(2))
goassert.New(t, KeyCount32{31, 1}).Equal(m.Get(31))
goassert.New(t, KeyCount32{0, 0}).Equal(m.Get(0))
goassert.New(t, KeyCount32{0, 0}).Equal(m.Get(63))
}
func TestKeyCounts32ExtractLargestCountsByInsert(t *testing.T) {
// The keys are ignored.
kcs := KeyCounts32{
KeyCount32{0, 4}, KeyCount32{0, 3}, KeyCount32{0, 1}, KeyCount32{0, 5}, KeyCount32{0, 2},
}
expected := KeyCounts32{
KeyCount32{0, 5}, KeyCount32{0, 4}, KeyCount32{0, 3}, KeyCount32{0, 2}, KeyCount32{0, 1},
}
goassert.New(t, expected).Equal(kcs.ExtractLargestCountsByInsert(5))
goassert.New(t, expected[:3]).Equal(kcs.ExtractLargestCountsByInsert(3))
goassert.New(t, expected).Equal(kcs.ExtractLargestCountsByInsert(10))
}
func TestKeyCounts32SortLargestCountsWithHeap(t *testing.T) {
clone := func(kcs KeyCounts32) KeyCounts32 {
kcs2 := make(KeyCounts32, len(kcs))
copy(kcs2, kcs)
return kcs2
}
// The keys are ignored.
kcs := KeyCounts32{
KeyCount32{0, 4}, KeyCount32{0, 3}, KeyCount32{0, 1}, KeyCount32{0, 5}, KeyCount32{0, 2},
}
expected := KeyCounts32{
KeyCount32{0, 5}, KeyCount32{0, 4}, KeyCount32{0, 3}, KeyCount32{0, 2}, KeyCount32{0, 1},
}
goassert.New(t, expected).Equal(clone(kcs).SortLargestCountsWithHeap(5))
goassert.New(t, expected[:3]).Equal(clone(kcs).SortLargestCountsWithHeap(3))
goassert.New(t, expected[:4]).Equal(clone(kcs).SortLargestCountsWithHeap(4))
goassert.New(t, expected).Equal(clone(kcs).SortLargestCountsWithHeap(10))
}
func TestKeyValues32OrderedByKey(t *testing.T) {
data := KeyValues32OrderedByKey(KeyValues32{
KeyValue32{4, 2.0}, KeyValue32{1, 1.0}, KeyValue32{1, 2.0}, KeyValue32{2, 3.0}, KeyValue32{3, 3.0},
})
sort.Sort(data)
goassert.New(t, KeyValues32{
KeyValue32{1, 1.0}, KeyValue32{1, 2.0}, KeyValue32{2, 3.0}, KeyValue32{3, 3.0}, KeyValue32{4, 2.0},
}).Equal(KeyValues32(data))
}
func TestKeyValues32OrderedByValue(t *testing.T) {
data := KeyValues32OrderedByValue(KeyValues32{
KeyValue32{5, 2.0}, KeyValue32{1, 1.0}, KeyValue32{2, 1.0}, KeyValue32{3, 1.0}, KeyValue32{4, 3.0},
})
sort.Sort(data)
goassert.New(t, KeyValues32{
KeyValue32{3, 1.0}, KeyValue32{2, 1.0}, KeyValue32{1, 1.0}, KeyValue32{5, 2.0}, KeyValue32{4, 3.0},
}).Equal(KeyValues32(data))
}
func TestDotCount(t *testing.T) {
goassert.New(t, float32(1.0), 1).Equal(DotCount(FeatureVector{KeyValue32{1, 1.0}}, FeatureVector{KeyValue32{1, 1.0}}))
goassert.New(t, float32(1.0), 1).Equal(DotCount(FeatureVector{KeyValue32{2, 1.0}}, FeatureVector{KeyValue32{1, 1.0}, KeyValue32{2, 1.0}}))
goassert.New(t, float32(1.0), 1).Equal(DotCount(FeatureVector{KeyValue32{1, 1.0}, KeyValue32{2, 1.0}}, FeatureVector{KeyValue32{2, 1.0}}))
}
func TestFeatureVectors(t *testing.T) {
X := FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
}
goassert.New(t, 10).Equal(X.Dim())
}
func TestLabelVector(t *testing.T) {
y := LabelVector{3, 2, 1}
sort.Sort(y)
goassert.New(t, LabelVector{1, 2, 3}).Equal(y)
}
func TestLabelVectors(t *testing.T) {
Y := LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 7},
}
goassert.New(t, 8).Equal(Y.Dim())
}
func TestDecodeEncodeDataset(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
FeatureVector{KeyValue32{10, 7.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
var buf bytes.Buffer
goassert.New(t).SucceedWithoutError(EncodeDataset(ds, &buf))
var decodedDs Dataset
goassert.New(t).SucceedWithoutError(DecodeDataset(&decodedDs, &buf))
goassert.New(t, ds).Equal(&decodedDs)
// gob.Decoder.Decode won't call Dataset.GobDecode, because the encoder did not encode Dataset.
goassert.New(t, "Dataset should be encoded with EncodeDataset").ExpectError(gob.NewEncoder(&buf).Encode(&decodedDs))
}
func TestDatasetFeatureSubSet(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
FeatureVector{KeyValue32{10, 7.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
subds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{9, 6.0}},
FeatureVector{},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
goassert.New(t, subds).Equal(ds.FeatureSubSet(map[uint32]struct{}{0: {}, 5: {}, 9: {}}))
}
func TestDatasetSize(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
goassert.New(t, 3).Equal(ds.Size())
}
func TestDatasetSubSet(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
goassert.New(t, &Dataset{
X: FeatureVectors{ds.X[1], ds.X[1], ds.X[0]},
Y: LabelVectors{ds.Y[1], ds.Y[1], ds.Y[0]},
}).Equal(ds.SubSet([]int{1, 1, 0}))
}
func TestDatasetWriteToText(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
FeatureVector{KeyValue32{10, 7.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9}, LabelVector{10},
},
}
var buf bytes.Buffer
goassert.New(t).SucceedWithoutError(ds.WriteTextDataset(&buf))
ds2 := goassert.New(t).SucceedNew(ReadTextDataset(&buf)).(*Dataset)
goassert.New(t, ds).Equal(ds2)
}
func TestReadTextDataset(t *testing.T) {
ds := &Dataset{
X: FeatureVectors{
FeatureVector{KeyValue32{0, 1.0}},
FeatureVector{KeyValue32{0, 2.0}, KeyValue32{1, 3.0}},
FeatureVector{KeyValue32{0, 4.0}, KeyValue32{2, 5.0}, KeyValue32{9, 6.0}},
},
Y: LabelVectors{
LabelVector{0}, LabelVector{0, 1}, LabelVector{0, 2, 9},
},
}
goassert.New(t, ds).EqualWithoutError(ReadTextDataset(strings.NewReader("3 10 10\r\n0 0:1\n0,1 0:2 1:3\r\n0,2,9 0:4 2:5 9:6\n")))
goassert.New(t, "cannot read first line").ExpectError(ReadTextDataset(strings.NewReader("")))
goassert.New(t, "illegal first line").ExpectError(ReadTextDataset(strings.NewReader("3\n")))
goassert.New(t, "illegal nentries in first line").ExpectError(ReadTextDataset(strings.NewReader("x y z\n")))
goassert.New(t, "illegal nfeatures in first line").ExpectError(ReadTextDataset(strings.NewReader("3 y z\n")))
goassert.New(t, "illegal nlabels in first line").ExpectError(ReadTextDataset(strings.NewReader("3 10 z\n")))
goassert.New(t, "L1: cannot read line").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n")))
goassert.New(t, "L1: illegal #1 label ID").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\nx\n")))
goassert.New(t, "L1: too large #1 label ID \\(>= 10\\)").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n10\n")))
goassert.New(t, "L1: illegal #1 featureID:value pair").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n0 x\n")))
goassert.New(t, "L1: illegal #1 feature ID").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n0 x:y\n")))
goassert.New(t, "L1: too large #1 feature ID \\(>= 10\\)").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n0 10:y\n")))
goassert.New(t, "L1: illegal #1 feature value").ExpectError(ReadTextDataset(strings.NewReader("3 10 10\n0 0:y\n")))
}
func createBenchmarkAvgTotalVariationAmongSparseVectors() SparseVectors {
n, m := 50, 20
svs := make(SparseVectors, n)
for i := range svs {
sv := make(SparseVector, m)
for j := 0; j < m; j++ {
sv[uint32(j)] = float32(j)
}
svs[i] = sv
}
return svs
}
func BenchmarkAvgTotalVariationAmongSparseVectors(b *testing.B) {
svs := createBenchmarkAvgTotalVariationAmongSparseVectors()
b.ResetTimer()
for t := 0; t < b.N; t++ {
AvgTotalVariationAmongSparseVectors(svs)
}
}
func BenchmarkKeyCounts32ExtractLargestCountsByInsert(b *testing.B) {
rng := rand.New(rand.NewSource(0))
for t := 0; t < b.N; t++ {
// The keys are ignored.
kcs := make(KeyCounts32, 64*4096)
for i := 0; i < len(kcs)/2; i++ {
kcs[rng.Intn(len(kcs))].Count = rng.Uint32()
}
kcs.ExtractLargestCountsByInsert(2 * 75)
}
}
func BenchmarkKeyCounts32SortLargestCountsWithHeap(b *testing.B) {
rng := rand.New(rand.NewSource(0))
for t := 0; t < b.N; t++ {
// The keys are ignored.
kcs := make(KeyCounts32, 64*4096)
for i := 0; i < len(kcs)/2; i++ {
kcs[rng.Intn(len(kcs))].Count = rng.Uint32()
}
kcs.SortLargestCountsWithHeap(2 * 75)
}
}
func BenchmarkKeyCountMap32(b *testing.B) {
rng := rand.New(rand.NewSource(0))
for t := 0; t < b.N; t++ {
m := NewKeyCountMap32(64 * 1024)
for i := 0; i < len(m)/2; i++ {
m.Inc(rng.Uint32())
}
}
}
func BenchmarkMapUint32Uint32(b *testing.B) {
rng := rand.New(rand.NewSource(0))
for t := 0; t < b.N; t++ {
m := make(map[uint32]uint32)
for i := 0; i < 64*1024/2; i++ {
m[rng.Uint32()] += 1
}
}
}