-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch_aggregate.go
150 lines (126 loc) · 4.62 KB
/
batch_aggregate.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
package math
import (
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
"github.com/bitflow-stream/go-bitflow/steps"
)
type BatchAggregateFunc func(aggregated bitflow.Value, newValue bitflow.Value) bitflow.Value
type BatchAggregator struct {
Aggregator BatchAggregateFunc
Description string
}
func (a *BatchAggregator) ProcessBatch(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
resultSample := samples[len(samples)-1].Clone()
resultSample.Values = a.computeValues(header, samples)
return header, []*bitflow.Sample{resultSample}, nil
}
func (a *BatchAggregator) computeValues(header *bitflow.Header, samples []*bitflow.Sample) []bitflow.Value {
if len(samples) == 0 {
return nil
}
// Start with the first sample
values := make([]bitflow.Value, len(header.Fields))
for i, value := range samples[0].Values {
values[i] = value
}
for _, sample := range samples[1:] {
for i, value := range sample.Values {
values[i] = a.Aggregator(values[i], value)
}
}
return values
}
func (a *BatchAggregator) String() string {
return "Batch Aggregation: " + a.Description
}
func registerAggregator(b reg.ProcessorRegistry, operation string, factoryFunc func() bitflow.BatchProcessingStep) {
b.RegisterBatchStep(operation,
func(params map[string]interface{}) (bitflow.BatchProcessingStep, error) {
return factoryFunc(), nil
}, "Compute for all values in the batch (per metric): "+operation)
}
func RegisterBatchAggregators(b reg.ProcessorRegistry) {
registerAggregator(b, "multiply", NewBatchMultiplyAggregator)
registerAggregator(b, "sum", NewBatchSumAggregator)
}
func NewBatchMultiplyAggregator() bitflow.BatchProcessingStep {
return &BatchAggregator{
Aggregator: func(aggregated bitflow.Value, newValue bitflow.Value) bitflow.Value {
return aggregated * newValue
},
Description: "multiply",
}
}
func NewBatchSumAggregator() bitflow.BatchProcessingStep {
return &BatchAggregator{
Aggregator: func(aggregated bitflow.Value, newValue bitflow.Value) bitflow.Value {
return aggregated + newValue
},
Description: "sum",
}
}
type GetFeatureFunc func(stats steps.FeatureStats) float64
type BatchFeatureStatsAggregator struct {
Aggregate GetFeatureFunc
Description string
}
func (ba *BatchFeatureStatsAggregator) ProcessBatch(header *bitflow.Header, samples []*bitflow.Sample) (*bitflow.Header, []*bitflow.Sample, error) {
resultSample := samples[len(samples)-1].Clone()
resultSample.Values = ba.computeValues(header, samples)
return header, []*bitflow.Sample{resultSample}, nil
}
func (ba *BatchFeatureStatsAggregator) computeValues(header *bitflow.Header, samples []*bitflow.Sample) []bitflow.Value {
stats := steps.GetStats(header, samples)
res := make([]bitflow.Value, len(header.Fields))
for i, metricStats := range stats {
res[i] = bitflow.Value(ba.Aggregate(metricStats))
}
return res
}
func (ba *BatchFeatureStatsAggregator) String() string {
return "Batch aggregation: " + ba.Description
}
func RegisterBatchFeatureStatsAggregators(b reg.ProcessorRegistry) {
registerAggregator(b, "avg", NewBatchAvgAggregator)
registerAggregator(b, "stddev", NewBatchStddevAggregator)
registerAggregator(b, "kurtosis", NewBatchKurtosisAggregator)
registerAggregator(b, "variance", NewBatchVarianceAggregator)
registerAggregator(b, "min", NewBatchMinAggregator)
registerAggregator(b, "max", NewBatchMaxAggregator)
}
func NewBatchAvgAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Mean() },
Description: "avg",
}
}
func NewBatchStddevAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Stddev() },
Description: "stddev",
}
}
func NewBatchKurtosisAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Kurtosis() },
Description: "kurtosis",
}
}
func NewBatchVarianceAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Var() },
Description: "variance",
}
}
func NewBatchMinAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Min },
Description: "min",
}
}
func NewBatchMaxAggregator() bitflow.BatchProcessingStep {
return &BatchFeatureStatsAggregator{
Aggregate: func(stats steps.FeatureStats) float64 { return stats.Max },
Description: "max",
}
}