-
Notifications
You must be signed in to change notification settings - Fork 429
/
cluster_confidence.py
184 lines (129 loc) · 5.23 KB
/
cluster_confidence.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
"""
=====================================================
Calculation of Outliers with Cluster Confidence Index
=====================================================
This is an outlier scoring method that compares the pathways of each streamline
in a bundle (pairwise) and scores each streamline by how many other streamlines
have similar pathways. The details can be found in [Jordan_2018_plm]_.
"""
from dipy.data import default_sphere, read_stanford_labels
from dipy.direction import peaks_from_model
from dipy.reconst.shm import CsaOdfModel
from dipy.tracking.stopping_criterion import ThresholdStoppingCriterion
from dipy.tracking import utils
from dipy.tracking.local_tracking import LocalTracking
from dipy.tracking.streamline import Streamlines, cluster_confidence
from dipy.tracking.utils import length
from dipy.viz import actor, window
import matplotlib.pyplot as plt
"""
First, we need to generate some streamlines. For a more complete
description of these steps, please refer to the CSA Probabilistic Tracking and
the Visualization of ROI Surface Rendered with Streamlines Tutorials.
"""
hardi_img, gtab, labels_img = read_stanford_labels()
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.affine
white_matter = (labels == 1) | (labels == 2)
csa_model = CsaOdfModel(gtab, sh_order=6)
csa_peaks = peaks_from_model(csa_model, data, default_sphere,
relative_peak_threshold=.8,
min_separation_angle=45,
mask=white_matter)
stopping_criterion = ThresholdStoppingCriterion(csa_peaks.gfa, .25)
"""
We will use a slice of the anatomically-based corpus callosum ROI as our
seed mask to demonstrate the method.
"""
# Make a corpus callosum seed mask for tracking
seed_mask = labels == 2
seeds = utils.seeds_from_mask(seed_mask, affine, density=[1, 1, 1])
# Make a streamline bundle model of the corpus callosum ROI connectivity
streamlines = LocalTracking(csa_peaks, stopping_criterion, seeds, affine,
step_size=2)
streamlines = Streamlines(streamlines)
"""
We do not want our results inflated by short streamlines, so we remove
streamlines shorter than 40mm prior to calculating the CCI.
"""
lengths = list(length(streamlines))
long_streamlines = Streamlines()
for i, sl in enumerate(streamlines):
if lengths[i] > 40:
long_streamlines.append(sl)
"""
Now we calculate the Cluster Confidence Index using the corpus callosum
streamline bundle and visualize them.
"""
cci = cluster_confidence(long_streamlines)
# Visualize the streamlines, colored by cci
ren = window.Renderer()
hue = [0.5, 1]
saturation = [0.0, 1.0]
lut_cmap = actor.colormap_lookup_table(scale_range=(cci.min(), cci.max()/4),
hue_range=hue,
saturation_range=saturation)
bar3 = actor.scalar_bar(lut_cmap)
ren.add(bar3)
stream_actor = actor.line(long_streamlines, cci, linewidth=0.1,
lookup_colormap=lut_cmap)
ren.add(stream_actor)
"""
If you set interactive to True (below), the rendering will pop up in an
interactive window.
"""
interactive = False
if interactive:
window.show(ren)
window.record(ren, n_frames=1, out_path='cci_streamlines.png',
size=(800, 800))
"""
.. figure:: cci_streamlines.png
:align: center
Cluster Confidence Index of corpus callosum dataset.
If you think of each streamline as a sample of a potential pathway through a
complex landscape of white matter anatomy probed via water diffusion,
intuitively we have more confidence that pathways represented by many samples
(streamlines) reflect a more stable representation of the underlying phenomenon
we are trying to model (anatomical landscape) than do lone samples.
The CCI provides a voting system where by each streamline (within a set
tolerance) gets to vote on how much support it lends to. Outlier pathways score
relatively low on CCI, since they do not have many streamlines voting for them.
These outliers can be removed by thresholding on the CCI metric.
"""
fig, ax = plt.subplots(1)
ax.hist(cci, bins=100, histtype='step')
ax.set_xlabel('CCI')
ax.set_ylabel('# streamlines')
fig.savefig('cci_histogram.png')
"""
.. figure:: cci_histogram.png
:align: center
Histogram of Cluster Confidence Index values.
Now we threshold the CCI, defining outliers as streamlines that score below 1.
"""
keep_streamlines = Streamlines()
for i, sl in enumerate(long_streamlines):
if cci[i] >= 1:
keep_streamlines.append(sl)
# Visualize the streamlines we kept
ren = window.Renderer()
keep_streamlines_actor = actor.line(keep_streamlines, linewidth=0.1)
ren.add(keep_streamlines_actor)
interactive = False
if interactive:
window.show(ren)
window.record(ren, n_frames=1, out_path='filtered_cci_streamlines.png',
size=(800, 800))
"""
.. figure:: filtered_cci_streamlines.png
:align: center
Outliers, defined as streamlines scoring CCI < 1, were excluded.
References
----------
.. [Jordan_2018_plm] Jordan, K., Amirbekian, B., Keshavan, A., Henry, R.G.
"Cluster Confidence Index: A Streamline‐Wise Pathway Reproducibility Metric
for Diffusion‐Weighted MRI Tractography", Journal of Neuroimaging, 2017.
.. include:: ../links_names.inc
"""