How to segment N2 stage sleep while preserving sleep spindle annotations

Hi mne community,

I have a sleep EEG dataset with both sleep spindle and sleep stage annotations from which I need to extract the N2 sleep stages while preserving the spindle annotations. The goal is to feed the N2 stage sleep EEG into a spindle classifier while using the spindle annotations as ground truth to quantify the quality of the obtained spindle classifications.

At some point in the processing pipeline I will have to go from RawArray and Annotations to numpy.ndarray to feed signals and labels into the classifier. I would like to do this at the latest possible stage to make as much use of mne’s included functionality as possible. I’m particularly interested whether I can use mne for the segmentation part of the pipeline.

Sleep stages were scored according to R&K rules, meaning that consecutive time windows of 20 s duration were labeled with the current sleep stage (i.e. I don’t have one sleep stage label per variable-length sleep stage, but one sleep stage label every 20 s). Here’s an example of the included sleep stage annotations of a single night of sleep EEG (for clarity’s sake without the spindle annotations).

The intended outcome is to have one segment for each variable-length N2 sleep stage, i.e. one segment for each uninterrupted orange block, with the corresponding sleep spindle annotations within that block having been processed such that they are relative to the block start. After that, I could easily extract the data and convert the annotations to labels suitable for machine learning.

I have already had a number of ideas but I quickly hit roadblocks, so I thought I’d ask this question open-ended and check if someone among the more experienced mne users here has a good idea.

It just occurred to me that the described approach is probably more complicated than it needs to be. It might make more sense to run inference on the whole night and then discarding all classifier output intervals that are not in N2 :slightly_smiling_face: