@@ -20,18 +20,24 @@ class Rlign(BaseEstimator, TransformerMixin, auto_wrap_output_keys=None):
20
20
ECG, lead selection, and artifact correction.
21
21
22
22
Parameters:
23
- sampling_rate: Defines the sampling rate for all ECG recordings.
23
+ sampling_rate: Defines the sampling rate for all ECG recordings and the template.
24
24
25
- template: Path or identifier for the template ECG. This template is
26
- used as a reference for identifying R-peaks in the ECG signals.
25
+ seconds_len: Determines the duration of all ECG recordings and the template in seconds.
26
+
27
+ template_bpm: The desired normalized BPM value for the template. This parameter sets the
28
+ heart rate around which the QRST pattern is structured, thereby standardizing the R-peak
29
+ positions according to a specific BPM.
30
+
31
+ offset: The offset specifies the starting point for the first normalized QRS complex in the
32
+ template. In percentage of sampling_rate. Default is set to 0.01.
27
33
28
34
select_lead: Specifies the lead (e.g., 'Lead II', 'Lead V1') for R-peak
29
35
and QRST point detection. Different leads can provide varying levels of
30
36
clarity for these features. Selection via channel numbers 0,1,... .
31
37
32
38
num_workers: Determines the number of CPU cores to be utilized for
33
39
parallel processing. Increasing this number can speed up computations
34
- but requires more system resources.
40
+ but requires more system resources. Default is set to 4.
35
41
36
42
neurokit_method: Chooses the algorithm for R-peak detection from the
37
43
NeuroKit package. Different algorithms may offer varying performance
@@ -44,48 +50,57 @@ class Rlign(BaseEstimator, TransformerMixin, auto_wrap_output_keys=None):
44
50
scale_method: Selects the scaling method from options 'identity', 'linear'
45
51
or 'hrc'. This choice dictates the interval used for resampling
46
52
the ECG signal, which can impact the quality of the processed signal.
53
+ Default is 'hrc'.
47
54
48
55
remove_fails: Determines the behavior when scaling is not possible. If
49
56
set to True, the problematic ECG is excluded from the dataset. If False,
50
- the original, unscaled ECG signal is returned instead.
57
+ the original, unscaled ECG signal is returned instead. Default is False.
51
58
52
59
median_beat: Calculates the median from a set of aligned beats and returns
53
- a single, representative beat.
60
+ a single, representative beat. Default is False.
54
61
55
- silent: Disable all warnings.
62
+ silent: Disable all warnings. Default True.
56
63
"""
57
64
58
- __allowed = ("sampling_rate" , "template " , "resample_method " ,
59
- "select_lead " , "num_workers " , "neurokit_method " ,
60
- "correct_artifacts " , "scale_method " )
65
+ __allowed = ("sampling_rate" , "resample_method " , "select_lead " ,
66
+ "num_workers " , "neurokit_method " , "correct_artifacts " ,
67
+ "scale_method " , "seconds_len" , "template_bpm" , "offset" , "silent " )
61
68
62
69
def __init__ (
63
70
self ,
64
71
sampling_rate : int = 500 ,
65
- template : Template = None ,
72
+ seconds_len : int = 10 ,
73
+ template_bpm : int = 60 ,
74
+ offset : float = .01 ,
66
75
select_lead : int = 1 ,
67
- num_workers : int = 8 , # only applies for multiprocessing
76
+ num_workers : int = 4 ,
68
77
neurokit_method : str = 'neurokit' ,
69
78
correct_artifacts : bool = True ,
70
79
scale_method : str = 'hrc' ,
71
80
remove_fails : bool = False ,
72
81
median_beat : bool = False ,
73
82
silent : bool = True
74
83
):
75
- self .sampling_rate = sampling_rate
76
- if template :
77
- self .template = template
78
- else :
79
- self .template = Template (
80
- sampling_rate = sampling_rate
81
- )
84
+
85
+ self ._sampling_rate = sampling_rate
86
+ self ._offset = offset
87
+ self ._template_bpm = template_bpm
88
+ self ._seconds_len = seconds_len
89
+
90
+ self .template = Template (
91
+ sampling_rate = self .sampling_rate ,
92
+ offset = self .offset ,
93
+ template_bpm = self .template_bpm ,
94
+ seconds_len = self .seconds_len
95
+ )
82
96
self .select_lead = select_lead
83
97
self .num_workers = num_workers
84
98
self .neurokit_method = neurokit_method
85
99
self .correct_artifacts = correct_artifacts
86
100
self .remove_fails = remove_fails
87
101
self .fails = []
88
102
self .median_beat = median_beat
103
+ self .silent = silent
89
104
90
105
available_scale_methods = ['identity' , 'linear' , 'hrc' ]
91
106
if scale_method in available_scale_methods :
@@ -97,13 +112,10 @@ def __init__(
97
112
raise ValueError (f'No such scaling method, '
98
113
f'please use one of the following: { available_scale_methods } ' )
99
114
100
- if silent :
115
+ if self . silent :
101
116
warnings .filterwarnings ("ignore" )
102
117
103
- def update_configuration (
104
- self ,
105
- ** kwargs
106
- ):
118
+ def update_configuration (self , ** kwargs ):
107
119
"""
108
120
Enables modification of existing configuration settings as required.
109
121
@@ -116,6 +128,50 @@ def update_configuration(
116
128
assert (k in self .__class__ .__allowed ), f"Disallowed keyword passed: { k } "
117
129
setattr (self , k , kwargs [k ])
118
130
131
+ @property
132
+ def offset (self ):
133
+ return self ._offset
134
+
135
+ @property
136
+ def template_bpm (self ):
137
+ return self ._template_bpm
138
+
139
+ @property
140
+ def seconds_len (self ):
141
+ return self ._seconds_len
142
+
143
+ @property
144
+ def sampling_rate (self ):
145
+ return self ._sampling_rate
146
+
147
+ @offset .setter
148
+ def offset (self , val ):
149
+ self ._offset = val
150
+ self ._update_template ()
151
+
152
+ @sampling_rate .setter
153
+ def sampling_rate (self , val ):
154
+ self ._sampling_rate = val
155
+ self ._update_template ()
156
+
157
+ @seconds_len .setter
158
+ def seconds_len (self , val ):
159
+ self ._seconds_len = val
160
+ self ._update_template ()
161
+
162
+ @template_bpm .setter
163
+ def template_bpm (self , val ):
164
+ self ._template_bpm = val
165
+ self ._update_template ()
166
+
167
+ def _update_template (self ):
168
+ self .template = Template (
169
+ sampling_rate = self .sampling_rate ,
170
+ offset = self .offset ,
171
+ template_bpm = self .template_bpm ,
172
+ seconds_len = self .seconds_len
173
+ )
174
+
119
175
def _normalize_interval (
120
176
self ,
121
177
source_ecg : np .ndarray ,
@@ -182,7 +238,6 @@ def _normalize_interval(
182
238
return source_ecg [:, :self .template .intervals [0 ]], 1
183
239
184
240
case "hrc" :
185
-
186
241
template_rr_dist = template_starts [2 ] - template_starts [1 ]
187
242
dist_upper_template = int ((self .template .bpm / 280 + 0.14 ) * template_rr_dist )
188
243
dist_lower_template = int ((- self .template .bpm / 330 + 0.96 ) * template_rr_dist )
@@ -272,10 +327,7 @@ def fit(self, X: np.ndarray, y=None):
272
327
"""
273
328
return self
274
329
275
- def _template_transform (
276
- self ,
277
- ecg : np .ndarray ,
278
- ):
330
+ def _template_transform (self , ecg : np .ndarray ):
279
331
"""
280
332
Normalizes the ECG by recentering R-peaks at equally spaced intervals, as defined in the template.
281
333
The QRST-complexes are added, and the baselines are interpolated to match the new connections between complexes.
@@ -329,11 +381,7 @@ def _template_transform(
329
381
hr = hr
330
382
)
331
383
332
- def transform (
333
- self ,
334
- X : np .ndarray ,
335
- y = None
336
- ) -> np .ndarray :
384
+ def transform (self , X : np .ndarray , y = None ) -> np .ndarray :
337
385
"""
338
386
Normalizes and returns the ECGs with multiprocessing.
339
387
0 commit comments