Note
Click here to download the full example code
Probabilistic fiber tracking is a way of reconstructing white matter connections using diffusion MR imaging. Like deterministic fiber tracking, the probabilistic approach follows the trajectory of a possible pathway step by step starting at a seed, however, unlike deterministic tracking, the tracking direction at each point along the path is chosen at random from a distribution. The distribution at each point is different and depends on the observed diffusion data at that point. The distribution of tracking directions at each point can be represented as a probability mass function (PMF) if the possible tracking directions are restricted to discrete numbers of well distributed points on a sphere.
This example is an extension of the example_tracking_introduction_eudx example. We’ll begin by repeating a few steps from that example, loading the data and fitting a Constrained Spherical Deconvolution (CSD) model.
from dipy.core.gradients import gradient_table
from dipy.data import get_fnames
from dipy.io.gradients import read_bvals_bvecs
from dipy.io.image import load_nifti, load_nifti_data
from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel,
auto_response_ssst)
from dipy.tracking import utils
from dipy.tracking.local_tracking import LocalTracking
from dipy.tracking.streamline import Streamlines
from dipy.tracking.stopping_criterion import ThresholdStoppingCriterion
from dipy.viz import window, actor, colormap, has_fury
# Enables/disables interactive visualization
interactive = False
hardi_fname, hardi_bval_fname, hardi_bvec_fname = get_fnames('stanford_hardi')
label_fname = get_fnames('stanford_labels')
data, affine, hardi_img = load_nifti(hardi_fname, return_img=True)
labels = load_nifti_data(label_fname)
bvals, bvecs = read_bvals_bvecs(hardi_bval_fname, hardi_bvec_fname)
gtab = gradient_table(bvals, bvecs)
seed_mask = (labels == 2)
white_matter = (labels == 1) | (labels == 2)
seeds = utils.seeds_from_mask(seed_mask, affine, density=1)
response, ratio = auto_response_ssst(gtab, data, roi_radii=10, fa_thr=0.7)
csd_model = ConstrainedSphericalDeconvModel(gtab, response, sh_order=6)
csd_fit = csd_model.fit(data, mask=white_matter)
0%| | 0/58788 [00:00<?, ?it/s]
1%|1 | 649/58788 [00:00<00:08, 6485.07it/s]
3%|2 | 1510/58788 [00:00<00:07, 7733.68it/s]
4%|4 | 2389/58788 [00:00<00:06, 8213.36it/s]
6%|5 | 3273/58788 [00:00<00:06, 8459.50it/s]
7%|7 | 4163/58788 [00:00<00:06, 8617.80it/s]
9%|8 | 5070/58788 [00:00<00:06, 8770.15it/s]
10%|# | 5978/58788 [00:00<00:05, 8869.44it/s]
12%|#1 | 6880/58788 [00:00<00:05, 8917.01it/s]
13%|#3 | 7775/58788 [00:00<00:05, 8926.85it/s]
15%|#4 | 8687/58788 [00:01<00:05, 8983.09it/s]
16%|#6 | 9617/58788 [00:01<00:05, 9077.68it/s]
18%|#7 | 10537/58788 [00:01<00:05, 9114.01it/s]
19%|#9 | 11449/58788 [00:01<00:05, 9081.69it/s]
21%|##1 | 12364/58788 [00:01<00:05, 9100.48it/s]
23%|##2 | 13300/58788 [00:01<00:04, 9176.25it/s]
24%|##4 | 14218/58788 [00:01<00:04, 9157.63it/s]
26%|##5 | 15139/58788 [00:01<00:04, 9173.00it/s]
27%|##7 | 16075/58788 [00:01<00:04, 9228.84it/s]
29%|##8 | 16998/58788 [00:01<00:04, 9203.98it/s]
31%|### | 17944/58788 [00:02<00:04, 9278.66it/s]
32%|###2 | 18872/58788 [00:02<00:04, 9210.64it/s]
34%|###3 | 19794/58788 [00:02<00:04, 9082.08it/s]
35%|###5 | 20717/58788 [00:02<00:04, 9124.61it/s]
37%|###6 | 21630/58788 [00:02<00:04, 9085.82it/s]
38%|###8 | 22553/58788 [00:02<00:03, 9128.60it/s]
40%|###9 | 23467/58788 [00:02<00:03, 9111.50it/s]
41%|####1 | 24387/58788 [00:02<00:03, 9136.70it/s]
43%|####3 | 25312/58788 [00:02<00:03, 9169.12it/s]
45%|####4 | 26230/58788 [00:02<00:03, 9059.11it/s]
46%|####6 | 27137/58788 [00:03<00:03, 8994.00it/s]
48%|####7 | 28037/58788 [00:03<00:03, 8931.31it/s]
49%|####9 | 28931/58788 [00:03<00:03, 8841.57it/s]
51%|##### | 29816/58788 [00:03<00:03, 8746.33it/s]
52%|#####2 | 30692/58788 [00:03<00:03, 8749.86it/s]
54%|#####3 | 31568/58788 [00:03<00:03, 8729.90it/s]
55%|#####5 | 32449/58788 [00:03<00:03, 8750.33it/s]
57%|#####6 | 33363/58788 [00:03<00:02, 8863.82it/s]
58%|#####8 | 34267/58788 [00:03<00:02, 8915.53it/s]
60%|#####9 | 35165/58788 [00:03<00:02, 8934.60it/s]
61%|######1 | 36093/58788 [00:04<00:02, 9035.47it/s]
63%|######2 | 36999/58788 [00:04<00:02, 9042.44it/s]
65%|######4 | 37930/58788 [00:04<00:02, 9119.95it/s]
66%|######6 | 38858/58788 [00:04<00:02, 9167.38it/s]
68%|######7 | 39775/58788 [00:04<00:02, 9154.91it/s]
69%|######9 | 40711/58788 [00:04<00:01, 9215.62it/s]
71%|####### | 41638/58788 [00:04<00:01, 9229.12it/s]
72%|#######2 | 42572/58788 [00:04<00:01, 9259.70it/s]
74%|#######3 | 43498/58788 [00:04<00:01, 9252.64it/s]
76%|#######5 | 44424/58788 [00:04<00:01, 9174.68it/s]
77%|#######7 | 45360/58788 [00:05<00:01, 9227.96it/s]
79%|#######8 | 46283/58788 [00:05<00:01, 9228.07it/s]
80%|######## | 47206/58788 [00:05<00:01, 9201.09it/s]
82%|########1 | 48140/58788 [00:05<00:01, 9240.65it/s]
83%|########3 | 49072/58788 [00:05<00:01, 9262.07it/s]
85%|########5 | 50001/58788 [00:05<00:00, 9270.01it/s]
87%|########6 | 50929/58788 [00:05<00:00, 9265.57it/s]
88%|########8 | 51856/58788 [00:05<00:00, 9219.25it/s]
90%|########9 | 52778/58788 [00:05<00:00, 9178.67it/s]
91%|#########1| 53696/58788 [00:05<00:00, 9143.74it/s]
93%|#########2| 54611/58788 [00:06<00:00, 9143.89it/s]
94%|#########4| 55528/58788 [00:06<00:00, 9150.91it/s]
96%|#########6| 56444/58788 [00:06<00:00, 9043.12it/s]
98%|#########7| 57349/58788 [00:06<00:00, 9005.38it/s]
99%|#########9| 58250/58788 [00:06<00:00, 8802.61it/s]
100%|##########| 58788/58788 [00:06<00:00, 8992.19it/s]
We use the GFA of the CSA model to build a stopping criterion.
from dipy.reconst.shm import CsaOdfModel
csa_model = CsaOdfModel(gtab, sh_order=6)
gfa = csa_model.fit(data, mask=white_matter).gfa
stopping_criterion = ThresholdStoppingCriterion(gfa, .25)
The Fiber Orientation Distribution (FOD) of the CSD model estimates the
distribution of small fiber bundles within each voxel. We can use this
distribution for probabilistic fiber tracking. One way to do this is to
represent the FOD using a discrete sphere. This discrete FOD can be used by the
ProbabilisticDirectionGetter
as a PMF for sampling tracking directions. We
need to clip the FOD to use it as a PMF because the latter cannot have negative
values. Ideally, the FOD should be strictly positive, but because of noise
and/or model failures sometimes it can have negative values.
from dipy.direction import ProbabilisticDirectionGetter
from dipy.data import small_sphere
from dipy.io.stateful_tractogram import Space, StatefulTractogram
from dipy.io.streamline import save_trk
fod = csd_fit.odf(small_sphere)
pmf = fod.clip(min=0)
prob_dg = ProbabilisticDirectionGetter.from_pmf(pmf, max_angle=30.,
sphere=small_sphere)
streamline_generator = LocalTracking(prob_dg, stopping_criterion, seeds,
affine, step_size=.5)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_probabilistic_dg_pmf.trk")
if has_fury:
scene = window.Scene()
scene.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(scene, out_path='tractogram_probabilistic_dg_pmf.png',
size=(800, 800))
if interactive:
window.show(scene)
One disadvantage of using a discrete PMF to represent possible tracking
directions is that it tends to take up a lot of memory (RAM). The size of the
PMF, the FOD in this case, must be equal to the number of possible tracking
directions on the hemisphere, and every voxel has a unique PMF. In this case
the data is (81, 106, 76)
and small_sphere
has 181 directions so the
FOD is (81, 106, 76, 181)
. One way to avoid sampling the PMF and holding it
in memory is to build the direction getter directly from the spherical harmonic
(SH) representation of the FOD. By using this approach, we can also use a
larger sphere, like default_sphere
which has 362 directions on the
hemisphere, without having to worry about memory limitations.
from dipy.data import default_sphere
prob_dg = ProbabilisticDirectionGetter.from_shcoeff(csd_fit.shm_coeff,
max_angle=30.,
sphere=default_sphere)
streamline_generator = LocalTracking(prob_dg, stopping_criterion, seeds,
affine, step_size=.5)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_probabilistic_dg_sh.trk")
if has_fury:
scene = window.Scene()
scene.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(scene, out_path='tractogram_probabilistic_dg_sh.png',
size=(800, 800))
if interactive:
window.show(scene)
Not all model fits have the shm_coeff
attribute because not all models use
this basis to represent the data internally. However we can fit the ODF of any
model to the spherical harmonic basis using the peaks_from_model
function.
from dipy.direction import peaks_from_model
peaks = peaks_from_model(csd_model, data, default_sphere, .5, 25,
mask=white_matter, return_sh=True, parallel=True,
num_processes=2)
fod_coeff = peaks.shm_coeff
prob_dg = ProbabilisticDirectionGetter.from_shcoeff(fod_coeff, max_angle=30.,
sphere=default_sphere)
streamline_generator = LocalTracking(prob_dg, stopping_criterion, seeds,
affine, step_size=.5)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, hardi_img, Space.RASMM)
save_trk(sft, "tractogram_probabilistic_dg_sh_pfm.trk")
if has_fury:
scene = window.Scene()
scene.add(actor.line(streamlines, colormap.line_colors(streamlines)))
window.record(scene, out_path='tractogram_probabilistic_dg_sh_pfm.png',
size=(800, 800))
if interactive:
window.show(scene)
Total running time of the script: ( 0 minutes 56.097 seconds)