<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Openig DTI data
This JUPYTER notebook has a demonstration of how to open DTI in nifti format.
importing modules
Step1:  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.
Step2:  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).
Step3:  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).
Step4:  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).
<ASSISTANT_TASK:>
Python Code:
# 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
Explanation: Openig DTI data
This JUPYTER notebook has a demonstration of how to open DTI in nifti format.
importing modules
End of explanation
#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)
Explanation: 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.
End of explanation
# 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 )
Explanation: 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).
End of explanation
# 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 )
Explanation: 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).
End of explanation
# 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()
Explanation: 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).
End of explanation
<END_TASK>