Skip to content

Commit ba12118

Browse files
HansujaBpre-commit-ci[bot]drammock
authored
FIX: raise clear ValueError when no ECG events found in create_ecg_ep… (#13771)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel McCloy <dan@mccloy.info>
1 parent 7e903a8 commit ba12118

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

doc/changes/dev/13771.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clearer error message when no ECG events are found in :func:`~mne.preprocessing.find_ecg_events`, by :newcontrib:`Hansuja Budhiraja`.

mne/preprocessing/ecg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ def find_ecg_events(
278278
if ecg_events.size > 0:
279279
ecg_events = remap[ecg_events]
280280
else:
281-
ecg_events = np.array([])
282-
281+
ecg_events = np.array([], int)
283282
n_events = len(ecg_events)
284283
duration_sec = len(ecg) / raw.info["sfreq"] - tstart
285284
duration_min = duration_sec / 60.0
@@ -430,6 +429,9 @@ def create_ecg_epochs(
430429
reject_by_annotation=reject_by_annotation,
431430
)
432431

432+
if len(events) == 0:
433+
raise ValueError("No ECG events could be found.")
434+
433435
picks = _picks_to_idx(raw.info, picks, "all", exclude=())
434436

435437
# create epochs around ECG events and baseline (important)

mne/preprocessing/tests/test_ecg.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ def test_find_ecg():
118118
assert average_pulse == 0
119119
assert np.allclose(ecg, np.zeros_like(ecg))
120120

121+
# Test that create_ecg_epochs raises clear error when no ECG events found
122+
with pytest.raises(ValueError, match="No ECG events could be found."):
123+
create_ecg_epochs(raw, ch_name=raw.ch_names[ecg_idx])
124+
121125
# Needs MEG
122126
with pytest.raises(ValueError, match="Generating an artificial"):
123127
find_ecg_events(read_raw_fif(raw_fname, preload=False).pick("eeg"))

0 commit comments

Comments
 (0)