-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfeatures_mni.py
276 lines (238 loc) · 10 KB
/
features_mni.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
"""Code to compute the feature matrix.
"""
import os
import numpy as np
import nibabel as nib
from os.path import isfile
from dipy.tracking.distances import bundles_distances_mam, bundles_distances_mdf
from dipy.tracking.streamline import set_number_of_points
from distances import parallel_distance_computation
from functools import partial
from endpoints_distance import bundles_distances_endpoints_fastest
from waypoints_distance import wrapper_bundle2roi_distance, bundle2roi_distance
from subsampling import compute_subset
import pickle
import json
from dipy.tracking import metrics as tm
from dipy.tracking.metrics import endpoint
from sklearn.cluster import KMeans
from dipy.tracking.vox2track import streamline_mapping
from utils import compute_kdtree_and_dr_tractogram, compute_superset, NN, NN_radius
try:
from joblib import Parallel, delayed, cpu_count
joblib_available = True
except ImportError:
joblib_available = False
## features settings
dm = True
extra_prototypes = False
local_prototypes = True
endpoints = True
rois = True
frenet_serret = False
fa_profile = False
context = False
context_hist = False
## global configuration parameters
distance_func = bundles_distances_mdf
num_local_prototypes = 100
nb_points = 20
with open('config.json') as f:
data = json.load(f)
tag = data["_inputs"][2]["datatype_tags"][0].encode("utf-8")
def compute_X_dm(superset, prototypes, distance_func=bundles_distances_mam, nb_points=20):
"""Compute the global dissimilarity matrix.
"""
if distance_func==bundles_distances_mdf:
print("Resampling the superset with %s points" %nb_points)
superset = set_number_of_points(superset, nb_points)
distance = partial(parallel_distance_computation, distance=distance_func)
print("Computing dissimilarity matrix (%s x %s)..." %(len(superset), len(prototypes)))
dm_superset = distance(superset, prototypes)
return dm_superset
def compute_X_dm_local(superset, subjID, tract_name, distance_func=bundles_distances_mam, nb_points=20):
"""Compute the local dissimilarity matrix.
"""
if distance_func==bundles_distances_mdf:
print("Resampling the superset with %s points" %nb_points)
superset = set_number_of_points(superset, nb_points)
distance = partial(parallel_distance_computation, distance=distance_func)
local_prot_fname = 'common_local_prototypes/%s_common_prototypes.npy' %tract_name
local_prototypes = np.load(local_prot_fname)
print("Computing dissimilarity matrix (%s x %s)..." %(len(superset), len(local_prototypes)))
dm_local_superset = distance(superset, local_prototypes)
return dm_local_superset
def compute_X_end(superset, prototypes):
"""Compute the endpoint matrix.
"""
endpoint_matrix = bundles_distances_endpoints_fastest(superset, prototypes)
endpoint_matrix = endpoint_matrix * 0.5
return endpoint_matrix
def compute_X_roi(superset, subjID, tract_name, tag):
"""Compute a matrix with dimension (len(superset), 2) that contains
the distances of each streamline of the superset with the 2 ROIs.
"""
superset = set_number_of_points(superset, nb_points) #to speed up the computational time
print("Loading the two-waypoint ROIs of the target...")
table_filename = 'ROIs_labels_dictionary.pickle'
table = pickle.load(open(table_filename))
roi1_lab = table[tract_name].items()[0][1]
roi2_lab = table[tract_name].items()[1][1]
subjID = 'MNI'
if (tag == 'afq'):
roi_dir = 'templates_mni125'
roi1_filename = '%s/sub-%s_var-AFQ_lab-%s_roi.nii.gz' %(roi_dir, subjID, roi1_lab)
roi2_filename = '%s/sub-%s_var-AFQ_lab-%s_roi.nii.gz' %(roi_dir, subjID, roi2_lab)
elif tag == 'wmaSeg':
roi_dir = 'templates_mni125_ICBM2009c'
roi1_filename = '%s/%s.nii.gz' %(roi_dir, roi1_lab)
roi2_filename = '%s/%s.nii.gz' %(roi_dir, roi2_lab)
roi1 = nib.load(roi1_filename)
roi2 = nib.load(roi2_filename)
print("Computing superset to ROIs distances...")
if joblib_available:
roi1_dist = wrapper_bundle2roi_distance(superset, roi1)
roi2_dist = wrapper_bundle2roi_distance(superset, roi2)
else:
roi1_dist = bundle2roi_distance(superset, roi1)
roi2_dist = bundle2roi_distance(superset, roi2)
X_roi = np.vstack((roi1_dist, roi2_dist))
return X_roi.T
def compute_endpoints(bundle):
endpoints = np.zeros((len(bundle),3))
for i, st in enumerate(bundle):
endpoints[i] = endpoint(st)
return endpoints
def orient_tract_kmeans(bundle):
"""Ensure the startpoints to be always higher than the endpoints.
"""
points = compute_endpoints(bundle)
kmeans = KMeans(n_clusters=2, random_state=0).fit(points)
class0_up = (kmeans.cluster_centers_[0][2] > kmeans.cluster_centers_[1][2]) #constraint on the z axis
oriented_bundle = []
for i, st in enumerate(bundle):
if kmeans.labels_[i]==class0_up:
oriented_bundle.append(st)
else:
tmp = np.flip(st, axis=0)
oriented_bundle.append(tmp)
return oriented_bundle
def compute_X_fs(superset, nb_points_fs=100):
"""Compute a matrix with dimension (len(superset), 2*nb_points_fs) that
contains the curvature and torsion profiles of the oriented superset.
"""
superset = set_number_of_points(superset, nb_points_fs)
oriented_superset = orient_tract_kmeans(superset)
#X_fs = np.zeros((len(superset), 2*nb_points_fs))
#X_fs = np.zeros((len(superset), nb_points_fs)) #only curvature
X_fs = np.zeros((len(superset), 2)) #mean
for i, st in enumerate(oriented_superset):
T,N,B,k,t = tm.frenet_serret(st)
#X_fs[i,0:nb_points_fs] = k.T
#X_fs[i,nb_points_fs:] = t.T
X_fs[i,0] = np.mean(k)
X_fs[i,1] = np.mean(t)
return X_fs
def compute_X_fa(superset, exID, subjID, nb_points_fa=100):
"""Compute a matrix with dimension (len(superset), nb_points_fa)
that contains the FA profile of the oriented superset.
"""
superset = set_number_of_points(superset, nb_points_fa)
oriented_superset = orient_tract_kmeans(superset)
X_fa = np.zeros((len(superset), nb_points_fa))
fa_nii = nib.load('%s/aligned_FA/FA_m%s_s%s.nii.gz' %(subjID, exID, subjID))
aff = fa_nii.affine
fa = fa_nii.get_data()
for s in range(len(oriented_superset)-1):
voxel_list = streamline_mapping(oriented_superset[s:s+1], affine=aff).keys()
for v, (i,j,k) in enumerate(voxel_list):
X_fa[s,v] = fa[i,j,k]
return X_fa
def compute_X_context(X_tmp, superset, k=10):
"""Add a second feature matrix composed of the features
of the k-Nearest-Neighbors.
"""
kdt, prototypes = compute_kdtree_and_dr_tractogram(superset, num_prototypes=40,
distance_func=distance_func, nb_points=nb_points)
X_context = np.zeros((len(X_tmp), k*len(X_tmp[0])))
for i in range(len(superset)):
nn_idx = compute_superset(superset[i:i+1], kdt, prototypes, k=k+1, distance_func=distance_func, nb_points=nb_points)
nn_idx = nn_idx[1:] #remove the index corresdponding to itself
nn_row = np.array([])
for nn in nn_idx:
nn_row = np.hstack([nn_row, X_tmp[nn,:]]) if nn_row.size else X_tmp[nn,:]
X_context[i,:] = nn_row
return X_context
def compute_X_context_hist(superset, k=100, r=40, n_bins=40):
"""Add features of the k-Nearest-Neighbors.
"""
kdt, prototypes = compute_kdtree_and_dr_tractogram(superset, num_prototypes=40,
distance_func=distance_func, nb_points=nb_points)
X_context_hist = np.zeros((len(superset), n_bins))
for i in range(len(superset)):
streamline = superset[i:i+1]
#D, I = NN(streamline, kdt, prototypes, k=k, distance_func=distance_func, nb_points=nb_points)
I = NN_radius(streamline, kdt, prototypes, r=r, distance_func=distance_func, nb_points=nb_points)
I = I[1:] #remove the index corresdponding to itself
nn_str = superset[I]
#Compute the exact distance
if distance_func == bundles_distances_mdf:
streamline = set_number_of_points(streamline, nb_points=nb_points)
nn_str = set_number_of_points(nn_str, nb_points=nb_points)
D = distance_func(streamline, nn_str)
hist, bin_edges = np.histogram(D, bins=0.5*np.arange(n_bins+1), range=(0,0.5*n_bins))
X_context_hist[i,:] = hist
if i==0:
print(hist)
return X_context_hist
def compute_feature_matrix(superset, exID, subjID, tract_name, distance_func=distance_func, nb_points=nb_points):
"""Compute the feature matrix.
"""
np.random.seed(0)
feature_list = []
if dm:
if extra_prototypes:
common_prototypes = np.load('sub-983773_var-ACPC_prototypes108.npy')
else:
common_prototypes = np.load('common_prototypes.npy')
X_dm = compute_X_dm(superset, common_prototypes, distance_func=distance_func, nb_points=nb_points)
feature_list.append(X_dm)
print("----> Added dissimilarity matrix of size (%s, %s)" %(X_dm.shape))
if local_prototypes:
X_dm_local = compute_X_dm_local(superset, subjID, tract_name, distance_func=distance_func, nb_points=nb_points)
feature_list.append(X_dm_local)
print("----> Added local dissimilarity matrix of size (%s, %s)" %(X_dm_local.shape))
if endpoints:
if extra_prototypes:
common_prototypes = np.load('sub-983773_var-ACPC_prototypes108.npy')
else:
common_prototypes = np.load('common_prototypes.npy')
X_end = compute_X_end(superset, common_prototypes)
feature_list.append(X_end)
print("----> Added endpoint matrix of size (%s, %s)" %(X_end.shape))
if rois:
X_roi = compute_X_roi(superset, subjID, tract_name, tag)
feature_list.append(X_roi)
print("----> Added ROI distance matrix of size (%s, %s)" %(X_roi.shape))
if frenet_serret:
X_fs = compute_X_fs(superset)
feature_list.append(X_fs)
print("----> Added Frenet-Serret matrix of size (%s, %s)" %(X_fs.shape))
if fa_profile:
X_fa = compute_X_fa(superset, exID, subjID)
feature_list.append(X_fa)
print("----> Added FA profile matrix of size (%s, %s)" %(X_fa.shape))
#concatenation
X_tmp = np.array([])
for matrix in feature_list:
X_tmp = np.hstack([X_tmp, matrix]) if X_tmp.size else matrix
print("----> Size of final feature matrix: (%s, %s)" %(X_tmp.shape))
if context:
X_context = compute_X_context(X_tmp, superset)
X_tmp = np.hstack([X_tmp, X_context])
print("----> Size of final feature matrix with context: (%s, %s)" %(X_tmp.shape))
elif context_hist:
X_context_hist = compute_X_context_hist(superset)
X_tmp = np.hstack([X_tmp, X_context_hist])
print("----> Size of final feature matrix with context_hist: (%s, %s)" %(X_tmp.shape))
return np.array(X_tmp, dtype=np.float32)