-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathfitting_models.py
363 lines (331 loc) · 13.3 KB
/
fitting_models.py
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from scipy.optimize import minimize;
import numpy as np;
from scipy import stats
import itertools;
import analysis;
class BaseModel:
def __init__(self):
self.p = None;
def set(self,p):
self.p = p;
class SexAgeModel(BaseModel):
def __init__(self):
self.p = np.array([[4.0,3,10.6,12,75,181],\
[3.0,8,7.0,30,53,144]]);
#fitted: 0.03737 in train
def fit(self, info, train_true):
print("use fitted values, no fitting")
def predict(self,info):
res = {};
for idx,row in info.iterrows():
case,age,sex = row['Id'],row['age'],row['sex'];
p = self.p;
if sex=='M':
if age<15:
hS = [p[0][0]*age+p[0][1],15];
hD = [p[0][2]*age+p[0][3], 35];
else:
hS = [p[0][4],35];
hD = [p[0][5],45];
elif sex=='F':
if age<15:
hS = [p[1][0]*age+p[1][1],15];
hD = [p[1][2]*age+p[1][3],35];
else:
hS = [p[1][4],35];
hD = [p[1][5],40];
else:
print("!!!no such sex type!");
hS = [p[1][4],35];
hD = [p[1][5],45];
res[case] = np.asarray(hS + hD);
return res;
class OneSliceModel(BaseModel):
def __init__(self):
self.p = np.array([5,0.00115,10,0.00124,0.080,6,0.075,7]);
#fitted on train, score = 0.01519
def fit(self, areas_all, train_true):
print("not implemented yet, use default to fit")
def predict(self, areas_all):
#take the area_data as input
#return sys_vol, sys_err, dias_vol, dias_error
res = {};
p = self.p;
for case,areas in areas_all.iteritems():
x = np.sum(areas[:,1:],axis=0);
tsys,tdias = np.argmin(x),np.argmax(x);
a = areas[:,tdias+1];
if np.sum(a>100) <= 2:
dias_v = np.nan;
sys_v = np.nan;
else:
da = np.percentile(a,80);
dias_v = np.clip(p[2] + p[3]*da**1.5,5,580);
a = areas[:,tsys+1];
if np.sum(a>100) <= 2:
sys_v = np.nan;
else:
sa = np.percentile(a,80);
sys_v = np.clip(p[0] + p[1]*(10+sa)*(da**0.5+sa**0.5)/2,5,580);
sys_err = np.clip(sys_v * p[4] + p[5],0,30);
dias_err = np.clip(dias_v * p[6] + p[7],0,30);
res[case] = np.asarray([sys_v, sys_err, dias_v, dias_err]);
return res;
class SaxModel(BaseModel):
def __init__(self,version=1):
self.version = version;
if version == 1:
self.p0 = [1.05,1.05,0.05,4];
self.bounds = [(0.8,1.5),(0.8,1.3),(0.03,0.07),(0,10)];
elif version == 2:
self.p0 = [1.0,1.0,0.05,4,0.05,4];
self.bounds = [(-0.5,1.8),(-0.5,1.5),(0.03,0.10),(0,10),(0.03,0.10),(0,10)];
elif version == 3:
self.p0 = [1.05,0, 1.05, 0, 0.05,4, 0.05, 4];
self.bounds = [(0.8,1.5),(0,30), (0.8,1.3),(0,50),(0.03,0.10),(0,10), (0.03,0.10),(0,10)];
self.p = None;
def _get_result(self,X,p):#X a single column vector of sys and dias volume
CLIP = 25;
Y = np.zeros((X.shape[0],2));
if self.version == 1:
Y[::2,0] = X[::2]*p[0];
Y[1::2,0] = X[1::2]*p[1];
Y[:,1] = np.clip(Y[:,0]*p[2]+p[3], 0, CLIP);
elif self.version == 2:
Y[::2,0] = X[::2] - np.sqrt(X[::2])*p[0];
Y[1::2,0] = X[1::2] - np.sqrt(X[1::2])*p[1];
Y[::2,1] = np.clip(Y[::2,0]*p[2]+p[3], 0, CLIP);
Y[1::2,1] = np.clip(Y[1::2,0]*p[4]+p[5], 0, CLIP);
elif self.version == 3:
Y[::2,0] = X[::2]*p[0] + p[1];
Y[1::2,0] = X[1::2]*p[2] + p[3];
Y[::2,1] = np.clip(Y[::2,0]*p[4]+p[5], 0, CILP);
Y[1::2,1] = np.clip(Y[1::2,0]*p[6]+p[7], 0, CLIP);
return Y;
def fit(self, results, train_true):
x = [];
y = [];
count = 0;
missing = [];
for idx,row in train_true.iterrows():
res = results.get(row['Id']);
if res is None:
missing.append(row['Id']);
continue
count+=1;
x.extend(res);
y.extend([row['Systole'],row['Diastole']]);
print("{} cases are used to fit the model".format(count));
if len(missing)>0:
print("cases are missing: " + ','.join([str(m_) for m_ in missing]));
x = np.asarray(x);
y = np.asarray(y);
ff = minimize(lambda p:analysis.crps_score(self._get_result(x,p),y), self.p0, bounds=self.bounds, options={'gtol':1e-5,'maxiter':500,'eps':1e-5});
self.p = ff.x;
print("fitting parameters " + str(self.p));
print("fitting score " + str(ff.fun));
def predict(self,results):
res = {};
if self.p is None:
print("need to fit the model first");
for case,sd in results.iteritems():
res[case] = self._get_result(np.asarray(sd),self.p).flatten();
return res;
class Ch4Model(BaseModel):
def __init__(self):
self.p0 = [.8,10,.3,.9,.09,4];
self.bounds = [(.6,.98),(0,20),(.2,0.7),(0.6,0.98),(.03,.2),(0,10)];
self.p = None;
def _get_result(self,X,p):#X a single column vector of sys and dias volume
Y = np.zeros((X.shape[0],2));
Y[1::2,0] = np.clip(X[1::2]*p[0]+p[1],4,580);
Y[::2,0] = np.clip(np.maximum(Y[1::2,0]*p[2], X[::2]*p[3]),4,580);
Y[:,1] = np.clip(Y[:,0]*p[4]+p[5], 0, 35);
dele = np.array([[i*2,i*2+1] for i in range(X.shape[0]/2) if X[i*2+1]<40]).reshape((-1))
if len(dele) > 0:
Y[dele]=np.nan
return Y;
def fit(self, results, train_true):
x = [];
y = [];
count = 0;
missing = [];
for idx,row in train_true.iterrows():
res = results.get(row['Id']);
if res is None or res[1] < 40:
missing.append(row['Id']);
continue
count+=1;
x.extend(res);
y.extend([row['Systole'],row['Diastole']]);
print("{} cases are used to fit the model".format(count));
if len(missing)>0:
print("cases are missing in train: " + ','.join([str(int(m)) for m in missing]));
x = np.asarray(x);
y = np.asarray(y);
ff = minimize(lambda p:analysis.crps_score(self._get_result(x,p),y), self.p0, bounds=self.bounds, options={'gtol':1e-5,'maxiter':500,'eps':1e-3});
self.p = ff.x;
print("fitting parameters " + str(self.p));
print("fitting score " + str(ff.fun));
def predict(self,results):
res = {};
if self.p is None:
print("need to fit the model first");
for case,sd in results.iteritems():
res[case] = self._get_result(np.asarray(sd),self.p).flatten();
return res;
class AverageModel(BaseModel):
def __init__(self,ll=9.5e-5):
self.p = None;
self.ll = ll;
def _get_result(self,X,p):
"""
how to deal with nans???
this code treat them as missing use the same coefficients
ideally, it should fit another model use only the rest of models
"""
NR = X.shape[0];
y = np.zeros((NR,2));
p = np.asarray(p);
for i in range(NR):
preds = np.copy(X[i]).reshape((-1,2));
err0 = np.copy(preds[:,1]);
preds[:,1] = err0*p;
preds = preds[~np.isnan(preds[:,0])];
if preds.shape[0]==0:
y[i] = [np.nan,np.nan];
continue;
me = np.sum(preds[:,0]/preds[:,1]**2);
err = np.sum(1.0/preds[:,1]**2);
me /= err;
err = 1.0/np.sqrt(err);
err = np.minimum(np.nanmin(err0),err);
err *=(1.0 + np.std(preds[:,0])/np.max(preds[:,1])/3)**0.5;
y[i] = [me,err];
return y;
def fit(self,preds,train_true):
N = len(preds);
print("combine # predictions:" + ','.join([str(len(x)) for x in preds]));
self.p0 = np.ones(N)*np.sqrt(N);
X = np.zeros((train_true.shape[0]*2,N*2));
X[:] = np.nan;
y = [];
i = 0;
for idx,row in train_true.iterrows():
case = row['Id'];
y.extend([row['Systole'],row['Diastole']]);
for j in range(N):
sede = preds[j].get(case);
if sede is not None:
X[i*2,2*j:2*j+2] = sede[0:2];
X[i*2+1,2*j:2*j+2] = sede[2:4];
i += 1;
y = np.asarray(y);
print("init score :{}".format(analysis.crps_score(self._get_result(X,self.p0),y)));
ff = minimize(lambda p:analysis.crps_score(self._get_result(X,p),y) + self.ll*np.var(p), self.p0, options={'gtol':1e-5,'eps':1e-4,'maxiter':500});
self.p = ff.x;
print("fitting parameters " + str(self.p));
print("fitting score " + str(ff.fun));
def predict(self,preds):
print("combine # predictions:" + ','.join([str(len(x)) for x in preds]));
res = {};
css = [list(x.keys()) for x in preds];
css = set(list(itertools.chain.from_iterable(css)));
N = len(preds);
assert(N == self.p.size);
for case in css:
X = np.zeros((2,2*N));
X[:] = np.nan;
for j in range(N):
sede = preds[j].get(case);
if sede is not None:
X[0,2*j:2*j+2] = sede[0:2];
X[1,2*j:2*j+2] = sede[2:4];
res[case] = self._get_result(X,self.p).flatten();
return res;
class SaxFilterModel(BaseModel):
def __init__(self):
self.p0 = [1.0,1.0,0.05,4,0.05,4];
self.bounds = [(-0.5,1.8),(-0.5,1.5),(0.03,0.10),(0,10),(0.03,0.10),(0,10)];
self.p = None;
def _get_result(self,X,p):#X a single column vector of sys and dias volume
Y = np.zeros((X.shape[0],2));
idx = X[:,1]>1;
ridx = np.logical_not(idx);
Y[idx,0] = X[idx,0] - np.sqrt(X[idx,0])*p[0];
Y[ridx,0] = X[ridx,0] - np.sqrt(X[ridx,0])*p[1];
Y[idx,1] = np.clip(Y[idx,0]*p[2]+p[3],0,25);
Y[ridx,1] = np.clip(Y[ridx,0]*p[4]+p[5],0,25);
return Y;
def fit(self, results,train_true):
x = [];
y = [];
count = 0;
missing = [];
for idx,row in train_true.iterrows():
res = results.get(row['Id']);
if res is None:
missing.append(row['Id']);
continue
count+=1;
x.extend(res);
y.extend([row['Systole'],row['Diastole']]);
print("{} cases are used to fit the model".format(count));
if len(missing)>0:
print("cases are missing: " + ','.join([str(_x) for _x in missing]));
x = np.asarray(x).reshape((-1,2));
y = np.asarray(y);
ff = minimize(lambda p:analysis.crps_score(self._get_result(x,p),y), self.p0, bounds=self.bounds, options={'gtol':1e-5,'maxiter':500,'eps':1e-5});
self.p = ff.x;
print("fitting parameters " + str(self.p));
print("fitting score " + str(ff.fun));
def predict(self,results):
res = {};
if self.p is None:
print("need to fit the model first");
for case,sd in results.iteritems():
res[case] = self._get_result(np.asarray(sd).reshape(-1,2),self.p).flatten();
return res;
class SaxFeatureModel(BaseModel):
def __init__(self):
self.p0 = [0.2,-0.2,0.9, 0.5,-0.5,0.5,4];
self.bounds = [(-0.5,0.5),(-0.5,0.5),(0.0,2.0),\
(-3.0,3.0),(-3.0,3.0),(-3.0,3.0),(2,10)];
self.p = None;
def _get_result(self,X,p):#X a single column vector of sys and dias volume
Y = np.zeros((X.shape[0],2));
e1 = (X[:,1]>1)*1.0;
e2 = (X[:,2]<=7)*1.0;
e3 = (X[:,3]>1.3)*1.0;
Y[:,0] = X[:,0] - np.sqrt(X[:,0])*(p[0]*e1+p[1]*e2+p[2])
Y[:,1] = np.clip(X[:,0]*(p[3]*e1+p[4]*e2+p[5]*e3+p[6])/100+4,4,30);
return Y;
def fit(self, results,train_true):
x = [];
y = [];
count = 0;
missing = [];
for idx,row in train_true.iterrows():
res = results.get(row['Id']);
if res is None:
missing.append(row['Id']);
continue
count+=1;
x.extend(res);
y.extend([row['Systole'],row['Diastole']]);
print("{} cases are used to fit the model".format(count));
if len(missing)>0:
print("cases are missing: " + ','.join([str(_x) for _x in missing]));
x = np.asarray(x).reshape((-1,4));
y = np.asarray(y);
ff = minimize(lambda p:analysis.crps_score(self._get_result(x,p),y), self.p0, bounds=self.bounds, options={'gtol':1e-6,'maxiter':500,'eps':1e-5});
self.p = ff.x;
print("fitting parameters " + str(self.p));
print("fitting score " + str(ff.fun));
def predict(self,results):
res = {};
if self.p is None:
print("need to fit the model first");
for case,sd in results.iteritems():
res[case] = self._get_result(np.asarray(sd).reshape(-1,4),self.p).flatten();
return res;