This JUPYTER notebook has a demonstration of how to open DTI in nifti format.
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 specific modules
sys.path.append('C:/iPython/DTIlib')
import DTIlib as DTI
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)
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 )
Out[3]:
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 )
Out[4]:
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()