-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
464 lines (385 loc) · 10.9 KB
/
pipeline.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package seafan
// pipeline.go has the interface and "With" funcs for Pipelines.
import (
"fmt"
"os"
"strings"
"github.com/invertedv/utilities"
"github.com/invertedv/chutils"
cf "github.com/invertedv/chutils/file"
s "github.com/invertedv/chutils/sql"
G "gorgonia.org/gorgonia"
)
// The Pipeline interface specifies the methods required to be a data Pipeline. The Pipeline is the middleware between
// the data and the fitting routines.
type Pipeline interface {
Init() error // initialize the pipeline
Rows() int // # of observations in the pipeline (size of the epoch)
Batch(inputs G.Nodes) bool // puts the next batch in the input nodes
Epoch(setTo int) int // manage epoch count
IsNormalized(field string) bool // true if feature is normalized
IsCat(field string) bool // true if feature is one-hot encoded
Cols(field string) int // # of columns in the feature
IsCts(field string) bool // true if the feature is continuous
GetFType(field string) *FType // Get FType for the feature
GetFTypes() FTypes // Get Ftypes for pipeline
BatchSize() int // batch size
FieldList() []string // fields available
FieldCount() int // number of fields in the pipeline
GData() *GData // return underlying GData
Get(field string) *GDatum // return data for field
GetKeepRaw() bool // returns whether raw data is kept
Join(right Pipeline, onField string, joinType JoinType) (Pipeline, error) // joins two pipelines
Slice(sl Slicer) (Pipeline, error) // slice the pipeline
Shuffle() // shuffle data
Describe(field string, topK int) string // describes a field
Subset(rows []int) (newPipe Pipeline, err error) // subsets pipeline to rows
Where(field string, equalTo []any) (Pipeline, error) // subset pipeline to where field=equalTo
Keep(fields []string) error // keep on fields in the pipeline
Drop(field string) error // drop field from the pipeline
AppendRows(gd *GData, fTypes FTypes) (Pipeline, error) // appends gd to pipeline
AppendRowsRaw(gd *GData) error // appends gd ONLY to *Raw data
ReInit(ftypes *FTypes) (Pipeline, error) // reinitialized pipeline from *Raw data
}
// Opts function sets an option to a Pipeline
type Opts func(c Pipeline)
// WithBatchSize sets the batch size for the pipeline
func WithBatchSize(bsize int) Opts {
f := func(c Pipeline) {
if bsize == 0 {
bsize = c.Rows()
}
switch d := c.(type) {
case *ChData:
d.bs = bsize
case *VecData:
d.bs = bsize
}
}
return f
}
// WithCycle sets the cycle bool. If false, the intent is for the Pipeline to generate a new
// data set is generated for each epoch.
func WithCycle(cycle bool) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
d.cycle = cycle
}
}
return f
}
// WithKeepRaw sets bool whether to keep the *Raw data in the pipeline.
func WithKeepRaw(keepRaw bool) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
d.keepRaw = keepRaw
case *VecData:
d.keepRaw = keepRaw
}
}
return f
}
// WithCats specifies a list of categorical features.
func WithCats(names ...string) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
for _, nm := range names {
ft := d.ftypes.Get(nm)
if ft != nil {
ft.Role = FRCat
continue
}
ft = &FType{
Name: nm,
Role: FRCat,
}
d.ftypes = append(d.ftypes, ft)
}
case *VecData:
for _, nm := range names {
ft := d.ftypes.Get(nm)
if ft != nil {
ft.Role = FRCat
continue
}
ft = &FType{
Name: nm,
Role: FRCat,
}
d.ftypes = append(d.ftypes, ft)
}
}
}
return f
}
// WithOneHot adds a one-hot field "name" based of field "from"
func WithOneHot(name, from string) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
ft := d.ftypes.Get(name)
if ft != nil {
ft.From = from
ft.Role = FROneHot
return
}
ft = &FType{
Name: name,
Role: FROneHot,
From: from,
}
d.ftypes = append(d.ftypes, ft)
case *VecData:
ft := d.ftypes.Get(name)
if ft != nil {
ft.From = from
ft.Role = FROneHot
return
}
ft = &FType{
Name: name,
Role: FROneHot,
From: from,
}
d.ftypes = append(d.ftypes, ft)
}
}
return f
}
// WithNormalized sets the features to be normalized.
func WithNormalized(names ...string) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
for _, nm := range names {
ft := d.ftypes.Get(nm)
if ft != nil {
ft.Role = FRCts
ft.Normalized = true
continue
}
ft = &FType{
Name: nm,
Role: FRCts,
Normalized: true,
}
d.ftypes = append(d.ftypes, ft)
}
case *VecData:
for _, nm := range names {
ft := d.ftypes.Get(nm)
if ft != nil {
ft.Role = FRCts
ft.Normalized = true
continue
}
ft = &FType{
Name: nm,
Role: FRCts,
Normalized: true,
}
d.ftypes = append(d.ftypes, ft)
}
}
}
return f
}
// WithFtypes sets the FTypes of the Pipeline. The feature is used to override the default levels.
func WithFtypes(fts FTypes) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
for _, ft := range fts {
ok := false
// see if it's already there
for ind := 0; ind < len(d.ftypes); ind++ {
if d.ftypes[ind].Name == ft.Name {
d.ftypes[ind] = ft
ok = true
break
}
}
if !ok {
d.ftypes = append(d.ftypes, ft)
}
}
d.ftypes = fts
case *VecData:
for _, ft := range fts {
ok := false
for ind := 0; ind < len(d.ftypes); ind++ {
if d.ftypes[ind].Name == ft.Name {
d.ftypes[ind] = ft
ok = true
break
}
}
if !ok {
d.ftypes = append(d.ftypes, ft)
}
}
d.ftypes = fts
}
}
return f
}
// WithCallBack sets a callback function.
func WithCallBack(cb Opts) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
d.callback = cb
case *VecData:
d.callback = cb
}
}
return f
}
// WithReader adds a reader.
func WithReader(rdr any) Opts {
f := func(c Pipeline) {
switch d := c.(type) {
case *ChData:
r, ok := rdr.(chutils.Input)
if !ok {
panic("reader not chutils.Input")
}
d.rdr = r
}
}
return f
}
// SQLToPipe creates a pipe from the query sql
// Optional fts specifies the FTypes, usually to match an existing pipeline.
func SQLToPipe(sql string, fts FTypes, keepRaw bool, conn *chutils.Connect) (pipe Pipeline, err error) {
rdr := s.NewReader(sql, conn)
defer func() { _ = rdr.Close() }()
if e := rdr.Init("", chutils.MergeTree); e != nil {
return nil, e
}
pipe = NewChData("MSR Pipeline")
if fts != nil {
WithFtypes(fts)(pipe)
}
WithReader(rdr)(pipe)
WithKeepRaw(keepRaw)(pipe)
WithBatchSize(0)(pipe)
if e := pipe.Init(); e != nil {
return nil, e
}
return pipe, nil
}
// CSVToPipe creates a pipe from a CSV file
// Optional fts specifies the FTypes, usually to match an existing pipeline.
func CSVToPipe(csvFile string, fts FTypes, keepRaw bool) (pipe Pipeline, err error) {
const tol = 0.98
handle, ex := os.Open(csvFile)
if ex != nil {
return nil, ex
}
defer func() { _ = handle.Close() }()
rdr := cf.NewReader(csvFile, ',', '\n', '"', 0, 1, 0, handle, 0)
if e := rdr.Init("", chutils.MergeTree); e != nil {
return nil, e
}
if e := rdr.TableSpec().Impute(rdr, 0, tol); e != nil {
return nil, e
}
if e := rdr.Reset(); e != nil {
return nil, e
}
pipe = NewChData("MSR Pipeline")
if fts != nil {
WithFtypes(fts)(pipe)
}
WithReader(rdr)(pipe)
WithBatchSize(0)(pipe)
WithKeepRaw(keepRaw)(pipe)
if e := pipe.Init(); e != nil {
return nil, e
}
return pipe, nil
}
// PipeToSQL creates "table" and saves the pipe data to it.
func PipeToSQL(pipe Pipeline, table string, after int, conn *chutils.Connect) error {
if table == "" {
return fmt.Errorf("exportSQL: table cannot be empty")
}
// make writer
wtr := s.NewWriter(table, conn)
defer func() { _ = wtr.Close() }()
gd := pipe.GData()
tb := gd.TableSpec()
if e := tb.Create(conn, table); e != nil {
return e
}
if e := pipe.GData().Reset(); e != nil {
return e
}
if e := chutils.Export(pipe.GData(), wtr, after, false); e != nil {
return e
}
return nil
}
// PipeToCSV saves the pipe as a CSV
func PipeToCSV(pipe Pipeline, outFile string, sep, eol, quote rune) error {
if outFile == "" {
return fmt.Errorf("exportCSV: outFile cannot be empty")
}
handle, err := os.Create(outFile)
if err != nil {
return err
}
defer func() { _ = handle.Close() }()
// write header
if _, e := handle.WriteString(strings.Join(pipe.FieldList(), ",") + "\n"); e != nil {
return e
}
// make writer
wtr := cf.NewWriter(handle, "output", nil, sep, eol, quote, "tmp.xyz")
defer func() { _ = wtr.Close() }()
if e := pipe.GData().Reset(); e != nil {
return e
}
// if after < 0, then won't also move to ClickHouse
if e := chutils.Export(pipe.GData(), wtr, -1, false); e != nil {
return e
}
return nil
}
// Append appends pipe2 to the bottom of pipe1. pipe2 must have all the fields of pipe1 but may have extra,
// which are not in the returned pipe
func Append(pipe1, pipe2 Pipeline) (Pipeline, error) {
if pipe1 == nil {
return pipe2, nil
}
flds1 := pipe1.FieldList()
flds2 := pipe2.FieldList()
for _, fld := range flds1 {
if utilities.Position(fld, "", flds2...) < 0 {
return nil, fmt.Errorf("field %s not in append pipe", fld)
}
}
gd1 := pipe1.GData()
gd2 := pipe2.GData()
forVec := make([][]any, gd1.FieldCount())
for ind := 0; ind < len(flds1); ind++ {
var (
d1, d2 *Raw
e error
)
fld := flds1[ind]
if d1, e = gd1.GetRaw(fld); e != nil {
return nil, e
}
if d2, e = gd2.GetRaw(fld); e != nil {
return nil, e
}
data := append(d1.Data, d2.Data...)
forVec[ind] = data
}
return VecFromAny(forVec, flds1, nil)
}