-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregator.go
213 lines (167 loc) · 5.62 KB
/
aggregator.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
package gandalff
import "fmt"
type aggregatorBuilder struct {
df BaseDataFrame
removeNAs bool
aggregators []aggregator
}
func (ab aggregatorBuilder) RemoveNAs(b bool) aggregatorBuilder {
ab.removeNAs = b
return ab
}
func (ab aggregatorBuilder) Run() DataFrame {
df := ab.df
if df.err != nil {
return ab.df
}
if len(ab.aggregators) == 0 {
return df
}
// CHECK: aggregators must have unique names and names must be valid
aggNames := make(map[string]bool)
for _, agg := range ab.aggregators {
// CASE: aggregator count has a default name
if agg.type_ != AGGREGATE_COUNT {
if aggNames[agg.name] {
df.err = fmt.Errorf("BaseDataFrame.Agg: aggregator names must be unique")
return df
}
aggNames[agg.name] = true
if df.__series(agg.name) == nil {
df.err = fmt.Errorf("BaseDataFrame.Agg: series \"%s\" not found", agg.name)
return df
}
}
}
var result DataFrame
if df.isGrouped {
var indeces [][]int
var flatIndeces []int
result, indeces, flatIndeces, _ = df.groupHelper()
groupsNum := len(indeces)
var series Series
for _, agg := range ab.aggregators {
series = df.__series(agg.name)
switch agg.type_ {
case AGGREGATE_COUNT:
counts := make([]int64, groupsNum)
for i, group := range indeces {
counts[i] = int64(len(group))
}
result = result.AddSeries(agg.newName, NewSeriesInt64(counts, nil, false, df.ctx))
case AGGREGATE_SUM:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_sum(dataF64, flatIndeces, groupsNum, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MIN:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_min(dataF64, flatIndeces, groupsNum, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MAX:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_max(dataF64, flatIndeces, groupsNum, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MEAN:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_mean(dataF64, flatIndeces, groupsNum, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_STD:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_std(dataF64, flatIndeces, groupsNum, ab.removeNAs), nil, false, df.ctx))
}
}
// var wg sync.WaitGroup
// wg.Add(THREADS_NUMBER)
// buffer := make(chan __stats_thread_data)
// for i := 0; i < THREADS_NUMBER; i++ {
// go __stats_worker(&wg, buffer)
// }
// for _, agg := range aggregators {
// series := df.__series(agg.name)
// resultData := make([]float64, len(*indeces))
// result = result.AddSeries(agg.name, NewSeriesFloat64(resultData, nil, false, df.ctx))
// for gi, group := range *indeces {
// buffer <- __stats_thread_data{
// op: agg.type_,
// gi: gi,
// indeces: group,
// series: series,
// res: resultData,
// }
// }
// }
// close(buffer)
// wg.Wait()
} else {
result = NewBaseDataFrame(df.ctx)
var series Series
for _, agg := range ab.aggregators {
series = df.__series(agg.name)
switch agg.type_ {
case AGGREGATE_COUNT:
result = result.AddSeries(agg.newName, NewSeriesInt64([]int64{int64(df.NRows())}, nil, false, df.ctx))
case AGGREGATE_SUM:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_sum(dataF64, nil, 1, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MIN:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_min(dataF64, nil, 1, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MAX:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_max(dataF64, nil, 1, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_MEAN:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_mean(dataF64, nil, 1, ab.removeNAs), nil, false, df.ctx))
case AGGREGATE_STD:
dataF64 := __gdl_stats_preprocess(series)
result = result.AddSeries(agg.newName, NewSeriesFloat64(__gdl_std(dataF64, nil, 1, ab.removeNAs), nil, false, df.ctx))
}
}
}
return result
}
type AggregateType int8
const (
AGGREGATE_COUNT AggregateType = iota
AGGREGATE_SUM
AGGREGATE_MEAN
AGGREGATE_MEDIAN
AGGREGATE_MIN
AGGREGATE_MAX
AGGREGATE_STD
)
const DEFAULT_COUNT_NAME = "n"
type aggregator struct {
name string
newName string
type_ AggregateType
}
func Count() aggregator {
return aggregator{DEFAULT_COUNT_NAME, DEFAULT_COUNT_NAME, AGGREGATE_COUNT}
}
func Sum(name string) aggregator {
return aggregator{name, fmt.Sprintf("sum(%s)", name), AGGREGATE_SUM}
}
func Mean(name string) aggregator {
return aggregator{name, fmt.Sprintf("mean(%s)", name), AGGREGATE_MEAN}
}
func Median(name string) aggregator {
return aggregator{name, fmt.Sprintf("median(%s)", name), AGGREGATE_MEDIAN}
}
func Min(name string) aggregator {
return aggregator{name, fmt.Sprintf("min(%s)", name), AGGREGATE_MIN}
}
func Max(name string) aggregator {
return aggregator{name, fmt.Sprintf("max(%s)", name), AGGREGATE_MAX}
}
func Std(name string) aggregator {
return aggregator{name, fmt.Sprintf("std(%s)", name), AGGREGATE_STD}
}
//////////////////////// SORT
type SortParam struct {
asc bool
name string
series Series
}
func Asc(name string) SortParam {
return SortParam{asc: true, name: name}
}
func Desc(name string) SortParam {
return SortParam{asc: false, name: name}
}