forked from chl8856/Dynamic-DeepHit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
326 lines (223 loc) · 11.3 KB
/
main.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
_EPSILON = 1e-08
import numpy as np
import pandas as pd
import tensorflow as tf
import random
import os
from sklearn.model_selection import train_test_split
import import_data as impt
from class_DeepLongitudinal import Model_Longitudinal_Attention
from utils_eval import c_index, brier_score
from utils_log import save_logging, load_logging
from utils_helper import f_get_minibatch, f_get_boosted_trainset
# In[ ]:
def _f_get_pred(sess, model, data, data_mi, pred_horizon):
'''
predictions based on the prediction time.
create new_data and new_mask2 that are available previous or equal to the prediction time (no future measurements are used)
'''
new_data = np.zeros(np.shape(data))
new_data_mi = np.zeros(np.shape(data_mi))
meas_time = np.concatenate([np.zeros([np.shape(data)[0], 1]), np.cumsum(data[:, :, 0], axis=1)[:, :-1]], axis=1)
for i in range(np.shape(data)[0]):
last_meas = np.sum(meas_time[i, :] <= pred_horizon)
new_data[i, :last_meas, :] = data[i, :last_meas, :]
new_data_mi[i, :last_meas, :] = data_mi[i, :last_meas, :]
return model.predict(new_data, new_data_mi)
def f_get_risk_predictions(sess, model, data_, data_mi_, pred_time, eval_time):
pred = _f_get_pred(sess, model, data_[[0]], data_mi_[[0]], 0)
_, num_Event, num_Category = np.shape(pred)
risk_all = {}
for k in range(num_Event):
risk_all[k] = np.zeros([np.shape(data_)[0], len(pred_time), len(eval_time)])
for p, p_time in enumerate(pred_time):
### PREDICTION
pred_horizon = int(p_time)
pred = _f_get_pred(sess, model, data_, data_mi_, pred_horizon)
for t, t_time in enumerate(eval_time):
eval_horizon = int(t_time) + pred_horizon #if eval_horizon >= num_Category, output the maximum...
# calculate F(t | x, Y, t >= t_M) = \sum_{t_M <= \tau < t} P(\tau | x, Y, \tau > t_M)
risk = np.sum(pred[:,:,pred_horizon:(eval_horizon+1)], axis=2) #risk score until eval_time
risk = risk / (np.sum(np.sum(pred[:,:,pred_horizon:], axis=2), axis=1, keepdims=True) +_EPSILON) #conditioniong on t > t_pred
for k in range(num_Event):
risk_all[k][:, p, t] = risk[:, k]
return risk_all
# ### 1. Import Dataset
# ##### - Users must prepare dataset in csv format and modify 'import_data.py' following our examplar 'PBC2'
# In[ ]:
data_mode = 'PBC2'
seed = 1234
##### IMPORT DATASET
'''
num_Category = max event/censoring time * 1.2
num_Event = number of evetns i.e. len(np.unique(label))-1
max_length = maximum number of measurements
x_dim = data dimension including delta (1 + num_features)
x_dim_cont = dim of continuous features
x_dim_bin = dim of binary features
mask1, mask2, mask3 = used for cause-specific network (FCNet structure)
'''
if data_mode == 'PBC2':
(x_dim, x_dim_cont, x_dim_bin), (data, time, label), (mask1, mask2, mask3), (data_mi) = impt.import_dataset(norm_mode = 'standard')
# This must be changed depending on the datasets, prediction/evaliation times of interest
pred_time = [52, 3*52, 5*52] # prediction time (in months)
eval_time = [12, 36, 60, 120] # months evaluation time (for C-index and Brier-Score)
else:
print ('ERROR: DATA_MODE NOT FOUND !!!')
_, num_Event, num_Category = np.shape(mask1) # dim of mask3: [subj, Num_Event, Num_Category]
max_length = np.shape(data)[1]
file_path = '{}'.format(data_mode)
if not os.path.exists(file_path):
os.makedirs(file_path)
# ### 2. Set Hyper-Parameters
# ##### - Play with your own hyper-parameters!
# In[ ]:
burn_in_mode = 'ON' #{'ON', 'OFF'}
boost_mode = 'ON' #{'ON', 'OFF'}
##### HYPER-PARAMETERS
new_parser = {'mb_size': 32,
'iteration_burn_in': 3000,
'iteration': 25000,
'keep_prob': 0.6,
'lr_train': 1e-4,
'h_dim_RNN': 100,
'h_dim_FC' : 100,
'num_layers_RNN':2,
'num_layers_ATT':2,
'num_layers_CS' :2,
'RNN_type':'LSTM', #{'LSTM', 'GRU'}
'FC_active_fn' : tf.nn.relu,
'RNN_active_fn': tf.nn.tanh,
'reg_W' : 1e-5,
'reg_W_out' : 0.,
'alpha' :1.0,
'beta' :0.1,
'gamma' :1.0
}
# INPUT DIMENSIONS
input_dims = { 'x_dim' : x_dim,
'x_dim_cont' : x_dim_cont,
'x_dim_bin' : x_dim_bin,
'num_Event' : num_Event,
'num_Category' : num_Category,
'max_length' : max_length }
# NETWORK HYPER-PARMETERS
network_settings = { 'h_dim_RNN' : new_parser['h_dim_RNN'],
'h_dim_FC' : new_parser['h_dim_FC'],
'num_layers_RNN' : new_parser['num_layers_RNN'],
'num_layers_ATT' : new_parser['num_layers_ATT'],
'num_layers_CS' : new_parser['num_layers_CS'],
'RNN_type' : new_parser['RNN_type'],
'FC_active_fn' : new_parser['FC_active_fn'],
'RNN_active_fn' : new_parser['RNN_active_fn'],
'initial_W' : tf.contrib.layers.xavier_initializer(),
'reg_W' : new_parser['reg_W'],
'reg_W_out' : new_parser['reg_W_out']
}
mb_size = new_parser['mb_size']
iteration = new_parser['iteration']
iteration_burn_in = new_parser['iteration_burn_in']
keep_prob = new_parser['keep_prob']
lr_train = new_parser['lr_train']
alpha = new_parser['alpha']
beta = new_parser['beta']
gamma = new_parser['gamma']
# SAVE HYPERPARAMETERS
log_name = file_path + '/hyperparameters_log.txt'
save_logging(new_parser, log_name)
# ### 3. Split Dataset into Train/Valid/Test Sets
# In[ ]:
### TRAINING-TESTING SPLIT
(tr_data,te_data, tr_data_mi, te_data_mi, tr_time,te_time, tr_label,te_label,
tr_mask1,te_mask1, tr_mask2,te_mask2, tr_mask3,te_mask3) = train_test_split(data, data_mi, time, label, mask1, mask2, mask3, test_size=0.2, random_state=seed)
(tr_data,va_data, tr_data_mi, va_data_mi, tr_time,va_time, tr_label,va_label,
tr_mask1,va_mask1, tr_mask2,va_mask2, tr_mask3,va_mask3) = train_test_split(tr_data, tr_data_mi, tr_time, tr_label, tr_mask1, tr_mask2, tr_mask3, test_size=0.2, random_state=seed)
if boost_mode == 'ON':
tr_data, tr_data_mi, tr_time, tr_label, tr_mask1, tr_mask2, tr_mask3 = f_get_boosted_trainset(tr_data, tr_data_mi, tr_time, tr_label, tr_mask1, tr_mask2, tr_mask3)
# ### 4. Train the Networ
# In[ ]:
##### CREATE DYNAMIC-DEEPFHT NETWORK
tf.reset_default_graph()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
model = Model_Longitudinal_Attention(sess, "Dyanmic-DeepHit", input_dims, network_settings)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
### TRAINING - BURN-IN
if burn_in_mode == 'ON':
print( "BURN-IN TRAINING ...")
for itr in range(iteration_burn_in):
x_mb, x_mi_mb, k_mb, t_mb, m1_mb, m2_mb, m3_mb = f_get_minibatch(mb_size, tr_data, tr_data_mi, tr_label, tr_time, tr_mask1, tr_mask2, tr_mask3)
DATA = (x_mb, k_mb, t_mb)
MISSING = (x_mi_mb)
_, loss_curr = model.train_burn_in(DATA, MISSING, keep_prob, lr_train)
if (itr+1)%1000 == 0:
print('itr: {:04d} | loss: {:.4f}'.format(itr+1, loss_curr))
### TRAINING - MAIN
print( "MAIN TRAINING ...")
min_valid = 0.5
for itr in range(iteration):
x_mb, x_mi_mb, k_mb, t_mb, m1_mb, m2_mb, m3_mb = f_get_minibatch(mb_size, tr_data, tr_data_mi, tr_label, tr_time, tr_mask1, tr_mask2, tr_mask3)
DATA = (x_mb, k_mb, t_mb)
MASK = (m1_mb, m2_mb, m3_mb)
MISSING = (x_mi_mb)
PARAMETERS = (alpha, beta, gamma)
_, loss_curr = model.train(DATA, MASK, MISSING, PARAMETERS, keep_prob, lr_train)
if (itr+1)%1000 == 0:
print('itr: {:04d} | loss: {:.4f}'.format(itr+1, loss_curr))
### VALIDATION (based on average C-index of our interest)
if (itr+1)%1000 == 0:
risk_all = f_get_risk_predictions(sess, model, va_data, va_data_mi, pred_time, eval_time)
for p, p_time in enumerate(pred_time):
pred_horizon = int(p_time)
val_result1 = np.zeros([num_Event, len(eval_time)])
for t, t_time in enumerate(eval_time):
eval_horizon = int(t_time) + pred_horizon
for k in range(num_Event):
val_result1[k, t] = c_index(risk_all[k][:, p, t], va_time, (va_label[:,0] == k+1).astype(int), eval_horizon) #-1 for no event (not comparable)
if p == 0:
val_final1 = val_result1
else:
val_final1 = np.append(val_final1, val_result1, axis=0)
tmp_valid = np.mean(val_final1)
if tmp_valid > min_valid:
min_valid = tmp_valid
saver.save(sess, file_path + '/model')
print( 'updated.... average c-index = ' + str('%.4f' %(tmp_valid)))
# ### 5. Test the Trained Network
# In[ ]:
saver.restore(sess, file_path + '/model')
risk_all = f_get_risk_predictions(sess, model, te_data, te_data_mi, pred_time, eval_time)
for p, p_time in enumerate(pred_time):
pred_horizon = int(p_time)
result1, result2 = np.zeros([num_Event, len(eval_time)]), np.zeros([num_Event, len(eval_time)])
for t, t_time in enumerate(eval_time):
eval_horizon = int(t_time) + pred_horizon
for k in range(num_Event):
result1[k, t] = c_index(risk_all[k][:, p, t], te_time, (te_label[:,0] == k+1).astype(int), eval_horizon) #-1 for no event (not comparable)
result2[k, t] = brier_score(risk_all[k][:, p, t], te_time, (te_label[:,0] == k+1).astype(int), eval_horizon) #-1 for no event (not comparable)
if p == 0:
final1, final2 = result1, result2
else:
final1, final2 = np.append(final1, result1, axis=0), np.append(final2, result2, axis=0)
row_header = []
for p_time in pred_time:
for t in range(num_Event):
row_header.append('pred_time {}: event_{}'.format(p_time,k+1))
col_header = []
for t_time in eval_time:
col_header.append('eval_time {}'.format(t_time))
# c-index result
df1 = pd.DataFrame(final1, index = row_header, columns=col_header)
# brier-score result
df2 = pd.DataFrame(final2, index = row_header, columns=col_header)
### PRINT RESULTS
print('========================================================')
print('--------------------------------------------------------')
print('- C-INDEX: ')
print(df1)
print('--------------------------------------------------------')
print('- BRIER-SCORE: ')
print(df2)
print('========================================================')