How to change annotations inside an EDF file

  • MNE-Python version: 0.21.0
  • operating system: Windows 10

I am looking for a way to change the annotations from an EDF file. When I use the function events_from_annotations(), I want to change the classes. So far example, class 2 will become 4 and class 3 will become 5. I want to save this change inside the raw file. This is my code so far:

raw = read_raw_edf(fname, preload = True)
custom_mapping = {"T0": 1, "T1": 4, "T2":5}
new_annotations = events_from_annotations(raw, event_id = custom_mapping)

new = mne.annotations_from_events(events=new_annotations[0], sfreq=raw.info['sfreq'], orig_time=raw.info['meas_date'], event_desc = {1:"T0", 4:"T1", 5:"T2"})

raw.set_annotations(new)
print(events_from_annotations(raw))

The problem I am experiencing right now is that even after I modify the classes and then use the set_annotations() function, the classes will not change and stays the same as before. So for example, I want to change the events from
array([[ 0, 0, 1],
[ 672, 0, 2],
[ 1328, 0, 1],
[ 2000, 0, 3],
[ 2656, 0, 1],
[ 3328, 0, 2],
…]])
to
array([[ 0, 0, 1],
[ 672, 0, 4],
[ 1328, 0, 1],
[ 2000, 0, 5],
[ 2656, 0, 1],
[ 3328, 0, 4],
…]])
but I don’t know why it stays the same even after I call the function set_annotations().

events_from_annotations

returns 2 things: the events and the correct event_id to work with it.

HTH
Alex

You can save to FIFF by calling raw.save(). There is no export to BDF from MNE-Python.

The problem I am experiencing right now is that even after I modify the classes and then use the set_annotations() function, the classes will not change and stays the same as before. So for example, I want to change the events from
array([[ 0, 0, 1],
[ 672, 0, 2],
[ 1328, 0, 1],
[ 2000, 0, 3],
[ 2656, 0, 1],
[ 3328, 0, 2],
…]])
to
array([[ 0, 0, 1],
[ 672, 0, 4],
[ 1328, 0, 1],
[ 2000, 0, 5],
[ 2656, 0, 1],
[ 3328, 0, 4],
…]])
but I don’t know why it stays the same even after I call the function set_annotations().

events_from_annotations() accepts an event_id parameter to map annotation names to event IDs. This is probably what you need to use to get your desired result?

I did that and I get the result I am looking for. But when I use the function set_annotations() it will output the initial state. When I use the function set_annotations() I want the old annotations to update to the new annotations in the raw data.

I believe there’s a fundamental misunderstanding here. Annotations are not events.

Annotations don’t have any event IDs attached to them directly. These are only generated when calling events_from_annotations(), either automatically or based on event_id passed to the function.