94 lines
2.3 KiB
Python
94 lines
2.3 KiB
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from plotstyle import *
|
|
|
|
def hompoisson(rate, trials, duration) :
|
|
spikes = []
|
|
for k in range(trials) :
|
|
times = []
|
|
t = 0.0
|
|
while t < duration :
|
|
t += np.random.exponential(1/rate)
|
|
times.append( t )
|
|
spikes.append( times )
|
|
return spikes
|
|
|
|
def inhompoisson(rate, trials, dt) :
|
|
spikes = []
|
|
p = rate*dt
|
|
for k in range(trials) :
|
|
x = np.random.rand(len(rate))
|
|
times = dt*np.nonzero(x<p)[0]
|
|
spikes.append( times )
|
|
return spikes
|
|
|
|
|
|
def pifspikes(input, trials, dt, D=0.1) :
|
|
vreset = 0.0
|
|
vthresh = 1.0
|
|
tau = 1.0
|
|
spikes = []
|
|
for k in range(trials) :
|
|
times = []
|
|
v = vreset
|
|
noise = np.sqrt(2.0*D)*np.random.randn(len(input))/np.sqrt(dt)
|
|
for k in range(len(noise)) :
|
|
v += (input[k]+noise[k])*dt/tau
|
|
if v >= vthresh :
|
|
v = vreset
|
|
times.append(k*dt)
|
|
spikes.append( times )
|
|
return spikes
|
|
|
|
def isis( spikes ) :
|
|
isi = []
|
|
for k in range(len(spikes)) :
|
|
isi.extend(np.diff(spikes[k]))
|
|
return np.array( isi )
|
|
|
|
def plotserialcorr(ax, isis, maxlag=10) :
|
|
lags = np.arange(maxlag+1)
|
|
corr = [1.0]
|
|
for lag in lags[1:] :
|
|
corr.append(np.corrcoef(isis[:-lag], isis[lag:])[0,1])
|
|
ax.set_xlabel(r'lag $k$')
|
|
ax.set_ylabel(r'ISI correlation $\rho_k$')
|
|
ax.set_xlim(0.0, maxlag)
|
|
ax.set_ylim(-1.0, 1.0)
|
|
ax.plot(lags, corr, '.-', markersize=15, c=colors['blue'])
|
|
|
|
# parameter:
|
|
rate = 20.0
|
|
drate = 50.0
|
|
trials = 10
|
|
duration = 500.0
|
|
dt = 0.001
|
|
tau = 0.1;
|
|
|
|
# homogeneous spike trains:
|
|
homspikes = hompoisson(rate, trials, duration)
|
|
|
|
# OU noise:
|
|
rng = np.random.RandomState(54637281)
|
|
time = np.arange(0.0, duration, dt)
|
|
x = np.zeros(time.shape)+rate
|
|
n = rng.randn(len(time))*drate*tau/np.sqrt(dt)+rate
|
|
for k in range(1,len(x)) :
|
|
x[k] = x[k-1] + (n[k]-x[k-1])*dt/tau
|
|
x[x<0.0] = 0.0
|
|
|
|
# pif spike trains:
|
|
inhspikes = pifspikes(x, trials, dt, D=0.3)
|
|
|
|
fig, (ax1, ax2) = plt.subplots(1, 2)
|
|
fig.subplots_adjust(**adjust_fs(fig, left=7.0, right=1.0))
|
|
|
|
plotserialcorr(ax1, isis(homspikes))
|
|
ax1.set_ylim(-0.2, 1.0)
|
|
|
|
plotserialcorr(ax2, isis(inhspikes))
|
|
ax2.set_ylim(-0.2, 1.0)
|
|
|
|
plt.savefig('serialcorrexamples.pdf')
|
|
plt.close()
|