A toolbox for sleep stage classification using ECG data
Project description
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 a 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
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
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 (we only show the C backend) is by far the fastest implementation among all tested packages (note that 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')
detection = 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')
detection = 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.2.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a73c6e85997429d083eff530c7702679281e7d425d94bc8245728b77d612dad |
|
MD5 | d86fcc73f6b6a94bda5542080d45b42f |
|
BLAKE2b-256 | a73dde678163fa13a55b3ca47a47f9744ee5d7222f6a4e52006bd48e157dde84 |
Hashes for sleepecg-0.2.0-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce2f8e4701d49beeb3ec70e3846099686cfc76342cfd2b17f895fa8417e86dab |
|
MD5 | 8bf7e46202478fed45aeaa492e79c7c4 |
|
BLAKE2b-256 | 709655a7d8cb7e81ab195e04fd02f38d78acb23cb8903ff98c51e0d433080315 |
Hashes for sleepecg-0.2.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2dfe2a8f58d82ca0800ccc0e89723960438adfa8623d9cacd956688ca94bd45 |
|
MD5 | 61a573fd94dfd89b237a5f296075e7bd |
|
BLAKE2b-256 | 8e984ef1b891bccdd82b65ca4e4868e90de327b9c0579415e79509e94b1cc82e |
Hashes for sleepecg-0.2.0-cp39-cp39-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58192ca46ab90a059f530c572834a4d3b95056e3b4695381f700aabe3e7e686f |
|
MD5 | 1921795eae5d4f8e6b8b34456421985f |
|
BLAKE2b-256 | fa4dac923d04356473613e03831249162f3a29ba6bfa532d97b187a62de1cea4 |
Hashes for sleepecg-0.2.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ecccaf88cbf79a0daac5e796714f23dae618ca7118a25a8ae2d75bb02d7ce43 |
|
MD5 | 5d87f84c31bc17a1229813e012a23e63 |
|
BLAKE2b-256 | a40b0554dca7af62b31d6d0f35c4e5dda2175393d07acbefadca569637e40106 |
Hashes for sleepecg-0.2.0-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90f90241d6445daae516696e51ae7dd21bc64b575ff3af6318933896f676ab37 |
|
MD5 | 4e686cfe7a88a6f653a77400ecf4e526 |
|
BLAKE2b-256 | c322ba9ee2a948d0e02bdee5cf49a1f94fdf72bd35e91efa5d6c1c4e27f16827 |
Hashes for sleepecg-0.2.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08949dfc529b8cecfd07726c0bbb82df8bb617033b5cdf18a39c6da4a4162ec2 |
|
MD5 | 78989a2d0807e213847cd7261c6a5ed5 |
|
BLAKE2b-256 | 57dcdc7eaa9c1a143243bc58fa45d47d2af9c7108ae9673249ab5cfb40b078c6 |
Hashes for sleepecg-0.2.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64eb19b7529385e6946aafc4c45afd8cfad622b286eeb713903a8afabb32e2f3 |
|
MD5 | 5439ea1601a57534afb2f351ddff9370 |
|
BLAKE2b-256 | cecd361064a480378f1418f77641d8f73d751061aa3cfb6036a88d238700e72e |
Hashes for sleepecg-0.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2528772efb71b8c57bdf2e5c79c63b6172e79d03835302345480ae00df89ec5 |
|
MD5 | 55b3ff0c9237c1842ed13426dcc69ea3 |
|
BLAKE2b-256 | 663f65dbafb840548064dfb2bd5d1350319ded0d027c35e5850143ad1f8e6de1 |
Hashes for sleepecg-0.2.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dea850684fe3d30dd3802c860f420807eebbfbc2d414bc0b1bc10858d83dd0f5 |
|
MD5 | 07a68d6d5f2e79df61b84c2b8c60e828 |
|
BLAKE2b-256 | a8b5621831e8a6e66728d71f7978c57e2b490564dd1337fe839fe5199617e3e1 |
Hashes for sleepecg-0.2.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c670396767a58f2c0e92f55756e9985d44bfb1f762ad55ecacbd56ee43169f86 |
|
MD5 | b406ad4fdc059c5558302dd275f97737 |
|
BLAKE2b-256 | 4f34371807c6964985f1257e4be6f8ccaf8f7a48bc94d241c07a8cb4c50037d4 |
Hashes for sleepecg-0.2.0-cp38-cp38-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 781d1a3fac3f29ecb766e5c51edb82c7c02663867d774286ca83118b2cd4db7f |
|
MD5 | 67b192fe41054349e8eb22504c8c07c6 |
|
BLAKE2b-256 | b491ba59f265467d622fd9ea4f60d104d7ca8eeb278c7bea3c23c471e3be58a0 |
Hashes for sleepecg-0.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4898b5b1d9ddc970fa9e6bcc848842374423bb5a83691aa189b59a27033baf5b |
|
MD5 | 8d2d4df3cc48de305bfa0655a0388f09 |
|
BLAKE2b-256 | ada26812ee14b549036f5ebd898abd8dfe67f3b53192b950fffc9c6aaac9b0ce |
Hashes for sleepecg-0.2.0-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e279683eea70d752a59c179f593e9c16a6e34070d2eae48f457b8c3635d112d |
|
MD5 | 788ea4cc6d485a5171388d40536c93e0 |
|
BLAKE2b-256 | 555ee8e87a9a455c6ec30110325d385bd5d09aec945648707b8ef4a7aa5ba9ac |
Hashes for sleepecg-0.2.0-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54d1086394e1cc4c0d604adfcf502f2ae47ab23ccb32b00f31ebaf0094ca53f6 |
|
MD5 | 2f9f55da8b2572d2f208a74f6e5fa3b0 |
|
BLAKE2b-256 | cf85a0e09988ae22474bdd3cf85100b5da1605db89083b1bb1c3ea2fb1808d5f |
Hashes for sleepecg-0.2.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8914244b7a65a2dd2cc2bcaa2e805de2b3b80d0d1e0519316ba97f40a4de7bb8 |
|
MD5 | 253a5a7cb015373446bc64280bd0ca38 |
|
BLAKE2b-256 | 35d6bab723451f30506b5f29715b8775128ee01c1285a8d4b3d1cd57909bcb54 |
Hashes for sleepecg-0.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e01ff7b656613a874e6575dc40f58e765f0d1dfdce3d60225c736d617e71e400 |
|
MD5 | c73bfc36946d7f226b236fe28588ad31 |
|
BLAKE2b-256 | 1d598ae7e6a4b160e330b2b515be5827ff05040ae5d2eef9862694ff6d322cf1 |
Hashes for sleepecg-0.2.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9c467ae6b030dd3ce88441170c7762a2e5ea6eb49154ab3376ce5b08ff2b21b |
|
MD5 | c976575e418c4422024e6c3aa58ad686 |
|
BLAKE2b-256 | f4ba761912d78fb68b7989dc153f34e112227a2b7220f2ada75dfc9c2162c145 |
Hashes for sleepecg-0.2.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5e588fdd00dd58edb5db63a6af749383c9fbb51e42bb32a472f79bdaa1df529 |
|
MD5 | 03af52b23b3754ba0d054c927cd05951 |
|
BLAKE2b-256 | 68337957b6ba79ec6f8c4d58840bd2b76d778cbfa7b888f9454ef48be0247f35 |
Hashes for sleepecg-0.2.0-cp37-cp37m-manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0c87dd6e9048e0ee49b770a27d965f6d0807c9af42e44448cd19d59b1ee6795 |
|
MD5 | 98285d954477bb61400d4b6909f0e055 |
|
BLAKE2b-256 | 0d72d7a7a68f1b1155d5684bd3ccf537f59b1f585cc147e8174802fb856964b6 |
Hashes for sleepecg-0.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c10673140254914a6d0153f58fd08bdfefb561266c609db43077fbf6facc346b |
|
MD5 | a1ee6bdd281cf89e1f32947633ba1d2d |
|
BLAKE2b-256 | f985a3cecd8508760bb29cc8ba440d4856f46ae2464e27bc77e9af187376b78b |
Hashes for sleepecg-0.2.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc82f12e7e6a890cc1f08afd7e2c446f032922709177a42b47dce017dd41587c |
|
MD5 | f9f2325fbab5946d4bac9efb3e8da913 |
|
BLAKE2b-256 | 070a95b36fd85d272ab0ad1ce756e353b85ccf119d87b69c133d97e21716bc59 |
Hashes for sleepecg-0.2.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ea03300faff3161ed31b3b76c59e521aa355b8572589cb366cc4b1b774ea33c |
|
MD5 | 4eab2dc717115c6c2fb05c7f726a0db5 |
|
BLAKE2b-256 | 686dcbf2984b02b61dc50f07a925cd4aafc46881c4837a255818224623158a10 |