Skip to main content

A toolbox for sleep stage classification using ECG data

Project description

Py Version PyPI Version conda-forge version Docs

SleepECG

SleepECG provides tools for sleep stage classification when EEG signals are not available. Based only on ECG (and to a lesser extent also movement data), SleepECG provides functions for

  • downloading and reading open polysomnography datasets (TODO),
  • detecting heartbeats from ECG signals, and
  • classifying sleep stages (which includes the complete preprocessing, feature extraction, and classification pipeline) (TODO).

Installation

SleepECG is available on PyPI and can be installed with pip:

pip install sleepecg

Alternatively, install via conda:

conda install -c conda-forge sleepecg

Contributing

The contributing guide contains detailed instructions on how to contribute to SleepECG.

Dataset reading

SleepECG provides a consistent functional interface for downloading and reading common polysomnography datasets. While reader functions are a WIP, SleepECG already provides an interface for downloading datasets from the National Sleep Research Resource (NSRR) on sleepdata.org, which replicates the functionality of the NSRR Ruby Gem.

The example below downloads all files within mesa/polysomnography/edfs matching *-00* to a local folder ./datasets:

from sleepecg.io import download_nsrr, set_nsrr_token

set_nsrr_token('<your-download-token-here>')
download_nsrr(
    db_slug='mesa',
    subfolder='polysomnography/edfs',
    pattern='*-00*',
    data_dir='./datasets',
)

ECG dataset readers

To facilitate evaluation of heartbeat detector performance, reader functions for the following annotated ECG datasets are provided:

Heartbeat detection

ECG-based sleep staging heavily relies on heartrate variability. Therefore, a reliable and efficient heartbeat detector is essential. SleepECG provides a detector based on the approach described by Pan & Tompkins (1985). We outsourced performance-critical code to a C extension, which makes the detector substantially faster than other implementations. However, we also provide Numba and pure Python backends (the Numba backend is almost as fast whereas the pure Python implementation is much slower).

Usage

The function detect_heartbeats() finds heartbeats in an unfiltered ECG signal ecg with sampling frequency fs (in Hz). It returns the indices of all detected heartbeats. A complete example including visualization and performance evaluation is available in examples/heartbeat_detection.py.

from sleepecg import detect_heartbeats

detection = detect_heartbeats(ecg, fs)

Performance evaluation

All code used for performance evaluation is available in examples/benchmark. The used package versions are listed in requirements-benchmark.txt.

We evaluated detector runtime using slices of different lengths from LTDB records with at least 20 hours duration. Error bars in the plot below correspond to the standard error of the mean. The C backend of our detector is by far the fastest implementation among all tested packages (note that the y-axis is logarithmically scaled). Runtime evaluation was performed on an Intel® Xeon® Prozessor E5-2440 v2 with 32 GiB RAM. No parallelization was used.

LTDB runtimes

We also evaluated detection performance on all MITDB records. We defined a successful detection if it was within 100ms (i.e. 36 samples) of the corresponding annotation (using a tolerance here is necessary because annotations usually do not coincide with the exact R peak locations). In terms of recall, precision, and F1 score, our detector is among the best heartbeat detectors available.

MITDB metrics

For analysis of heartrate variability, detecting the exact location of heartbeats is essential. As a measure of how accurate a detector is, we computed Pearson's correlation coefficient between resampled RRI time series deduced from annotated and detected beat locations from all GUDB records. Our implementation detects peaks in the bandpass-filtered ECG signal, so it produces stable RRI time series without any post-processing.

GUDB pearson correlation

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

sleepecg-0.3.0.tar.gz (31.1 kB view hashes)

Uploaded Source

Built Distributions

sleepecg-0.3.0-cp39-cp39-win_amd64.whl (35.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

sleepecg-0.3.0-cp39-cp39-win32.whl (34.4 kB view hashes)

Uploaded CPython 3.9 Windows x86

sleepecg-0.3.0-cp39-cp39-manylinux2014_x86_64.whl (51.8 kB view hashes)

Uploaded CPython 3.9

sleepecg-0.3.0-cp39-cp39-manylinux2014_i686.whl (51.6 kB view hashes)

Uploaded CPython 3.9

sleepecg-0.3.0-cp39-cp39-manylinux1_x86_64.whl (51.8 kB view hashes)

Uploaded CPython 3.9

sleepecg-0.3.0-cp39-cp39-manylinux1_i686.whl (51.6 kB view hashes)

Uploaded CPython 3.9

sleepecg-0.3.0-cp39-cp39-macosx_11_0_arm64.whl (31.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

sleepecg-0.3.0-cp39-cp39-macosx_10_9_x86_64.whl (31.3 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

sleepecg-0.3.0-cp38-cp38-win_amd64.whl (35.7 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

sleepecg-0.3.0-cp38-cp38-win32.whl (34.4 kB view hashes)

Uploaded CPython 3.8 Windows x86

sleepecg-0.3.0-cp38-cp38-manylinux2014_x86_64.whl (52.2 kB view hashes)

Uploaded CPython 3.8

sleepecg-0.3.0-cp38-cp38-manylinux2014_i686.whl (51.9 kB view hashes)

Uploaded CPython 3.8

sleepecg-0.3.0-cp38-cp38-manylinux1_x86_64.whl (52.2 kB view hashes)

Uploaded CPython 3.8

sleepecg-0.3.0-cp38-cp38-manylinux1_i686.whl (51.9 kB view hashes)

Uploaded CPython 3.8

sleepecg-0.3.0-cp38-cp38-macosx_11_0_arm64.whl (31.4 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

sleepecg-0.3.0-cp38-cp38-macosx_10_9_x86_64.whl (31.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

sleepecg-0.3.0-cp37-cp37m-win_amd64.whl (35.6 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

sleepecg-0.3.0-cp37-cp37m-win32.whl (34.4 kB view hashes)

Uploaded CPython 3.7m Windows x86

sleepecg-0.3.0-cp37-cp37m-manylinux2014_x86_64.whl (52.8 kB view hashes)

Uploaded CPython 3.7m

sleepecg-0.3.0-cp37-cp37m-manylinux2014_i686.whl (52.6 kB view hashes)

Uploaded CPython 3.7m

sleepecg-0.3.0-cp37-cp37m-manylinux1_x86_64.whl (52.8 kB view hashes)

Uploaded CPython 3.7m

sleepecg-0.3.0-cp37-cp37m-manylinux1_i686.whl (52.6 kB view hashes)

Uploaded CPython 3.7m

sleepecg-0.3.0-cp37-cp37m-macosx_10_9_x86_64.whl (31.2 kB view hashes)

Uploaded CPython 3.7m 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