Feature extraction
Detailed information on implemented features (both time domain and frequency domain) is available here.
sleepecg.extract_features(records, lookback=0, lookforward=30, sleep_stage_duration=30, feature_selection=None, fs_rri_resample=4, min_rri=None, max_rri=None, max_nans=0, n_jobs=1)
Calculate features from sleep data (e.g. heart rate).
Time and frequency domain heart rate variability (HRV) features are calculated based on the following publications (see feature extraction for available features and feature groups.):
- Task Force of the European Society of Cardiology. (1996). Heart rate variability: standards of measurement, physiological interpretation and clinical use. Circulation, 93, 1043-1065. https://doi.org/10.1161/01.CIR.93.5.1043
- Shaffer, F., & Ginsberg, J. P. (2017). An overview of heart rate variability metrics and norms. Frontiers in Public Health, 258. https://doi.org/10.3389/fpubh.2017.00258
- Toichi, M., Sugiura, T., Murai, T., & Sengoku, A. (1997). A new method of assessing cardiac autonomic function and its comparison with spectral analysis and coefficient of variation of R–R interval. Journal of the Autonomic Nervous System, 62(1-2), 79-84. https://doi.org/10.1016/S0165-1838(96)00112-9
Parameters:
-
records
(Iterable[SleepRecord]
) –An iterable of
SleepRecord
objects as yielded by the various reader functions in SleepECG. -
lookback
(int
) –Backward extension of the analysis window from each sleep stage time in seconds, by default
0
. -
lookforward
(int
) –Forward extension of the analysis window from each sleep stage time in seconds, by default
30
. -
sleep_stage_duration
(int
) –Duration of a single sleep stage in the returned
stages
in seconds, by default30
. -
feature_selection
(list[str]
) –Which features to extract. Can be feature groups or single feature identifiers, as listed in feature extraction. If
None
(default), all possible features are extracted. -
fs_rri_resample
(float
) –Frequency in Hz at which the RRI time series should be resampled before spectral analysis. Only relevant for frequency domain features, by default
4
. -
min_rri
(Optional[float]
) –Minimum RRI value in seconds to be considered valid. Will be passed to
preprocess_rri()
, by defaultNone
. -
max_rri
(Optional[float]
) –Maximum RRI value in seconds to be considered valid. Will be passed to
preprocess_rri()
, by defaultNone
. -
max_nans
(float
) –Maximum fraction of NaNs in an analysis window for which frequency features are computed. Should be a value between
0
and1
, by default0
. -
n_jobs
(int
) –The number of jobs to run in parallel. If
1
(default), no parallelism is used;-1
means using all processors.
Returns:
-
features
(list[ndarray]
) –A list containing feature matrices, which are arrays of shape
(len(sleep_stages), <num_features>)
and contain the extracted features per record. -
stages
(list[ndarray | None]
) –A list containing label vectors, i.e. the annotated sleep stages. For any
SleepRecord
without annotated stages, the corresponding list entry will beNone
. -
feature_ids
(list[str]
) –A list containing the identifiers of the extracted features. Feature groups passed in
feature_selection
are expanded to all individual features they contain. The order matches the column order of the feature matrix.
Source code in sleepecg/feature_extraction.py
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
|
sleepecg.preprocess_rri(rri, min_rri=None, max_rri=None)
Replace invalid RRI samples with np.nan
.
Parameters:
-
rri
(ndarray
) –An array containing consecutive RR interval lengths in seconds.
-
min_rri
(float
) –Minimum RRI in seconds to be considered valid. If
None
(default), no lower bounds check is performed. -
max_rri
(float
) –Maximum RRI in seconds to be considered valid. If
None
(default), no upper bounds check is performed.
Returns:
-
ndarray
–The cleaned RRI series.
Examples:
Mask RR intervals outside the range of 0.4 to 2 s (= 30 to 150 bpm):
>>> from sleepecg import preprocess_rri
>>> preprocess_rri([0.5, 0.2, 0.8, 2.5, 0.6], min_rri=0.4, max_rri=2)
array([0.5, nan, 0.8, nan, 0.6])