In [1]:
from dipy.data import fetch_sherbrooke_3shell
fetch_sherbrooke_3shell()
Out[1]:
In [2]:
from os.path import expanduser, join
home = expanduser('~')
In [3]:
dname = join(home, '.dipy', 'sherbrooke_3shell')
In [4]:
fdwi = join(dname, 'HARDI193.nii.gz')
print(fdwi)
fbval = join(dname, 'HARDI193.bval')
print(fbval)
fbvec = join(dname, 'HARDI193.bvec')
print(fbvec)
In [5]:
import nibabel as nib
img = nib.load(fdwi)
data = img.get_data()
In [6]:
print(data.shape)
In [7]:
print(img.get_header().get_zooms()[:3])
In [8]:
%matplotlib inline
import matplotlib.pyplot as plt
axial_middle = data.shape[2] / 2
plt.figure('Showing the datasets')
plt.subplot(1, 2, 1).set_axis_off()
plt.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
plt.subplot(1, 2, 2).set_axis_off()
plt.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
plt.show()
In [9]:
from dipy.io import read_bvals_bvecs
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
In [10]:
from dipy.core.gradients import gradient_table
gtab = gradient_table(bvals, bvecs)
In [11]:
print(gtab.info)
In [12]:
print(gtab.bvals)
In [13]:
print(gtab.bvecs[:10, :])
In [14]:
S0s = data[:, :, :, gtab.b0s_mask]
In [15]:
print(S0s.shape)
In [16]:
nib.save(nib.Nifti1Image(S0s, img.get_affine()), 'HARDI193_S0.nii.gz')
In [17]:
import numpy as np
import nibabel as nib
import dipy.reconst.dti as dti
from dipy.data import fetch_stanford_hardi
fetch_stanford_hardi()
Out[17]:
In [18]:
from dipy.data import read_stanford_hardi
img, gtab = read_stanford_hardi()
In [19]:
data = img.get_data()
print('data.shape (%d, %d, %d, %d)' % data.shape)
In [20]:
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)
In [21]:
tenmodel = dti.TensorModel(gtab)
In [22]:
tenfit = tenmodel.fit(maskdata)
In [23]:
print('Computing anisotropy measures (FA, MD, RGB)')
from dipy.reconst.dti import fractional_anisotropy, color_fa, lower_triangular
FA = fractional_anisotropy(tenfit.evals)
In [24]:
FA[np.isnan(FA)] = 0
In [25]:
fa_img = nib.Nifti1Image(FA.astype(np.float32), img.get_affine())
nib.save(fa_img, 'tensor_fa.nii.gz')
In [26]:
evecs_img = nib.Nifti1Image(tenfit.evecs.astype(np.float32), img.get_affine())
nib.save(evecs_img, 'tensor_evecs.nii.gz')
In [27]:
MD1 = dti.mean_diffusivity(tenfit.evals)
nib.save(nib.Nifti1Image(MD1.astype(np.float32), img.get_affine()), 'tensors_md.nii.gz')
In [28]:
MD2 = tenfit.md
The FA is the normalized variance of the eigen-values of the 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}}$
In [29]:
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')
In [30]:
print('Computing tensor ellipsoids in a part of the splenium of the CC')
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]
In [ ]:
cfa = RGB[13:43, 44:74, 28:29]
cfa /= cfa.max()
fvtk.add(ren, fvtk.tensor(evals, evecs, cfa, sphere))
fvtk.show(ren)
Tensor elliptoids normalized to increase contrast
In [ ]:
fvtk.clear(ren)
In [ ]:
tensor_odfs = tenmodel.fit(data[20:50, 55:85, 38:39]).odf(sphere)
fvtk.add(ren, fvtk.sphere_funcs(tensor_odfs, sphere, colormap=None))
fvtk.show(ren)
tensor orientation distribution functions
In [ ]: