In [1]:
clear all
In [2]:
import nibabel as nib
import os
import numpy as np
import scipy.io as sio
import scipy.optimize
from Tkinter import Tk
from tkFileDialog import askdirectory
import libtiff
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
foldername = askdirectory() # show an "Open" dialog box and return the path to the selected file
print(foldername)
In [4]:
path=foldername
In [5]:
A=foldername.split('/')
Dataname=A[-1]
In [6]:
Dataname
Out[6]:
In [7]:
t = libtiff.TiffFile(path+'/'+Dataname+'-00001.tif')
tt = t.get_tiff_array()
t.close()
S=tt.shape
data=np.zeros([S[0],S[1],S[2],len(os.listdir(path))])
#for i in range(1,15000):
for i in range(len(os.listdir(path))):
#for fn in os.listdir(path):
t = libtiff.TiffFile(path+'/'+Dataname+'-'+str(i+1).zfill(5)+'.tif')
#t = libtiff.TiffFile(path+fn)
tt = t.get_tiff_array()
data[:,:,:,i]=tt[:][:][:]
t.close()
i=i+1
In [8]:
S=data.shape
S
Out[8]:
Calculate average time series
In [9]:
M=np.mean(np.mean(np.mean(data,0),0),0)
Mav=M.mean()
In [10]:
plt.plot(M,'+')
#plt.axis([-1,50,0,1.5])
Out[10]:
Get approxiamte on and off times
In [11]:
liston=[i for i in range(len(M)) if M[i]>Mav*0.7]
liston[0]
Out[11]:
Model for fitting onset and offset
In [12]:
def model(x,a,b,c,d):
if x<a:
return b
elif x<c:
return b+(x-a)*d
else:
return (c-a)*d+b
Model onset and find precise onset time
In [13]:
Ms=M[range(liston[0]-8,liston[0]+8)]
In [14]:
def Sq(X):
return sum([(model(i,X[0],X[1],X[2],X[3])-Ms[i])**2 for i in range(len(Ms))])
In [15]:
liston[0]-8
Out[15]:
In [16]:
res = scipy.optimize.minimize(Sq,x0=[7,0.3,9,0.7])
In [17]:
ON=liston[0]-8+res.x[2]
print(ON)
In [18]:
ONint=np.int(np.ceil(ON))
#ONint=1
print(ONint)
In [19]:
plt.plot(np.squeeze(M[range(liston[0]-8,liston[0]+8)]),'+')
plt.plot(np.arange(0,len(Ms),0.1),[model(i,res.x[0],res.x[1],res.x[2],res.x[3]) for i in np.arange(0,len(Ms),0.1)])
plt.show()
Model offset and find precise offset time
In [20]:
Ms=M[range(liston[len(liston)-1]-6,liston[len(liston)-1]+6)]
In [21]:
def Sq(X):
return sum([(model(i,X[0],X[1],X[2],X[3])-Ms[i])**2 for i in range(len(Ms))])
In [22]:
res = scipy.optimize.minimize(Sq,x0=[7,3,9,-1])
In [23]:
OFF=liston[len(liston)-1]-6+res.x[0]
#OFF=liston[len(liston)-1]
print(OFF)
OFFint=np.int(np.floor(OFF))
print(OFFint)
In [24]:
plt.plot(np.squeeze(Ms),'+')
plt.plot(np.arange(0,len(Ms),0.1),[model(i,res.x[0],res.x[1],res.x[2],res.x[3]) for i in np.arange(0,len(Ms),0.1)])
plt.show()
In [25]:
TimeFile='/home/sophie/Downloads/Data'+''.join([Dataname[i] for i in range(6)])+'_.csv'
In [26]:
print(TimeFile)
In [27]:
Listfile = open(TimeFile, 'r')
ListTime = [line.split('\n')[0] for line in Listfile.readlines()]
In [67]:
Timespl=[float(ListTime[i].split(',')[2]) for i in range(1,len(ListTime))]
In [68]:
Timespl[12]
Out[68]:
In [69]:
print(ONint)
print(OFFint)
print(ON)
In [70]:
TimeOn=[Timespl[i] for i in range(ONint,(OFFint+1))]
In [71]:
Tinit=(ON-(ONint-1))*(Timespl[ONint]-Timespl[ONint-1])+Timespl[ONint-1]
In [72]:
Toff=(OFFint+1-OFF)*(Timespl[OFFint+1]-Timespl[OFFint])+Timespl[OFFint]
In [73]:
Toff-Tinit
Out[73]:
In [74]:
Timespl[ONint]-Timespl[ONint-1]
Out[74]:
In [75]:
TimeOn[0]-Tinit
Out[75]:
In [76]:
import numpy as np
In [77]:
TimeOnFinal=np.array(TimeOn)-Tinit
In [78]:
sio.savemat('/home/sophie/Desktop/'+Dataname+'TimeFluoOn.mat', {'TimeFluoOn':TimeOnFinal})
In [28]:
D4=np.transpose(data[:,:,:,range(ONint,(OFFint+1))],(2,1,0,3))
nim=nib.Nifti1Image(D4,np.eye(4))
nib.save(nim,'/home/sophie/Desktop/'+Dataname+'on.nii.gz')
In [ ]: