A toolbox for sleep stage classification using ECG data
Project description
SleepECG
This package 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 a functional interface 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
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 leads to substantially faster detections as compared to implementations in pure Python.
Usage
The function detect_heartbeats()
finds heartbeats in an unfiltered ECG signal ecg
(a one-dimensional NumPy array) 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
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. Our detector is by far the fastest implementation among all tested packages (note the y-axis is logarithmically scaled).
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.
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.
We used the following detectors for our benchmarks:
# mne
import mne # https://pypi.org/project/mne/
detection = mne.preprocessing.ecg.qrs_detector(fs, ecg, verbose=False)
# wfdb_xqrs
import wfdb.processing # https://pypi.org/project/wfdb/
detection = wfdb.processing.xqrs_detect(ecg, fs, verbose=False)
# pyecg_pan_tompkins
import ecgdetectors # https://pypi.org/project/py-ecg-detectors/
detection = ecgdetectors.Detectors(fs).pan_tompkins_detector(ecg)
# biosppy_hamilton
import biosppy # https://pypi.org/project/biosppy/
detection = biosppy.signals.ecg.hamilton_segmenter(ecg, fs)[0]
# heartpy
import heartpy # https://pypi.org/project/heartpy/
wd, m = heartpy.process(ecg, fs)
detection = np.array(wd['peaklist'])[wd['binary_peaklist'].astype(bool)]
# neurokit2_nk
import neurokit2 # https://pypi.org/project/neurokit2/
clean_ecg = neurokit2.ecg.ecg_clean(ecg, int(fs), method='neurokit')
peak_indices = neurokit2.ecg.ecg_findpeaks(clean_ecg, int(fs), method='neurokit')['ECG_R_Peaks']
# neurokit2_kalidas2017
import neurokit2 # https://pypi.org/project/neurokit2/
clean_ecg = neurokit2.ecg.ecg_clean(ecg, int(fs), method='kalidas2017')
peak_indices = neurokit2.ecg.ecg_findpeaks(clean_ecg, int(fs), method='kalidas2017')['ECG_R_Peaks']
# sleepecg
import sleepecg # https://pypi.org/project/sleepecg/
detection = sleepecg.heartbeat_detection.detect_heartbeats(ecg, fs)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for sleepecg-0.1.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49c85051d8394765599c6ada93e967423b524cfbe7402c53fef42501388a5fe7 |
|
MD5 | 33f9b612a9694c4ef82bf8172e2f6e33 |
|
BLAKE2b-256 | 3a83bab169eb42e17e6a7e1c9052615ea5fe421dff34eb3511e3c480b13a34e5 |
Hashes for sleepecg-0.1.0-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c20bb8d3461be1a642231e4e587568fbdb389d71074201ed0a8235b1db483b8 |
|
MD5 | 40ada7bae6ec691eb160e99e1c95f56c |
|
BLAKE2b-256 | 608001570a9999f633aab24a5c363e45e83dbfa1a29e845547191931f4e64ca3 |
Hashes for sleepecg-0.1.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a90b4dff79119434508b6ddfdb42b6fcdbba412e6a126021259b7ac5749a97ae |
|
MD5 | 9f3e3acab5126d2d421e51cceb6c22d1 |
|
BLAKE2b-256 | e7108bd436ce378c75036492f45ddc9706310f1ee1ec769ba8897eac8a858267 |
Hashes for sleepecg-0.1.0-cp39-cp39-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bf7b061431b76b1e370234d524cb63bb19b9c5016b69c02b10264a1df762bee |
|
MD5 | b4f0ce7f6307fcdfabd4e40823be7063 |
|
BLAKE2b-256 | 9ac02fe6e07bbb9db2d9aa70ccb9bf12406d23817bc6d917f7b1861eca90fae4 |
Hashes for sleepecg-0.1.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5245c26904228426cbba343aadf60f2de0992517065df7176a428adec82a7789 |
|
MD5 | f40080c67c43252c2d2484a6d3cdbdc0 |
|
BLAKE2b-256 | cb9bfe75052c7332d614a91c53f8213a50de871c6d20849ecf536c0cd72eaaff |
Hashes for sleepecg-0.1.0-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a43674dab97b33557ddef2d2a1ad57028e871a22c2e81fa6b29bae9a97b208dc |
|
MD5 | 227694aa0a6be2846c1ea74a2c1d2a95 |
|
BLAKE2b-256 | d95ca059b948d9d37a21da47c545d8e6f4e911e64db6d98fb8bf7f4bee6ef163 |
Hashes for sleepecg-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce261c1e9fca290870355e696f93cd8ba31b5ac445eda0d4a0ab4157f586a74b |
|
MD5 | 5cdb3a7da7732bf2c323ad5e760d65b3 |
|
BLAKE2b-256 | cc59d79b082169942184592bb777c7f7b31d91ee6c0a9e54fe45f12f90e1387f |
Hashes for sleepecg-0.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9230e93f4c67ba2ec9261a386d9d717a15b51f7a483acdf57508d45ac6cbc274 |
|
MD5 | 806dbe0b6bdcf9c85d5dc2a13cb28406 |
|
BLAKE2b-256 | aaba95dac817aa9e895b4cb48f6bf620caff109acbae873702d717720395e78e |
Hashes for sleepecg-0.1.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89d07be46a552b2802f78f64a8a115fa337100bc5dfb44ca392a4a9d779b77a |
|
MD5 | 5a5e1c53c84a6eec7b8b9d8ae8aef1af |
|
BLAKE2b-256 | bdbfaf79be01dbf8c5cea87c6b7e06f0d25c45722eabb1144ce6a4cfe53e6d01 |
Hashes for sleepecg-0.1.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57ea740d5239a3afeeba0bd4d676cc6ca5606dabb025eab5bb2af7eab9b5040a |
|
MD5 | bc914d1e703189ff96e3eb1e6bb2af11 |
|
BLAKE2b-256 | 8bd94cea04a622ff0b129c856cd73e3c1859383ae7818e3e543ae383c0cc3eaf |
Hashes for sleepecg-0.1.0-cp38-cp38-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 034513a082cc474894f56dca270fce9bade947cfdce980b633c5a8a34e4332bd |
|
MD5 | 8b4113d8432cff50c87e97adf9ecad6f |
|
BLAKE2b-256 | 72ebca10fa663cd081b2ff1208b87003a493d7d83017c4278b0ac90d41e1d87a |
Hashes for sleepecg-0.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e8224594c595d91f744868300b0bd9531e828c60744c1a1ca6e6d0c34839188 |
|
MD5 | 7ae041f5ac5d6da98b769416e3eb9111 |
|
BLAKE2b-256 | 68490616d843ec991570db51cbd3121ed03fb7188f57f24a7d7bce5efcb324ff |
Hashes for sleepecg-0.1.0-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc82e3b145926af1dbb7de06dacc95643650f4d132220dd92be33f95eba81833 |
|
MD5 | 4acacf1abe2cbdee7443d8d7af2728c0 |
|
BLAKE2b-256 | e4fafc50eb1d727e2d0125bd250a9dd053d0c8c11290a2dc3cdc95e555dc74bc |
Hashes for sleepecg-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5264e82d69ac9b25a43c5a920dde824e6481686953bddb3272ddb63ea9567df |
|
MD5 | 1cb43468829f193a03b6b8786d47c697 |
|
BLAKE2b-256 | 338fe8977181cb28e31a74ae8a784759fbe07ff9cd6b417ed358f8ead2a9cf91 |
Hashes for sleepecg-0.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2933ce778abc5e53b37928194340244871eacbbc281c922d4477948c0e37bfec |
|
MD5 | 13f2826b7585443dafdd52a30de56b2d |
|
BLAKE2b-256 | 944f7eecbef20d62a4c5ad51807f2d9c7023c61ad99cbb7eff35a22e87054d8e |
Hashes for sleepecg-0.1.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d93b0243105a8ee1bbf844aec9bb4054a8a577b8c8c610ae1ea9edc069b14e25 |
|
MD5 | 46277bbbdfdd00a1997b3e428cec5d7b |
|
BLAKE2b-256 | 93f29944e94e9096c77006ee5ae283ab14fa7cf05a3bcacb2acbabc0e74bc223 |
Hashes for sleepecg-0.1.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3832f8112b1b555e2b8d81a475e291d38a7767b78908f7b9f6d28b5856b3510 |
|
MD5 | f83789072483e4401484921ed2717e52 |
|
BLAKE2b-256 | c83a33eda4e8d39a291db983c3f71a657158b0a01e13af73420ea7ce4bb288cb |
Hashes for sleepecg-0.1.0-cp37-cp37m-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5fbb2a7824a9d9f5db53fe2a351c5b0f538043aac5a5dc2a77b1f02617bd5dc |
|
MD5 | 1179e41d060d5762455ae8411435a039 |
|
BLAKE2b-256 | ca416d853b09b83a1ced8f37059fdf70f5513afa795b405de7c17d18917eac6b |
Hashes for sleepecg-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26c0384fa9e4732ddb55d8c67fe30186d2730fd47e9e5f141d1b8bbc8d6b8234 |
|
MD5 | 820644369da45475c173a1c67e9f649e |
|
BLAKE2b-256 | cd1b419457dbf238dc0b96a9ce4713d8d9949c9e46263e186382a805558dd58f |
Hashes for sleepecg-0.1.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | abe632b7464ba9decaf8077a0ed07e5b7fae3d629914ed9dba8b4c5cc6f0a506 |
|
MD5 | 31ed45f34bd19bb9442a975534db058e |
|
BLAKE2b-256 | ff354d98e13fe1189377519db3e3b4b934c01d0bd52c4242e7ffa5bef0c7e6d2 |
Hashes for sleepecg-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c0de01ca30284b16c0fa592ac40733daadb948cc1a0d4097db851094a6ec7d2 |
|
MD5 | cad3003d6429e4e6e0e938a88b926ebd |
|
BLAKE2b-256 | 9a400fd8c3dc4b2217108b735919928a4b3fd638ace8faad23d1e52f3499a638 |