-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.go
286 lines (245 loc) · 6.37 KB
/
fields.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
package seafan
// fields.go implements structures/methods dealing with fields
import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strconv"
"time"
)
// FType represents a single field. It holds key information about the feature: its role, dimensions, summary info.
type FType struct {
Name string
Role FRole
Cats int
EmbCols int
Normalized bool
From string
FP *FParam
}
type FTypes []*FType
// DropFields will drop fields from the FTypes
func (fts FTypes) DropFields(dropFields ...string) FTypes {
ftOut := make(FTypes, 0)
for _, ft := range fts {
keep := true
for _, nm := range dropFields {
if ft.Name == nm {
keep = false
break
}
}
if keep {
ftOut = append(ftOut, ft)
}
}
return ftOut
}
// FParam -- field parameters -- is summary data about a field. These values may not be derived from the current
// data but are applied to the current data.
type FParam struct {
Location float64 `json:"location"` // location parameter for *Cts
Scale float64 `json:"scale"` // scale parameter for *Cts
Default any `json:"default"` // default level for *Dscrt
Lvl Levels `json:"lvl"` // map of values to int32 category for *Dscrt
}
// FRole is the role a feature plays
type FRole int
const (
FRCts FRole = 0 + iota
FRCat
FROneHot
FREmbed
FREither
)
//go:generate stringer -type=FRole
// Summary has descriptive statistics of a field using its current data.
type Summary struct {
NRows int // size of the data
DistrC *Desc // summary of continuous field
DistrD Levels // summary of discrete field
}
func (ft *FType) String() string {
str := fmt.Sprintf("Field %s\n", ft.Name)
switch ft.Role {
case FRCts:
str = fmt.Sprintf("%s\tcontinuous\n", str)
if ft.Normalized {
str = fmt.Sprintf("%s\tnormalized by:\n", str)
str = fmt.Sprintf("%s\tlocation\t%.2f\n", str, ft.FP.Location)
str = fmt.Sprintf("%s\tscale\t\t%.2f\n", str, ft.FP.Scale)
}
case FROneHot:
str = fmt.Sprintf("%s\tone-hot\n", str)
str = fmt.Sprintf("%s\tderived from feature %s\n", str, ft.From)
str = fmt.Sprintf("%s\tlength %d\n", str, ft.Cats)
case FREmbed:
str = fmt.Sprintf("%s\tembedding\n", str)
str = fmt.Sprintf("%s\tderived from feature %s\n", str, ft.From)
str = fmt.Sprintf("%s\tlength %d\n", str, ft.Cats)
str = fmt.Sprintf("%s\tembedding dimension of %d\n", str, ft.EmbCols)
}
return str
}
// Get returns the *FType of name
func (fts FTypes) Get(name string) *FType {
for _, f := range fts {
if f.Name == name {
return f
}
}
return nil
}
// fps is a json-friendly version of FParam
type fps struct {
Location float64 `json:"location"` // location parameter for *Cts
Scale float64 `json:"scale"` // scale parameter for *Cts
Default any `json:"default"` // default level for *Dscrt
Kind string `json:"kind"`
Lvl map[string]int32 `json:"lvl"`
}
// ftype is a json-friendly version of FType
type fType struct {
Name string
Role FRole
Cats int
EmbCols int
Normalized bool
From string
FP *fps
}
// Save saves FTypes to a json file--fileName
func (fts FTypes) Save(fileName string) (err error) {
f, err := os.Create(fileName)
if err != nil {
return
}
defer func() { _ = f.Close() }()
out := make([]fType, 0)
for _, ft := range fts {
fpStr := &fps{}
if ft.Role == FRCts || ft.Role == FRCat {
var dataType string
lvl := make(map[string]int32)
for k, v := range ft.FP.Lvl {
dataType = reflect.TypeOf(k).Kind().String()
kOut := fmt.Sprintf("%v", k)
if dataType == "struct" {
val, ok := k.(time.Time)
if !ok {
return Wrapper(ErrFields, fmt.Sprintf("(FTypes) Save: unexpect struct type, field %s", ft.Name))
}
dataType = "date"
kOut = val.Format(time.RFC3339)
}
lvl[kOut] = v
}
fpStr = &fps{Location: ft.FP.Location, Scale: ft.FP.Scale, Default: ft.FP.Default}
fpStr.Lvl = lvl
fpStr.Kind = dataType
}
ftype := fType{
Name: ft.Name,
Role: ft.Role,
Cats: ft.Cats,
EmbCols: ft.EmbCols,
Normalized: ft.Normalized,
From: ft.From,
FP: fpStr,
}
out = append(out, ftype)
}
jfp, err := json.MarshalIndent(out, "", " ")
if err != nil {
return
}
if _, err = f.WriteString(string(jfp)); err != nil {
return err
}
return err
}
// LoadFTypes loads a file created by the FTypes Save method
func LoadFTypes(fileName string) (fts FTypes, err error) {
fts = nil
f, err := os.Open(fileName)
if err != nil {
return
}
defer func() { _ = f.Close() }()
js, err := io.ReadAll(f)
if err != nil {
return
}
data := make([]fType, 0)
if e := json.Unmarshal(js, &data); e != nil {
return nil, e
}
fts = make(FTypes, 0)
for _, d := range data {
ft := FType{
Name: d.Name,
Role: d.Role,
Cats: d.Cats,
EmbCols: d.EmbCols,
Normalized: d.Normalized,
From: d.From,
FP: nil,
}
fp := FParam{Location: d.FP.Location, Scale: d.FP.Scale, Default: d.FP.Default}
switch d.FP.Kind {
case "string":
fp.Default = fmt.Sprintf("%v", d.FP.Default)
case "int32":
switch d.FP.Default.(type) {
case float64:
fp.Default = int32(d.FP.Default.(float64))
default:
fp.Default = nil
}
case "int64":
switch d.FP.Default.(type) {
case float64:
fp.Default = int64(d.FP.Default.(float64))
default:
fp.Default = nil
}
case "date":
val, e := time.Parse(time.RFC3339, fmt.Sprintf("%s", d.FP.Default))
if e != nil {
return nil, Wrapper(ErrFields, fmt.Sprintf("LoadTypes: cannot convert default value %v to date", d.FP.Default))
}
fp.Default = val
}
lvl := make(Levels)
for k, v := range d.FP.Lvl {
switch d.FP.Kind {
case "string":
lvl[k] = v
case "int32":
i, e := strconv.ParseInt(k, 10, 32)
if e != nil {
return nil, Wrapper(ErrFields, fmt.Sprintf("LoadFTypes: cannot convert %s to int32", k))
}
lvl[int32(i)] = v
case "int64":
i, e := strconv.ParseInt(k, 10, 64)
if e != nil {
return nil, Wrapper(ErrFields, fmt.Sprintf("LoadFTypes: cannot convert %s to int64", k))
}
lvl[i] = v
case "date":
dt, e := time.Parse(time.RFC3339, k)
if e != nil {
return nil, Wrapper(ErrFields, fmt.Sprintf("LoadFTypes: cannot convert %s to date", k))
}
lvl[dt] = v
}
}
fp.Lvl = lvl
ft.FP = &fp
fts = append(fts, &ft)
}
return fts, err
}