forked from JHU-Winslow-Lab/hypoxemia-pred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHP_Opt.py
142 lines (122 loc) · 4.26 KB
/
HP_Opt.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
def get_time_series_smooth(ID,vitals):
#pull the patients data
vitals=vitals[vitals['patientunitstayid']==ID]
vitals=vitals[['observationoffset','sao2']]
vitals=vitals.fillna(method='pad')
vitals=vitals.dropna()
vital_times=np.array(vitals['observationoffset'])
#smooth the necessary signals
#from scipy.signal import savgol_filter
sao2=vitals['sao2']
sao2_transformed=1-np.exp((sao2-100)/10)
sao2_smooth = np.concatenate((sao2_transformed[0:4],np.convolve(sao2_transformed, np.ones(5)/5,mode='valid')))
#the patient time series
#SaO2 only
pt_series=np.transpose(np.vstack(sao2_smooth))
return pt_series,vital_times
def time_data30(pt_series,vital_times):
ys=[]
xs=[]
t=[]
ts=2
inputshape=0
#for SaO2 model only
for i in range(0,len(pt_series[0])-(ts+1)):
y=pt_series[0][i+ts]
t.append(vital_times[i+ts])
#all
x=pt_series[0][i:i+ts]
ys.append(y)
xs.append(x)
ys=np.transpose(ys)
n=0
#downsampling
d=6
xs=xs[::d]
ys=ys[::d]
t=t[::d]
return xs,ys,ts,t,n
def time_data5(pt_series,vital_times):
ys=[]
xs=[]
t=[]
ts=2
inputshape=0
#for SaO2 model only
for i in range(0,len(pt_series[0])-(ts+1)):
y=pt_series[0][i+ts]
t.append(vital_times[i+ts])
#all
x=pt_series[0][i:i+ts]
ys.append(y)
xs.append(x)
ys=np.transpose(ys)
n=0
return xs,ys,ts,t,n
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasRegressor
def create_model(lr,Deep):
lstm=tf.keras.Sequential()
inputshape=1
lstm.add(tf.keras.layers.BatchNormalization(input_shape=(ts,inputshape)))
lstm.add(tf.keras.layers.LSTM(256,return_sequences=True,input_shape=(ts,inputshape)))
if Deep==1:
lstm.add(tf.keras.layers.Dropout(.1))
lstm.add(tf.keras.layers.LSTM(128,return_sequences=True))
#
lstm.add(tf.keras.layers.Dropout(.1))
lstm.add(tf.keras.layers.LSTM(64,return_sequences=True))
#
lstm.add(tf.keras.layers.Dropout(.1))
lstm.add(tf.keras.layers.LSTM(32,return_sequences=True))
lstm.add(tf.keras.layers.Dropout(.1))
lstm.add(tf.keras.layers.LSTM(16,return_sequences=False))
lstm.add(tf.keras.layers.Dense(units=1))
opt = keras.optimizers.Adam(learning_rate=lr)
lstm.compile(loss='mse',optimizer=opt)
return lstm
import pandas as pd
import numpy as np
vitals=pd.read_csv('vitals.csv')
TrainIDs=pd.read_csv('TrainEICU.csv',header=None)
TrainIDs=np.array(TrainIDs[0].astype(int))
VentTest=pd.read_csv('TestVent.csv',header=None)
VentTest=np.array(VentTest[0].astype(int))
NoVentTest=pd.read_csv('Test_NoVent.csv',header=None)
NoVentTest=np.array(NoVentTest[0].astype(int))
ID = TrainIDs[0]
pt_series,vital_times=get_time_series_smooth(ID,vitals)
#Have to change which time_data fxn to use depending on 30 min vs. 5 min
full_x_train,full_y_train,ts,t,n = time_data30(pt_series,vital_times)
True_TrainIDs=[]
for r in range(1,len(TrainIDs)):
ID = TrainIDs[r]
v=vitals[vitals['patientunitstayid']==ID]
if len(v)>60:
if ~(np.sum(v.isna()['sao2'])==len(v)):
True_TrainIDs.append(ID)
#need to pass ID, vents, vitals
pt_series,vital_times=get_time_series_smooth(ID,vitals)
xtrain,ytrain,ts,t,n = time_data30(pt_series,vital_times)
full_x_train=np.vstack((full_x_train,xtrain))
full_y_train=np.hstack((full_y_train,ytrain))
s=np.shape(full_x_train)[0]
xtrain=full_x_train.reshape(s,2,1)
model = KerasRegressor(build_fn=create_model, epochs=100,verbose=2)
# define the grid search parameters
lr=[.001,.01,.1]
Deep=[1,0]
param_grid = dict(lr=lr,Deep=Deep)
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=3,verbose=4)
grid_result = grid.fit(xtrain,full_y_train)
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
#Change names to run for 5 minute model
import joblib
joblib.dump(grid_result.best_params_, '30min_CV_bestparams.pkl')
joblib.dump(grid_result.best_score_, '30min_CV_bestscore.pkl')
joblib.dump(grid_result.cv_results_, '30min_CV_results.pkl')