-
Notifications
You must be signed in to change notification settings - Fork 158
/
encoder.go
187 lines (148 loc) · 4.49 KB
/
encoder.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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017-2020 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package encoder
import (
"log"
"math"
"strconv"
"sync"
"github.com/davecgh/go-spew/spew"
)
// ValueEncoder handles online encoding of incoming data and keeps the required state for each feature.
type ValueEncoder struct {
// columnSummaries
columns map[string]*ColumnSummary
// configuration
conf *Config
sync.Mutex
}
// array of all registered encoders
var encoders []*ValueEncoder
// LoadValueEncoders loads all value encoders from disk.
func LoadValueEncoders() {
// TODO:
}
// StoreValueEncoders stores all value encoders on disk.
func StoreValueEncoders() {
// TODO:
}
// SetConfig will set the config for all registered encoders.
func SetConfig(c *Config) {
for _, m := range encoders {
m.conf = c
}
}
// NewValueEncoder returns a new encoding manager instance.
func NewValueEncoder() *ValueEncoder {
man := &ValueEncoder{
columns: map[string]*ColumnSummary{},
}
encoders = append(encoders, man)
return man
}
// Config holds configuration parameters.
type Config struct {
// use zscore for normalization
ZScore bool
// use minmax for normalization
MinMax bool
// normalize the categorical values after encoding them to numeric format
NormalizeCategoricals bool
}
// String handles encoding of categorical values according to the ValueEncoder configuration.
func (m *ValueEncoder) String(field string, val string) string {
sum := m.GetSummary(TypeString, field)
m.Lock()
i, ok := sum.UniqueStrings[val]
if !ok {
sum.UniqueStrings[val] = sum.Index
i = sum.Index
sum.Index++
}
m.Unlock()
if m.conf.NormalizeCategoricals {
if m.conf.ZScore {
return ZScore(i, sum)
}
if m.conf.MinMax {
return MinMax(i, sum)
}
}
return strconv.FormatFloat(i, 'f', precision, 64)
}
// Int handles encoding of integer values according to the ValueEncoder configuration.
func (m *ValueEncoder) Int(field string, val int) string {
return m.Float64(field, float64(val))
}
// Int64 handles encoding of 64bit integer values according to the ValueEncoder configuration.
func (m *ValueEncoder) Int64(field string, val int64) string {
return m.Float64(field, float64(val))
}
// Int32 handles encoding of 32bit integer values according to the ValueEncoder configuration.
func (m *ValueEncoder) Int32(field string, val int32) string {
return m.Float64(field, float64(val))
}
// Uint32 handles encoding of unsigned 32bit integer values according to the ValueEncoder configuration.
func (m *ValueEncoder) Uint32(field string, val uint32) string {
return m.Float64(field, float64(val))
}
// Uint64 handles encoding of unsigned 64bit integer values according to the ValueEncoder configuration.
func (m *ValueEncoder) Uint64(field string, val uint64) string {
return m.Float64(field, float64(val))
}
const (
valueTrue = "1.0000000000"
valueFalse = "0.0000000000"
)
// Bool handles encoding of boolean values to numeric format.
func (m *ValueEncoder) Bool(b bool) string {
if b {
return valueTrue
}
return valueFalse
}
// Float64 handles encoding of 64bit float values according to the ValueEncoder configuration.
func (m *ValueEncoder) Float64(field string, val float64) string {
var (
result string
sum = m.GetSummary(TypeNumeric, field)
)
switch {
case m.conf.ZScore:
result = ZScore(val, sum)
case m.conf.MinMax:
result = MinMax(val, sum)
default:
spew.Dump(m.conf)
log.Fatal("invalid config: no normalization strategy")
}
return result
}
// GetSummary returns the summary for the given column type and field name.
// It will create a new one if none is being tracked yet.
func (m *ValueEncoder) GetSummary(colType ColumnType, field string) *ColumnSummary {
// get column summary for current field
m.Lock()
sum, ok := m.columns[field]
if !ok {
sum = &ColumnSummary{
Col: field,
Typ: colType,
UniqueStrings: map[string]float64{},
Min: math.MaxFloat64,
}
m.columns[field] = sum
}
m.Unlock()
return sum
}