<jupyter_start><jupyter_text>Reconstruction of the diffusion signal with the Tensor modelThe diffusion tensor model is a model that describes the diffusion within avoxel. First proposed by Basser and colleagues [Basser1994]_, it has been veryinfluential in demonstrating the utility of diffusion MRI in characterizing themicro-structure of white matter tissue and of the biophysical properties oftissue, inferred from local diffusion properties and it is still very commonlyused.The diffusion tensor models the diffusion signal as:$\frac{S(\mathbf{g}, b)}{S_0} = e^{-b\mathbf{g}^T \mathbf{D} \mathbf{g}}$Where $\mathbf{g}$ is a unit vector in 3 space indicating the direction ofmeasurement and b are the parameters of measurement, such as the strength andduration of diffusion-weighting gradient. $S(\mathbf{g}, b)$ is thediffusion-weighted signal measured and $S_0$ is the signal conducted in ameasurement with no diffusion weighting. $\mathbf{D}$ is a positive-definite quadraticform, which contains six free parameters to be fit. These six parameters are:   $\mathbf{D} = \begin{pmatrix} D_{xx} & D_{xy} & D_{xz} \\                       D_{yx} & D_{yy} & D_{yz} \\                       D_{zx} & D_{zy} & D_{zz} \\ \end{pmatrix}$This matrix is a variance/covariance matrix of the diffusivity along the threespatial dimensions. Note that we can assume that diffusivity has antipodalsymmetry, so elements across the diagonal are equal. For example:$D_{xy} = D_{yx}$. This is why there are only 6 free parameters to estimatehere.In the following example we show how to reconstruct your diffusion datasetsusing a single tensor model.First import the necessary modules:``numpy`` is for numerical computation<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import matplotlib
%matplotlib inline<jupyter_output><empty_output><jupyter_text>We will also import elements needed in order to display images in the notebook:<jupyter_code>from IPython import display<jupyter_output><empty_output><jupyter_text>``nibabel`` is for loading imaging datasets<jupyter_code>import nibabel as nib<jupyter_output><empty_output><jupyter_text>``dipy.reconst`` is for the reconstruction algorithms which we use to createvoxel models from the raw data.<jupyter_code>import dipy.reconst.dti as dti<jupyter_output><empty_output><jupyter_text>``dipy.data`` is used for small datasets that we use in tests and examples.<jupyter_code>from dipy.data import fetch_stanford_hardi<jupyter_output><empty_output><jupyter_text>Fetch will download the raw dMRI dataset of a single subject. The size of thedataset is 87 MBytes.  You only need to fetch once.<jupyter_code>fetch_stanford_hardi()<jupyter_output>Dataset is already in place. If you want to fetch it again, please first remove the folder /Users/arokem/.dipy/stanford_hardi<jupyter_text>Next, we read the saved dataset<jupyter_code>from dipy.data import read_stanford_hardi
img, gtab = read_stanford_hardi()<jupyter_output><empty_output><jupyter_text>img contains a nibabel Nifti1Image object (with the data) and gtab contains aGradientTable object (information about the gradients e.g. b-values andb-vectors).<jupyter_code>data = img.get_data()
print('data.shape (%d, %d, %d, %d)' % data.shape)<jupyter_output>data.shape (81, 106, 76, 160)<jupyter_text>First of all, we mask and crop the data. This is a quick way to avoidcalculating Tensors on the background of the image. This is done using dipy'smask module.<jupyter_code>from dipy.segment.mask import median_otsu
maskdata, mask = median_otsu(data, 3, 1, True,
                             vol_idx=range(10, 50), dilate=2)
print('maskdata.shape (%d, %d, %d, %d)' % maskdata.shape)<jupyter_output>maskdata.shape (71, 87, 62, 160)<jupyter_text>Now that we have prepared the datasets we can go forward with the voxelreconstruction. First, we instantiate the Tensor model in the following way.<jupyter_code>tenmodel = dti.TensorModel(gtab)<jupyter_output><empty_output><jupyter_text>Fitting the data is very simple. We just need to call the fit method of theTensorModel in the following way:<jupyter_code>tenfit = tenmodel.fit(maskdata)<jupyter_output><empty_output><jupyter_text>The fit method creates a TensorFit object which contains the fitting parametersand other attributes of the model. For example we can generate fractionalanisotropy (FA) from the eigen-values of the tensor. FA is used to characterizethe degree to which the distribution of diffusion in a voxel isdirectional. That is, whether there is relatively unrestricted diffusion in oneparticular direction.Mathematically, FA is defined as the normalized variance of the eigen-values ofthe tensor:$FA = \sqrt{\frac{1}{2}\frac{(\lambda_1-\lambda_2)^2+(\lambda_1-            \lambda_3)^2+(\lambda_2-\lambda_3)^2}{\lambda_1^2+            \lambda_2^2+\lambda_3^2}}$Note that FA should be interperted carefully. It may be an indication of thedensity of packing of fibers in a voxel, and the amount of myelin wrapping theseaxons, but it is not always a measure of "tissue integrity". For example, FAmay decrease in locations in which there is fanning of white matter fibers, orwhere more than one population of white matter fibers crosses.<jupyter_code>from dipy.reconst.dti import fractional_anisotropy, color_fa, lower_triangular
FA = fractional_anisotropy(tenfit.evals)<jupyter_output><empty_output><jupyter_text>In the background of the image the fitting will not be accurate there is nosignal and possibly we will find FA values with nans (not a number). We caneasily remove these in the following way.<jupyter_code>FA[np.isnan(FA)] = 0<jupyter_output><empty_output><jupyter_text>Saving the FA images is very easy using nibabel. We need the FA volume and theaffine matrix which transform the image's coordinates to the world coordinates.Here, we choose to save the FA in float32.<jupyter_code>fa_img = nib.Nifti1Image(FA.astype(np.float32), img.get_affine())
nib.save(fa_img, 'tensor_fa.nii.gz')<jupyter_output><empty_output><jupyter_text>You can now see the result with any nifti viewer or check it slice by sliceusing matplotlib_'s imshow. In the same way you can save the eigen values, theeigen vectors or any other properties of the Tensor.<jupyter_code>evecs_img = nib.Nifti1Image(tenfit.evecs.astype(np.float32), img.get_affine())
nib.save(evecs_img, 'tensor_evecs.nii.gz')<jupyter_output><empty_output><jupyter_text>Other tensor statistics can be calculated from the `tenfit` object. For example,a commonly calculated statistic is the mean diffusivity (MD). This is simply themean of the  eigenvalues of the tensor. Since FA is a normalizedmeasure of variance and MD is the mean, they are often used as complimentarymeasures. In `dipy`, there are two equivalent ways to calculate the meandiffusivity. One is by calling the `mean_diffusivity` module function on theeigen-values of the TensorFit class instance:<jupyter_code>MD1 = dti.mean_diffusivity(tenfit.evals)
nib.save(nib.Nifti1Image(MD1.astype(np.float32), img.get_affine()), 'tensors_md.nii.gz')<jupyter_output><empty_output><jupyter_text>The other is to call the TensorFit class method:<jupyter_code>MD2 = tenfit.md<jupyter_output><empty_output><jupyter_text>Obviously, the quantities are identical.We can also compute the colored FA or RGB-map [Pajevic1999]_. First, we make surethat the FA is scaled between 0 and 1, we compute the RGB map and save it.<jupyter_code>FA = np.clip(FA, 0, 1)
RGB = color_fa(FA, tenfit.evecs)
nib.save(nib.Nifti1Image(np.array(255 * RGB, 'uint8'), img.get_affine()), 'tensor_rgb.nii.gz')<jupyter_output><empty_output><jupyter_text>Let's try to visualize the tensor ellipsoids of a small rectangulararea in an axial slice of the splenium of the corpus callosum (CC).<jupyter_code>from dipy.data import get_sphere
sphere = get_sphere('symmetric724')

from dipy.viz import fvtk
ren = fvtk.ren()

evals = tenfit.evals[13:43, 44:74, 28:29]
evecs = tenfit.evecs[13:43, 44:74, 28:29]<jupyter_output><empty_output><jupyter_text>We can color the ellipsoids using the ``color_fa`` values that we calculatedabove. In this example we additionally normalize the values to increase the contrast.<jupyter_code>ren.SetBackground(1, 1, 1)
cfa = RGB[13:43, 44:74, 28:29]
cfa /= cfa.max()

fvtk.clear(ren)
fvtk.add(ren, fvtk.tensor(evals, evecs, cfa, sphere, scale=2.2))

print('Saving illustration as tensor_ellipsoids.png')
fvtk.record(ren, n_frames=1, out_path='tensor_ellipsoids.png', size=(600, 600))
display.display(display.Image('tensor_ellipsoids.png'))
fvtk.camera
tensor_odfs = tenmodel.fit(data[20:50, 55:85, 38:39]).odf(sphere)
fvtk.clear(ren)
fvtk.add(ren, fvtk.sphere_funcs(tensor_odfs, sphere, colormap=None))
fvtk.record(ren, n_frames=1, out_path='tensor_odfs.png', size=(600, 600))
display.display(display.Image('tensor_odfs.png'))<jupyter_output><empty_output><jupyter_text>Note that while the tensor model is an accurate and reliable model of thediffusion signal in the white matter, it has the drawback that it only has oneprincipal diffusion direction. Therefore, in locations in the brain thatcontain multiple fiber populations crossing each other, the tensor model mayindicate that the principal diffusion direction is intermediate to thesedirections. Therefore, using the principal diffusion direction for tracking inthese locations may be misleading and may lead to errors in defining thetracks. Fortunately, other reconstruction methods can be used to represent thediffusion and fiber orientations in those locations. These are presented inother examples. References[Basser1994] <NAME>, <NAME>, LeBihan (1994). MR diffusion tensor spectroscopy and imaging.[Pajevic1999] <NAME> (1999). Color schemes to represent the orientation of anisotropic tissues from diffusion tensor data: application to white matter fiber tract mapping in the human brain.<jupyter_code>import seaborn as sns
fig, ax = plt.subplots(1)
ax.matshow(np.flipud(FA[13:43, 44:74, 28].T), cmap=matplotlib.cm.bone)
ax.set_xticks([])
ax.set_yticks([])
fig.savefig("d2t-bkgrnd.png", transparent=True)
tensor_odfs = tenmodel.fit(data[40, 70, 33]).odf(sphere)
fvtk.clear(ren)
fvtk.add(ren, fvtk.sphere_funcs(tensor_odfs, sphere, colormap='hot'))
fvtk.record(ren, n_frames=360, out_path='./tensor_signal/', size=(600, 600), az_ang=1)
import moviepy.editor as mp
imseq = mp.ImageSequenceClip('./tensor_ellipses/', fps=18)
imseq.write_videofile('./tensor-odf-movie.mp4')
tensor_odfs = tenmodel.fit(data[40, 70, 33]).odf(sphere)
fvtk.clear(ren)

fvtk.add(ren, fvtk.sphere_funcs(tensor_odfs, sphere, colormap='hot'))
fvtk.record(ren, n_frames=360, out_path='./tensor_odfs/', size=(600, 600), az_ang=1)
import moviepy.editor as mp
imseq = mp.ImageSequenceClip('./tensor_odfs/', fps=18)
imseq.write_videofile('./tensor-odf-movie.mp4')
tensor_evecs = tenmodel.fit(data[40, 70, 33]).evecs
tensor_evals = tenmodel.fit(data[40, 70, 33]).evals

fvtk.clear(ren)
fvtk.add(ren, fvtk.tensor(tensor_evals, tensor_evecs, scalar_colors=np.array([0, 0, 1])))
fvtk.record(ren, n_frames=360, out_path='./tensor_ellipses/', size=(600, 600), az_ang=1)
import moviepy.editor as mp
imseq = mp.ImageSequenceClip('./tensor_ellipses/', fps=18)
imseq.write_videofile('./tensor-ellipse-movie.mp4')
points = gtab.bvecs[~gtab.b0s_mask] * (np.log(data[40, 70, 33, ~gtab.b0s_mask]/np.mean(data[40, 70, 33, gtab.b0s_mask]))/-2)[:, None]
points.shape
fvtk.clear(ren)
fvtk.add(ren, fvtk.point(points, fvtk.colors.blue))
fvtk.record(ren, n_frames=360, out_path='./tensor_signal//', size=(600, 600), az_ang=1)
import moviepy.editor as mp
imseq = mp.ImageSequenceClip('./tensor_signal/', fps=18)
imseq.write_videofile('./tensor-signal-movie.mp4')
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
#for ii in range(sig1.shape[0]):
#    x,y,z = gtab.bvecs[ii]
#    this_sig = np.array([sig1[ii]])
#    ax.plot3D(this_sig * x , this_sig * y, this_sig * z, 'b.')
#    ax.axis('off')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1031179090679889, 'fraction_numerical': 0.023783511219430203, 'mean_word_length': 6.0082449941107186, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '333960', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 4142, 'n_tokens_neox': 3824, 'n_words': 1217, 'path': 'ipynb/tensor-reconstruction.ipynb', 'repo_name': 'arokem/2017-ohbm-dipy-slides', 'size': '11920'}