[7b3b0e]: / loader.py

Download this file

331 lines (267 with data), 11.6 kB

  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
'''
This file contains a class which will load the different
attributes of the patients of a given dataset.
Firstly, NSCLC-Radiogenomics
'''
from copy import deepcopy
import pandas as pd
import numpy as np
import os
from itertools import chain
from sklearn.preprocessing import LabelBinarizer
import nibabel as nib
class Dataset(object):
'''
Class which contains a list of all patients,
images, genomics, recurrence, survival, and mutation information.
If the corresponding data is unavailable, 'NA' will
be used.
'''
def __init__(self, config, dataset='NSCLC-Radiogenomics'):
self.dataset_info = dataset
self.config = config
self.get_patient_list()
# these are list of files which should be read appropriately
self.image_list = {}
self.images = {}
self.seg_list = {}
self.feature_list = {}
# these are already loaded attributes
self.genomics_list = {}
self.egfr_mutation = {}
self.recurrence_bool = {}
self.recurrence_value = {}
self.survival_bool = {}
self.survival_value = {}
self.durations = {}
#TODO: include this: self.last_known_alive = []
self.clinical_list = {}
self.load_all()
def get_patient_list(self):
self.data_location = self.config.location
self.patient_list = list(pd.read_csv(self.config.clinical)['Case ID'])
return
def set_patient_list(self, patient_list):
self.patient_list = patient_list
def load_all(self):
self.load_images()
self.load_segmentations()
self.load_pyradiomics()
self.load_genomics()
self.load_recurrence()
self.load_survival()
self.load_egfr_mutation()
# self.load_clinical()
self.load_densenet_features()
def load_images(self):
for patientID in self.patient_list:
image_path = self.config.images + patientID + '.nii'
if os.path.exists(image_path):
self.image_list[patientID] = image_path
else:
self.image_list[patientID] = 'N/A'
return
def get_images_cropped(self):
self.images = {}
for patientID in self.patient_list:
image_path = self.config.cropped + patientID + '_cropped_nodule.nii'
if os.path.exists(image_path):
self.images[patientID] = nib.load(image_path).get_fdata()
else:
self.images[patientID] = 'N/A'
return
def get_pyradiomics(self, patient_list):
features=[]
for patientID in patient_list:
feature_path = self.config.pyradiomics + patientID + '_dilated.npz'
features.append(np.load(feature_path)['arr_0'])
return features
def get_densenet_features(self, patient_list):
features=[]
for patientID in patient_list:
feature_path = self.config.densenet + patientID + '_densenet.npy'
loaded = np.load(feature_path)
features.append(loaded)
features = np.array(features)
features = np.squeeze(features)
return features
def get_genomics(self, patient_list):
genomics = pd.read_csv(self.config.genomics, index_col=False)
genomics.set_index('Unnamed: 0.1', inplace=True)
genomics = genomics.drop('Unnamed: 0', axis=1)
genomics = genomics.transpose()
# TODO: we can add code here to normalize the genomics data
genomics_list = []
for id in patient_list:
genomics_list.append(list(genomics.loc[id]))
return genomics_list, genomics.columns
def get_clinical(self, patient_list):
clinical = pd.read_csv(self.config.clinical)
list_of_variables = clinical.columns.values
predictors_labels = list(chain([list_of_variables[0]], [list_of_variables[2]], list_of_variables[6:8], list_of_variables[9:22], list_of_variables[23:24])) #:30]))
predictors = clinical[predictors_labels]
predictors.set_index('Case ID', inplace=True)
predictors['Smoking status'].replace(self.config.smoking_dict, inplace=True)
predictors['Pack Years'].replace({'N/A': 0, 'Not Collected': 40}, inplace=True)
predictors['%GG'].replace(self.config.gg_dict, inplace=True)
for idx in range(10, 17):
predictors[list_of_variables[idx]].replace(self.config.location_dict, inplace=True)
encoder = LabelBinarizer()
for idx in range(17, 24):
if idx == 22:
continue
predictors[list_of_variables[idx]] = encoder.fit_transform(predictors[list_of_variables[idx]])
predictors.fillna(0, inplace=True)
clinical_data = []
for id in patient_list:
clinical_data.append([int(x) for x in list(predictors.loc[id])])
return clinical_data
def load_segmentations(self):
self.seg_list = {}
for patientID in self.patient_list:
seg_path = self.config.segs + patientID + '.nii.gz'
if os.path.exists(seg_path):
self.seg_list[patientID] = seg_path
else:
self.seg_list[patientID] = 'N/A'
return
def load_pyradiomics(self):
self.feature_list = {}
for patientID in self.patient_list:
feature_path = self.config.pyradiomics + patientID + '.npz'
if os.path.exists(feature_path):
self.feature_list[patientID] = feature_path
else:
self.feature_list[patientID] = 'N/A'
return
def load_densenet_features(self):
self.densenet_features = {}
for patientID in self.patient_list:
feature_path = self.config.densenet + patientID + '_densenet.npy'
if os.path.exists(feature_path):
temp = np.load(feature_path)
if np.size(temp) == 1:
self.densenet_features[patientID] = 'N/A'
else:
self.densenet_features[patientID] = feature_path
else:
self.densenet_features[patientID] = 'N/A'
return
def load_genomics(self):
self.genomics_list = {}
genomics = pd.read_csv(self.config.genomics, index_col=False)
genomics.set_index('Unnamed: 0.1', inplace=True)
genomics = genomics.drop('Unnamed: 0', axis=1)
genomics = genomics.transpose()
for id in self.patient_list:
if id in genomics.index.values:
self.genomics_list[id] = list(genomics.loc[id])
else:
self.genomics_list[id] = 'N/A'
return
def load_recurrence(self):
#TODO: Include the location information as well
self.recurrence_value = {}
self.recurrence_bool = {}
self.durations = {}
recurrence = pd.read_csv(self.config.recurrence, index_col=False)
recurrence.set_index('Case ID', inplace=True)
for id in self.patient_list:
if id in recurrence.index.values:
curr_patient = recurrence.loc[id]
value = curr_patient['Recurrence']
self.recurrence_bool[id] = value
self.recurrence_value[id] = curr_patient['Days']
return
def load_survival(self):
self.survival_value = {}
self.survival_bool = {}
recurrence = pd.read_csv(self.config.clinical, index_col=False)
recurrence.set_index('Case ID', inplace=True)
for id in self.patient_list:
if id in recurrence.index.values:
curr_patient = recurrence.loc[id]
value = curr_patient['Survival Status']
mapped_value = self.config.survival_mapping[value]
self.survival_bool[id] = mapped_value
if mapped_value == 1:
self.survival_value[id] = curr_patient['Time to Death (days)']
else:
self.survival_value[id] = 'N/A'
else:
self.survival_bool[id] = 'N/A'
self.survival_value[id] = 'N/A'
return
def load_egfr_mutation(self):
self.egfr_mutation = {}
egfr = pd.read_csv(self.config.clinical, index_col=False)
egfr.set_index('Case ID', inplace=True)
for id in self.patient_list:
if id in egfr.index.values:
value = egfr.loc[id]['EGFR mutation status']
mapped_value = self.config.mutation_mapping[value]
self.egfr_mutation[id] = mapped_value
else:
self.egfr_mutation[id] = 'N/A'
return
def load_clinical(self):
self.clinical_list = {}
clinical = pd.read_csv(self.config.clinical)
list_of_variables = clinical.columns.values
# clinical = clinical.loc[49:]
# for id in range(len(list_of_variables)):
# print(id, list_of_variables[id])
predictors_labels = list(chain([list_of_variables[0]], [list_of_variables[2]], list_of_variables[6:8], list_of_variables[9:22], list_of_variables[23:24])) #:30]))
predictors = clinical[predictors_labels]
predictors.set_index('Case ID', inplace=True)
predictors['Smoking status'].replace(self.config.smoking_dict, inplace=True)
predictors['Pack Years'].replace({'N/A': 0, 'Not Collected': 40}, inplace=True)
predictors['%GG'].replace(self.config.gg_dict, inplace=True)
for idx in range(10, 17):
predictors[list_of_variables[idx]].replace(self.config.location_dict, inplace=True)
encoder = LabelBinarizer()
for idx in range(17, 24):
if idx == 22:
continue
predictors[list_of_variables[idx]] = encoder.fit_transform(predictors[list_of_variables[idx]])
predictors.fillna(0, inplace=True)
print(predictors.columns.values)
for id in self.patient_list:
self.clinical_list[id] = [int(x) for x in list(predictors.loc[id])]
return
def select_subset_patients(self, to_select, replace_list=False):
'''
:param dataset: dataset of type Dataset
:param to_select: list of features to subselect
:return: updated dataset
'''
patient_list = deepcopy(self.patient_list)
for id in self.patient_list:
remove_bool = False
for attr in to_select:
if attr == 'pyradiomics':
if self.feature_list[id] == 'N/A':
remove_bool = True
if attr == 'gene_expressions':
if self.genomics_list[id] == 'N/A':
remove_bool = True
if attr == 'clinical':
if self.clinical_list[id] == 'N/A':
remove_bool = True
if attr == 'recurrence':
if self.recurrence_bool[id] == 'N/A':
remove_bool = True
if attr == 'densenet':
if self.densenet_features[id] == 'N/A':
remove_bool = True
if attr == 'egfr':
if self.egfr_mutation[id] == 'N/A':
remove_bool = True
if remove_bool is True:
patient_list.remove(id)
if replace_list == True:
self.set_patient_list(patient_list)
self.load_all()
return patient_list
if __name__ == '__main__':
nrg = Dataset()