yasa.Hypnogram.as_events#

Hypnogram.as_events()[source]#

Return a pandas DataFrame summarizing epoch-level information.

The onset and duration columns (in seconds) follow the BIDS events file specification. The description column is compatible with MNE Annotations, making it straightforward to attach the hypnogram to an MNE Raw object (see Examples below).

Returns:
eventspandas.DataFrame

A dataframe containing epoch onset, duration, stage, etc.

Examples

>>> from yasa import Hypnogram
>>> hyp = Hypnogram(["W", "W", "LIGHT", "LIGHT", "DEEP", "REM", "WAKE"], n_stages=4)
>>> hyp.as_events()
       onset  duration  value description
epoch
0        0.0      30.0      0        WAKE
1       30.0      30.0      0        WAKE
2       60.0      30.0      2       LIGHT
3       90.0      30.0      2       LIGHT
4      120.0      30.0      3        DEEP
5      150.0      30.0      4         REM
6      180.0      30.0      0        WAKE

To attach the hypnogram to an MNE Raw object as annotations, pass the onset, duration, and description columns to mne.Annotations and call set_annotations:

import mne

events = hyp.as_events()
annotations = mne.Annotations(
    onset=events["onset"],
    duration=events["duration"],
    description=events["description"],
)
raw.set_annotations(annotations)  # raw is an mne.io.Raw object