7
7
import numpy as np
8
8
import pandas as pd
9
9
10
- from typing import Optional , Tuple
10
+ from typing import Tuple
11
11
from scipy .signal import resample
12
12
from sklearn .utils import check_array
13
13
from neurokit2 import ecg_clean , ecg_peaks
@@ -66,53 +66,52 @@ def find_rpeaks(
66
66
ecg_lead : np .ndarray ,
67
67
sampling_rate : int ,
68
68
neurokit_method : str = "neurokit" ,
69
- correct_artifacts : bool = True
70
- ) -> Tuple [np .ndarray , dict ]:
71
- '''
72
- Internal function which calls neurokit for rpeak and qrs-complex computation.
69
+ correct_artifacts : bool = True ) -> Tuple [np .ndarray , dict ]:
70
+ """
71
+ Internal function which calls neurokit for rpeak and qrs-complex computation.
73
72
74
- Parameters:
75
- ecg_lead: An array representing a single-lead ECG signal. The input is
76
- expected to be a one-dimensional array of voltage values over time, representing
77
- the electrical activity of the heart as captured by a specific ECG lead.
78
-
79
- sampling_rate: Defines the sampling rate for all ECG recordings.
80
-
81
- neurokit_method: Chooses the algorithm for R-peak detection from the
82
- NeuroKit package. Different algorithms may offer varying performance
83
- based on the ECG signal characteristics.
84
-
85
- correct_artifacts: If set to True, artifact correction is applied
86
- exclusively for R-peak detections, enhancing the accuracy of peak
87
- identification in noisy signals.
88
-
89
- Returns:
90
- (rpeaks, qrs_epochs): A pair of elements (rpeaks, qrs_epochs) representing
91
- the outcomes of the R-peak detection and QRS complex computation processes,
92
- respectively. If an error occurs during the processing, the function
93
- returns (None, None), indicating a failure in signal analysis.
94
- '''
95
- try :
96
- # clean the ecg as recommended by neurokit
97
- data_ = ecg_clean (
98
- ecg_lead ,
99
- sampling_rate = sampling_rate
100
- )
101
-
102
- # caluclate rpeaks
103
- _ , r_peaks = ecg_peaks (
104
- data_ ,
105
- sampling_rate = sampling_rate ,
106
- method = neurokit_method ,
107
- correct_artifacts = correct_artifacts
108
- )
109
- rpeaks = r_peaks ['ECG_R_Peaks' ].astype (np .int32 )
110
-
111
- except Exception as e :
112
- logging .warning (f'Failure in neurokit: { e } \n ' )
113
- return None , None
114
-
115
- return rpeaks
73
+ Parameters:
74
+ ecg_lead: An array representing a single-lead ECG signal. The input is
75
+ expected to be a one-dimensional array of voltage values over time, representing
76
+ the electrical activity of the heart as captured by a specific ECG lead.
77
+
78
+ sampling_rate: Defines the sampling rate for all ECG recordings.
79
+
80
+ neurokit_method: Chooses the algorithm for R-peak detection from the
81
+ NeuroKit package. Different algorithms may offer varying performance
82
+ based on the ECG signal characteristics.
83
+
84
+ correct_artifacts: If set to True, artifact correction is applied
85
+ exclusively for R-peak detections, enhancing the accuracy of peak
86
+ identification in noisy signals.
87
+
88
+ Returns:
89
+ (rpeaks, qrs_epochs): A pair of elements (rpeaks, qrs_epochs) representing
90
+ the outcomes of the R-peak detection and QRS complex computation processes,
91
+ respectively. If an error occurs during the processing, the function
92
+ returns (None, None), indicating a failure in signal analysis.
93
+ """
94
+ try :
95
+ # clean the ecg as recommended by neurokit
96
+ data_ = ecg_clean (
97
+ ecg_lead ,
98
+ sampling_rate = sampling_rate
99
+ )
100
+
101
+ # caluclate rpeaks
102
+ _ , r_peaks = ecg_peaks (
103
+ data_ ,
104
+ sampling_rate = sampling_rate ,
105
+ method = neurokit_method ,
106
+ correct_artifacts = correct_artifacts
107
+ )
108
+ rpeaks = r_peaks ['ECG_R_Peaks' ].astype (np .int32 )
109
+
110
+ except Exception as e :
111
+ logging .warning (f'Failure in neurokit: { e } \n ' )
112
+ return None , None
113
+
114
+ return rpeaks
116
115
117
116
118
117
def _resample_multichannel (xs , fs , fs_target ):
@@ -160,8 +159,6 @@ def _resample_signal(x, fs, fs_target):
160
159
Note:
161
160
The method have been modified from wfdbs resample_multichan and resample_sig.
162
161
"""
163
- #t = np.arange(x.shape[0]).astype("float64")
164
-
165
162
if fs == fs_target :
166
163
return x
167
164
@@ -181,4 +178,4 @@ def _check_3d_array(X):
181
178
X = check_array (X , ensure_2d = False , allow_nd = True )
182
179
if X .ndim != 3 :
183
180
raise ValueError (f"X must be 3-dimensional (got { X .ndim } )." )
184
- return X
181
+ return X
0 commit comments