MCPcopy
hub / github.com/mne-tools/mne-python / test_find_ecg

Function test_find_ecg

mne/preprocessing/tests/test_ecg.py:20–127  ·  view source on GitHub ↗

Test find ECG peaks.

()

Source from the content-addressed store, hash-verified

18
19
20def test_find_ecg():
21 """Test find ECG peaks."""
22 # Test if ECG analysis will work on data that is not preloaded
23 raw = read_raw_fif(raw_fname, preload=False).pick(picks="meg")
24 raw.pick(raw.ch_names[::10] + ["MEG 2641"])
25 raw.info.normalize_proj()
26
27 # once with mag-trick
28 # once with characteristic channel
29 raw_bad = raw.copy().load_data()
30 ecg_idx = raw.ch_names.index("MEG 1531")
31 raw_bad._data[ecg_idx, :1] = 1e6 # this will break the detector
32 raw_bad.annotations.append(
33 raw.first_samp / raw.info["sfreq"], 1.0 / raw.info["sfreq"], "BAD_values"
34 )
35 raw_noload = raw.copy()
36 raw.resample(100)
37
38 for ch_name, tstart in zip(["MEG 1531", None], [raw.times[-1] / 2, 0]):
39 events, ch_ECG, average_pulse, ecg = find_ecg_events(
40 raw, event_id=999, ch_name=ch_name, tstart=tstart, return_ecg=True
41 )
42 if ch_name is None:
43 assert ch_ECG is None
44 else:
45 assert raw.ch_names[ch_ECG] == ch_name
46 assert raw.n_times == ecg.shape[-1]
47 assert 40 < average_pulse < 60
48 n_events = len(events)
49
50 # with annotations
51 average_pulse = find_ecg_events(
52 raw_bad, ch_name=ch_name, tstart=tstart, reject_by_annotation=False
53 )[2]
54 assert average_pulse < 1.0
55 average_pulse = find_ecg_events(
56 raw_bad, ch_name=ch_name, tstart=tstart, reject_by_annotation=True
57 )[2]
58 assert 55 < average_pulse < 60
59
60 picks = pick_types(
61 raw.info,
62 meg="grad",
63 eeg=False,
64 stim=False,
65 eog=False,
66 ecg=True,
67 emg=False,
68 ref_meg=False,
69 exclude="bads",
70 )
71
72 # There should be no ECG channels, or else preloading will not be
73 # tested
74 assert "ecg" not in raw
75
76 ecg_epochs = create_ecg_epochs(raw_noload, picks=picks, keep_ecg=True)
77 assert len(ecg_epochs.events) == n_events

Callers

nothing calls this directly

Calls 12

read_raw_fifFunction · 0.90
find_ecg_eventsFunction · 0.90
pick_typesFunction · 0.90
create_ecg_epochsFunction · 0.90
normalize_projMethod · 0.80
del_projMethod · 0.80
set_channel_typesMethod · 0.80
pickMethod · 0.45
load_dataMethod · 0.45
copyMethod · 0.45
appendMethod · 0.45
resampleMethod · 0.45

Tested by

no test coverage detected