mne.annotations.rename changes value of key-value pair in event_id variable

  • MNE version: 1.1.1
  • operating system: ManjaroLinux 21.3.6

I want to rename my annotations. Calling mne.events_from_annotations I can see that the event_id variable looks like this

  'New Segment/': 99999, 
  'Stimulus/S240': 240, 
  'Stimulus/S241': 241, 
  'Stimulus/S242': 242, 
  'Stimulus/S243': 243, 
  'Stimulus/S244': 244, 
  'Stimulus/S245': 245, 
  'Stimulus/S246': 246, 
  'Stimulus/S247': 247

as the keys are not good descriptors I want to rename the annotations by using mne.annotations.rename

EEG.annotations.rename({'Stimulus/S240': 'Rest',
                                           'Stimulus/S241': 'Right/Open', 
                                           'Stimulus/S242': 'Right/Close', 
                                           'Stimulus/S243': 'Right/Two',
                                           'Stimulus/S244': 'Left/Open',
                                           'Stimulus/S245': 'Left/Close',
                                           'Stimulus/S246': 'Left/Two',
                                           'Stimulus/S247': 'Prepare'})

which changes the event_id variable to

  'Left/Close': 10001, 
  'Left/Open': 10002, 
  'Left/Two': 10003, 
  'New Segment/': 99999, 
  'Prepare': 10004, 
  'Rest': 10005, 
  'Right/Close': 10006, 
  'Right/Open': 10007, 
  'Right/Two': 10008

as you will notice, while the keys changed, the values did too. However, I want to preserve the values. Why does mne.annotations.rename behave like this and how to handle this?


I noticed that this also happens when saving a raw file to fif with fmt=‘double’. After loading the file, the values have changed.

Hello @Killua and welcome to the forum!

This seems like a bug to me. Do you think you could produce a very small example that demonstrates the issue and is based on the MNE-Python sample dataset? That way, it will be much easier for the developers to reproduce (and fix) the problem.



mne.events_from_annotations produces non-deterministic numbers for IDs as long

as they are different.

If you rename the annotations and redo mne.events_from_annotations it’s expected
the event_id values can be changed but it should not change your ability to select
epochs with the keys



I believe @agramfort is right and my statement above:

was therefore incorrect.

Good to know that this is indeed the intended behavior of the method. However, I’m wondering why mne.events_from_annotations was designed like this, as it seems like behavior that you don’t really want and may be confusing. Maybe in following releases generation of non-deterministic numbers for IDs can be optionally turned off? If you don’t plan to change this method in the near future, I think at least a note in the documentation about this would be beneficial to other users.

Annotations don’t have any event IDs associated with them. That’s the source of the problem.

events_from_annotations() tries to work with the event naming scheme of BrainVision data to assign event IDs, if your annotation names follow this scheme. This is just a heuristic, and this behavior is documented:

By renaming your annotations, you’re essentially destroying the BV naming scheme you started out with, and now MNE has no way to figure out which event IDs should/could be desired, so it just assigns new ones.

You can pass an event_id mapping to events_from_annotations() where you explicitly map the new (renamed) annotation names to your desired event IDs.

Best wishes,

1 Like

Just pass the event_id parameter as a dict to control this behavior