Modifying annotations in MNE-R from eeglab Raw and save them back to the raw file

Hello, Apologies if this is a stupid question. If not, I can post a reproducible code.
I am trying to modify annotations in a .set file generated by Eeglab, but even if I can modify the event file as I want, I simply find no way to re-inject it in the original eeglab Raw file.

To show the problem as simple as possible: forget about my aim to change annotations and focus on the following sequence:
if I load the file with
eegfile<- mne$io$read_raw_eeglab

and then I extract the annotations as events, with

myevents<-mne$events_from_annotations(eegfile)

and then I transform this structure back into an annotation structure

ReplaceAnnotation<-mne$annotations_from_events(myevents[[1]],
event_desc = myevents[[2]]
, sfreq = 500)

and then re-inject this annotation file to my raw eeg file

eegfile$set_annotations(ReplaceAnnotation)

then the event labels are lost.

No matter how I tried, I found no way in which the event labels are preserved. I have tried all the possible modifications that came to my mind in preparing the new event map, but labels are never imported in the Annotations object. I am aware that the process of extracting events from annotations and retransforming annotations from events requires to invert the events ID and the labels, as per the documentation here:

which states:
“Creating events from annotations via the function mne.events_from_annotations takes in event mappings with key→value pairs as description→ID, whereas mne.annotations_from_events takes in event mappings with key→value pairs as ID→description. If you need to use these together, you can invert the mapping by doing: event_desc = {v: k for k, v in event_id.items()}”"

But what would be the equivalent procedure in MNE-r? ( I tried to transform myevents[[2]] into a dataframe, access it, change the types, swap the order of names and values… nothing worked).

Thanks a lot for reading this, and thanks a lot for any info (and for the wonderful work in MNE and MNE-r)

l.

mne-r is not actively maintained.

I doubt @Denis has time to help here.

Send us a PR if you find a bug. I am sure @Denis will be happy to have another maintainer for this code.

sorry

Alex

1 Like

Thanks for your answer. I am not sure it’s a bug, it is more likely that I missed something. If I have a solution, then, I will post it (but I think I ran short of ideas).

l.