-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdataset_splitter_test.go
165 lines (134 loc) · 3.26 KB
/
dataset_splitter_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
package elasticthought
import (
"archive/tar"
"bytes"
"io"
"log"
"path"
"testing"
"github.com/couchbaselabs/go.assert"
)
func init() {
EnableAllLogKeys()
}
func TestTransform(t *testing.T) {
splitter := create8020Splitter()
// Create a test tar archive
buf := new(bytes.Buffer)
var files = []tarFile{
{"foo/1.txt", "."},
{"foo/2.txt", "."},
{"bar/1.txt", "."},
{"bar/2.txt", "."},
{"bar/3.txt", "."},
{"bar/4.txt", "."},
{"bar/5.txt", "."},
}
createArchive(buf, files)
// Open the tar archive for reading.
r := bytes.NewReader(buf.Bytes())
tr := tar.NewReader(r)
// create two writers
bufTrain := new(bytes.Buffer)
bufTest := new(bytes.Buffer)
twTrain := tar.NewWriter(bufTrain)
twTest := tar.NewWriter(bufTest)
// pass these into transform
err := splitter.transform(tr, twTrain, twTest)
assert.True(t, err == nil)
if err != nil {
assert.Errorf(t, "Err from transform2: %v", err)
}
trainingResult := make(filemap)
testResult := make(filemap)
// assert that the both the training and test tar archives
// have been split correctly over each "label" folder
// Also, the resulting split sets must be disjoint.
buffers := []*bytes.Buffer{bufTrain, bufTest}
for _, buffer := range buffers {
readerVerify := bytes.NewReader(buffer.Bytes())
trVerify := tar.NewReader(readerVerify)
for {
hdr, err := trVerify.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
log.Fatalln(err)
}
assert.Equals(t, hdr.Uid, 100)
assert.Equals(t, hdr.Gid, 101)
dir := path.Dir(hdr.Name)
switch buffer {
case bufTrain:
trainingResult.addFileToDirectory(dir, hdr.Name)
case bufTest:
testResult.addFileToDirectory(dir, hdr.Name)
}
}
}
// make sure they have correct number of entries
assert.Equals(t, len(trainingResult["foo"]), 1)
assert.Equals(t, len(testResult["foo"]), 1)
assert.Equals(t, len(trainingResult["bar"]), 4)
assert.Equals(t, len(testResult["foo"]), 1)
}
func create5050Splitter() DatasetSplitter {
dataset := Dataset{
TrainingDataset: TrainingDataset{
SplitPercentage: 0.5,
},
TestDataset: TestDataset{
SplitPercentage: 0.5,
},
}
splitter := DatasetSplitter{
Dataset: dataset,
}
return splitter
}
func create8020Splitter() DatasetSplitter {
dataset := Dataset{
TrainingDataset: TrainingDataset{
SplitPercentage: 0.8,
},
TestDataset: TestDataset{
SplitPercentage: 0.2,
},
}
splitter := DatasetSplitter{
Dataset: dataset,
}
return splitter
}
func TestValidateValid(t *testing.T) {
buf := new(bytes.Buffer)
var files = []tarFile{
{"foo/1.txt", "Hello 1."},
{"foo/2.txt", "Hello 2."},
{"bar/1.txt", "Hello bar 1."},
{"bar/2.txt", "Hello bar 2."},
}
createArchive(buf, files)
reader := bytes.NewReader(buf.Bytes())
tr := tar.NewReader(reader)
splitter := DatasetSplitter{}
ok, err := splitter.validate(tr)
assert.True(t, ok)
assert.True(t, err == nil)
}
func TestValidateTooDeep(t *testing.T) {
buf := new(bytes.Buffer)
var files = []tarFile{
{"a/foo/1.txt", "Hello 1."},
{"a/bar/1.txt", "Hello bar 1."},
}
createArchive(buf, files)
reader := bytes.NewReader(buf.Bytes())
tr := tar.NewReader(reader)
splitter := DatasetSplitter{}
ok, err := splitter.validate(tr)
assert.False(t, ok)
assert.True(t, err != nil)
}