/
ecg.py
418 lines (368 loc) · 15.3 KB
/
ecg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Authors: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>
# Denis Engemann <denis.engemann@gmail.com>
# Eric Larson <larson.eric.d@gmail.com>
#
# License: BSD (3-clause)
import numpy as np
from .. import pick_types, pick_channels
from ..annotations import _annotations_starts_stops
from ..utils import logger, verbose, sum_squared, warn
from ..filter import filter_data
from ..epochs import Epochs, BaseEpochs
from ..io.base import BaseRaw
from ..evoked import Evoked
from ..io import RawArray
from ..io.pick import _picks_to_idx
from .. import create_info
def qrs_detector(sfreq, ecg, thresh_value=0.6, levels=2.5, n_thresh=3,
l_freq=5, h_freq=35, tstart=0, filter_length='10s'):
"""Detect QRS component in ECG channels.
QRS is the main wave on the heart beat.
Parameters
----------
sfreq : float
Sampling rate
ecg : array
ECG signal
thresh_value : float | str
qrs detection threshold. Can also be "auto" for automatic
selection of threshold.
levels : float
number of std from mean to include for detection
n_thresh : int
max number of crossings
l_freq : float
Low pass frequency
h_freq : float
High pass frequency
tstart : float
Start detection after tstart seconds.
filter_length : str | int | None
Number of taps to use for filtering.
Returns
-------
events : array
Indices of ECG peaks
"""
win_size = int(round((60.0 * sfreq) / 120.0))
filtecg = filter_data(ecg, sfreq, l_freq, h_freq, None, filter_length,
0.5, 0.5, phase='zero-double', fir_window='hann',
fir_design='firwin2')
ecg_abs = np.abs(filtecg)
init = int(sfreq)
n_samples_start = int(sfreq * tstart)
ecg_abs = ecg_abs[n_samples_start:]
n_points = len(ecg_abs)
maxpt = np.empty(3)
maxpt[0] = np.max(ecg_abs[:init])
maxpt[1] = np.max(ecg_abs[init:init * 2])
maxpt[2] = np.max(ecg_abs[init * 2:init * 3])
init_max = np.mean(maxpt)
if thresh_value == 'auto':
thresh_runs = np.arange(0.3, 1.1, 0.05)
elif isinstance(thresh_value, str):
raise ValueError('threshold value must be "auto" or a float')
else:
thresh_runs = [thresh_value]
# Try a few thresholds (or just one)
clean_events = list()
for thresh_value in thresh_runs:
thresh1 = init_max * thresh_value
numcross = list()
time = list()
rms = list()
ii = 0
while ii < (n_points - win_size):
window = ecg_abs[ii:ii + win_size]
if window[0] > thresh1:
max_time = np.argmax(window)
time.append(ii + max_time)
nx = np.sum(np.diff(((window > thresh1).astype(np.int) ==
1).astype(int)))
numcross.append(nx)
rms.append(np.sqrt(sum_squared(window) / window.size))
ii += win_size
else:
ii += 1
if len(rms) == 0:
rms.append(0.0)
time.append(0.0)
time = np.array(time)
rms_mean = np.mean(rms)
rms_std = np.std(rms)
rms_thresh = rms_mean + (rms_std * levels)
b = np.where(rms < rms_thresh)[0]
a = np.array(numcross)[b]
ce = time[b[a < n_thresh]]
ce += n_samples_start
clean_events.append(ce)
# pick the best threshold; first get effective heart rates
rates = np.array([60. * len(cev) / (len(ecg) / float(sfreq))
for cev in clean_events])
# now find heart rates that seem reasonable (infant through adult athlete)
idx = np.where(np.logical_and(rates <= 160., rates >= 40.))[0]
if len(idx) > 0:
ideal_rate = np.median(rates[idx]) # get close to the median
else:
ideal_rate = 80. # get close to a reasonable default
idx = np.argmin(np.abs(rates - ideal_rate))
clean_events = clean_events[idx]
return clean_events
@verbose
def find_ecg_events(raw, event_id=999, ch_name=None, tstart=0.0,
l_freq=5, h_freq=35, qrs_threshold='auto',
filter_length='10s', return_ecg=False,
reject_by_annotation=None, verbose=None):
"""Find ECG peaks.
Parameters
----------
raw : instance of Raw
The raw data
event_id : int
The index to assign to found events
ch_name : None | str
The name of the channel to use for ECG peak detection.
If None (default), a synthetic ECG channel is created from
cross channel average. Synthetic channel can only be created from
'meg' channels.
tstart : float
Start detection after tstart seconds. Useful when beginning
of run is noisy.
l_freq : float
Low pass frequency to apply to the ECG channel while finding events.
h_freq : float
High pass frequency to apply to the ECG channel while finding events.
qrs_threshold : float | str
Between 0 and 1. qrs detection threshold. Can also be "auto" to
automatically choose the threshold that generates a reasonable
number of heartbeats (40-160 beats / min).
filter_length : str | int | None
Number of taps to use for filtering.
return_ecg : bool
Return ecg channel if synthesized. Defaults to False. If True and
and ecg exists this will yield None.
reject_by_annotation : bool
Whether to omit data that is annotated as bad.
.. versionadded:: 0.18
%(verbose)s
Returns
-------
ecg_events : array
Events.
ch_ecg : string
Name of channel used.
average_pulse : float
Estimated average pulse.
See Also
--------
create_ecg_epochs
compute_proj_ecg
"""
if reject_by_annotation is None:
if len(raw.annotations) > 0:
warn('reject_by_annotation in find_ecg_events defaults to False '
'in 0.18 but will change to True in 0.19, set it explicitly '
'to avoid this warning', DeprecationWarning)
reject_by_annotation = False
skip_by_annotation = ('edge', 'bad') if reject_by_annotation else ()
del reject_by_annotation
idx_ecg = _get_ecg_channel_index(ch_name, raw)
if idx_ecg is not None:
logger.info('Using channel %s to identify heart beats.'
% raw.ch_names[idx_ecg])
ecg = raw.get_data(picks=idx_ecg)
else:
ecg = _make_ecg(raw, None, None)[0]
assert ecg.ndim == 2 and ecg.shape[0] == 1
ecg = ecg[0]
# Deal with filtering the same way we do in raw, i.e. filter each good
# segment
onsets, ends = _annotations_starts_stops(
raw, skip_by_annotation, 'reject_by_annotation', invert=True)
ecgs = list()
max_idx = (ends - onsets).argmax()
for si, (start, stop) in enumerate(zip(onsets, ends)):
# Only output filter params once (for info level), and only warn
# once about the length criterion (longest segment is too short)
use_verbose = verbose if si == max_idx else 'error'
ecgs.append(filter_data(
ecg[start:stop], raw.info['sfreq'], l_freq, h_freq, [0],
filter_length, 0.5, 0.5, 1, 'fir', None, copy=False,
phase='zero-double', fir_window='hann', fir_design='firwin2',
verbose=use_verbose))
ecg = np.concatenate(ecgs)
# detecting QRS and generating event file
ecg_events = qrs_detector(raw.info['sfreq'], ecg, tstart=tstart,
thresh_value=qrs_threshold, l_freq=None,
h_freq=None)
# map ECG events back to original times
remap = np.empty(len(ecg), int)
offset = 0
for start, stop in zip(onsets, ends):
this_len = stop - start
assert this_len >= 0
remap[offset:offset + this_len] = np.arange(start, stop)
offset += this_len
assert offset == len(ecg)
ecg_events = remap[ecg_events]
n_events = len(ecg_events)
minutes = len(ecg) / (raw.info['sfreq'] * 60.)
average_pulse = n_events / minutes
logger.info("Number of ECG events detected : %d (average pulse %d / "
"min.)" % (n_events, average_pulse))
ecg_events = np.array([ecg_events + raw.first_samp,
np.zeros(n_events, int),
event_id * np.ones(n_events, int)]).T
out = (ecg_events, idx_ecg, average_pulse)
ecg = ecg[np.newaxis] # backward compat output 2D
if return_ecg:
out += (ecg,)
return out
def _get_ecg_channel_index(ch_name, inst):
"""Get ECG channel index, if no channel found returns None."""
if ch_name is None:
ecg_idx = pick_types(inst.info, meg=False, eeg=False, stim=False,
eog=False, ecg=True, emg=False, ref_meg=False,
exclude='bads')
else:
if ch_name not in inst.ch_names:
raise ValueError('%s not in channel list (%s)' %
(ch_name, inst.ch_names))
ecg_idx = pick_channels(inst.ch_names, include=[ch_name])
if len(ecg_idx) == 0:
return None
# raise RuntimeError('No ECG channel found. Please specify ch_name '
# 'parameter e.g. MEG 1531')
if len(ecg_idx) > 1:
warn('More than one ECG channel found. Using only %s.'
% inst.ch_names[ecg_idx[0]])
return ecg_idx[0]
@verbose
def create_ecg_epochs(raw, ch_name=None, event_id=999, picks=None, tmin=-0.5,
tmax=0.5, l_freq=8, h_freq=16, reject=None, flat=None,
baseline=None, preload=True, keep_ecg=False,
reject_by_annotation=True, verbose=None):
"""Conveniently generate epochs around ECG artifact events.
Parameters
----------
raw : instance of Raw
The raw data
ch_name : None | str
The name of the channel to use for ECG peak detection.
If None (default), ECG channel is used if present. If None and no
ECG channel is present, a synthetic ECG channel is created from
cross channel average. Synthetic channel can only be created from
MEG channels.
event_id : int
The index to assign to found events
%(picks_all)s
tmin : float
Start time before event.
tmax : float
End time after event.
l_freq : float
Low pass frequency to apply to the ECG channel while finding events.
h_freq : float
High pass frequency to apply to the ECG channel while finding events.
reject : dict | None
Rejection parameters based on peak-to-peak amplitude.
Valid keys are 'grad' | 'mag' | 'eeg' | 'eog' | 'ecg'.
If reject is None then no rejection is done. Example::
reject = dict(grad=4000e-13, # T / m (gradiometers)
mag=4e-12, # T (magnetometers)
eeg=40e-6, # V (EEG channels)
eog=250e-6 # V (EOG channels)
)
flat : dict | None
Rejection parameters based on flatness of signal.
Valid keys are 'grad' | 'mag' | 'eeg' | 'eog' | 'ecg', and values
are floats that set the minimum acceptable peak-to-peak amplitude.
If flat is None then no rejection is done.
baseline : tuple | list of length 2 | None
The time interval to apply rescaling / baseline correction.
If None do not apply it. If baseline is (a, b)
the interval is between "a (s)" and "b (s)".
If a is None the beginning of the data is used
and if b is None then b is set to the end of the interval.
If baseline is equal to (None, None) all the time
interval is used. If None, no correction is applied.
preload : bool
Preload epochs or not (default True). Must be True if
keep_ecg is True.
keep_ecg : bool
When ECG is synthetically created (after picking), should it be added
to the epochs? Must be False when synthetic channel is not used.
Defaults to False.
reject_by_annotation : bool
Whether to reject based on annotations. If True (default), epochs
overlapping with segments whose description begins with ``'bad'`` are
rejected. If False, no rejection based on annotations is performed.
.. versionadded:: 0.14.0
%(verbose)s
Returns
-------
ecg_epochs : instance of Epochs
Data epoched around ECG r-peaks.
See Also
--------
find_ecg_events
compute_proj_ecg
Notes
-----
Filtering is only applied to the ECG channel while finding events.
The resulting ``ecg_epochs`` will have no filtering applied (i.e., have
the same filter properties as the input ``raw`` instance).
"""
has_ecg = 'ecg' in raw or ch_name is not None
if keep_ecg and (has_ecg or not preload):
raise ValueError('keep_ecg can be True only if the ECG channel is '
'created synthetically and preload=True.')
events, _, _, ecg = find_ecg_events(
raw, ch_name=ch_name, event_id=event_id, l_freq=l_freq, h_freq=h_freq,
return_ecg=True, reject_by_annotation=reject_by_annotation)
picks = _picks_to_idx(raw.info, picks, 'all', exclude=())
# create epochs around ECG events and baseline (important)
ecg_epochs = Epochs(raw, events=events, event_id=event_id,
tmin=tmin, tmax=tmax, proj=False, flat=flat,
picks=picks, reject=reject, baseline=baseline,
reject_by_annotation=reject_by_annotation,
preload=preload)
if keep_ecg:
# We know we have created a synthetic channel and epochs are preloaded
ecg_raw = RawArray(
ecg, create_info(ch_names=['ECG-SYN'],
sfreq=raw.info['sfreq'], ch_types=['ecg']),
first_samp=raw.first_samp)
ignore = ['ch_names', 'chs', 'nchan', 'bads']
for k, v in raw.info.items():
if k not in ignore:
ecg_raw.info[k] = v
syn_epochs = Epochs(ecg_raw, events=ecg_epochs.events,
event_id=event_id, tmin=tmin, tmax=tmax,
proj=False, picks=[0], baseline=baseline,
preload=True)
ecg_epochs = ecg_epochs.add_channels([syn_epochs])
return ecg_epochs
@verbose
def _make_ecg(inst, start, stop, reject_by_annotation=False, verbose=None):
"""Create ECG signal from cross channel average."""
if not any(c in inst for c in ['mag', 'grad']):
raise ValueError('Unable to generate artificial ECG channel')
for ch in ['mag', 'grad']:
if ch in inst:
break
logger.info('Reconstructing ECG signal from {}'
.format({'mag': 'Magnetometers',
'grad': 'Gradiometers'}[ch]))
picks = pick_types(inst.info, meg=ch, eeg=False, ref_meg=False)
if isinstance(inst, BaseRaw):
reject_by_annotation = 'omit' if reject_by_annotation else None
ecg, times = inst.get_data(picks, start, stop, reject_by_annotation,
True)
elif isinstance(inst, BaseEpochs):
ecg = np.hstack(inst.copy().crop(start, stop).get_data())
times = inst.times
elif isinstance(inst, Evoked):
ecg = inst.data
times = inst.times
return ecg.mean(0, keepdims=True), times