In [ ]:
import nibabel as nb
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
from Tkinter import Tk
from tkFileDialog import askopenfilename
#import cv2
%matplotlib inline

Get video time (output from matlab)


In [ ]:
# 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)
Ua=sio.loadmat(filename)
Tvid=Ua['Time']
Tvid.shape

Get head video data

Open the head saved as 32bit nii


In [ ]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename2 = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename2)
img1 = nb.load(filename2)
data = img1.get_data()
S=data.shape
S

In [ ]:
# Find end of onset of light and begining of offset (to align to behavior)

In [ ]:
M=np.mean(np.mean(data,0),0)
M=M-np.min(M)
Mav=M.mean()
M=M/Mav

In [ ]:
M.shape

In [ ]:
plt.plot(M)

In [ ]:
liston=[i for i in range(len(M)) if M[i]>0.7]
#liston[0]
max(liston)

In [ ]:
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

In [ ]:
Ms=M[range(liston[0]-8,liston[0]+8)]-M[liston[0]-8]

In [ ]:
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 [ ]:
liston[0]-8

In [ ]:
res = scipy.optimize.minimize(Sq,x0=[7,0.00,8.2,1])

In [ ]:
ON=liston[0]-8+res.x[2]
print(ON)

In [ ]:
ONint=np.int(np.ceil(ON))
print(ONint)

In [ ]:
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()

Model offset and find precise offset time


In [ ]:
len(M)

In [ ]:
Ms=M[range(max(liston)-6,max(liston)+6)]

In [ ]:
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 [ ]:
res = scipy.optimize.minimize(Sq,x0=[6.1,1.29,7,-1])

In [ ]:
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 [ ]:
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()

Save times were light is on

In imageJ: choose subsack between these 2 values


In [ ]:
print(ONint+1)
print(OFFint+1)
print(ON)
print(OFF)
print(OFFint-ONint)
print(OFF-ON)

In [ ]:
Tvid=Tvid.T

In [ ]:
TimeOn=[Tvid[i] for i in range(ONint,(OFFint+1))]

In [ ]:
Dt=np.array(TimeOn[2:4923])-np.array(TimeOn[1:4922])

In [ ]:
np.min(Dt)

In [ ]:
plt.plot(list(Dt.T[0]))
#plt.ylim(10,11)

In [ ]:
Tinit=(ON-(ONint-1))*(Tvid[ONint]-Tvid[ONint-1])+Tvid[ONint-1]

In [ ]:
Toff=(OFF-OFFint)*(Tvid[OFFint+1]-Tvid[OFFint])+Tvid[OFFint]

In [ ]:
Toff-Tinit

In [ ]:
np.mean((Tvid[100:(len(Tvid))])-np.array(Tvid[99:(len(Tvid)-1)]))

In [ ]:
np.std((Tvid[100:(len(Tvid))])-np.array(Tvid[99:(len(Tvid)-1)]))

In [ ]:
#Ttest=np.array(Tvid.T[2:(len(Tvid.T))])-np.array(Tvid.T[1:(len(Tvid.T)-1)])
#plt.plot(Ttest)
#plt.axis([0,100,19.5,150])

In [ ]:
TimeOnFinal=(np.array(TimeOn)-Tinit)/1000

In [ ]:
np.max(TimeOnFinal)

In [ ]:
Fileout="".join(filename[i] for i in range(len(filename)-4))

In [ ]:
OutFilename=Fileout+'OnVid.mat'

In [ ]:
OutFilename

In [ ]:
sio.savemat(OutFilename, {'TimeFluoOnVid':TimeOnFinal,'ONint':ONint+1,'OFFint':OFFint+1})

In [ ]: