Skip to main content

Spike-train statistics (firing rate, CV, CV2, Fano factor, local variation Lv and LvR, PSTH, time-resolved rate and Fano, spike-time tiling coefficient) in pure Python with zero dependencies.

Project description

spikestats

spikestats logo

Spike-train statistics in pure Python, with zero dependencies.

spikestats computes the standard measures of spike-train rate and variability from a plain list of spike times: firing rate, inter-spike intervals, coefficient of variation, CV2, local variation (Lv and the refractory-corrected LvR), spike counts, the Fano factor, and the spike-time tiling coefficient (STTC) for pairwise correlation. There is nothing to configure and nothing to install beyond the package itself: the input is a list[float] of spike times and the output is a float.

It pairs with spikegen for generating trains and spikedist for comparing them. The three share the same plain-list data model, so they compose without adapters.

Why

The established tools for these measures, Elephant and spiketools, are excellent but pull in NumPy, SciPy, and custom data objects (neo.SpikeTrain and friends). When you only need a firing rate or a CV from a list of spike times, that is a heavy dependency tree to carry, and it is awkward in teaching material, small scripts, and lightweight pipelines. spikestats keeps the math, drops the dependencies, and works on the lists you already have.

Install

pip install spikestats

Usage

import spikestats as ss

spikes = [0.012, 0.031, 0.058, 0.090, 0.110, 0.155]  # seconds

ss.firing_rate(spikes, duration=0.2)   # spikes per second
ss.inter_spike_intervals(spikes)       # consecutive differences
ss.cv_isi(spikes)                      # coefficient of variation of the ISIs
ss.cv2(spikes)                         # Holt et al. 1996, robust to rate drift
ss.lv(spikes)                          # Shinomoto et al. 2003 local variation
ss.lvr(spikes, refractory=0.002)       # Shinomoto et al. 2009, refractory-corrected
ss.spike_counts(spikes, duration=0.2, bin_width=0.05)
ss.fano_factor(spikes, duration=0.2, bin_width=0.05)

Pairwise correlation

import spikestats as ss

a = [0.012, 0.058, 0.110, 0.155]  # seconds
b = [0.015, 0.061, 0.300]         # seconds

# Spike-time tiling coefficient (Cutts and Eglen 2014): firing-rate-robust correlation
# of two trains over a recording interval, with a synchronicity window dt.
ss.spike_time_tiling_coefficient(a, b, dt=0.005, interval=(0.0, 0.2))

Time-resolved metrics

import spikestats as ss

spikes = [0.012, 0.031, 0.058, 0.090, 0.110, 0.155, 0.210, 0.245]  # seconds

# Non-overlapping bin counts tiling [0, duration)
counts = ss.binned_spike_counts(spikes, duration=0.3, bin_width=0.1)
# => [3, 3, 2]  (one list[int] per bin)

# Sliding-window firing rate: returns (center_times, rates_hz)
centers, rates = ss.time_resolved_rate(spikes, duration=0.3, bin_width=0.1, step=0.05)

# PSTH averaged over multiple trials: returns (bin_center_times, mean_rate_hz)
trials = [spikes, [0.020, 0.060, 0.100, 0.140, 0.200]]
bin_centers, mean_rates = ss.psth(trials, duration=0.3, bin_width=0.1)

# Per-bin Fano factor across trials: returns (bin_center_times, fano_per_bin)
bin_centers, fano = ss.time_resolved_fano(trials, duration=0.3, bin_width=0.1)

API

All functions take spike times as a sequence of numbers and sort them internally.

Scalar metrics

  • firing_rate(spikes, *, duration): spike count divided by duration.
  • inter_spike_intervals(spikes): list of consecutive differences (empty for fewer than two spikes).
  • cv_isi(spikes): population standard deviation of the ISIs over their mean. Regular train 0, Poisson 1.
  • cv2(spikes): mean of 2 * |I(n+1) - I(n)| / (I(n+1) + I(n)) over adjacent intervals.
  • lv(spikes): mean of 3 * ((I(n) - I(n+1)) / (I(n) + I(n+1)))^2. Regular train 0, Poisson 1.
  • lvr(spikes, *, refractory): LvR with a refractoriness constant in the spike-time unit.
  • spike_counts(spikes, *, duration, bin_width): counts per equal-width bin tiling [0, n * bin_width).
  • fano_factor(spikes, *, duration, bin_width): population variance of the bin counts over their mean.

Pairwise metrics

  • spike_time_tiling_coefficient(spikes_a, spikes_b, *, dt, interval): spike-time tiling coefficient (Cutts and Eglen 2014). interval is a (start, end) recording window and dt is the synchronicity window. Returns a value in [-1, 1]; identical trains give 1.0, and the measure is symmetric and robust to differing firing rates. Empty trains return 0.0.

Time-resolved metrics

All functions use the half-open boundary [0, duration): a spike at exactly duration is excluded.

  • binned_spike_counts(spikes, *, duration, bin_width) -> list[int]: spike counts in int(duration / bin_width) consecutive non-overlapping bins tiling [0, duration).
  • time_resolved_rate(spikes, *, duration, bin_width, step) -> tuple[list[float], list[float]]: sliding-window firing rate. Window of width bin_width steps by step across [0, duration). Returns (window_center_times, rates_in_hz). Window positions are computed by integer indexing of the step size to avoid floating-point drift.
  • psth(trials, *, duration, bin_width) -> tuple[list[float], list[float]]: peristimulus time histogram. trials is a sequence of spike-time sequences. Returns (bin_center_times, mean_rate_per_bin_hz) averaged over trials. Raises ValueError on empty trials.
  • time_resolved_fano(trials, *, duration, bin_width) -> tuple[list[float], list[float]]: per-bin Fano factor (population variance / mean of per-trial counts). Returns (bin_center_times, fano_per_bin). Bins with zero mean across all trials are returned as float('nan'). Uses population variance (denominator N), so a single trial always gives 0.

Parameters after * are keyword-only and have no default values; pass them explicitly.

Notes

  • cv_isi uses the population standard deviation, matching the common numpy.std default.
  • cv2, lv, and lvr need at least three spikes; they raise a clear ValueError otherwise.
  • spike_counts uses floor(duration / bin_width) equal-width bins, so every bin has the same width; any remainder of the duration and any spikes outside the binned window are ignored.
  • binned_spike_counts uses int(duration / bin_width) bins (same count). All time-resolved functions use the half-open interval [0, duration): a spike at t == duration is excluded.
  • time_resolved_fano uses population variance (denominator N). With a single trial, variance is 0 for every bin; use multiple trials to get meaningful Fano estimates. Bins where the mean count is 0 across all trials are returned as float('nan').
  • spike_time_tiling_coefficient requires every spike to lie within the closed interval [start, end]; a spike outside raises a ValueError. Following the Cutts and Eglen convention, when a denominator 1 - P * T is zero (for example when dt is large enough that the tiling saturates to 1), that half of the STTC contributes 0. An empty train gives 0.0.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spikestats-0.3.0.tar.gz (765.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spikestats-0.3.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file spikestats-0.3.0.tar.gz.

File metadata

  • Download URL: spikestats-0.3.0.tar.gz
  • Upload date:
  • Size: 765.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for spikestats-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e6d9e563ad6e253714973852985bff4799489bc28464b5b8842114abe9752f96
MD5 31daf7aa9a9eb886f3acc7c7ca8fda60
BLAKE2b-256 0045e1bcd1b5070de1cf0ba2341a14bf41ab9866e2070680a7f94cc6f4447dee

See more details on using hashes here.

File details

Details for the file spikestats-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: spikestats-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for spikestats-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19c169505a1b1c7b3570362a04fb62f6c7944053d83cdcb3f311fe17b6ac5247
MD5 ebd614ccb57f653526972efea3064c99
BLAKE2b-256 0bfbc100d5a4fa5a1bae90ebddfc17d8b87ef9e35652e72e43122c77cccbe57e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page