Question on ValueError: 2 from event_id is not present in events.

  • MNE version: e.g. 0.24.0
  • operating system: e.g. Windows 11

I am new to python and MNE. Please can you help I am having a hard time fixing this error.

ValueError: 2 from event_id is not present in events.

This is my code:

raw_train.plot(start=60, duration=60,
scalings=dict(eeg=1e-4, resp=1e3, eog=1e-4, emg=1e-7,
misc=1e-1))

annotation_desc_2_event_id = {ā€˜Sleep stage Wā€™: 1,
ā€˜Sleep stage 1ā€™: 2,
ā€˜Sleep stage 2ā€™: 3,
ā€˜Sleep stage 3ā€™: 4,
ā€˜Sleep stage 4ā€™: 4,
ā€˜Sleep stage Rā€™: 5}

annot_train.crop(annot_train[1][ā€˜onsetā€™] - 30 * 60,
annot_train[-2][ā€˜onsetā€™] + 30 * 60)
raw_train.set_annotations(annot_train, emit_warning=False)

events_train, _ = mne.events_from_annotations(
raw_train, event_id=annotation_desc_2_event_id, chunk_duration=30.)

create a new event_id that unifies stages 3 and 4

event_id = {ā€˜Sleep stage Wā€™: 1,
ā€˜Sleep stage 1ā€™: 2,
ā€˜Sleep stage 2ā€™: 3,
ā€˜Sleep stage 3/4ā€™: 4,
ā€˜Sleep stage Rā€™: 5}

#mne.viz.plot_events(events_train[:100])

#print(events_train[:4])

plot events

fig = mne.viz.plot_events(events_train, event_id=event_id,
sfreq=raw_train.info[ā€˜sfreqā€™],
first_samp=events_train[0,0])

Hello @Jollygraces and welcome to the forum!

Could you please post the entire traceback (full error message) so we can see where this problem actually occurs in your code?

Thanks!

Richard

PS
When posting code blocks, simply select them and click on the Preformatted text button in the toolbar to get a good layout.

This is the entire traceback

fig = mne.viz.plot_events(events_train, event_id=event_id,
                          sfreq=raw_train.info['sfreq'],
                          first_samp=events_train[0,0])
Traceback (most recent call last):

  File "<ipython-input-55-38fbf2ed81da>", line 1, in <module>
    fig = mne.viz.plot_events(events_train, event_id=event_id,

  File "<decorator-gen-147>", line 24, in plot_events

  File "C:\Users\jolly\.conda\envs\tf\lib\site-packages\mne\viz\misc.py", line 690, in plot_events
    _on_missing(on_missing, msg)

  File "C:\Users\jolly\.conda\envs\tf\lib\site-packages\mne\utils\check.py", line 811, in _on_missing
    raise error_klass(msg)

ValueError: 2 from event_id is not present in events.```

events_train doesnā€™t contain any events with event code 2, but your event_id dictionary does; hence, MNE throws an error.

The clean way to proceed is to ensure that events_train looks correct; and if it does, remove the entry for event code 2 from event_id.

The quick-and-dirty way to proceed is to pass on_missing='warn' to plot_events(), so it will only warn, but not raise an exception.

Best wishes,
Richard

Thank you, it works. For now, I decided to use the quick- and -dirty way. I am also looking at my code to ensure that events_train looks good.

1 Like