Skip to main content

spikegen

spikegen logo

PyPI CI License: MIT

Generate spike trains in pure Python with zero dependencies. Poisson, gamma, inverse-Gaussian and lognormal renewal, regular, Bernoulli, and inhomogeneous processes, returned as plain sorted lists of spike times, with explicit seeds for reproducibility.

Install

pip install spikegen

30-second example

from spikegen import homogeneous_poisson, regular, gamma_renewal, with_refractory

homogeneous_poisson(rate=50.0, duration=2.0, seed=0)   # Poisson spikes in [0, 2)
regular(rate=10.0, duration=1.0)                        # [0.0, 0.1, 0.2, ...]
gamma_renewal(rate=20.0, shape=2.0, duration=1.0, seed=0)  # more regular than Poisson

from spikegen import inverse_gaussian_renewal

# Inverse-Gaussian (Wald) renewal: ISIs ~ IG(mu, lam); the LIF first-passage law.
inverse_gaussian_renewal(mu=0.05, lam=0.2, duration=1.0, seed=0)

from spikegen import lognormal_renewal

# Lognormal renewal: ISIs are lognormal with mean 20 ms and CV 0.6 (common cortical ISI fit).
lognormal_renewal(mean=0.02, cv=0.6, duration=1.0, seed=0)

spikes = homogeneous_poisson(rate=80.0, duration=1.0, seed=0)
with_refractory(spikes, refractory=0.002)              # enforce a 2 ms refractory period

from spikegen import population
population(lambda s: homogeneous_poisson(rate=50.0, duration=2.0, seed=s), units=10, seed=0)

from spikegen import bernoulli, jitter

# Discrete-time Bernoulli process: 1 ms bins, 50 Hz rate over 1 second
bernoulli(rate=50.0, duration=1.0, dt=0.001, seed=0)

# Jitter: add Gaussian noise (sigma=2 ms) to each spike time, useful for surrogate data
spikes = homogeneous_poisson(rate=40.0, duration=1.0, seed=0)
jitter(spikes, sigma=0.002, seed=1)

Times are in the same units as 1 / rate (seconds if rate is in Hz). Seeded processes are reproducible: the same seed gives the same train.

Optional NumPy fast path

The core package has zero runtime dependencies. For long, high-rate Poisson trains there is an optional vectorized generator behind the [fast] extra:

pip install spikegen[fast]
from spikegen import homogeneous_poisson_numpy

# Same homogeneous Poisson process as homogeneous_poisson, but vectorized with NumPy.
homogeneous_poisson_numpy(rate=1000.0, duration=10.0, seed=0)

homogeneous_poisson_numpy(rate, duration, seed) draws exponential inter-spike intervals in batches and takes their cumulative sum with NumPy instead of looping in Python, which is much faster for long, high-rate trains. NumPy is imported lazily only inside this function, so the pure-Python homogeneous_poisson stays the default and the package still imports with no dependencies; calling homogeneous_poisson_numpy without [fast] installed raises an ImportError.

The fast path is reproducible for a fixed seed but is not bit-identical to homogeneous_poisson for the same seed: it uses NumPy's Generator (PCG64), a different random stream from the pure path's random.Random (Mersenne Twister). The two are statistically equivalent: both produce a homogeneous Poisson process with the same rate, so their spike counts, mean rate, and inter-spike-interval distribution agree.

Why this exists

Generating synthetic spike trains is a daily need, but the generators live inside heavy frameworks: elephant requires neo and quantities, pyspike is NumPy-based, and other options are old or partial. spikegen is a small, dependency-free generator that returns plain lists of floats, so reproducible spike trains are one import away. It pairs with spikedist: generate trains, then measure the distance between them.

Processes

  • regular(rate, duration): evenly spaced spikes. Deterministic.
  • homogeneous_poisson(rate, duration, seed): constant-rate Poisson process.
  • inhomogeneous_poisson(rate_fn, max_rate, duration, seed): time-varying rate by thinning.
  • gamma_renewal(rate, shape, duration, seed): gamma inter-spike intervals; shape 1 is Poisson, larger shape is more regular.
  • inverse_gaussian_renewal(mu, lam, duration, seed): inverse-Gaussian (Wald) inter-spike intervals IG(mu, lam) with mean mu and variance mu**3 / lam, so the squared coefficient of variation is mu / lam. Large lam gives regular spiking, small lam gives irregular spiking. This is the first-passage-time distribution of a drift-diffusion (perfect integrate-and-fire) neuron, a principled companion to gamma_renewal. Intervals are sampled with the Michael-Schucany-Haas algorithm (Michael, Schucany, Haas 1976).
  • lognormal_renewal(mean, cv, duration, seed): lognormal inter-spike intervals, the common empirical fit for cortical ISI distributions. Parameterized directly by the ISI mean and coefficient of variation cv: the underlying normal N(mu, sigma**2) follows from sigma**2 = ln(1 + cv**2) and mu = ln(mean) - sigma**2 / 2, and each interval is exp(mu + sigma * Z) with Z ~ N(0, 1). This gives E[ISI] = mean and CV[ISI] = cv exactly in expectation; the lognormal CV depends only on sigma, CV = sqrt(exp(sigma**2) - 1). Small cv gives nearly regular spiking, large cv gives bursty, irregular spiking. A natural companion to gamma_renewal and inverse_gaussian_renewal.
  • bernoulli(rate, duration, dt, seed): discrete-time Bernoulli process. Time is divided into bins of width dt; each bin fires at its start time with probability rate * dt. Raises ValueError when rate * dt > 1.
  • with_refractory(times, refractory): drop spikes within a minimum interval.
  • jitter(times, sigma, seed): add independent Gaussian jitter (standard deviation sigma) to each spike time and return sorted results. Useful for surrogate or null datasets that destroy precise timing while preserving spike count. sigma = 0 sorts without change.
  • population(make, units, seed): build a population of trains by calling make(seed) once per unit with independent, reproducible child seeds derived from the base seed.

All parameters after the first are keyword-only and explicit.

Testing

pip install -e ".[dev]"
pytest

Tests cover exact values for the deterministic generators, seeded reproducibility, the rate-bound and ordering invariants, and the validation paths, with property tests via Hypothesis.

Contributing

Issues and pull requests are welcome. See CONTRIBUTING.md.

License

MIT. See LICENSE.

Metadata

Release files for spikegen 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spikegen 0.6.0
File Size Uploaded
spikegen-0.6.0.tar.gz 869.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spikegen 0.6.0
File Interpreter ABI Platform
spikegen-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 881.7 kB

Release files / spikegen-0.6.0.tar.gz

Download URL spikegen-0.6.0.tar.gz
Size 869.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b2e9ca34fcf50456213a7bec816863c571ec7d0e0973b1b230f374f2c9e9d0db
BLAKE2b-256 checksum
How to use checksums
396e1634eb764dd1e497302a16918b7ce068114edb02a756b1f3dba32672c7e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release files / spikegen-0.6.0-py3-none-any.whl

Download URL spikegen-0.6.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67e94dc0eca5b6f1fcae1cae2ec7b277ac0c1fe5699dbf9309f2fa73e5cb50f2
BLAKE2b-256 checksum
How to use checksums
49a076d063effcd076ee06f4c94a26cc781e5b39d6a52128dc15e19a24dd5e95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page