/
TractSegmentation_One-Class-SVM.py
214 lines (160 loc) · 7.22 KB
/
TractSegmentation_One-Class-SVM.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 14 04:06:31 2019
@author: u
"""
import numpy as np
from dipy.viz import window, actor
from nibabel import trackvis
from dipy.tracking.streamline import transform_streamlines
import vtk.util.colors as colors
from dipy.viz import fvtk
from dipy.tracking import utils
from sklearn.neighbors import KDTree
from dipy.tracking.streamline import set_number_of_points
from dipy.tracking.distances import bundles_distances_mam
from dipy.tracking.vox2track import streamline_mapping
from sklearn import svm
import nibabel as nib
from joblib import Parallel, delayed
import time
from dipy.tracking.utils import length
def comp_dsc(estimated_tract, true_tract):
aff=np.array([[-1.25, 0, 0, 90],[0, 1.25, 0, -126],[0, 0, 1.25, -72],[0, 0, 0, 1]])
voxel_list_estimated_tract = streamline_mapping(estimated_tract, affine=aff).keys()
voxel_list_true_tract = streamline_mapping(true_tract, affine=aff).keys()
TP = len(set(voxel_list_estimated_tract).intersection(set(voxel_list_true_tract)))
vol_A = len(set(voxel_list_estimated_tract))
vol_B = len(set(voxel_list_true_tract))
DSC = 2.0 * float(TP) / float(vol_A + vol_B)
return DSC
def show_tract(segmented_tract_positive, color_positive,color_negative,segmented_tract_negative):
"""Visualization of the segmented tract.
"""
ren = fvtk.ren()
fvtk.add(ren, fvtk.line(segmented_tract_positive.tolist(),
colors=color_positive,
linewidth=2,
opacity=0.3))
# fvtk.add(ren, fvtk.line(segmented_tract_negative.tolist(),
# colors=color_negative,
# linewidth=2,
# opacity=0.3))
fvtk.show(ren)
fvtk.clear(ren)
def load(filename):
"""Load tractogram from TRK file
"""
wholeTract= nib.streamlines.load(filename)
wholeTract = wholeTract.streamlines
return wholeTract
def resample(streamlines, no_of_points):
"""Resample streamlines using 12 points and also flatten the streamlines
"""
return np.array([set_number_of_points(s, no_of_points).ravel() for s in streamlines])
def build_kdtree(points, leafsize):
"""Build kdtree with resample streamlines
"""
return KDTree(points,leaf_size =leafsize)
def kdtree_query(tract,kd_tree):
"""compute 1 NN using kdtree query and return the id of NN
"""
dist_kdtree, ind_kdtree = kd_tree.query(tract, k=10)
return np.hstack(ind_kdtree)
def bundles_distances_mam_smarter_faster(A, B, n_jobs=-1, chunk_size=100):
"""Parallel version of bundles_distances_mam that also avoids
computing distances twice.
"""
lenA = len(A)
chunks = chunker(A, chunk_size)
if B is None:
dm = np.empty((lenA, lenA), dtype=np.float32)
dm[np.diag_indices(lenA)] = 0.0
results = Parallel(n_jobs=-1)(delayed(bundles_distances_mam)(ss, A[i*chunk_size+1:]) for i, ss in enumerate(chunks))
# Fill triu
for i, res in enumerate(results):
dm[(i*chunk_size):((i+1)*chunk_size), (i*chunk_size+1):] = res
# Copy triu to trid:
rows, cols = np.triu_indices(lenA, 1)
dm[cols, rows] = dm[rows, cols]
else:
dm = np.vstack(Parallel(n_jobs=n_jobs)(delayed(bundles_distances_mam)(ss, B) for ss in chunks))
return dm
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def create_train_data_set(train_subjectList,tract):
T_filename_full_brain="full1M_161731.trk"
wholeTractogram = load(T_filename_full_brain)
train_data=[]
for sub in train_subjectList:
print (sub)
T_filename=sub+tract
wholeTract = load (T_filename)
train_data=np.concatenate((train_data, wholeTract),axis=0)
###################kdtree#################
print ("train data Shape")
print (train_data.shape)
t0=time.time()
resample_tractogram=resample(wholeTractogram,no_of_points=no_of_points)
resample_tract=resample(train_data,no_of_points=no_of_points)
kd_tree=build_kdtree (resample_tractogram, leafsize=leafsize)
#kdtree query to retrive the NN id
query_idx=kdtree_query(resample_tract, kd_tree)
#extract the streamline from tractogram
unique_query_idx= np.unique(np.array(query_idx))
subsample_tract=wholeTractogram[ unique_query_idx]
wholeTract=np.array(wholeTract)
x_train = bundles_distances_mam_smarter_faster(train_data, subsample_tract )
print("Total amount of time tokdtree is %f seconds" % (time.time()-t0))
return x_train,subsample_tract,train_data
if __name__ == '__main__':
train_subjectList =["124422"]#"192540","117122", "192540", "106016", "201111","105115","100307","366446"]
#"136833","106016","100408","127933"]
tract = "_af.right.trk"
no_of_points=12
leafsize=10
################################ Train Data######################################
print ("Preparing Train Data")
x_train,subsample_tract,train_data= create_train_data_set(train_subjectList,tract)
print (x_train.shape)
train_lengths = list(length(train_data))
################labeling#######################
# siz=x_train.size
# y=np.ones(siz)
###################### Test Data################################
testTarget="111312"
testTarget_brain="full1M_"+testTarget+".trk"
print ("Preparing Test Data")
t0=time.time()
t_filename=testTarget_brain #"124422_af.left.trk"
test_data=load(t_filename)
test_lengths = list(length(test_data))
x_test = bundles_distances_mam_smarter_faster(test_data,subsample_tract )
print ("test data Shape")
print (x_test.shape )
##########################################
###########################one class SVM######################
gamma_value = 0.0001
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=gamma_value)
clf.fit(x_train)
####################################################
#x_train= np.array(x_train)
x_pred_train=clf.predict(x_train.tolist())
n_error_test = x_pred_train[x_pred_train==-1].size
print('number of error for training =', n_error_test)
x_pred_test=clf.predict(x_test.tolist())
n_error_test = x_pred_test[x_pred_test==-1].size
print('number of error for testing=',n_error_test)
###########################visualize tract######################
test_data=np.array(test_data)
segmented_tract_positive= test_data[np.where(x_pred_test==1)]
segmented_tract_negative= test_data[np.where(x_pred_test==-1)]
print("Total amount of time to compute svm is %f seconds" % (time.time()-t0))
print("Show the tract")
color_positive= colors.green
color_negative=colors.red
show_tract(segmented_tract_positive, color_positive,color_negative,segmented_tract_negative)
###########################Calculating Dice Similarity Co-efficient###########################
trueTract=load(testTarget + tract)
dsc=comp_dsc(segmented_tract_positive,trueTract)
print("Accuracy: ",dsc)