/
templatecode.go
166 lines (140 loc) 路 4.4 KB
/
templatecode.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
package main
const templateCode = `
// Code generated by go-featureprocessing DO NOT EDIT
package {{$.PackageName}}
import (
"sync"
fp "github.com/nikolaydubina/go-featureprocessing/transformers"
)
// {{$.StructName}}FeatureTransformer is a feature processor for {{$.StructName}}.
// It was automatically generated by go-featureprocessing tool.
type {{$.StructName}}FeatureTransformer struct {
{{range $i, $tr := $.Fields}}{{$tr.Name}} fp.{{$tr.Transformer}} ` + "`" + `json:"{{$tr.Name}}_{{$tr.TransformerTag}}"` + "`" + `
{{end}}
}
// Fit fits transformer for each field
func (e *{{$.StructName}}FeatureTransformer) Fit(s []{{$.StructName}}) {
if e == nil || len(s) == 0 {
return
}
{{if $.HasNumericalTransformers}}dataNum := make([]float64, len(s)){{end}}
{{if $.HasStringTransformers}}dataStr := make([]string, len(s)){{end}}
{{range $i, $tr := $.Fields}}
for i, v := range s {
{{if $tr.NumericalInput }}dataNum[i] = float64(v.{{$tr.Name}}){{else}}dataStr[i] = v.{{$tr.Name}}{{end}}
}
e.{{$tr.Name}}.Fit({{if $tr.NumericalInput }}dataNum{{else}}dataStr{{end}})
{{end}}
}
// Transform transforms struct into feature vector accordingly to transformers
func (e *{{$.StructName}}FeatureTransformer) Transform(s *{{$.StructName}}) []float64 {
if s == nil || e == nil {
return nil
}
features := make([]float64, e.NumFeatures())
e.TransformInplace(features, s)
return features
}
// TransformInplace transforms struct into feature vector accordingly to transformers, and does so inplace
func (e *{{$.StructName}}FeatureTransformer) TransformInplace(dst []float64, s *{{$.StructName}}) {
if s == nil || e == nil || len(dst) != e.NumFeatures() {
return
}
idx := 0
{{range $i, $tr := $.Fields}}
{{if $tr.Expanding }}e.{{$tr.Name}}.TransformInplace(dst[idx:idx + e.{{$tr.Name}}.NumFeatures()], s.{{$tr.Name}})
idx += e.{{$tr.Name}}.NumFeatures()
{{else}}dst[idx] = e.{{$tr.Name}}.Transform( {{if $tr.NumericalInput }}float64{{end}}( s.{{$tr.Name}} ))
idx++
{{end}}
{{end}}
}
// TransformAll transforms a slice of {{$.StructName}}
func (e *{{$.StructName}}FeatureTransformer) TransformAll(s []{{$.StructName}}) []float64 {
if e == nil {
return nil
}
features := make([]float64, len(s) * e.NumFeatures())
e.TransformAllInplace(features, s)
return features
}
// TransformAllInplace transforms a slice of {{$.StructName}} inplace
func (e *{{$.StructName}}FeatureTransformer) TransformAllInplace(dst []float64, s []{{$.StructName}}) {
if e == nil {
return
}
n := e.NumFeatures()
if len(dst) != n * len(s) {
return
}
for i := range s {
e.TransformInplace(dst[i * n: (i + 1) * n], &s[i])
}
}
// TransformAllParallel transforms a slice of {{$.StructName}} in parallel
func (e *{{$.StructName}}FeatureTransformer) TransformAllParallel(s []{{$.StructName}}, nworkers uint) []float64 {
if e == nil {
return nil
}
features := make([]float64, len(s) * e.NumFeatures())
e.TransformAllInplaceParallel(features, s, nworkers)
return features
}
// TransformAllInplaceParallel transforms a slice of {{$.StructName}} inplace parallel
// Useful for very large slices.
func (e *{{$.StructName}}FeatureTransformer) TransformAllInplaceParallel(dst []float64, s []{{$.StructName}}, nworkers uint) {
if e == nil || nworkers == 0 {
return
}
ns := uint(len(s))
nf := uint(e.NumFeatures())
if uint(len(dst)) != nf * ns {
return
}
nbatch := ns / nworkers
var wg sync.WaitGroup
for i := uint(0); i < nworkers; i++ {
wg.Add(1)
go func (i uint) {
defer wg.Done()
iStart := nbatch * i
iEnd := nbatch * (i + 1)
if i == (nworkers - 1) {
iEnd = ns
}
e.TransformAllInplace(dst[iStart * nf: iEnd * nf], s[iStart:iEnd])
} (i);
}
wg.Wait()
}
// NumFeatures returns number of features in output feature vector
func (e *{{$.StructName}}FeatureTransformer) NumFeatures() int {
if e == nil {
return 0
}
count := {{$.NumFieldsFlat}}
{{range $i, $tr := $.Fields}}{{if $tr.Expanding}}count += e.{{$tr.Name}}.NumFeatures(){{end}}
{{end}}
return count
}
// FeatureNames provides names of features that match output of transform
func (e *{{$.StructName}}FeatureTransformer) FeatureNames() []string {
if e == nil {
return nil
}
idx := 0
names := make([]string, e.NumFeatures())
{{range $i, $tr := $.Fields}}
{{if $tr.Expanding }}
for _, w := range e.{{$tr.Name}}.FeatureNames() {
names[idx] = "{{$tr.Name}}_" + w
idx++
}
{{else}}
names[idx] = "{{$tr.Name}}"
idx++
{{end}}
{{end}}
return names
}
`