Openig DTI data

This JUPYTER notebook has a demonstration of how to open DTI in nifti format.

importing modules


In [1]:
# import modules and libs
import io, os, sys, types
import numpy as np

# image and graphic
from IPython.display import Image
from IPython.display import display
import matplotlib.pyplot as plt
%matplotlib



#import notebook as module
sys.path.append('C:/iPython/DTIlib')
import DTIlib as DTI


Using matplotlib backend: Qt5Agg

Loading the data

In this folder there are 84 subjects data in nifti format (.nii).

The dada for each subject is a volume of (70x256x256) voxels and composed of 3 eigenvalues, 3 eigenvectors, and FA volume.


In [2]:
#subjects folder
BASE_PATH = 'G:/DTI_DS/original'

#subject number
# subject_number = 84 # very inclined subject
subject_number = 1

if(subject_number < 10):
    subject_dir = str(BASE_PATH)+str('/subject00')+str(subject_number)
else:
    subject_dir = str(BASE_PATH)+str('/subject0')+str(subject_number)
        

#load DTI
FA, evl, evt = DTI.load_fa_evl_evt(subject_dir)
MD = DTI.Mean_Difusivity(evl)

#print shapes
print('evt.shape =', evt.shape)
print('evl.shape =', evl.shape)
print('FA.shape =', FA.shape)
print('MD.shape =', MD.shape)


('evt.shape =', (3L, 3L, 70L, 256L, 256L))
('evl.shape =', (3L, 70L, 256L, 256L))
('FA.shape =', (70L, 256L, 256L))
('MD.shape =', (70L, 256L, 256L))

Data visualization

FA

MD is a 3D scalar map that shows the difusion assimetry for each voxel, so each one is associated with an intensity value.

Inline image of FA in three different viels (Axial, coronal, and sagittal viels).


In [3]:
# Show FA
%matplotlib inline
# %matplotlib notebook
from matplotlib.widgets import Slider

sz, sy, sx = FA.shape
# set up figure
fig = plt.figure(figsize=(15,15))
xy = fig.add_subplot(1,3,1)
plt.title("Axial Slice")
xz = fig.add_subplot(1,3,2)
plt.title("Coronal Slice")
yz = fig.add_subplot(1,3,3)
plt.title("Sagittal Slice")

frame = 0.5
maximo = np.max(np.abs(FA)) # normalize the FA values for better visualization
minimo = np.min(np.abs(FA))
xy.imshow(FA[np.floor(frame*sz),:,:], origin='lower', interpolation='nearest', cmap="gray",vmin=0, vmax=maximo )
xz.imshow(FA[:,np.floor(frame*sy),:], origin='lower', interpolation='nearest', cmap="gray",vmin=0 , vmax=maximo )
yz.imshow(FA[:,:,np.floor(frame*sx)], origin='lower', interpolation='nearest', cmap="gray",vmin=0 , vmax=maximo )


C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:19: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:21: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
Out[3]:
<matplotlib.image.AxesImage at 0x8fb2b70>

MD

MD is a 3D scalar map that shows the mean difusion for each voxel, so each one is associated with an intensity value.

Inline image of slices of the MD in three different viels (Axial, coronal, and sagittal viels).


In [4]:
# Show MD
%matplotlib inline
# %matplotlib notebook
from matplotlib.widgets import Slider

sz, sy, sx = MD.shape
# set up figure
fig = plt.figure(figsize=(15,15))
xy = fig.add_subplot(1,3,1)
plt.title("Axial Slice")
xz = fig.add_subplot(1,3,2)
plt.title("Coronal Slice")
yz = fig.add_subplot(1,3,3)
plt.title("Sagittal Slice")

frame = 0.5
maximo = np.max(np.abs(MD)) # normalize the MD values for better visualization
minimo = np.min(np.abs(MD))
xy.imshow(MD[np.floor(frame*sz),:,:], origin='lower', interpolation='nearest', cmap="gray",vmin=0, vmax=maximo )
xz.imshow(MD[:,np.floor(frame*sy),:], origin='lower', interpolation='nearest', cmap="gray",vmin=0 , vmax=maximo )
yz.imshow(MD[:,:,np.floor(frame*sx)], origin='lower', interpolation='nearest', cmap="gray",vmin=0 , vmax=maximo )


C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:19: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:20: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:21: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
Out[4]:
<matplotlib.image.AxesImage at 0x97c58d0>

First vector (main tensor direction)

This is a 3D vecotr field, so each voxel is associated with a vector.

Inline image of slices of the FA in three different viels (Axial, coronal, and sagittal viels).


In [11]:
# Show Vector Field

%matplotlib inline
# %matplotlib notebook
from matplotlib.widgets import Slider

evt_d = evt[0]*evt[0]

nv, sz, sy, sx = evl.shape


fig = plt.figure(figsize=(15,15))
xy = fig.add_subplot(1,3,1)
plt.title("Axial Slice")
plt.axis("off")
xz = fig.add_subplot(1,3,2)
plt.title("Coronal Slice")
plt.axis("off")
yz = fig.add_subplot(1,3,3)
plt.title("Sagittal Slice")
plt.axis("off")

step_ = 1 #Subamostragem dos vetores
maxlen_= 32 #Tamanho do maior vetor
rescale_ = 16 #Fator de rescala da imagem

# crop = np.array([sz/3, sz*2/3, sy/3, sy*2/3, sx/3, sy*2/3]) # crop [z<, z>, y<, y>, x< x>]
crop = np.array([30, 40, 120, 136, 120, 136]) # crop [z<, z>, y<, y>, x< x>]

frame = 0.5
# seismic
V1 = DTI.show_vector_field(evt_d[1,np.floor(frame*sz),:,:], evt_d[2,np.floor(frame*sz),:,:], step=step_, maxlen=maxlen_, rescale=rescale_)
xy.imshow(V1[0,:,:], origin='lower',cmap="gray")
V2 = DTI.show_vector_field(evt_d[0,:,np.floor(frame*sy),:], evt_d[2,:,np.floor(frame*sy),:], step=step_, maxlen=maxlen_, rescale=rescale_)
xz.imshow(V2[0,:,:], origin='lower',cmap="gray")
V3 = DTI.show_vector_field(evt_d[0,:,:,np.floor(frame*sx)], evt_d[1,:,:,np.floor(frame*sx)], step=step_, maxlen=maxlen_, rescale=rescale_)
yz.imshow(V3[0,:,:], origin='lower',cmap="gray")
plt.xticks([])
plt.yticks([])

fig = plt.figure(figsize=(15,15))
xy = fig.add_subplot(1,3,1)
plt.title("Axial Slice (zoom)")
plt.axis("off")
xz = fig.add_subplot(1,3,2)
plt.title("Coronal Slice (zoom)")
plt.axis("off")
yz = fig.add_subplot(1,3,3)
plt.title("Sagittal Slice (zoom)")
plt.axis("off")

V1 = DTI.show_vector_field(evt_d[1,np.floor(frame*sz),crop[2]:crop[3],crop[4]:crop[5]], evt_d[2,np.floor(frame*sz),crop[2]:crop[3],crop[4]:crop[5]], step=step_, maxlen=maxlen_, rescale=rescale_)
xy.imshow(V1[0,:,:], origin='lower',cmap="gray")
V2 = DTI.show_vector_field(evt_d[0,crop[0]:crop[1],np.floor(frame*sy),crop[4]:crop[5]], evt_d[2,crop[0]:crop[1],np.floor(frame*sy),crop[4]:crop[5]], step=step_, maxlen=maxlen_, rescale=rescale_)
xz.imshow(V2[0,:,:], origin='lower',cmap="gray")
V3 = DTI.show_vector_field(evt_d[0,crop[0]:crop[1],crop[2]:crop[3],np.floor(frame*sx)], evt_d[1,crop[0]:crop[1],crop[2]:crop[3],np.floor(frame*sx)], step=step_, maxlen=maxlen_, rescale=rescale_)
yz.imshow(V3[0,:,:], origin='lower',cmap="gray")




plt.show()


C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:32: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:34: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:36: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:52: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:54: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
C:\Anaconda3\envs\py2\lib\site-packages\ipykernel\__main__.py:56: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future