/
histogram_sets.go
127 lines (112 loc) · 3.18 KB
/
histogram_sets.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
package histogram
import (
"strings"
"github.com/grokify/mogo/type/maputil"
)
type HistogramSets struct {
Name string
HistogramSetMap map[string]*HistogramSet
}
func NewHistogramSets(name string) *HistogramSets {
return &HistogramSets{
Name: name,
HistogramSetMap: map[string]*HistogramSet{}}
}
func (hsets *HistogramSets) Add(hsetName, histName, binName string, binCount int, trimSpace bool) {
if trimSpace {
hsetName = strings.TrimSpace(hsetName)
histName = strings.TrimSpace(histName)
binName = strings.TrimSpace(binName)
}
hset, ok := hsets.HistogramSetMap[hsetName]
if !ok {
hset = NewHistogramSet(hsetName)
}
hset.Add(histName, binName, binCount)
hsets.HistogramSetMap[hsetName] = hset
}
func (hsets *HistogramSets) Flatten(name string) *HistogramSet {
hsetFlat := NewHistogramSet(name)
for _, hset := range hsets.HistogramSetMap {
for histName, hist := range hset.HistogramMap {
for binName, binCount := range hist.Bins {
hsetFlat.Add(histName, binName, binCount)
}
}
}
return hsetFlat
}
func (hsets *HistogramSets) BinNames() []string {
binNamesMap := map[string]int{}
hsets.Visit(func(hsetName, histName, binName string, binCount int) {
binNamesMap[binName] = 1
})
return maputil.Keys(binNamesMap)
}
func (hsets *HistogramSets) Sum() int {
sum := 0
for _, hset := range hsets.HistogramSetMap {
for _, hist := range hset.HistogramMap {
for _, binSum := range hist.Bins {
sum += binSum
}
}
}
return sum
}
func (hsets *HistogramSets) BinSumsByHset() *Histogram {
sums := NewHistogram("Bin Sums")
for hsetName, hset := range hsets.HistogramSetMap {
for _, hist := range hset.HistogramMap {
for _, binVal := range hist.Bins {
sums.Add(hsetName, binVal)
}
}
}
return sums
}
func (hsets *HistogramSets) Counts() *HistogramSetsCounts {
return NewHistogramSetsCounts(*hsets)
}
// ItemCount returns the number of histogram sets.
func (hsets *HistogramSets) ItemCount() uint {
return uint(len(hsets.HistogramSetMap))
}
func (hsets *HistogramSets) ItemNames() []string {
return maputil.Keys(hsets.HistogramSetMap)
}
func (hsets *HistogramSets) Map() map[string]map[string]map[string]int {
out := map[string]map[string]map[string]int{}
for hsetName, hset := range hsets.HistogramSetMap {
if _, ok := out[hsetName]; !ok {
out[hsetName] = map[string]map[string]int{}
}
for histName, hist := range hset.HistogramMap {
if _, ok := out[histName]; !ok {
out[histName] = map[string]map[string]int{}
}
for binName, binCount := range hist.Bins {
out[hsetName][histName][binName] += binCount
}
}
}
return out
}
func (hsets *HistogramSets) MapAdd(m map[string]map[string]map[string]int, trimSpace bool) {
for hsetName, hsetMap := range m {
for histName, histMap := range hsetMap {
for binName, binCount := range histMap {
hsets.Add(hsetName, histName, binName, binCount, trimSpace)
}
}
}
}
func (hsets *HistogramSets) Visit(visit func(hsetName, histName, binName string, binCount int)) {
for hsetName, hset := range hsets.HistogramSetMap {
for histName, hist := range hset.HistogramMap {
for binName, binCount := range hist.Bins {
visit(hsetName, histName, binName, binCount)
}
}
}
}