-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpostprocessing4ExploreDTIdata.py
163 lines (109 loc) · 3.99 KB
/
postprocessing4ExploreDTIdata.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
# coding: utf-8
# Import data
# In[2]:
def importData(ftrk,ffa,fdwi):
import nibabel as nib
fa_img = nib.load(ffa)
fa = fa_img.get_data()
img = nib.load(fdwi)
data = img.get_data()
affine = img.get_affine()
from nibabel import trackvis
streams, hdr = trackvis.read(ftrk)
streamlines = [s[0] for s in streams]
return (fa,affine,data,streamlines)
# Length infos
# In[3]:
def getLengths(streamlines):
from dipy.tracking.utils import length
lengths = list(length(streamlines))
nb_stl = len(streamlines)
min_len = min(length(streamlines))
max_len = max(length(streamlines))
print('Nb. streamlines:')
print(nb_stl)
print('Min length:')
print(min_len)
print('Max length:')
print(max_len)
return lengths
# Filter with length
# In[4]:
def filterLength(streamlines, thr_len):
from dipy.tracking.utils import length
new_streamlines = [ s for s, l in zip(streamlines, getLengths(streamlines)) if l > thr_len ] #3.5 #2.5
#new_streamlines_l = list(new_streamlines)
new_lengths = list(length(new_streamlines))
print('Nb. new streamlines:')
print(len(new_streamlines))
return new_streamlines
# QuickBundle
# In[16]:
def computeQuickBundles(streamlines, threshold): #1.
from dipy.segment.clustering import QuickBundles
qb = QuickBundles(threshold)
clusters = qb.cluster(streamlines)
print("Nb. clusters:", len(clusters))
#print("Cluster sizes:", map(len, clusters))
print("Nb. small clusters (<10 streamlines):", sum(clusters < 10))
#print("Streamlines indices of the first cluster:\n", clusters[0].indices)
#print("Centroid of the last cluster:\n", clusters[-1].centroid)
return clusters
# In[17]:
def renderCentroids(streamlines, clusters):
from dipy.viz import fvtk
import numpy as np
ren = fvtk.ren()
ren.SetBackground(0, 0, 0)
colormap = fvtk.create_colormap(np.arange(len(clusters)))
colormap_full = np.ones((len(streamlines), 3))
for cluster in clusters:
colormap_full[cluster.indices] = np.random.rand(3)
#fvtk.add(ren, fvtk.streamtube(streamlines, fvtk.colors.white, opacity=0.05))
fvtk.add(ren, fvtk.line(clusters.centroids, linewidth=0.4, opacity=1))
#fvtk.record(ren, n_frames=1, out_path='fornix_centroids.png', size=(600, 600))
fvtk.show(ren)
fvtk.clear(ren)
# In[1]:
def renderBundles(streamlines, clusters):
from dipy.viz import fvtk
import numpy as np
ren = fvtk.ren()
ren.SetBackground(0, 0, 0)
colormap = fvtk.create_colormap(np.arange(len(clusters)))
colormap_full = np.ones((len(streamlines), 3))
for cluster in clusters:
colormap_full[cluster.indices] = np.random.rand(3)
fvtk.add(ren, fvtk.line(streamlines, colormap_full))
#fvtk.record(ren, n_frames=1, out_path='fornix_clusters.png', size=(600, 600))
fvtk.show(ren)
fvtk.clear(ren)
# Filter if small bundle
# In[21]:
def filterSmallBundles(streamlines, clusters, len_thr): #10
j = 0
smallbundles_list =[]
for c,i in zip(clusters,range(len(clusters))):
if len(c)<len_thr:
j = j+1
#print j
#print clusters[i]
for ii in range(len(clusters[i])):
smallbundles_list.append(clusters[i].indices[ii])
print 'Nb. streamlines from small bundles: '
print len(smallbundles_list)
#print smallbundles_list
new_streamlines = []
for i in range(len(streamlines)):
if i not in smallbundles_list:
new_streamlines.append(streamlines[i])
print 'Nb. streamlines before filtering'
print len(streamlines)
print 'Nb. streamlines after filtering'
print len(new_streamlines)
return new_streamlines
# Print info on the number of streamlines, bundles and min and max length
# In[ ]:
def getData4Analyses(streamlines,clusters,lengths):
print('Nb. streamlines, Nb. Bundles, Min length, Max length')
print len(streamlines),len(clusters),min(lengths),max(lengths)