In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import random
import csv
%matplotlib inline
In [2]:
import os
import sys
sys.path.append(os.path.join(os.getcwd(),'..'))
sys.path.append(os.path.join(os.getcwd(),'..','code'))
sys.path.append(os.path.join(os.getcwd(),'..','data'))
In [3]:
import filters
import likelihood_functions as lk
import PoissonProcessClasses as PP
import auxiliary_functions as auxfun
In [4]:
import imp
imp.reload(filters)
imp.reload(lk)
imp.reload(auxfun)
imp.reload(PP)
Out[4]:
In [ ]:
In [5]:
# Number of neurons
nofCells = 2
Reading input-output data:
In [6]:
# creating the path to the data
data_path = os.path.join(os.getcwd(),'..','data')
# reading stimulus
Stim = np.array(pd.read_csv(os.path.join(data_path,'Stim2.csv'),header = None))
# reading location of spikes
# (lengths of tsp sequences are not equal so reading them line by line)
tsp_list = []
with open(os.path.join(data_path,'tsp2.csv')) as csvfile:
tspreader = csv.reader(csvfile)
for row in tspreader:
tsp_list.append(row)
Extracting a spike train from spike positions:
In [7]:
dt = 0.01
y_list = []
for tsp in tsp_list:
tsp = np.array(tsp).astype(np.float)
tsp_int = np.ceil((tsp - dt*0.001)/dt)
tsp_int = np.reshape(tsp_int,(tsp_int.shape[0],1))
tsp_int = tsp_int.astype(int)
y_list.append(np.array([item in tsp_int for item in np.arange(Stim.shape[0]/dt)+1]).astype(int))
Creating filters:
In [8]:
# create a stimulus filter
kpeaks = np.array([0,round(20/3)])
pars_k = {'neye':5,'n':5,'kpeaks':kpeaks,'b':3}
K,K_orth,kt_domain = filters.createStimulusBasis(pars_k, nkt = 20)
In [9]:
# create a post-spike filter
hpeaks = np.array([0.1,2])
pars_h = {'n':5,'hpeaks':hpeaks,'b':.4}
H,H_orth,ht_domain = filters.createPostSpikeBasis(pars_h,dt)
In [10]:
# Interpolate Post Spike Filter
MSP = auxfun.makeInterpMatrix(len(ht_domain),1)
MSP[0,0] = 0
H_orth = np.dot(MSP,H_orth)
Conditional Intensity (spike rate): $$\lambda_{\beta}(i) = \exp(K(\beta_k)*Stim + H(\beta_h)*y + \sum_{j\ne i}w_j I(\beta_{I})*y_j) + \mu$$
Creating a matrix of covariates:
In [11]:
M_k = lk.construct_M_k(Stim,K,dt)
In [ ]:
In [12]:
M_h_list = []
for tsp in tsp_list:
tsp = np.array(tsp).astype(np.float)
M_h_list.append(lk.construct_M_h(tsp,H_orth,dt,Stim))
In [13]:
# creating a matrix of output covariates
Y = np.array(y_list).T
In [14]:
# tsp_list = []
# for i in range(nofCells):
# tsp_list.append(auxfun.simSpikes(np.hstack((coeff_k,coeff_h)),M,dt))
In [ ]:
In [ ]:
In [ ]:
In [15]:
M_list = []
for i in range(len(M_h_list)):
# exclude the i'th spike-train
M_list.append(np.hstack((M_k,M_h_list[i],np.delete(Y,i,1),np.ones((M_k.shape[0],1)))))
#M_list.append(np.hstack((M_k,M_h_list[i],np.ones((M_h.shape[0],1)))))
Conditional intensity as a function of the covariates: $$ \lambda_{\beta} = \exp(M\beta) $$
Create a Poisson process model with this intensity:
In [ ]:
Setting initial parameters:
In [16]:
coeff_k0 = np.array([ 0.061453,0.284916,0.860335,1.256983,0.910615,0.488660,-0.887091,0.097441,0.026607,-0.090147])
coeff_h0 = np.zeros((5,))
coeff_w0 = np.zeros((nofCells,))
mu_0 = 0
pars0 = np.hstack((coeff_k0,coeff_h0,coeff_w0,mu_0))
pars0 = np.hstack((coeff_k0,coeff_h0,mu_0))
pars0 = np.zeros((17,))
Fitting the likelihood:
In [17]:
res_list = []
for i in range(len(y_list)):
model = PP.PPModel(M_list[i].T,dt = dt/100)
res_list.append(model.fit(y_list[i],start_coef = pars0,maxiter = 500, method = 'L-BFGS-B'))
Specifying the true parameters:
In [18]:
k_coeff = np.array([0.061453, 0.284916, 0.860335, 1.256983, 0.910615, 0.488660, -0.887091, 0.097441, 0.026607, -0.090147])
h_coeff = np.array([-15.18,38.24,-67.58,-14.06,-3.36])
In [19]:
for i in range(len(res_list)):
k_coeff_predicted = res_list[i].x[:10]
h_coeff_predicted = res_list[i].x[10:15]
print('Estimated dc for neuron '+str(i)+': '+str(res_list[i].x[-1]))
fig,axs = plt.subplots(1,2,figsize = (10,5))
fig.suptitle('Neuron%d'%(i+1))
axs[0].plot(-kt_domain[::-1],np.dot(K,k_coeff_predicted),'r',label = 'predicted')
axs[0].set_title('Stimulus Filter')
axs[0].hold(True)
axs[0].plot(-kt_domain[::-1],np.dot(K,k_coeff),'b',label = 'true')
axs[0].plot(-kt_domain[::-1],np.dot(K,pars0[:10]),'g',label = 'initial')
axs[0].set_xlabel('Time')
axs[0].legend(loc = 'upper left')
axs[1].set_title('Post-Spike Filter')
axs[1].plot(ht_domain,np.dot(H_orth,h_coeff_predicted),'r',label = 'predicted')
axs[1].plot(ht_domain,np.dot(H_orth,h_coeff),'b',label = 'true')
axs[1].plot(ht_domain,np.dot(H_orth,coeff_h0[:H_orth.shape[1]]),'g',label = 'initial')
axs[1].set_title('Post-Spike Filter')
axs[1].set_xlabel('Time')
axs[1].legend(loc = 'upper right')
Extracting the weight matrix:
In [20]:
W = np.array([np.hstack((res_list[i].x[-(nofCells):-nofCells+i],0,res_list[i].x[-nofCells+i:-1])) for i in range(len(res_list))])
print(W)
Note: the stimulus and the post-spike estimates can be different for different neurons.
Note: there might be some scale issue. Need to normalize these weights in some way.
In [ ]:
In [ ]:
In [ ]:
In [ ]: