Skip to content

Instantly share code, notes, and snippets.

@kylerbrown
Created June 21, 2017 16:25
Show Gist options
  • Save kylerbrown/b0c0c4e3d5b5e350f1f43dc298a427e8 to your computer and use it in GitHub Desktop.
Save kylerbrown/b0c0c4e3d5b5e350f1f43dc298a427e8 to your computer and use it in GitHub Desktop.
Compute average firing rate of a spike train over trials
def da_firing_rate(df, dt, duration, gaussian_std=None, center_time=True):
'''computes Dayan and Abbot average firing rate or <r>
df: pandas dataframe with a "start" column in seconds and a
"trial" column. Starts are relative to the start of a trial
dt: timestep for computing histogram
duration: length of a trial in seconds
gaussian_std: if None, no smoothing is applied. Units: seconds.
center_time: if True, the returned time vector is the same length as
the firing rate. Otherwise, the time vector is the bin edges,
which are useful for histogram style plots.
returns (fr, t) where fr is the firing rate and t is a time vector
'''
t = np.arange(0, duration, dt)
T = len(unit_data.trial.drop_duplicates())
n, bin_edges = np.histogram(unit_data.start, t)
firing_rate = n * (1/T) * (1/dt)
if gaussian_std:
std_samp = gaussian_std * 1/dt
window = gaussian(int(std_samp*10), std_samp)
window /= sum(window)
firing_rate = fftconvolve(firing_rate, window, 'same')
if center_time:
t = t[:-1]+dt/2
return firing_rate, t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment