/
segmentml.go
311 lines (278 loc) · 9.78 KB
/
segmentml.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
package lytics
import (
"fmt"
"time"
)
const (
segmentMLEndpoint = "segmentml/:id"
segmentMLListEndpoint = "segmentml"
segmentMLDepEndpoint = "segmentml/:id/_dependencies"
)
type SegmentML struct {
Name string `json:"name"`
State string `json:"state"`
Reason string `json:"reason"`
Created time.Time `json:"created"`
AuthorID string `json:"author_id"`
Conf struct {
Source struct {
ID string `json:"id"`
Updated time.Time `json:"updated"`
Created time.Time `json:"created"`
Aid int `json:"aid"`
AccountID string `json:"account_id"`
ShortID string `json:"short_id"`
Name string `json:"name"`
Kind string `json:"kind"`
IsPublic bool `json:"is_public"`
PublicName string `json:"public_name"`
SlugName string `json:"slug_name"`
Description string `json:"description"`
Table string `json:"table"`
AuthorID string `json:"author_id"`
Invalid bool `json:"invalid"`
InvalidReason string `json:"invalid_reason"`
Deleted bool `json:"deleted"`
DatemathCalc bool `json:"datemath_calc"`
ForwardDatemath bool `json:"forward_datemath"`
SaveHist bool `json:"save_hist"`
FieldUpdates bool `json:"field_updates"`
FieldChangesFields interface{} `json:"field_changes_fields"`
ScheduleExit bool `json:"schedule_exit"`
SegmentQl string `json:"segment_ql"`
Tags []string `json:"tags"`
Ast struct {
Ident string `json:"ident"`
} `json:"ast"`
Fields interface{} `json:"fields"`
} `json:"source"`
Target struct {
ID string `json:"id"`
Updated time.Time `json:"updated"`
Created time.Time `json:"created"`
Aid int `json:"aid"`
AccountID string `json:"account_id"`
ShortID string `json:"short_id"`
Name string `json:"name"`
Kind string `json:"kind"`
IsPublic bool `json:"is_public"`
SlugName string `json:"slug_name"`
Description string `json:"description"`
Table string `json:"table"`
AuthorID string `json:"author_id"`
Invalid bool `json:"invalid"`
InvalidReason string `json:"invalid_reason"`
Deleted bool `json:"deleted"`
DatemathCalc bool `json:"datemath_calc"`
ForwardDatemath bool `json:"forward_datemath"`
SaveHist bool `json:"save_hist"`
FieldUpdates bool `json:"field_updates"`
FieldChangesFields interface{} `json:"field_changes_fields"`
ScheduleExit bool `json:"schedule_exit"`
SegmentQl string `json:"segment_ql"`
Tags []string `json:"tags"`
Ast struct {
Op string `json:"op"`
Args []struct {
Op string `json:"op"`
Args []struct {
Ident string `json:"ident,omitempty"`
Val string `json:"val,omitempty"`
} `json:"args"`
} `json:"args"`
} `json:"ast"`
Fields []string `json:"fields"`
} `json:"target"`
Additional interface{} `json:"additional"`
Collections interface{} `json:"collections"`
CustomSegmentIds interface{} `json:"custom_segment_ids"`
Collect int `json:"collect"`
UseScores bool `json:"use_scores"`
UseContent bool `json:"use_content"`
BuildOnly bool `json:"build_only"`
AutoTune bool `json:"auto_tune"`
ModelName string `json:"model_name"`
ModelType string `json:"model_type"`
ReRun bool `json:"re_run"`
} `json:"conf"`
Features []Feature `json:"features"`
Summary struct {
Conf struct {
FalsePositive int `json:"FalsePositive"`
TruePositive int `json:"TruePositive"`
FalseNegative int `json:"FalseNegative"`
TrueNegative int `json:"TrueNegative"`
} `json:"conf"`
Mse float64 `json:"mse"`
Rsq float64 `json:"rsq"`
Success map[string]int `json:"success"`
Fail map[string]int `json:"fail"`
Auc float64 `json:"auc"`
Threshold float64 `json:"threshold"`
Accuracy int `json:"accuracy"`
Reach int `json:"reach"`
AudienceSimilarity float64 `json:"audience_similarity"`
ModelHealth string `json:"model_health"`
Msgs []struct {
Text string `json:"text"`
Tags []string `json:"tags"`
Severity string `json:"severity"`
} `json:"msgs"`
} `json:"summary"`
}
/*
type Feature struct {
Kind string `json:"kind, omitempty"`
Type string `json:"type, omitempty"`
Name string `json:"name, omitempty"`
Importance float64 `json:"importance, omitempty"`
Correlation float64 `json:"correlation, omitempty"`
Impact struct {
Lift float64 `json:"lift, omitempty"`
Threshold float64 `json:"threshold, omitempty"`
} `json:"impact, omitempty"`
}
type SegmentML struct {
Name string `json:"name"`
Features []Feature `json:"features,omitempty"`
Summary struct {
Mse float64 `json:"mse"`
Rsq float64 `json:"rsq"`
AUC float64 `json:"auc"`
Conf struct {
FalsePositive int `json:"FalsePositive,omitempty"`
TruePositive int `json:"TruePositive,omitempty"`
FalseNegative int `json:"FalseNegative,omitempty"`
TrueNegative int `json:"TrueNegative,omitempty"`
} `json:"Conf,omitempty"`
Fail map[string]int `json:"fail"`
Success map[string]int `json:"success"`
} `json:"summary,omitempty"`
Conf struct {
Source Segment `json:"source"`
Target Segment `json:"target"`
Additional []string `json:"additional"`
Collections []string `json:"collections"`
Collect int `json:"collect"`
UseScores bool `json:"use_scores"`
UseContent bool `json:"use_content"`
WriteToGcs bool `json:"write_to_gcs"`
} `json:"conf,omitempty"`
}
type Dependencies struct {
Fields map[string][][2]float64 "fields"
}
type Prediction struct {
Val string `json:"val"`
FailCt int `json:"fail_ct"`
SuccessCt int `json:"success_ct"`
}
*/
type Prediction struct {
Val string `json:"val"`
FailCt int `json:"fail_ct"`
SuccessCt int `json:"success_ct"`
}
type Dependencies struct {
Fields map[string][][2]float64 "fields"
}
type Feature struct {
Kind string `json:"kind"`
Type string `json:"type"`
Name string `json:"name"`
Importance float64 `json:"importance"`
FieldPrevalence struct {
Source float64 `json:"source"`
Target float64 `json:"target"`
} `json:"field_prevalence"`
Correlation float64 `json:"correlation"`
Impact struct {
Lift float64 `json:"lift"`
Threshold float64 `json:"threshold"`
} `json:"impact"`
}
// GetSegmentMLModel returns the details for a single segmentML Model based on id
// https://www.getlytics.com/developers/rest-api#segment-m-l
func (l *Client) GetSegmentMLModel(id string) (SegmentML, error) {
res := ApiResp{}
data := SegmentML{}
// make the request
err := l.Get(parseLyticsURL(segmentMLEndpoint, map[string]string{"id": id}), nil, nil, &res, &data)
if err != nil {
return SegmentML{}, err
}
return data, nil
}
// GetSegmentMLModels returns all models for the account
// https://www.getlytics.com/developers/rest-api#segment-m-l
func (l *Client) GetSegmentMLModels() ([]SegmentML, error) {
res := ApiResp{}
data := []SegmentML{}
// make the request
err := l.Get(segmentMLListEndpoint, nil, nil, &res, &data)
if err != nil {
return data, err
}
return data, nil
}
func (l *Client) GetSegmentMLDependencies(id string) (Dependencies, error) {
res := ApiResp{}
data := Dependencies{}
err := l.Get(parseLyticsURL(segmentMLDepEndpoint, map[string]string{"id": id}), nil, nil, &res, &data)
if err != nil {
return Dependencies{}, err
}
return data, nil
}
func (f Feature) Headers() []interface{} {
return []interface{}{
"Field_Name", "Field_Type", "Field_Kind", "Importance", "Correlation", "Impact-Lift", "Impact-Threshold",
}
}
func (f Feature) Row() []interface{} {
return []interface{}{
f.Name, f.Type, f.Kind, f.Importance, f.Correlation, f.Impact.Lift, f.Impact.Threshold,
}
}
func (s *SegmentML) Headers() []interface{} {
return []interface{}{
"Name", "Source", "Target", "MSE", "RSQ", "AUC", "False_Negative", "False_Positive", "True_Negative", "True_Positive",
}
}
func (s *SegmentML) Row() []interface{} {
sum := s.Summary
c := s.Conf
return []interface{}{
s.Name, c.Source.Name, c.Source.Name, sum.Mse, sum.Rsq, sum.Auc, sum.Conf.FalseNegative, sum.Conf.FalsePositive, sum.Conf.TrueNegative, sum.Conf.TruePositive,
}
}
func (p Prediction) Headers() []interface{} {
return []interface{}{
"Prediction", "Fail_Count", "Success_Count",
}
}
func (p Prediction) Row() []interface{} {
return []interface{}{
p.Val, p.FailCt, p.SuccessCt,
}
}
func (s *SegmentML) GetPredictions() []Prediction {
var predictions []Prediction
failPredMap := make(map[string]int)
for failX, failCt := range s.Summary.Fail {
failPredMap[failX] = failCt
}
successPredMap := make(map[string]int)
for successX, successCt := range s.Summary.Success {
successPredMap[successX] = successCt
}
for i := 0; i < 101; i++ {
xVal := fmt.Sprintf("%.2f", float64(i)/100)
sl := Prediction{}
sl.Val = xVal
sl.FailCt = failPredMap[xVal]
sl.SuccessCt = successPredMap[xVal]
predictions = append(predictions, sl)
}
return predictions
}