-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
367 lines (288 loc) · 14.4 KB
/
utils.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
364
import logging
import numpy as np
import pandas as pd
import torch
import random
from torch.utils.data import TensorDataset, DataLoader
from pycox.evaluation import EvalSurv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from collections import defaultdict
import h5py
from main import *
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def mkdirs(dirpath):
try:
os.makedirs(dirpath)
except Exception as _:
pass
def load_datasets(dataset_file):
datasets = defaultdict(dict)
with h5py.File(dataset_file, 'r') as fp:
for ds in fp:
for array in fp[ds]:
datasets[ds][array] = fp[ds][array][:]
return datasets
def one_hot_encoder(data, encode):
"""
Attributes:
----------
data: A dataframe
encode: A list of columns in the dataframe to be one-hot-encoded
Returns
-------
An one-hot-encoded dataframe
"""
data_encoded = data.copy()
encoded = pd.get_dummies(data_encoded, prefix=encode, columns=encode, drop_first=False)
return encoded
################################################## IID data Partition ######################################################
def sample_iid(data, num_centers, seed_no):
"""
Randomly split data evenly across each center
Arguments:
data -- combined data
num_centers -- number of centers
Returns:
Dict with center_id : indices of data assigned to center
"""
np.random.seed(seed_no)
n = data.shape[0]
idxs = np.random.permutation(n)
batch_idxs = np.array_split(idxs, num_centers)
dict_center_idxs = {i: batch_idxs[i] for i in range(num_centers)}
return dict_center_idxs
################################################################################################################################
################################################## Non-IID data Partition ######################################################
def sample_by_quantiles(df, num_centers):
"""
Randomly split data by quantiles
Arguments:
df -- combined data as dataframe
num_centers -- number of centres
Returns:
Dict with center_id : indices of data assigned to center
"""
# labels are tuples, features are DFs
data = np.array(df['time'])
dict_center_idxs, all_idxs = {}, np.array([i for i in range(len(data))])
quantile = 1 / num_centers
previous_idxs = torch.zeros(len(data),dtype=torch.bool).numpy()
for i in range(num_centers):
if quantile > 1:
ValueError
cutoff = np.quantile(data,quantile)
selected_idxs = data <= cutoff
idxs_in_quantile = selected_idxs & ~previous_idxs
previous_idxs = previous_idxs | idxs_in_quantile
dict_center_idxs[i] = all_idxs[idxs_in_quantile]
quantile += 1 / num_centers
return dict_center_idxs
def mergeDictionary(dict_1, dict_2):
ds = [dict_1, dict_2]
d = {}
for k in dict_1.keys():
d[k] = np.concatenate(list(d[k] for d in ds))
return d
def non_iid_skewed(data, num_centers, seed_no):
np.random.seed(seed_no)
random.seed(seed_no)
n = data.shape[0]
idxs = range(0, n)
idxs1 = np.array(random.sample(range(0, n), int(n/num_centers)))
idxs2=np.setdiff1d(idxs,idxs1)
data1 = data.loc[data.index[list(idxs1)], :]
data2 = data.loc[data.index[list(idxs2)], :]
dict_center_idxs1=sample_iid(data1, num_centers, seed_no)
dict_center_idxs2=sample_by_quantiles(data2, num_centers)
dict_center_idxs=mergeDictionary(dict_center_idxs1, dict_center_idxs2)
return dict_center_idxs
################################################################################################################################
################################################## Get Local Dataset ######################################################
def get_local_dataset(data_directory, dataset, partition, n_parties, client_id, seed_no):
cols_to_remove=['time','status']
if dataset=='metabric':
column_name=['time','status','MKI67','EGFR','PGR','ERBB2','hormone_treatment','radiotherapy','chemotherapy','ER_positive','age_at_diagnosis']
std_list=['MKI67','EGFR','PGR','ERBB2','age_at_diagnosis']
# Downloaded the dataset from https://github.com/jaredleekatzman/DeepSurv/tree/master/experiments/data/metabric
datasets=load_datasets(data_directory+dataset+'/metabric_IHC4_clinical_train_test.h5')
training_data=datasets['train']
test_dataset=datasets['test']
train_df=pd.DataFrame(np.concatenate([training_data['t'].reshape(-1,1), training_data['e'].reshape(-1,1),training_data['x']],axis=1))
test_df=pd.DataFrame(np.concatenate([test_dataset['t'].reshape(-1,1), test_dataset['e'].reshape(-1,1), test_dataset['x']],axis=1))
central_data=pd.concat([train_df, test_df]).reset_index(drop=True)
central_data.columns=column_name
if partition == "centralized":
dataidxs=sample_iid(central_data, 1, seed_no)
elif partition == "iid":
dataidxs=sample_iid(central_data, n_parties, seed_no)
elif partition == "non-iid":
dataidxs=non_iid_skewed(central_data, n_parties, seed_no)
central_data=np.array(central_data, dtype='float32')
local_data=central_data[dataidxs[client_id]]
feature_list = [x for x in column_name if x not in cols_to_remove]
elif dataset == 'support':
column_name=['time','status','age', 'sex', 'race', 'num_comorbid','diabetes', 'dementia', 'cancer', 'mean_abp', 'hr','resp_rate', 'temp', 'wbc_count','serum_sodium', 'serum_creatinine']
OHE_list=['race','cancer']
std_list=['age', 'num_comorbid', 'mean_abp', 'hr','resp_rate', 'temp', 'wbc_count','serum_sodium', 'serum_creatinine']
# Downloaded the dataset from https://github.com/jaredleekatzman/DeepSurv/tree/master/experiments/data/support
datasets=load_datasets(data_directory+dataset+'/support_train_test.h5')
training_data=datasets['train']
test_dataset=datasets['test']
train_df=pd.DataFrame(np.concatenate([training_data['t'].reshape(-1,1), training_data['e'].reshape(-1,1),training_data['x']],axis=1))
test_df=pd.DataFrame(np.concatenate([test_dataset['t'].reshape(-1,1), test_dataset['e'].reshape(-1,1), test_dataset['x']],axis=1))
centralized_data=pd.concat([train_df, test_df]).reset_index(drop=True)
centralized_data.columns=column_name
central_data = one_hot_encoder(centralized_data, OHE_list)
if partition == "centralized":
dataidxs=sample_iid(central_data, 1, seed_no)
elif partition == "iid":
dataidxs=sample_iid(central_data, n_parties, seed_no)
elif partition == "non-iid":
dataidxs=non_iid_skewed(central_data, n_parties, seed_no)
new_col_name=central_data.columns
central_data=np.array(central_data, dtype='float32')
local_data=central_data[dataidxs[client_id]]
feature_list = [x for x in new_col_name if x not in cols_to_remove]
elif dataset == 'gbsg':
column_name=['time','status','htreat', 'tumgrad', 'menostat', 'age', 'posnodal', 'prm', 'esm']
OHE_list=['tumgrad']
std_list=['age', 'posnodal', 'prm', 'esm']
# Downloaded the dataset from https://github.com/jaredleekatzman/DeepSurv/tree/master/experiments/data/gbsg
datasets=load_datasets(data_directory+dataset+'/gbsg_cancer_train_test.h5')
training_data=datasets['train']
test_dataset=datasets['test']
train_df=pd.DataFrame(np.concatenate([training_data['t'].reshape(-1,1), training_data['e'].reshape(-1,1),training_data['x']],axis=1))
test_df=pd.DataFrame(np.concatenate([test_dataset['t'].reshape(-1,1), test_dataset['e'].reshape(-1,1), test_dataset['x']],axis=1))
centralized_data=pd.concat([train_df, test_df]).reset_index(drop=True)
centralized_data.columns=column_name
central_data = one_hot_encoder(centralized_data, OHE_list)
if partition == "centralized":
dataidxs=sample_iid(central_data, 1, seed_no)
elif partition == "iid":
dataidxs=sample_iid(central_data, n_parties, seed_no)
elif partition == "non-iid":
dataidxs=non_iid_skewed(central_data, n_parties, seed_no)
new_col_name=central_data.columns
central_data=np.array(central_data, dtype='float32')
local_data=central_data[dataidxs[client_id]]
feature_list = [x for x in new_col_name if x not in cols_to_remove]
else:
local_data=pd.read_csv(data_directory+dataset+'/data_{}.csv'.format(client_id+1))
feature_list = [x for x in local_data.columns if x not in cols_to_remove]
## Split local data into train and test set
loc_train_data, loc_test_data = train_test_split(local_data, test_size=0.2, random_state=seed_no)
## Get covariates, event time and event status
train_time=np.array(loc_train_data[:,0], dtype='float32')
train_status=np.array(loc_train_data[:,1], dtype='float32')
test_time=np.array(loc_test_data[:,0], dtype='float32')
test_status=np.array(loc_test_data[:,1], dtype='float32')
X_train=np.array(loc_train_data[:,2:], dtype='float32')
X_test=np.array(loc_test_data[:,2:], dtype='float32')
return X_train, train_time, train_status, X_test, test_time, test_status, feature_list
################################################################################################################################
################################################## Evaluation metrics ######################################################
def Evaluation(model, X_test, test_time, test_status, evaltime):
"""
Attributes:
----------
model: Trained Model
X_test: Covariates in test set
test_status: Event status array for test set
test_time: Observed times array for test set
evaltime: Prespecified evaluation times
Returns
-------
Time-dependent C-index and Integrated Brier Score
"""
device = "cuda" if torch.cuda.is_available() else "cpu"
################ Performance Measure on entire test data #####################
sp_test = model(torch.tensor(X_test).to(device)) ## Predict the survival probability for test set
surv=pd.DataFrame(np.transpose(sp_test.cpu().detach().numpy())) # Transpose the prediction array
surv=surv.set_index([evaltime]) ## extract the predictions at evaluation time points
ev = EvalSurv(surv, test_time, test_status, censor_surv='km') ## EvalSurv function from pycox package.
#Setting censor_surv='km' means that we estimate the censoring distribution by Kaplan-Meier on the test set.
cindex=ev.concordance_td() ## time-dependent cindex
brier=ev.integrated_brier_score(evaltime) ## integrated brier score
return cindex, brier
################################################################################################################################
################################################## Concodance Index metrics ######################################################
def Concordance(model, x, durations, events, evaltime):
"""
Attributes:
----------
model: Model
x: Covariates
durations: Event status array
events: Observed times array
evaltime: Prespecified evaluation times
Returns
-------
Time-dependent C-index
"""
device = "cuda" if torch.cuda.is_available() else "cpu"
x=x.to(device)
surv = model(x)
y_pred = pd.DataFrame(np.transpose(surv.cpu().detach().numpy()))
y_pred=y_pred.set_index([evaltime])
ev = EvalSurv(y_pred, durations, events, censor_surv='km')
cindex = ev.concordance_td()
return cindex
################################################################################################################################
################################################## Pseudo Value based Loss Function ##############################################
def pseudo_loss(output, target):
loss = torch.mean(target*(1-2*output)+(output**2))
return loss
################################################################################################################################
class FastTensorDataLoader:
"""
A DataLoader-like object for a set of tensors that can be much faster than
TensorDataset + DataLoader because dataloader grabs individual indices of
the dataset and calls cat (slow).
Source: https://discuss.pytorch.org/t/dataloader-much-slower-than-manual-batching/27014/6
"""
def __init__(self, *tensors, batch_size=32, shuffle=False):
"""
Initialize a FastTensorDataLoader.
:param *tensors: tensors to store. Must have the same length @ dim 0.
:param batch_size: batch size to load.
:param shuffle: if True, shuffle the data *in-place* whenever an
iterator is created out of this object.
:returns: A FastTensorDataLoader.
"""
assert all(t.shape[0] == tensors[0].shape[0] for t in tensors)
self.tensors = tensors
self.dataset_len = self.tensors[0].shape[0]
self.batch_size = batch_size
self.shuffle = shuffle
# Calculate # batches
n_batches, remainder = divmod(self.dataset_len, self.batch_size)
if remainder > 0:
n_batches += 1
self.n_batches = n_batches
def __iter__(self):
if self.shuffle:
r = torch.randperm(self.dataset_len)
self.tensors = [t[r] for t in self.tensors]
self.i = 0
return self
def __next__(self):
if self.i >= self.dataset_len:
raise StopIteration
batch = tuple(t[self.i:self.i+self.batch_size] for t in self.tensors)
self.i += self.batch_size
return batch
def __len__(self):
return self.n_batches
def save_model(model, model_index, args):
logger.info("saving local model-{}".format(model_index))
with open(args.modeldir+"trained_local_model"+str(model_index), "wb") as f_:
torch.save(model.state_dict(), f_)
return
def load_model(model, model_index, device="cpu"):
with open("trained_local_model"+str(model_index), "rb") as f_:
model.load_state_dict(torch.load(f_))
model.to(device)
return model