Skip to content

Kernels

Some useful kernels.

import numpy as np
import scipy.signal
import matplotlib.pyplot as plt


srate = 1000
spk_rate = 13.0  # Avg 30 spikes per second
tvec = np.arange(srate) / srate
spikeevents = (np.random.rand(srate) < (spk_rate / srate)).astype(np.float32)
spiketimes = tvec[spikeevents.nonzero()]
from indl.misc.kernels import sskernel, Gauss

# Shimazaki et al. auto-kernel-width
kernel_width = sskernel(spiketimes - spiketimes[0], nbs=0)[2]
kernel_param = 1 / (2.0 * 2.7) * kernel_width
span_fac = 3.0
t_kern = np.arange(-span_fac * kernel_param, span_fac * kernel_param + (1 / srate), 1 / srate)
kernel = Gauss(t_kern, kernel_param)
spikerates = scipy.signal.convolve(spikeevents, kernel, 'same')

plt.subplot(3, 1, 1)
plt.plot(tvec, spikeevents)
plt.subplot(3, 1, 2)
plt.plot(t_kern, kernel)
plt.xlim([-0.5, 0.5])
plt.subplot(3, 1, 3)
plt.plot(tvec, spikerates)
[<matplotlib.lines.Line2D at 0x7fd457d51910>]
kernel_param = 0.050  # msec stdev
span_fac = 3.0  # How many stdevs wide the kernel should be. Too short will truncate kernel.

t_kern = np.arange(-span_fac * kernel_param, span_fac * kernel_param + (1 / srate), 1 / srate)
kernel = Gauss(t_kern, kernel_param)
spikerates = scipy.signal.convolve(spikeevents, kernel, 'same')

plt.subplot(3, 1, 1)
plt.plot(tvec, spikeevents)
plt.subplot(3, 1, 2)
plt.plot(t_kern, kernel)
plt.xlim([-0.5, 0.5])
plt.subplot(3, 1, 3)
plt.plot(tvec, spikerates)
[<matplotlib.lines.Line2D at 0x7fd455bcb0a0>]
from indl.misc.kernels import Boxcar

kernel_param = 0.05  # The width of the rectangle in seconds
span_fac = np.sqrt(3.0)
kernel_param /= (2*np.sqrt(3.0))

t_kern = np.arange(-span_fac * kernel_param, span_fac * kernel_param + (1 / srate), 1 / srate)
kernel = Boxcar(t_kern, kernel_param)
spikerates = scipy.signal.convolve(spikeevents, kernel, 'same')

plt.subplot(3, 1, 1)
plt.plot(tvec, spikeevents)
plt.subplot(3, 1, 2)
plt.xlim([-0.5, 0.5])
plt.plot(t_kern, kernel)
plt.subplot(3, 1, 3)
plt.plot(tvec, spikerates)
[<matplotlib.lines.Line2D at 0x7fd455a97c70>]
from indl.misc.kernels import Alpha
kernel_param = 0.03  # tau
kernel_param *= np.sqrt(2)
span_fac = 6.0

t_kern = np.arange(-span_fac * kernel_param, span_fac * kernel_param + (1 / srate), 1 / srate)
kernel = Alpha(t_kern, kernel_param)
spikerates = scipy.signal.convolve(spikeevents, kernel, 'same')
print(np.sum(spikeevents), np.mean(spikerates))

plt.subplot(3, 1, 1)
plt.plot(tvec, spikeevents)
plt.subplot(3, 1, 2)
plt.xlim([-0.5, 0.5])
plt.plot(t_kern, kernel)
plt.subplot(3, 1, 3)
plt.plot(tvec, spikerates)
10.0 9.538681202301868

[<matplotlib.lines.Line2D at 0x7fd4559f6ac0>]
from indl.misc.kernels import Exponential
kernel_param = 0.05  # the time constant tau when the kernel reaches 1/e the maximum.
span_fac = 6.0

t_kern = np.arange(-span_fac * kernel_param, span_fac * kernel_param + (1 / srate), 1 / srate)
kernel = Exponential(t_kern, kernel_param)
spikerates = scipy.signal.convolve(spikeevents, kernel, 'same')

plt.subplot(3, 1, 1)
plt.plot(tvec, spikeevents)
plt.subplot(3, 1, 2)
plt.xlim([-0.5, 0.5])
plt.plot(t_kern, kernel)
plt.subplot(3, 1, 3)
plt.plot(tvec, spikerates)
[<matplotlib.lines.Line2D at 0x7fd45594adf0>]