-
Notifications
You must be signed in to change notification settings - Fork 3
/
shuffle.go
31 lines (27 loc) · 887 Bytes
/
shuffle.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
package steps
import (
"math/rand"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
log "github.com/sirupsen/logrus"
)
func NewSampleShuffler() *bitflow.SimpleBatchProcessingStep {
return &bitflow.SimpleBatchProcessingStep{
Description: "sample shuffler",
Process: func(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
log.Println("Shuffling", len(samples), "samples")
for i := range samples {
j := rand.Intn(i + 1)
samples[i], samples[j] = samples[j], samples[i]
}
return header, samples, nil
},
}
}
func RegisterSampleShuffler(b reg.ProcessorRegistry) {
b.RegisterBatchStep("shuffle",
func(_ map[string]interface{}) (bitflow.BatchProcessingStep, error) {
return NewSampleShuffler(), nil
},
"Shuffle a batch of samples to a random ordering")
}