-
Notifications
You must be signed in to change notification settings - Fork 19
/
dataset_splitter.go
344 lines (279 loc) · 8.28 KB
/
dataset_splitter.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
337
338
339
340
341
342
343
344
package elasticthought
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
"path"
"strings"
"sync"
"github.com/couchbaselabs/logg"
)
// Worker job that splits a dataset into training/test set
type DatasetSplitter struct {
Configuration Configuration
Dataset Dataset
}
// Run this job
func (d DatasetSplitter) Run(wg *sync.WaitGroup) {
defer wg.Done()
dataset := &d.Dataset
updatedState, err := dataset.UpdateProcessingState(Processing)
if err != nil {
d.recordProcessingError(err)
return
}
if !updatedState {
logg.LogTo("TRAINING_JOB", "%+v already processed. Ignoring.", d)
return
}
switch d.Dataset.isSplittable() {
case true:
d.SplitDatafile()
default:
d.DownloadDatafiles()
}
}
func (d DatasetSplitter) SplitDatafile() {
// Find the datafile object associated with dataset
db := d.Configuration.DbConnection()
datafile, err := d.Dataset.GetSplittableDatafile(db)
if err != nil {
errMsg := fmt.Errorf("Error looking up datafile: %+v. Error: %v", d.Dataset, err)
d.recordProcessingError(errMsg)
return
}
// Open the url -- content type should be application/x-gzip
tr, err := openTarGzStream(datafile.Url)
if err != nil {
errMsg := fmt.Errorf("Error opening tar.gz streams: %v", err)
d.recordProcessingError(errMsg)
return
}
// Create pipes
prTrain, pwTrain := io.Pipe()
prTest, pwTest := io.Pipe()
// Wrap in gzip writers
pwGzTest := gzip.NewWriter(pwTest)
pwGzTrain := gzip.NewWriter(pwTrain)
// Create tar writers on the write end of the pipes
tarWriterTesting := tar.NewWriter(pwGzTest)
tarWriterTraining := tar.NewWriter(pwGzTrain)
// Create a cbfs client
cbfs, err := NewBlobStore(d.Configuration.CbfsUrl)
options := BlobPutOptions{}
options.ContentType = "application/x-gzip"
if err != nil {
errMsg := fmt.Errorf("Error creating cbfs client: %v", err)
d.recordProcessingError(errMsg)
return
}
// Figure out where to store these on cbfs
destTraining := d.Dataset.TrainingArtifactPath()
destTesting := d.Dataset.TestingArtifactPath()
// Spawn a goroutine that will read from tar.gz reader coming from url data
// and write to the training and test tar writers (which are on write ends of pipe)
transformDoneChan := make(chan error, 1)
go func() {
// Must close _underlying_ piped writers, or the piped readers will
// never get an EOF. Closing just the tar writers that wrap the underlying
// piped writers is not enough.
defer pwTest.Close()
defer pwTrain.Close()
defer pwGzTest.Close()
defer pwGzTrain.Close()
logg.LogTo("DATASET_SPLITTER", "Calling transform")
err = d.transform(tr, tarWriterTraining, tarWriterTesting)
if err != nil {
errMsg := fmt.Errorf("Error transforming tar stream: %v", err)
logg.LogError(errMsg)
transformDoneChan <- errMsg
return
}
transformDoneChan <- nil
}()
// Spawn goroutines to read off the read ends of the pipe and store in cbfs
cbfsTrainDoneChan := make(chan error, 1)
cbfsTestDoneChan := make(chan error, 1)
go func() {
if err := cbfs.Put("", destTesting, prTest, options); err != nil {
errMsg := fmt.Errorf("Error writing %v to cbfs: %v", destTesting, err)
logg.LogError(errMsg)
cbfsTestDoneChan <- errMsg
return
}
logg.LogTo("DATASET_SPLITTER", "Wrote %v to cbfs", destTesting)
cbfsTestDoneChan <- nil
}()
go func() {
if err := cbfs.Put("", destTraining, prTrain, options); err != nil {
errMsg := fmt.Errorf("Error writing %v to cbfs: %v", destTraining, err)
logg.LogError(errMsg)
cbfsTrainDoneChan <- errMsg
return
}
logg.LogTo("DATASET_SPLITTER", "Wrote %v to cbfs", destTraining)
cbfsTrainDoneChan <- nil
}()
// Wait for the results from all the goroutines
cbfsTrainResult := <-cbfsTrainDoneChan
cbfsTestResult := <-cbfsTestDoneChan
transformResult := <-transformDoneChan
// If any results had an error, log it and return
results := []error{transformResult, cbfsTestResult, cbfsTrainResult}
for _, result := range results {
if result != nil {
logg.LogTo("DATASET_SPLITTER", "Setting dataset to failed: %v", result)
d.Dataset.Failed(db, fmt.Errorf("%v", result))
return
}
}
// Update the state of the dataset to be finished
d.Dataset.FinishedSuccessfully(db)
if err := d.Dataset.FinishedSuccessfully(db); err != nil {
errMsg := fmt.Errorf("Error marking dataset %+v finished: %v", d, err)
d.recordProcessingError(errMsg)
return
}
}
func (d DatasetSplitter) DownloadDatafiles() {
// Create a cbfs client
cbfs, err := NewBlobStore(d.Configuration.CbfsUrl)
options := BlobPutOptions{}
options.ContentType = "application/x-gzip"
if err != nil {
errMsg := fmt.Errorf("Error creating cbfs client: %v", err)
d.recordProcessingError(errMsg)
return
}
db := d.Configuration.DbConnection()
source2destEntries := []struct {
Url string
DestPath string
}{
{
Url: d.Dataset.GetTrainingDatafileUrl(db),
DestPath: d.Dataset.TrainingArtifactPath(),
},
{
Url: d.Dataset.GetTestingDatafileUrl(db),
DestPath: d.Dataset.TestingArtifactPath(),
},
}
for _, source2destEntry := range source2destEntries {
// open tar.gz stream to source
// Open the url -- content type should be application/x-gzip
func() {
resp, err := http.Get(source2destEntry.Url)
if err != nil {
errMsg := fmt.Errorf("Error opening stream to: %v. Err %v", source2destEntry.Url, err)
d.recordProcessingError(errMsg)
return
}
defer resp.Body.Close()
if err := cbfs.Put("", source2destEntry.DestPath, resp.Body, options); err != nil {
errMsg := fmt.Errorf("Error writing %v to cbfs: %v", source2destEntry.DestPath, err)
d.recordProcessingError(errMsg)
return
}
logg.LogTo("DATASET_SPLITTER", "Wrote %v to cbfs", source2destEntry.DestPath)
}()
}
// Update the state of the dataset to be finished
if err := d.Dataset.FinishedSuccessfully(db); err != nil {
errMsg := fmt.Errorf("Error marking dataset %+v finished: %v", d, err)
d.recordProcessingError(errMsg)
return
}
}
// Codereview: de-dupe
func (d DatasetSplitter) recordProcessingError(err error) {
logg.LogError(err)
db := d.Configuration.DbConnection()
if err := d.Dataset.Failed(db, err); err != nil {
errMsg := fmt.Errorf("Error setting dataset as failed: %v", err)
logg.LogError(errMsg)
}
}
// Read from source tar stream and write training and test to given tar writers
func (d DatasetSplitter) transform(source *tar.Reader, train, test *tar.Writer) error {
splitter := d.splitter(train, test)
for {
hdr, err := source.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
return err
}
tw := splitter(hdr.Name)
if err := tw.WriteHeader(hdr); err != nil {
return err
}
_, err = io.Copy(tw, source)
if err != nil {
return err
}
}
// close writers
if err := train.Close(); err != nil {
errMsg := fmt.Errorf("Error closing tar writer: %v", err)
logg.LogError(errMsg)
return err
}
if err := test.Close(); err != nil {
errMsg := fmt.Errorf("Error closing tar reader: %v", err)
logg.LogError(errMsg)
return err
}
return nil
}
func (d DatasetSplitter) splitter(train, test *tar.Writer) func(string) *tar.Writer {
trainingRatio := int(d.Dataset.TrainingDataset.SplitPercentage * 100)
testRatio := int(d.Dataset.TestDataset.SplitPercentage * 100)
ratio := [2]int{trainingRatio, testRatio}
dircounts := make(map[string][2]int)
return func(file string) *tar.Writer {
dir := path.Dir(file)
counts := dircounts[dir]
count0ratio1 := counts[0] * ratio[1]
count1ratio0 := counts[1] * ratio[0]
if count0ratio1 <= count1ratio0 {
counts[0]++
dircounts[dir] = counts
return train
} else {
counts[1]++
dircounts[dir] = counts
return test
}
}
}
// Validate that the source tar stream conforms to expected specs
func (d DatasetSplitter) validate(source *tar.Reader) (bool, error) {
// validation rules:
// 1. has at least 2 files
// 2. the depth of each file is 2 (folder/filename.xxx)
numFiles := 0
for {
hdr, err := source.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
return false, err
}
numFiles += 1
pathComponents := strings.Split(hdr.Name, "/")
if len(pathComponents) != 2 {
return false, fmt.Errorf("Path does not have 2 components: %v", hdr.Name)
}
}
if numFiles < 2 {
return false, fmt.Errorf("Archive must contain at least 2 files")
}
return true, nil
}