Skip to content

Instantly share code, notes, and snippets.

@sylvchev
Created November 15, 2021 12:01
Show Gist options
  • Save sylvchev/fdceaef63b1c27cfe03610d9215f1c62 to your computer and use it in GitHub Desktop.
Save sylvchev/fdceaef63b1c27cfe03610d9215f1c62 to your computer and use it in GitHub Desktop.
Manually annotate raw MNE objects on GUI, save annotation file, create events from annotations.
from mne import find_events, events_from_annotations
import mne
# get some raw data, replace by your own files
from moabb.datasets import BNCI2014001
sessions = BNCI2014001().get_data(subjects=[1])
raw = sessions[1]['session_T']['run_1']
ev = find_events(raw)
# Annotate plot
raw.plot(events=ev, color={'eeg': 'steelblue'})
# 1. press a to open annotation toolbox
# 2. directly type annotation name (e.g. inspi or expi)
# 3. click add new label and repeat for all annotation you need
# 4. drag and click on plot to create annotation, rght click to remove
# 5. Focus on annotation windows: Esc to finish annotations and Esc to quit plot
# Verify annotations
for ann in raw.annotations:
print(f"{ann['description']}: onset={ann['onset']}, duration:{ann['duration']}")
# Save annotations
raw.annotations.save("annotations.csv")
# Convert annotation to events
ev_resp, ev_resp_id = events events_from_annotations(raw, event_id={'inspi': 1, 'expi': 2})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment