-
Notifications
You must be signed in to change notification settings - Fork 429
/
tracking_stopping_criterion.py
372 lines (301 loc) · 13 KB
/
tracking_stopping_criterion.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""
=================================================
Using Various Stopping Criterion for Tractography
=================================================
The stopping criterion determines if the tracking stops or continues at each
tracking position. The tracking stops when it reaches an ending region
(e.g. low FA, gray matter or corticospinal fluid regions) or exits the image
boundaries. The tracking also stops if the direction getter has no direction
to follow.
Each stopping criterion determines if the stopping is 'valid' or
'invalid'. A streamline is 'valid' when the stopping criterion determines if
the streamline stops in a position classified as 'ENDPOINT' or 'OUTSIDEIMAGE'.
A streamline is 'invalid' when it stops in a position classified as
'TRACKPOINT' or 'INVALIDPOINT'. These conditions are described below. The
'LocalTracking' generator can be set to output all generated streamlines
or only the 'valid' ones. See Girard et al. (2004) [Girard2014]_ and Smith et
al.(2012) [Smith2012]_ for more details on these methods.
This example is an extension of the
:ref:`example_tracking_deterministic` example. We begin by loading the
data, creating a seeding mask from white matter voxels of the corpus callosum,
fitting a Constrained Spherical Deconvolution (CSD) reconstruction
model and creating the maximum deterministic direction getter.
"""
import matplotlib.pyplot as plt
import numpy as np
from dipy.core.gradients import gradient_table
from dipy.data import get_fnames, default_sphere
from dipy.direction import DeterministicMaximumDirectionGetter
from dipy.io.gradients import read_bvals_bvecs
from dipy.io.image import load_nifti, load_nifti_data
from dipy.io.streamline import save_trk
from dipy.io.stateful_tractogram import Space, StatefulTractogram
from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel,
auto_response)
from dipy.reconst.dti import fractional_anisotropy, TensorModel
from dipy.tracking import utils
from dipy.tracking.local_tracking import LocalTracking
from dipy.tracking.streamline import Streamlines
from dipy.tracking.stopping_criterion import (ActStoppingCriterion,
BinaryStoppingCriterion,
ThresholdStoppingCriterion)
from dipy.viz import window, actor, colormap, has_fury
# Enables/disables interactive visualization
interactive = False
hardi_fname, hardi_bval, hardi_bvec = get_fnames('stanford_hardi')
label_fname = get_fnames('stanford_labels')
_, _, f_pve_wm = get_fnames('stanford_pve_maps')
data, affine, hardi_img = load_nifti(hardi_fname, return_img=True)
labels = load_nifti_data(label_fname)
bvals, bvecs = read_bvals_bvecs(hardi_bval, hardi_bvec)
gtab = gradient_table(bvals, bvecs)
white_matter = load_nifti_data(f_pve_wm)
seed_mask = (labels == 2)
seed_mask[white_matter < 0.5] = 0
seeds = utils.seeds_from_mask(seed_mask, affine, density=2)
response, ratio = auto_response(gtab, data, roi_radius=10, fa_thr=0.7)
csd_model = ConstrainedSphericalDeconvModel(gtab, response)
csd_fit = csd_model.fit(data, mask=white_matter)
dg = DeterministicMaximumDirectionGetter.from_shcoeff(csd_fit.shm_coeff,
max_angle=30.,
sphere=default_sphere)
"""
Threshold Stopping Criterion
============================
A scalar map can be used to define where the tracking stops. The threshold
stopping criterion uses a scalar map to stop the tracking whenever the
interpolated scalar value is lower than a fixed threshold. Here, we show
an example using the fractional anisotropy (FA) map of the DTI model.
The threshold stopping criterion uses a trilinear interpolation at the
tracking position.
**Parameters**
- metric_map: numpy array [:, :, :]
- threshold: float
**Stopping States**
- 'ENDPOINT': stops at a position where metric_map < threshold; the streamline
reached the target stopping area.
- 'OUTSIDEIMAGE': stops at a position outside of metric_map; the streamline
reached an area outside the image where no direction data is available.
- 'TRACKPOINT': stops at a position because no direction is available; the
streamline is stopping where metric_map >= threshold, but there is no valid
direction to follow.
- 'INVALIDPOINT': N/A.
"""
tensor_model = TensorModel(gtab)
tenfit = tensor_model.fit(data, mask=labels > 0)
FA = fractional_anisotropy(tenfit.evals)
threshold_criterion = ThresholdStoppingCriterion(FA, .2)
fig = plt.figure()
mask_fa = FA.copy()
mask_fa[mask_fa < 0.2] = 0
plt.xticks([])
plt.yticks([])
plt.imshow(mask_fa[:, :, data.shape[2] // 2].T, cmap='gray', origin='lower',
interpolation='nearest')
fig.tight_layout()
fig.savefig('threshold_fa.png')
"""
.. figure:: threshold_fa.png
:align: center
**Thresholded fractional anisotropy map.**
"""
streamline_generator = LocalTracking(dg,
threshold_criterion,
seeds,
affine,
step_size=.5,
return_all=True)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_probabilistic_thresh_all.trk")
if has_fury:
r = window.Renderer()
r.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(r, out_path='tractogram_deterministic_thresh_all.png',
size=(800, 800))
if interactive:
window.show(r)
"""
.. figure:: tractogram_deterministic_thresh_all.png
:align: center
**Corpus Callosum using deterministic tractography with a thresholded
fractional anisotropy mask.**
"""
"""
Binary Stopping Criterion
=========================
A binary mask can be used to define where the tracking stops. The binary
stopping criterion stops the tracking whenever the tracking position is outside
the mask. Here, we show how to obtain the binary stopping criterion from
the white matter mask defined above. The binary stopping criterion uses a
nearest-neighborhood interpolation at the tracking position.
**Parameters**
- mask: numpy array [:, :, :]
**Stopping States**
- 'ENDPOINT': stops at a position where mask = 0; the streamline
reached the target stopping area.
- 'OUTSIDEIMAGE': stops at a position outside of metric_map; the streamline
reached an area outside the image where no direction data is available.
- 'TRACKPOINT': stops at a position because no direction is available; the
streamline is stopping where mask > 0, but there is no valid direction to
follow.
- 'INVALIDPOINT': N/A.
"""
binary_criterion = BinaryStoppingCriterion(white_matter == 1)
fig = plt.figure()
plt.xticks([])
plt.yticks([])
fig.tight_layout()
plt.imshow(white_matter[:, :, data.shape[2] // 2].T, cmap='gray',
origin='lower', interpolation='nearest')
fig.savefig('white_matter_mask.png')
"""
.. figure:: white_matter_mask.png
:align: center
**White matter binary mask.**
"""
streamline_generator = LocalTracking(dg,
binary_criterion,
seeds,
affine,
step_size=.5,
return_all=True)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_deterministic_binary_all.trk")
if has_fury:
r = window.Renderer()
r.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(r, out_path='tractogram_deterministic_binary_all.png',
size=(800, 800))
if interactive:
window.show(r)
"""
.. figure:: tractogram_deterministic_binary_all.png
:align: center
**Corpus Callosum using deterministic tractography with a binary white
matter mask.**
"""
"""
ACT Stopping Criterion
======================
Anatomically-constrained tractography (ACT) [Smith2012]_ uses information from
anatomical images to determine when the tractography stops. The ``include_map``
defines when the streamline reached a 'valid' stopping region (e.g. gray
matter partial volume estimation (PVE) map) and the ``exclude_map`` defines
when the streamline reached an 'invalid' stopping region (e.g. corticospinal
fluid PVE map). The background of the anatomical image should be added to the
``include_map`` to keep streamlines exiting the brain (e.g. through the
brain stem). The ACT stopping criterion uses a trilinear interpolation
at the tracking position.
**Parameters**
- ``include_map``: numpy array ``[:, :, :]``,
- ``exclude_map``: numpy array ``[:, :, :]``,
**Stopping States**
- 'ENDPOINT': stops at a position where ``include_map`` > 0.5; the streamline
reached the target stopping area.
- 'OUTSIDEIMAGE': stops at a position outside of ``include_map`` or
``exclude_map``; the streamline reached an area outside the image where no
direction data is available.
- 'TRACKPOINT': stops at a position because no direction is available; the
streamline is stopping where ``include_map`` < 0.5 and ``exclude_map`` < 0.5,
but there is no valid direction to follow.
- 'INVALIDPOINT': ``exclude_map`` > 0.5; the streamline reach a position which
is anatomically not plausible.
"""
f_pve_csf, f_pve_gm, f_pve_wm = get_fnames('stanford_pve_maps')
pve_csf_data = load_nifti_data(f_pve_csf)
pve_gm_data = load_nifti_data(f_pve_gm)
pve_wm_data = load_nifti_data(f_pve_wm)
background = np.ones(pve_gm_data.shape)
background[(pve_gm_data + pve_wm_data + pve_csf_data) > 0] = 0
include_map = pve_gm_data
include_map[background > 0] = 1
exclude_map = pve_csf_data
act_criterion = ActStoppingCriterion(include_map, exclude_map)
fig = plt.figure()
plt.subplot(121)
plt.xticks([])
plt.yticks([])
plt.imshow(include_map[:, :, data.shape[2] // 2].T, cmap='gray',
origin='lower', interpolation='nearest')
plt.subplot(122)
plt.xticks([])
plt.yticks([])
plt.imshow(exclude_map[:, :, data.shape[2] // 2].T, cmap='gray',
origin='lower', interpolation='nearest')
fig.tight_layout()
fig.savefig('act_maps.png')
"""
.. figure:: act_maps.png
:align: center
**Include (left) and exclude (right) maps for ACT.**
"""
streamline_generator = LocalTracking(dg,
act_criterion,
seeds,
affine,
step_size=.5,
return_all=True)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_deterministic_act_all.trk")
if has_fury:
r = window.Renderer()
r.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(r, out_path='tractogram_deterministic_act_all.png',
size=(800, 800))
if interactive:
window.show(r)
"""
.. figure:: tractogram_deterministic_act_all.png
:align: center
**Corpus Callosum using deterministic tractography with ACT stopping
criterion.**
"""
streamline_generator = LocalTracking(dg,
act_criterion,
seeds,
affine,
step_size=.5,
return_all=False)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_deterministic_act_valid.trk")
if has_fury:
r = window.Renderer()
r.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(r, out_path='tractogram_deterministic_act_valid.png',
size=(800, 800))
if interactive:
window.show(r)
"""
.. figure:: tractogram_deterministic_act_valid.png
:align: center
**Corpus Callosum using deterministic tractography with ACT stopping
criterion. Streamlines ending in gray matter region only.**
"""
"""
The threshold and binary stopping criterion use respectively a scalar map and a
binary mask to stop the tracking. The ACT stopping criterion use partial volume
fraction (PVE) maps from an anatomical image to stop the tracking.
Additionally, the ACT stopping criterion determines if the tracking stopped in
expected regions (e.g. gray matter) and allows the user to get only
streamlines stopping in those regions.
Notes
------
Currently,the proposed method that cuts streamlines going through
subcortical gray matter regions is not implemented. The
backtracking technique for streamlines reaching INVALIDPOINT is not
implemented either [Smith2012]_.
References
----------
.. [Smith2012] Smith, R. E., Tournier, J.-D., Calamante, F., & Connelly, A.
Anatomically-constrained tractography: Improved diffusion MRI
streamlines tractography through effective use of anatomical
information. NeuroImage, 63(3), 1924-1938, 2012.
.. [Girard2014] Girard, G., Whittingstall, K., Deriche, R., & Descoteaux, M.
Towards quantitative connectivity analysis: reducing tractography biases.
NeuroImage, 98, 266-278, 2014.
.. include:: ../links_names.inc
"""