Skip to content

Commit

Permalink
Better find_events message if no stim channel is found (mne-tools#6347)
Browse files Browse the repository at this point in the history
* Better find_events message if no stim channel is found

* Actually check if annotations are present and show custom error message
  • Loading branch information
cbrnr authored and larsoner committed May 21, 2019
1 parent 34d358d commit 10f5239
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
11 changes: 10 additions & 1 deletion mne/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,16 @@ def find_events(raw, stim_channel=None, output='onset',
min_samples = min_duration * raw.info['sfreq']

# pull stim channel from config if necessary
stim_channel = _get_stim_channel(stim_channel, raw.info)
try:
stim_channel = _get_stim_channel(stim_channel, raw.info)
except ValueError:
if len(raw.annotations) > 0:
raise ValueError("No stim channels found, but the raw object has "
"annotations. Consider using "
"mne.events_from_annotations to convert these to "
"events.")
else:
raise

picks = pick_channels(raw.info['ch_names'], include=stim_channel)
if len(picks) == 0:
Expand Down
14 changes: 13 additions & 1 deletion mne/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from mne import (read_events, write_events, make_fixed_length_events,
find_events, pick_events, find_stim_steps, pick_channels,
read_evokeds, Epochs, create_info, compute_raw_covariance)
read_evokeds, Epochs, create_info, compute_raw_covariance,
Annotations)
from mne.io import read_raw_fif, RawArray
from mne.utils import _TempDir, run_tests_if_main
from mne.event import (define_target_events, merge_events, AcqParserFIF,
Expand Down Expand Up @@ -362,6 +363,17 @@ def test_find_events():
assert_array_equal(find_events(raw, 'MYSTI', initial_event=True),
[[0, 0, 100], [30, 0, 200]])

# test error message for raw without stim channels
raw = read_raw_fif(raw_fname, preload=True)
raw.pick_types(meg=True, stim=False)
# raw does not have annotations
with pytest.raises(ValueError, match="'stim_channel'"):
find_events(raw)
# if raw has annotations, we show a different error message
raw.set_annotations(Annotations(0, 2, "test"))
with pytest.raises(ValueError, match="mne.events_from_annotations"):
find_events(raw)


def test_pick_events():
"""Test pick events in a events ndarray."""
Expand Down

0 comments on commit 10f5239

Please sign in to comment.