/usr/share/doc/dipy/examples/tracking_tissue_classifier.py is in python-dipy 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | """
=================================================
Using Various Tissue Classifiers for Tractography
=================================================
The tissue classifier 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 tissue classifier determines if the stopping is 'valid' or
'invalid'. A streamline is 'valid' when the tissue classifier 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.
This example is an extension of the
:ref:`example_deterministic_fiber_tracking` example. We begin by loading the
data, fitting a Constrained Spherical Deconvolution (CSD) reconstruction
model and creating the maximum deterministic direction getter.
"""
import numpy as np
from dipy.data import read_stanford_labels, default_sphere
from dipy.direction import DeterministicMaximumDirectionGetter
from dipy.io.trackvis import save_trk
from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel,
auto_response)
from dipy.tracking.local import LocalTracking
from dipy.tracking import utils
from dipy.viz import fvtk
from dipy.viz.colormap import line_colors
ren = fvtk.ren()
hardi_img, gtab, labels_img = read_stanford_labels()
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.get_affine()
seed_mask = labels == 2
white_matter = (labels == 1) | (labels == 2)
seeds = utils.seeds_from_mask(seed_mask, density=2, affine=affine)
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 Tissue Classifier
---------------------------
A scalar map can be used to define where the tracking stops. The threshold
tissue classifier 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 tissue classifier uses a trilinear interpolation at the
tracking position.
**Parameters**
- metric_map: numpy array [:, :, :]
- threshold: float
**Stopping criterion**
- 'ENDPOINT': metric_map < threshold,
- 'OUTSIDEIMAGE': tracking point outside of metric_map,
- 'TRACKPOINT': stop because no direction is available,
- 'INVALIDPOINT': N/A.
"""
import matplotlib.pyplot as plt
import dipy.reconst.dti as dti
from dipy.reconst.dti import fractional_anisotropy
from dipy.tracking.local import ThresholdTissueClassifier
tensor_model = dti.TensorModel(gtab)
tenfit = tensor_model.fit(data, mask=labels > 0)
FA = fractional_anisotropy(tenfit.evals)
threshold_classifier = ThresholdTissueClassifier(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.**
"""
all_streamlines_threshold_classifier = LocalTracking(dg,
threshold_classifier,
seeds,
affine,
step_size=.5,
return_all=True)
save_trk("deterministic_threshold_classifier_all.trk",
all_streamlines_threshold_classifier,
affine,
labels.shape)
streamlines = [sl for sl in all_streamlines_threshold_classifier]
fvtk.clear(ren)
fvtk.add(ren, fvtk.line(streamlines, line_colors(streamlines)))
fvtk.record(ren, out_path='all_streamlines_threshold_classifier.png',
size=(600, 600))
"""
.. figure:: all_streamlines_threshold_classifier.png
:align: center
**Deterministic tractography using a thresholded fractional anisotropy.**
"""
"""
Binary Tissue Classifier
------------------------
A binary mask can be used to define where the tracking stops. The binary
tissue classifier stops the tracking whenever the tracking position is outside
the mask. Here, we show how to obtain the binary tissue classifier from
the white matter mask defined above. The binary tissue classifier uses a
nearest-neighborhood interpolation at the tracking position.
**Parameters**
- mask: numpy array [:, :, :]
**Stopping criterion**
- 'ENDPOINT': mask = 0
- 'OUTSIDEIMAGE': tracking point outside of mask
- 'TRACKPOINT': no direction is available
- 'INVALIDPOINT': N/A
"""
from dipy.tracking.local import BinaryTissueClassifier
binary_classifier = BinaryTissueClassifier(white_matter)
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.**
"""
all_streamlines_binary_classifier = LocalTracking(dg,
binary_classifier,
seeds,
affine,
step_size=.5,
return_all=True)
save_trk("deterministic_binary_classifier_all.trk",
all_streamlines_binary_classifier,
affine,
labels.shape)
streamlines = [sl for sl in all_streamlines_binary_classifier]
fvtk.clear(ren)
fvtk.add(ren, fvtk.line(streamlines, line_colors(streamlines)))
fvtk.record(ren, out_path='all_streamlines_binary_classifier.png',
size=(600, 600))
"""
.. figure:: all_streamlines_binary_classifier.png
:align: center
**Deterministic tractography using a binary white matter mask.**
"""
"""
ACT Tissue Classifier
---------------------
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 tissue classifier uses a trilinear interpolation
at the tracking position.
**Parameters**
- include_map: numpy array [:, :, :],
- exclude_map: numpy array [:, :, :],
**Stopping criterion**
- 'ENDPOINT': include_map > 0.5,
- 'OUTSIDEIMAGE': tracking point outside of include_map or exclude_map,
- 'TRACKPOINT': no direction is available,
- 'INVALIDPOINT': exclude_map > 0.5.
"""
from dipy.data import read_stanford_pve_maps
from dipy.tracking.local import ActTissueClassifier
img_pve_csf, img_pve_gm, img_pve_wm = read_stanford_pve_maps()
background = np.ones(img_pve_gm.shape)
background[(img_pve_gm.get_data() +
img_pve_wm.get_data() +
img_pve_csf.get_data()) > 0] = 0
include_map = img_pve_gm.get_data()
include_map[background > 0] = 1
exclude_map = img_pve_csf.get_data()
act_classifier = ActTissueClassifier(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.**
"""
all_streamlines_act_classifier = LocalTracking(dg,
act_classifier,
seeds,
affine,
step_size=.5,
return_all=True)
save_trk("deterministic_act_classifier_all.trk",
all_streamlines_act_classifier,
affine,
labels.shape)
streamlines = [sl for sl in all_streamlines_act_classifier]
fvtk.clear(ren)
fvtk.add(ren, fvtk.line(streamlines, line_colors(streamlines)))
fvtk.record(ren, out_path='all_streamlines_act_classifier.png',
size=(600, 600))
"""
.. figure:: all_streamlines_act_classifier.png
:align: center
**Deterministic tractography using ACT stopping criterion.**
"""
valid_streamlines_act_classifier = LocalTracking(dg,
act_classifier,
seeds,
affine,
step_size=.5,
return_all=False)
save_trk("deterministic_act_classifier_valid.trk",
valid_streamlines_act_classifier,
affine,
labels.shape)
streamlines = [sl for sl in valid_streamlines_act_classifier]
fvtk.clear(ren)
fvtk.add(ren, fvtk.line(streamlines, line_colors(streamlines)))
fvtk.record(ren, out_path='valid_streamlines_act_classifier.png',
size=(600, 600))
"""
.. figure:: valid_streamlines_act_classifier.png
:align: center
**Deterministic tractography using a anatomically-constrained tractography
stopping criterion. Streamlines ending in gray matter region only.**
"""
"""
The threshold and binary tissue classifiers use respectively a scalar map and a
binary mask to stop the tracking. The ACT tissue classifier use partial volume
fraction (PVE) maps from an anatomical image to stop the tracking. Additionally,
the ACT tissue classifier 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 in ACT 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.
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.
"""
|