-
Notifications
You must be signed in to change notification settings - Fork 1
/
feature_extractor.py
245 lines (192 loc) · 15.2 KB
/
feature_extractor.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
import numpy as np
import os
import SimpleITK as sitk
from scipy.io import loadmat
from skimage.measure import regionprops
import paths
import utils
def ReadImage(path):
''' This code returns the numpy nd array for a MR image at path'''
return sitk.GetArrayFromImage(sitk.ReadImage(path)).astype(np.float32)
def binarize_connectivity_matrix(connectivity_matrix, threshold=0.01):
''' binarize the matrix '''
binary_connectivity_matrix = np.zeros(connectivity_matrix.shape, dtype=np.float)
#print threshold*np.amax(connectivity_matrix)
binary_connectivity_matrix[connectivity_matrix >= threshold*np.amax(connectivity_matrix)] = 1
return binary_connectivity_matrix
def normalize_conncetivity_matrix(connectivity_matrix):
''' normalize the connectivity matrix'''
normalized_connectivity_matrix = np.copy(connectivity_matrix)
return normalized_connectivity_matrix/np.amax(connectivity_matrix)
def threshold_connectivity_matrix(connectivity_matrix, threshold=0.01):
''' threshold the connectiivty matrix in order to remove the noise'''
thresholded_connectivity_matrix= np.copy(connectivity_matrix)
thresholded_connectivity_matrix[connectivity_matrix <= threshold*np.amax(connectivity_matrix)] = 0
return thresholded_connectivity_matrix
def weight_conversion(W):
''' convert to the normalized version and binary version'''
W_bin = np.copy(W)
W_bin[W!=0]=1
W_nrm = np.copy(W)
W_nrm = W_nrm/np.amax(np.absolute(W))
return W_nrm, W_bin
def get_pat_name(pat_dir):
''' get the patient's name'''
temp = os.path.split(pat_dir)[1]
return temp[:temp.find('_whole_tumor')]
def get_lesion_weights(whole_tumor_mni_path):
''' get the weight vector'''
#print(whole_tumor_mni_path)
aal_path = os.path.join(paths.dsi_studio_path, 'atlas', 'aal.nii.gz')
aal_nda = utils.ReadImage(aal_path)
aal_182_218_182 = utils.reshape_by_padding_upper_coords(aal_nda, (182,218,182), 0)
whole_tumor_mni_nda = utils.ReadImage(whole_tumor_mni_path)
weights = np.zeros(int(np.amax(aal_182_218_182)), dtype=float)
for bp_number in range(int(np.amax(aal_182_218_182))):
mask = np.zeros(aal_182_218_182.shape, aal_182_218_182.dtype)
mask[aal_182_218_182==(bp_number+1)]=1
bp_size = float(np.count_nonzero(mask))
whole_tumor_in_bp = np.multiply(mask, whole_tumor_mni_nda)
whole_tumor_in_bp_size = float(np.count_nonzero(whole_tumor_in_bp))
weights[bp_number] = whole_tumor_in_bp_size/bp_size
return weights
def get_weighted_connectivity_feature_vectors_test(dsi_studio_path=paths.dsi_studio_path, region='seed'):
connectivity_testing_dir = os.path.join(dsi_studio_path, 'connectivity', region, 'testing')
whole_tumor_mni_testing_dir = os.path.join(dsi_studio_path, 'predicted_whole_tumor', 'testing')
connectivity_pass_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_testing_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'pass' in name and name.endswith('.mat')]
connectivity_pass_files.sort()
connectivity_end_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_testing_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'end' in name and name.endswith('.mat')]
connectivity_end_files.sort()
whole_tumor_mni_paths = [os.path.join(root, name) for root, dirs, files in os.walk(whole_tumor_mni_testing_dir) for name in files if 'whole_tumor' in name and 'MNI152_1mm' in name and name.endswith('nii.gz')]
whole_tumor_mni_paths.sort()
assert(len(connectivity_pass_files) == len(connectivity_end_files) == len(whole_tumor_mni_paths)==77)
W_dsi_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_dsi_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
pat_names=[]
for idx, (connectivity_pass_file, connectivity_end_file, whole_tumor_mni_path) in enumerate(zip(connectivity_pass_files, connectivity_end_files, whole_tumor_mni_paths)):
assert(get_pat_name(connectivity_pass_file)==get_pat_name(connectivity_end_file))
assert(get_pat_name(connectivity_pass_file) in whole_tumor_mni_path)
pat_name = get_pat_name(connectivity_pass_file)
pat_names.append(pat_name)
#lesion weights
lesion_weights = get_lesion_weights(whole_tumor_mni_path)
connectivity_matrix_pass_obj = loadmat(connectivity_pass_file)
weighted_connectivity_matrix_pass_temp = connectivity_matrix_pass_obj['connectivity']
weighted_connectivity_matrix_pass = threshold_connectivity_matrix(weighted_connectivity_matrix_pass_temp, 0)
W_nrm_pass, W_bin_pass = weight_conversion(weighted_connectivity_matrix_pass)
connectivity_matrix_end_obj = loadmat(connectivity_end_file)
weighted_connectivity_matrix_end_temp = connectivity_matrix_end_obj['connectivity']
weighted_connectivity_matrix_end = threshold_connectivity_matrix(weighted_connectivity_matrix_end_temp, 0)
W_nrm_end, W_bin_end = weight_conversion(weighted_connectivity_matrix_end)
# weighted connectivity histogram
W_dsi_pass_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_pass, axis=0), lesion_weights)
W_nrm_pass_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_pass, axis=0), lesion_weights)
W_bin_pass_histogram_features[idx, :] = np.multiply(np.sum(W_bin_pass, axis=0), lesion_weights)
W_dsi_end_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_end, axis=0), lesion_weights)
W_nrm_end_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_end, axis=0), lesion_weights)
W_bin_end_histogram_features[idx, :] = np.multiply(np.sum(W_bin_end, axis=0), lesion_weights)
return pat_names , W_dsi_pass_histogram_features, W_nrm_pass_histogram_features, W_bin_pass_histogram_features, W_dsi_end_histogram_features, W_nrm_end_histogram_features, W_bin_end_histogram_features
def get_weighted_connectivity_feature_vectors_valid(dsi_studio_path=paths.dsi_studio_path, region='seed'):
connectivity_valid_dir = os.path.join(dsi_studio_path, 'connectivity', region, 'validation')
whole_tumor_mni_valid_dir = os.path.join(dsi_studio_path, 'predicted_whole_tumor', 'validation')
connectivity_pass_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_valid_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'pass' in name and name.endswith('.mat')]
connectivity_pass_files.sort()
connectivity_end_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_valid_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'end' in name and name.endswith('.mat')]
connectivity_end_files.sort()
whole_tumor_mni_paths = [os.path.join(root, name) for root, dirs, files in os.walk(whole_tumor_mni_valid_dir) for name in files if 'whole_tumor' in name and 'MNI152_1mm' in name and name.endswith('nii.gz')]
whole_tumor_mni_paths.sort()
assert(len(connectivity_pass_files) == len(connectivity_end_files) == len(whole_tumor_mni_paths)==28)
W_dsi_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_dsi_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
pat_names=[]
for idx, (connectivity_pass_file, connectivity_end_file, whole_tumor_mni_path) in enumerate(zip(connectivity_pass_files, connectivity_end_files, whole_tumor_mni_paths)):
assert(get_pat_name(connectivity_pass_file)==get_pat_name(connectivity_end_file))
assert(get_pat_name(connectivity_pass_file) in whole_tumor_mni_path)
pat_name = get_pat_name(connectivity_pass_file)
pat_names.append(pat_name)
#lesion weights
lesion_weights = get_lesion_weights(whole_tumor_mni_path)
connectivity_matrix_pass_obj = loadmat(connectivity_pass_file)
weighted_connectivity_matrix_pass_temp = connectivity_matrix_pass_obj['connectivity']
weighted_connectivity_matrix_pass = threshold_connectivity_matrix(weighted_connectivity_matrix_pass_temp, 0)
W_nrm_pass, W_bin_pass = weight_conversion(weighted_connectivity_matrix_pass)
connectivity_matrix_end_obj = loadmat(connectivity_end_file)
weighted_connectivity_matrix_end_temp = connectivity_matrix_end_obj['connectivity']
weighted_connectivity_matrix_end = threshold_connectivity_matrix(weighted_connectivity_matrix_end_temp, 0)
W_nrm_end, W_bin_end = weight_conversion(weighted_connectivity_matrix_end)
# weighted connectivity histogram
W_dsi_pass_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_pass, axis=0), lesion_weights)
W_nrm_pass_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_pass, axis=0), lesion_weights)
W_bin_pass_histogram_features[idx, :] = np.multiply(np.sum(W_bin_pass, axis=0), lesion_weights)
W_dsi_end_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_end, axis=0), lesion_weights)
W_nrm_end_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_end, axis=0), lesion_weights)
W_bin_end_histogram_features[idx, :] = np.multiply(np.sum(W_bin_end, axis=0), lesion_weights)
return pat_names , W_dsi_pass_histogram_features, W_nrm_pass_histogram_features, W_bin_pass_histogram_features, W_dsi_end_histogram_features, W_nrm_end_histogram_features, W_bin_end_histogram_features
def get_weighted_connectivity_feature_vectors_train(dsi_studio_path=paths.dsi_studio_path, mode='gt', region='seed'):
''' Loading the survival dataset '''
survival_dataset = utils.load_survival_training_dataset()
if mode == 'gt':
connectivity_train_dir = os.path.join(dsi_studio_path, 'connectivity', region, 'gt')
whole_tumor_mni_train_dir = os.path.join(dsi_studio_path, 'gt_whole_tumor')
if mode == 'predicted':
connectivity_train_dir = os.path.join(dsi_studio_path, 'connectivity', region, 'training')
whole_tumor_mni_train_dir = os.path.join(dsi_studio_path, 'predicted_whole_tumor', 'training')
connectivity_pass_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_train_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'pass' in name and name.endswith('.mat')]
connectivity_pass_files.sort()
connectivity_end_files = [os.path.join(root, name) for root, dirs, files in os.walk(connectivity_train_dir) for name in files if 'count' in name and 'ncount' not in name and 'connectivity' in name and 'end' in name and name.endswith('.mat')]
connectivity_end_files.sort()
whole_tumor_mni_paths = [os.path.join(root, name) for root, dirs, files in os.walk(whole_tumor_mni_train_dir) for name in files if 'whole_tumor' in name and 'MNI152_1mm' in name and name.endswith('nii.gz')]
whole_tumor_mni_paths.sort()
assert(len(connectivity_pass_files) == len(connectivity_end_files) == len(whole_tumor_mni_paths)==59)
pat_names = []
gt = np.zeros((len(connectivity_pass_files),2), dtype = np.float32)
W_dsi_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_pass_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_dsi_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_nrm_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
W_bin_end_histogram_features = np.zeros((len(connectivity_pass_files), 116), dtype=np.float32)
for idx, (connectivity_pass_file, connectivity_end_file, whole_tumor_mni_path) in enumerate(zip(connectivity_pass_files, connectivity_end_files, whole_tumor_mni_paths)):
assert(get_pat_name(connectivity_pass_file)==get_pat_name(connectivity_end_file))
assert(get_pat_name(connectivity_pass_file) in whole_tumor_mni_path)
pat_name = get_pat_name(connectivity_pass_file)
pat_names.append(pat_name)
# short
if int(survival_dataset[pat_name]['survival']) < 305:
gt[idx, 0] = 0
gt[idx, 1] = int(survival_dataset[pat_name]['survival'])
#short_period += 1
# long should be 454 or 456.25
elif int(survival_dataset[pat_name]['survival']) > 456:
gt[idx, 0] = 2
gt[idx, 1] = int(survival_dataset[pat_name]['survival'])
#long_period += 1
# mid
else:
gt[idx, 0] = 1
gt[idx, 1] = int(survival_dataset[pat_name]['survival'])
lesion_weights = get_lesion_weights(whole_tumor_mni_path)
connectivity_matrix_pass_obj = loadmat(connectivity_pass_file)
weighted_connectivity_matrix_pass_temp = connectivity_matrix_pass_obj['connectivity']
weighted_connectivity_matrix_pass = threshold_connectivity_matrix(weighted_connectivity_matrix_pass_temp, 0)
W_nrm_pass, W_bin_pass = weight_conversion(weighted_connectivity_matrix_pass)
connectivity_matrix_end_obj = loadmat(connectivity_end_file)
weighted_connectivity_matrix_end_temp = connectivity_matrix_end_obj['connectivity']
weighted_connectivity_matrix_end = threshold_connectivity_matrix(weighted_connectivity_matrix_end_temp, 0)
W_nrm_end, W_bin_end = weight_conversion(weighted_connectivity_matrix_end)
# weighted connectivity histogram
W_dsi_pass_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_pass, axis=0), lesion_weights)
W_nrm_pass_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_pass, axis=0), lesion_weights)
W_bin_pass_histogram_features[idx, :] = np.multiply(np.sum(W_bin_pass, axis=0), lesion_weights)
W_dsi_end_histogram_features[idx, :] = np.multiply(np.sum(weighted_connectivity_matrix_end, axis=0), lesion_weights)
W_nrm_end_histogram_features[idx, :] = np.multiply(np.sum(W_nrm_end, axis=0), lesion_weights)
W_bin_end_histogram_features[idx, :] = np.multiply(np.sum(W_bin_end, axis=0), lesion_weights)
return pat_names, gt, W_dsi_pass_histogram_features, W_nrm_pass_histogram_features, W_bin_pass_histogram_features, W_dsi_end_histogram_features, W_nrm_end_histogram_features, W_bin_end_histogram_features