Skip to main content

detect and extract spikes in time series data

Project description

quickspikes

DOI

This is a very basic but very fast window discriminator for detecting and extracting spikes in a time series. It was developed for analyzing extracellular neural recordings, but also works with intracellular data and probably many other kinds of time series.

Here's how it works:

detection diagram

The algorithm iterates through the time series. When the signal crosses the threshold (1) going away from zero, the algorithm then looks for a peak (2) that occurs within some number of samples from the threshold crossing. The number of samples can be adjusted to filter out broad spikes that are likely to be artifacts. If a peak occurs, its sample index is added to an array. These times can be used as-is, or they can be used to extract samples to either side of the peak for further analysis (e.g. spike sorting).

The algorithm uses a streaming pattern (i.e., it processes chunks of data and keeps its state between chunks), so it's suitable for realtime operations. Many signals of interest will require highpass filtering to remove slow variations.

Installation and Use

The algorithm is written in cython. You can get a python package from PyPI:

pip install quickspikes

Or to build from a copy of the repository:

pip install .

To detect peaks, you instantiate the detector with parameters that match the events you want to detect, and then send the detector chunks of data. For example, an extracellular recording at 20 kHz stored in 16-bit integers may have a noise floor around 2000, and the spikes will be on the order of 20 samples wide:

import quickspikes as qs
det = qs.detector(1000, 30)
times = det.send(samples)

You can continue sending chunks of data by calling send(). The detector will keep its state between calls, so you can detect spikes that occur on chunk boundaries. For example, if you're receiving data from some kind of generator, you could use a pattern like this:

for chunk in my_data_generator():
    times = det.send(chunk)
    # process times

Conversely, if the data are not contiguous, you should reinitialize the detector for each chunk.

You can adjust the detector's threshold at any point, for example to compensate for shifts in the mean and standard deviation of the signal:

reldet = qs.detector(2.5, 30)
reldet.scale_thresh(samples.mean(), samples.std())
times = reldet.send(samples)

To detect negative-going events, you'll need to invert the signal.

There are also some functions you can use to extract and align spike waveforms. Given a list of times returned from the detector.send() method, to extract waveforms starting 30 samples before the peak and ending 270 samples after:

f_times = qs.filter_times(times, 30, samples.size - 270)
spikes = qs.peaks(samples, f_times, 30, 270)
times, aligned = qs.realign_spikes(f_times, spikes, upsample=3, jitter=4)

Note that the list of event times may need to be filtered to avoid trying to access data points outside the bounds of the input time series. If you care about these events, you'll need to pad your input signal. The realign_spikes function uses a sinc-based resampling to more accurately locate the peak of the event.

There is also a reference copy of an ANSI C implementation and an f2py wrapper in f2py/. This algorithm is slightly less efficient and flexible, but may give better results if included directly in a C codebase.

Intracellular spikes

There are some specialized tools for working with intracellular data. Intracellular recordings present some special challenges for detecting spikes. The data are not centered, and spike waveforms can change over the course of stimulation. The approach used here is to find the largest spike in the recording, (typically the first) and locate the onset of the spike based on when the derivative begins to rapidly increase. The peak and onset are used to set a threshold for extracting subsequent spikes.

The following is an example for a recording at 50 kHz of a neuron with spikes that have a rise time of about 1 ms (50 samples). Spikes waveforms will start 7 ms before the peak and end 40 ms after, and will be trimmed to avoid any overlap with subsequent spikes.

from quickspikes.intracellular import SpikeFinder
detector = SpikeFinder(n_rise=50, n_before=350, n_after=2000)
detector.calculate_threshold(samples)
times, spikes = zip(*detector.extract_spikes(samples, min_amplitude=10))

License

Free for use under the terms of the GNU General Public License. See [[COPYING]] for details.

If you use this code in an academic work, citations are appreciated. There is no methods paper describing the algorithm, but the most relevant reference is:

C. D. Meliza and D. Margoliash (2012). Emergence of selectivity and tolerance in the avian auditory cortex. Journal of Neuroscience, doi:10.1523/JNEUROSCI.0845-12.2012

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

quickspikes-2.0.2.tar.gz (116.3 kB view hashes)

Uploaded Source

Built Distributions

quickspikes-2.0.2-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (160.7 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (152.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-pp310-pypy310_pp73-macosx_10_9_x86_64.whl (129.0 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

quickspikes-2.0.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (156.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (150.3 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (128.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

quickspikes-2.0.2-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (163.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (159.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (127.8 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

quickspikes-2.0.2-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (163.2 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (159.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (128.0 kB view hashes)

Uploaded PyPy macOS 10.9+ x86-64

quickspikes-2.0.2-cp312-cp312-win_amd64.whl (135.4 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

quickspikes-2.0.2-cp312-cp312-win32.whl (115.2 kB view hashes)

Uploaded CPython 3.12 Windows x86

quickspikes-2.0.2-cp312-cp312-musllinux_1_1_x86_64.whl (830.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp312-cp312-musllinux_1_1_i686.whl (774.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (833.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (802.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp312-cp312-macosx_10_9_x86_64.whl (155.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

quickspikes-2.0.2-cp312-cp312-macosx_10_9_universal2.whl (281.0 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

quickspikes-2.0.2-cp311-cp311-win_amd64.whl (134.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

quickspikes-2.0.2-cp311-cp311-win32.whl (114.1 kB view hashes)

Uploaded CPython 3.11 Windows x86

quickspikes-2.0.2-cp311-cp311-musllinux_1_1_x86_64.whl (858.1 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp311-cp311-musllinux_1_1_i686.whl (805.9 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (860.9 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (829.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp311-cp311-macosx_10_9_x86_64.whl (152.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

quickspikes-2.0.2-cp311-cp311-macosx_10_9_universal2.whl (275.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

quickspikes-2.0.2-cp310-cp310-win_amd64.whl (133.6 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

quickspikes-2.0.2-cp310-cp310-win32.whl (114.3 kB view hashes)

Uploaded CPython 3.10 Windows x86

quickspikes-2.0.2-cp310-cp310-musllinux_1_1_x86_64.whl (799.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp310-cp310-musllinux_1_1_i686.whl (746.3 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (794.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (768.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp310-cp310-macosx_10_9_x86_64.whl (152.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

quickspikes-2.0.2-cp310-cp310-macosx_10_9_universal2.whl (275.9 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

quickspikes-2.0.2-cp39-cp39-win_amd64.whl (134.0 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

quickspikes-2.0.2-cp39-cp39-win32.whl (114.9 kB view hashes)

Uploaded CPython 3.9 Windows x86

quickspikes-2.0.2-cp39-cp39-musllinux_1_1_x86_64.whl (801.3 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp39-cp39-musllinux_1_1_i686.whl (749.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (796.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (767.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl (153.2 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

quickspikes-2.0.2-cp39-cp39-macosx_10_9_universal2.whl (277.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

quickspikes-2.0.2-cp38-cp38-win_amd64.whl (134.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

quickspikes-2.0.2-cp38-cp38-win32.whl (114.9 kB view hashes)

Uploaded CPython 3.8 Windows x86

quickspikes-2.0.2-cp38-cp38-musllinux_1_1_x86_64.whl (837.8 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp38-cp38-musllinux_1_1_i686.whl (786.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (815.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (783.8 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp38-cp38-macosx_10_9_x86_64.whl (152.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

quickspikes-2.0.2-cp38-cp38-macosx_10_9_universal2.whl (274.2 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

quickspikes-2.0.2-cp37-cp37m-win_amd64.whl (132.4 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

quickspikes-2.0.2-cp37-cp37m-win32.whl (113.9 kB view hashes)

Uploaded CPython 3.7m Windows x86

quickspikes-2.0.2-cp37-cp37m-musllinux_1_1_x86_64.whl (744.3 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp37-cp37m-musllinux_1_1_i686.whl (688.4 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (735.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (711.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl (150.9 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

quickspikes-2.0.2-cp36-cp36m-win_amd64.whl (144.6 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

quickspikes-2.0.2-cp36-cp36m-win32.whl (120.8 kB view hashes)

Uploaded CPython 3.6m Windows x86

quickspikes-2.0.2-cp36-cp36m-musllinux_1_1_x86_64.whl (730.7 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

quickspikes-2.0.2-cp36-cp36m-musllinux_1_1_i686.whl (677.6 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

quickspikes-2.0.2-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (727.8 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

quickspikes-2.0.2-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (705.1 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

quickspikes-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl (148.0 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

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