@@ -546,6 +546,11 @@ def events_file_to_annotation_kwargs(events_fname: str | Path) -> dict:
546546 The descriptions of the events.
547547 - 'event_id' : dict
548548 A dictionary mapping event descriptions to integer event IDs.
549+ - 'extras' : list of dict
550+ A list of dictionaries containing additional columns from the
551+ ``events.tsv`` file. Each dictionary corresponds to a row.
552+ This corresponds to the ``extras`` argument of class
553+ :class:`mne.Annotations`.
549554
550555 Notes
551556 -----
@@ -584,7 +589,8 @@ def events_file_to_annotation_kwargs(events_fname: str | Path) -> dict:
584589 {'onset': array([0.1, 0.2, 0.3]),
585590 'duration': array([0.1, 0.1, 0.1]),
586591 'description': array(['event1', 'event2', 'event1'], dtype='<U6'),
587- 'event_id': {'event1': 1, 'event2': 2}}
592+ 'event_id': {'event1': 1, 'event2': 2},
593+ 'extras': [{'sample': 10}, {'sample': 20}, {'sample': 30}]}
588594
589595 """
590596 logger .info (f"Reading events from { events_fname } ." )
@@ -663,7 +669,30 @@ def events_file_to_annotation_kwargs(events_fname: str | Path) -> dict:
663669 [0 if du == "n/a" else du for du in events_dict ["duration" ]], dtype = float
664670 )
665671
666- return {"onset" : ons , "duration" : durs , "description" : descrs , "event_id" : event_id }
672+ extras = None
673+ extra_columns = list (
674+ set (events_dict )
675+ - {
676+ "onset" ,
677+ "duration" ,
678+ "value" ,
679+ "trial_type" ,
680+ "stim_type" ,
681+ }
682+ )
683+ if extra_columns :
684+ extras = [
685+ dict (zip (extra_columns , values ))
686+ for values in zip (* [events_dict [col ] for col in extra_columns ])
687+ ]
688+
689+ return {
690+ "onset" : ons ,
691+ "duration" : durs ,
692+ "description" : descrs ,
693+ "event_id" : event_id ,
694+ "extras" : extras ,
695+ }
667696
668697
669698def _handle_events_reading (events_fname , raw ):
@@ -673,11 +702,28 @@ def _handle_events_reading(events_fname, raw):
673702
674703 # Add events as Annotations, but keep essential Annotations present in raw file
675704 annot_from_raw = raw .annotations .copy ()
676- annot_from_events = mne .Annotations (
677- onset = annotations_info ["onset" ],
678- duration = annotations_info ["duration" ],
679- description = annotations_info ["description" ],
680- )
705+ try :
706+ annot_from_events = mne .Annotations (
707+ onset = annotations_info ["onset" ],
708+ duration = annotations_info ["duration" ],
709+ description = annotations_info ["description" ],
710+ extras = annotations_info ["extras" ],
711+ )
712+ except TypeError :
713+ if (
714+ annotations_info ["extras" ] is not None
715+ and len (annotations_info ["extras" ]) > 0
716+ ):
717+ warn (
718+ "The version of MNE-Python you are using (<1.10) "
719+ "does not support the extras argument in mne.Annotations. "
720+ f"The extra columns ({ annotations_info ['extras' ][0 ].keys ()} ) will be ignored. "
721+ )
722+ annot_from_events = mne .Annotations (
723+ onset = annotations_info ["onset" ],
724+ duration = annotations_info ["duration" ],
725+ description = annotations_info ["description" ],
726+ )
681727 raw .set_annotations (annot_from_events )
682728
683729 annot_idx_to_keep = [
0 commit comments