forked from johnkerl/miller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.go
386 lines (335 loc) · 10.1 KB
/
histogram.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
package transformers
import (
"container/list"
"fmt"
"os"
"strings"
"github.com/johnkerl/miller/pkg/cli"
"github.com/johnkerl/miller/pkg/lib"
"github.com/johnkerl/miller/pkg/mlrval"
"github.com/johnkerl/miller/pkg/types"
)
// ----------------------------------------------------------------
const verbNameHistogram = "histogram"
const histogramDefaultBinCount = int64(20)
var HistogramSetup = TransformerSetup{
Verb: verbNameHistogram,
UsageFunc: transformerHistogramUsage,
ParseCLIFunc: transformerHistogramParseCLI,
IgnoresInput: false,
}
func transformerHistogramUsage(
o *os.File,
) {
argv0 := "mlr"
verb := verbNameHistogram
fmt.Fprintf(o, "Just a histogram. Input values < lo or > hi are not counted.\n")
fmt.Fprintf(o, "Usage: %s %s [options]\n", argv0, verb)
fmt.Fprintf(o, "-f {a,b,c} Value-field names for histogram counts\n")
fmt.Fprintf(o, "--lo {lo} Histogram low value\n")
fmt.Fprintf(o, "--hi {hi} Histogram high value\n")
fmt.Fprintf(o, "--nbins {n} Number of histogram bins. Defaults to %d.\n", histogramDefaultBinCount)
fmt.Fprintf(o, "--auto Automatically computes limits, ignoring --lo and --hi.\n")
fmt.Fprintf(o, " Holds all values in memory before producing any output.\n")
fmt.Fprintf(o, "-o {prefix} Prefix for output field name. Default: no prefix.\n")
fmt.Fprintf(o, "-h|--help Show this message.\n")
}
func transformerHistogramParseCLI(
pargi *int,
argc int,
args []string,
_ *cli.TOptions,
doConstruct bool, // false for first pass of CLI-parse, true for second pass
) IRecordTransformer {
// Skip the verb name from the current spot in the mlr command line
argi := *pargi
verb := args[argi]
argi++
// Parse local flags
var valueFieldNames []string = nil
lo := 0.0
nbins := histogramDefaultBinCount
hi := 0.0
doAuto := false
outputPrefix := ""
for argi < argc /* variable increment: 1 or 2 depending on flag */ {
opt := args[argi]
if !strings.HasPrefix(opt, "-") {
break // No more flag options to process
}
if args[argi] == "--" {
break // All transformers must do this so main-flags can follow verb-flags
}
argi++
if opt == "-h" || opt == "--help" {
transformerHistogramUsage(os.Stdout)
os.Exit(0)
} else if opt == "-f" {
valueFieldNames = cli.VerbGetStringArrayArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "--lo" {
lo = cli.VerbGetFloatArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "--nbins" {
nbins = cli.VerbGetIntArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "--hi" {
hi = cli.VerbGetFloatArgOrDie(verb, opt, args, &argi, argc)
} else if opt == "--auto" {
doAuto = true
} else if opt == "-o" {
outputPrefix = cli.VerbGetStringArgOrDie(verb, opt, args, &argi, argc)
} else {
transformerHistogramUsage(os.Stderr)
os.Exit(1)
}
}
if valueFieldNames == nil {
transformerHistogramUsage(os.Stderr)
os.Exit(1)
}
if nbins <= 0 {
transformerHistogramUsage(os.Stderr)
os.Exit(1)
}
if lo == hi && !doAuto {
transformerHistogramUsage(os.Stderr)
os.Exit(1)
}
*pargi = argi
if !doConstruct { // All transformers must do this for main command-line parsing
return nil
}
transformer, err := NewTransformerHistogram(
valueFieldNames,
lo,
nbins,
hi,
doAuto,
outputPrefix,
)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return transformer
}
// ----------------------------------------------------------------
const histogramVectorInitialSize = 1024
type TransformerHistogram struct {
valueFieldNames []string
lo float64
nbins int64
hi float64
mul float64
countsByField map[string][]int64
vectorsByFieldName map[string][]float64 // For auto-mode
outputPrefix string
recordTransformerFunc RecordTransformerFunc
}
// ----------------------------------------------------------------
func NewTransformerHistogram(
valueFieldNames []string,
lo float64,
nbins int64,
hi float64,
doAuto bool,
outputPrefix string,
) (*TransformerHistogram, error) {
countsByField := make(map[string][]int64)
for _, valueFieldName := range valueFieldNames {
countsByField[valueFieldName] = make([]int64, nbins)
for i := int64(0); i < nbins; i++ {
countsByField[valueFieldName][i] = 0
}
}
tr := &TransformerHistogram{
valueFieldNames: valueFieldNames,
countsByField: countsByField,
outputPrefix: outputPrefix,
nbins: nbins,
}
if !doAuto {
tr.recordTransformerFunc = tr.transformNonAuto
tr.lo = lo
tr.hi = hi
tr.mul = float64(nbins) / (hi - lo)
} else {
tr.vectorsByFieldName = make(map[string][]float64)
for _, valueFieldName := range valueFieldNames {
tr.vectorsByFieldName[valueFieldName] = make([]float64, 0, histogramVectorInitialSize)
}
tr.recordTransformerFunc = tr.transformAuto
}
return tr, nil
}
// ----------------------------------------------------------------
func (tr *TransformerHistogram) Transform(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
HandleDefaultDownstreamDone(inputDownstreamDoneChannel, outputDownstreamDoneChannel)
tr.recordTransformerFunc(inrecAndContext, outputRecordsAndContexts, inputDownstreamDoneChannel, outputDownstreamDoneChannel)
}
// ----------------------------------------------------------------
func (tr *TransformerHistogram) transformNonAuto(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
tr.ingestNonAuto(inrecAndContext)
} else {
tr.emitNonAuto(&inrecAndContext.Context, outputRecordsAndContexts)
outputRecordsAndContexts.PushBack(inrecAndContext) // end-of-stream marker
}
}
func (tr *TransformerHistogram) ingestNonAuto(
inrecAndContext *types.RecordAndContext,
) {
inrec := inrecAndContext.Record
for _, valueFieldName := range tr.valueFieldNames {
stringValue := inrec.Get(valueFieldName)
if stringValue != nil {
floatValue, ok := stringValue.GetNumericToFloatValue()
if !ok {
fmt.Fprintf(
os.Stderr,
"%s %s: cannot parse \"%s\" as float.\n",
"mlr", verbNameHistogram, stringValue.String(),
)
os.Exit(1)
}
if (floatValue >= tr.lo) && (floatValue < tr.hi) {
idx := int((floatValue - tr.lo) * tr.mul)
tr.countsByField[valueFieldName][idx]++
} else if floatValue == tr.hi {
idx := tr.nbins - 1
tr.countsByField[valueFieldName][idx]++
}
}
}
}
func (tr *TransformerHistogram) emitNonAuto(
endOfStreamContext *types.Context,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
) {
countFieldNames := make(map[string]string)
for _, valueFieldName := range tr.valueFieldNames {
countFieldNames[valueFieldName] = tr.outputPrefix + valueFieldName + "_count"
}
for i := int64(0); i < tr.nbins; i++ {
outrec := mlrval.NewMlrmapAsRecord()
outrec.PutReference(
tr.outputPrefix+"bin_lo",
mlrval.FromFloat(tr.lo+float64(i)/tr.mul),
)
outrec.PutReference(
tr.outputPrefix+"bin_hi",
mlrval.FromFloat(tr.lo+float64(i+1)/tr.mul),
)
for _, valueFieldName := range tr.valueFieldNames {
outrec.PutReference(
countFieldNames[valueFieldName],
mlrval.FromInt(tr.countsByField[valueFieldName][i]),
)
}
outputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, endOfStreamContext))
}
}
// ----------------------------------------------------------------
func (tr *TransformerHistogram) transformAuto(
inrecAndContext *types.RecordAndContext,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
inputDownstreamDoneChannel <-chan bool,
outputDownstreamDoneChannel chan<- bool,
) {
if !inrecAndContext.EndOfStream {
tr.ingestAuto(inrecAndContext)
} else {
tr.emitAuto(&inrecAndContext.Context, outputRecordsAndContexts)
outputRecordsAndContexts.PushBack(inrecAndContext) // end-of-stream marker
}
}
func (tr *TransformerHistogram) ingestAuto(
inrecAndContext *types.RecordAndContext,
) {
inrec := inrecAndContext.Record
for _, valueFieldName := range tr.valueFieldNames {
mvalue := inrec.Get(valueFieldName)
if mvalue != nil {
value := mvalue.GetNumericToFloatValueOrDie()
tr.vectorsByFieldName[valueFieldName] = append(tr.vectorsByFieldName[valueFieldName], value)
}
}
}
func (tr *TransformerHistogram) emitAuto(
endOfStreamContext *types.Context,
outputRecordsAndContexts *list.List, // list of *types.RecordAndContext
) {
haveLoHi := false
lo := 0.0
hi := 1.0
nbins := tr.nbins
// Limits pass
for _, valueFieldName := range tr.valueFieldNames {
vector := tr.vectorsByFieldName[valueFieldName]
n := len(vector)
for i := 0; i < n; i++ {
value := vector[i]
if haveLoHi {
if lo > value {
lo = value
}
if hi < value {
hi = value
}
} else {
lo = value
hi = value
haveLoHi = true
}
}
}
// Binning pass
mul := float64(nbins) / (hi - lo)
for _, valueFieldName := range tr.valueFieldNames {
vector := tr.vectorsByFieldName[valueFieldName]
counts := tr.countsByField[valueFieldName]
lib.InternalCodingErrorIf(counts == nil)
n := len(vector)
for i := 0; i < n; i++ {
value := vector[i]
if (value >= lo) && (value < hi) {
idx := int(((value - lo) * mul))
counts[idx]++
} else if value == hi {
idx := nbins - 1
counts[idx]++
}
}
}
// Emission pass
countFieldNames := make(map[string]string)
for _, valueFieldName := range tr.valueFieldNames {
countFieldNames[valueFieldName] = tr.outputPrefix + valueFieldName + "_count"
}
for i := int64(0); i < nbins; i++ {
outrec := mlrval.NewMlrmapAsRecord()
outrec.PutReference(
tr.outputPrefix+"bin_lo",
mlrval.FromFloat(lo+(float64(i)/mul)),
)
outrec.PutReference(
tr.outputPrefix+"bin_hi",
mlrval.FromFloat(lo+(float64(i+1)/mul)),
)
for _, valueFieldName := range tr.valueFieldNames {
outrec.PutReference(
countFieldNames[valueFieldName],
mlrval.FromInt(tr.countsByField[valueFieldName][i]),
)
}
outputRecordsAndContexts.PushBack(types.NewRecordAndContext(outrec, endOfStreamContext))
}
}