In [16]:
%matplotlib inline
In [17]:
from dipy.reconst.dti import fractional_anisotropy, color_fa
from argparse import ArgumentParser
from scipy import ndimage
import os
import re
import numpy as np
import nibabel as nb
import sys
import matplotlib
#matplotlib.use('Agg') # very important above pyplot import
import matplotlib.pyplot as plt
In [3]:
cd
In [4]:
ls
In [7]:
cd new/
In [8]:
ls
In [9]:
from dipy.reconst.dti import from_lower_triangular
In [10]:
ls
In [12]:
img = nb.load('dogsigma_0gausigma_0tensorfsl.nii')
In [13]:
data = img.get_data()
In [14]:
output = from_lower_triangular(data)
In [15]:
output_ds = output[4250:4300, 250:300, :, :, :]
In [16]:
print output.shape
print output_ds.shape
In [17]:
FA = fractional_anisotropy(output)
In [18]:
FA = np.clip(FA, 0, 1)
In [19]:
FA[np.isnan(FA)] = 0
In [20]:
print FA.shape
In [21]:
from dipy.reconst.dti import decompose_tensor
In [22]:
evalues, evectors = decompose_tensor(output)
In [23]:
print evectors[..., 0, 0].shape
print evectors.shape[-2:]
In [24]:
print FA[:, :, :, 0].shape
In [25]:
RGB = color_fa(FA[:, :, :, 0], evectors)
In [26]:
nb.save(nb.Nifti1Image(np.array(255 * RGB, 'uint8'), img.get_affine()), 'tensor_rgb_upper.nii.gz')
In [27]:
def plot_rgb(im):
plt.rcParams.update({'axes.labelsize': 'x-large',
'axes.titlesize': 'x-large'})
if im.shape == (182, 218, 182):
x = [78, 90, 100]
y = [82, 107, 142]
z = [88, 103, 107]
else:
shap = im.shape
x = [int(shap[0]*0.35), int(shap[0]*0.51), int(shap[0]*0.65)]
y = [int(shap[1]*0.35), int(shap[1]*0.51), int(shap[1]*0.65)]
z = [int(shap[2]*0.35), int(shap[2]*0.51), int(shap[2]*0.65)]
coords = (x, y, z)
labs = ['Sagittal Slice (YZ fixed)',
'Coronal Slice (XZ fixed)',
'Axial Slice (XY fixed)']
var = ['X', 'Y', 'Z']
idx = 0
for i, coord in enumerate(coords):
for pos in coord:
idx += 1
ax = plt.subplot(3, 3, idx)
ax.set_title(var[i] + " = " + str(pos))
if i == 0:
image = ndimage.rotate(im[pos, :, :], 90)
elif i == 1:
image = ndimage.rotate(im[:, pos, :], 90)
else:
image = im[:, :, pos]
if idx % 3 == 1:
ax.set_ylabel(labs[i])
ax.yaxis.set_ticks([0, image.shape[0]/2, image.shape[0] - 1])
ax.xaxis.set_ticks([0, image.shape[1]/2, image.shape[1] - 1])
plt.imshow(image)
fig = plt.gcf()
fig.set_size_inches(12.5, 10.5, forward=True)
return fig
In [28]:
affine = img.get_affine()
fa = nb.Nifti1Image(np.array(255 * RGB, 'uint8'), affine)
im = fa.get_data()
In [29]:
print np.asarray(fa)
In [30]:
fig = plot_rgb(im)
In [31]:
import os
In [32]:
ls
In [33]:
cd ..
In [34]:
ls
In [35]:
cd ..
In [7]:
ls
In [8]:
from PIL import Image
A = Image.open('downsampled0.tiff')
B = Image.open('downsampled1.tiff')
C = Image.open('downsampled2.tiff')
D = Image.open('downsampled3.tiff')
E = Image.open('downsampled4.tiff')
F = Image.open('downsampled5.tiff')
finalImage = np.dstack((A, B, C, D, E, F))
In [25]:
from PIL import Image
im = plt.imread('downsampled'+str(i)+'.tiff')
plt.imshow(im)
Out[25]:
In [ ]:
In [37]:
FA.shape
Out[37]:
In [ ]:
In [ ]:
In [ ]: