This notebook separates the annotated JFRC template into masks
In [1]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from scipy import io
%matplotlib inline
import pylab
import nibabel as nb
import numpy as np
import scipy.ndimage
Open Data template
In [3]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
img = nb.load(filename)
data = img.get_data()
S=data.shape
Open Masks
In [4]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filenameM = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filenameM)
img1 = nb.load(filenameM)
Masks = img1.get_data()
Sm=Masks.shape
Masks=np.array(Masks)
In [5]:
filenameM='/home/sophie/Desktop/Registration/RegionList'
with open(filenameM) as f:
content = f.readlines()
Names=[Line.split('\t') for Line in content]
RegionName=[Names[i][0] for i in range(75)]
Num=[int(Names[i][2]) for i in range(75)]
In [6]:
RegionName
Out[6]:
In [7]:
Sm
Out[7]:
In [8]:
S
Out[8]:
In [9]:
Masks2=np.zeros((Sm[0],Sm[1],Sm[2],87))
MasksResized=np.zeros((S[0],S[1],S[2],87))
for j in range(87):
Masks2[:,:,:,j]=np.squeeze((Masks==j))
for i in range(S[2]):
MasksResized[:,:,i,j]=scipy.ndimage.zoom(Masks2[:,:,i,j], float(S[0])/Sm[0], order=0)
In [10]:
MasksResized.shape
Out[10]:
Save the risized maps
In [22]:
nim=nb.Nifti1Image(MasksResized[:,:,:,1:87],np.eye(4))
nb.save(nim,'/home/sophie/Desktop/Registration/862/862ResizedMaps.nii')
In [117]:
TS=np.zeros((S[3],86))
for i in range(1,86):
for j in range(S[3]):
TS[j,i]=np.mean(np.mean(np.mean(np.multiply(MasksResized[:,:,:,i],data[:,:,:,j]))))
might need to do that in matlab parfor
In [184]:
pylab.rcParams['figure.figsize'] = (11, 8)
h=1.7
i=0
fig = plt.figure()
axes=plt.gca()
#axes.set_ylim([-0.1,0.4])
axes.set_xlim([-500,10000])
ax = fig.add_subplot(111)
for j in range(70,75):
i=Num[j]
plt.plot((TS[1000:11000,i]/np.max(TS[1000:11000,i])-h*j),color=np.squeeze(np.random.rand(3,1))/1.2)
ax.text(10100, -h*j, RegionName[j], style='italic',fontsize=16,bbox={'alpha':0.5, 'pad':0})
plt.show
In [191]:
TS2save=np.zeros((10000,74))
for j in range(74):
i=Num[j]
TS2save[:,j]=TS[1000:11000,i]/np.max(TS[1000:11000,i])
In [194]:
import scipy.io as sio
sio.savemat('/home/sophie/Desktop/100115RegionsTS.mat',{'TS':TS2save,'RegionName':RegionName})
In [ ]: