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` <https://sleepdata.org/datasets/mesa/files/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()` <https://github.com/cbrnr/sleepecg/blob/main/sleepecg/heartbeats.py#L40>`_ 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` <https://raw.githubusercontent.com/cbrnr/sleepecg/main/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` <https://github.com/cbrnr/sleepecg/tree/main/examples/benchmark>`_. The used package versions are listed in \ ``requirements-benchmark.txt` <https://github.com/cbrnr/sleepecg/blob/main/examples/benchmark/requirements-benchmark.py>`_.

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