chirp_probing/util.py

52 lines
1.7 KiB
Python

import numpy as np
def despine(axis, spines=None, hide_ticks=True):
def hide_spine(spine):
spine.set_visible(False)
for spine in axis.spines.keys():
if spines is not None:
if spine in spines:
hide_spine(axis.spines[spine])
else:
hide_spine(axis.spines[spine])
if hide_ticks:
axis.xaxis.set_ticks([])
axis.yaxis.set_ticks([])
def gaussKernel(sigma, dt):
""" Creates a Gaussian kernel with a given standard deviation and an integral of 1.
Args:
sigma (float): The standard deviation of the kernel.
dt (float): The temporal resolution of the kernel, given in seconds.
Returns:
numpy.ndarray : the kernel in the range -4 to +4 sigma
"""
x = np.arange(-4. * sigma, 4. * sigma, dt)
y = np.exp(-0.5 * (x / sigma) ** 2) / np.sqrt(2. * np.pi) / sigma
return y
def firing_rate(spikes, duration, sigma=0.005, dt=1./20000.):
"""Convert spike times to a firing rate using the kernel convolution with a Gaussian kernel
Args:
spikes (iterable): list of spike times, times should be in seconds
duration (float): duration of the trial in seconds
sigma (float, optional): standard deviation of the Gaussian kernel. Defaults to 0.005s.
dt (float, optional): The stepsize of the trace. Defaults to 1./20000.s.
Returns:
np.ndarray: the firing rate
"""
binary = np.zeros(int(np.round(duration/dt)))
indices = np.asarray(np.round(spikes / dt), dtype=np.int)
binary[indices[indices < len(binary)]] = 1
kernel = gaussKernel(sigma, dt)
rate = np.convolve(kernel, binary, mode="same")
return rate