Question about 60_sleep.ipynb tutorial, especially the raw_train.plot() function

Hi MNE team,

I am new to MNE package and just started playing with the code in Tutorial

import numpy as np
import matplotlib.pyplot as plt
import matplotlib 
import mne
from mne.datasets.sleep_physionet.age import fetch_data

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import FunctionTransformer

ALICE, BOB = 0, 1

[alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[1])

raw_train = mne.io.read_raw_edf(
    alice_files[0], stim_channel="Event marker", infer_types=True
)


annot_train = mne.read_annotations(alice_files[1])

raw_train.set_annotations(annot_train, emit_warning=False)

# plot some data
# scalings were chosen manually to allow for simultaneous visualization of
# different channel types in this specific dataset
raw_train.plot(
    start=60,
    duration=60,
    scalings=dict(eeg=1e-4, resp=1e3, eog=1e-4, emg=1e-7, misc=1e-1),
)

(1) My First question related to this plot as it has extra content than in the tutorial slide.

  • Question 1 - What does this bar do? More specifically, why all the vertical line are located between 30000 and 50000, what does the different color represent? I tried to play with the plot start and duration parameter and this bar does not change. ( I did not see this in official tutorial)

  • Question 2 - What does this bar do? especially the area highlighted in red ( did not see this in the official tutorial)

  • Question 3 - I am trying to understand the event marker here. I see it changes very fast within 5 secs the event marker changes drastically. However, if I print out the annotation, the event duration is much longer. Did i miss anything here? Is there a better way to see the annotation raw data?

annot_train[0]

>> OrderedDict([('onset', 0.0),
             ('duration', 30630.0),
             ('description', 'Sleep stage W'),
             ('orig_time', None)]

annot_train[1]
>> OrderedDict([('onset', 30630.0),
             ('duration', 120.0),
             ('description', 'Sleep stage 1'),
             ('orig_time', None)])
annot_train[2]
>> OrderedDict([('onset', 30750.0),
             ('duration', 390.0),
             ('description', 'Sleep stage 2'),
             ('orig_time', None)])
  • Question 4 - How does the name to be “Sleep stage W” while there are so many more event from event marker?

(2) Then I tried to plot the 2nd recording using the following script ( only changed recording = [1] to [2])

ALICE, BOB = 0, 1

[alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[2])

raw_train = mne.io.read_raw_edf(
    alice_files[0], stim_channel="Event marker", infer_types=True
)


annot_train = mne.read_annotations(alice_files[1])

raw_train.set_annotations(annot_train, emit_warning=False)

# plot some data
# scalings were chosen manually to allow for simultaneous visualization of
# different channel types in this specific dataset
raw_train.plot(
    start=60,
    duration=60,
    scalings=dict(eeg=1e-4, resp=1e3, eog=1e-4, emg=1e-7, misc=1e-1),
)

Basically i will see an overshoot see the annotation, i tried to change the scale on rectal but does not change anything.

Also, if i were to plot both recording 1st and 2nd by changing recording=[2] to recording=[1,2] per function definition, it will return error.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/2_/tzj313wj1bx90zrbzj1wjjqw0000gn/T/ipykernel_71666/800765249.py in <module>
      1 ALICE, BOB = 0, 1
      2 
----> 3 [alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[1,2])
      4 
      5 raw_train = mne.io.read_raw_edf(

ValueError: too many values to unpack (expected 2)

Could you please shine some light on this or pointed me to the right thread?

Thank you.

Hello,

Apologies, I haven’t read the full post, so let me know if I miss something.

ValueError: too many values to unpack (expected 2) on line:
3: [alice_files, bob_files] = fetch_data(subjects=[ALICE, BOB], recording=[1,2])

means that fetch_data is returning too many objects to store in only 2 variables, alice_files and bob_files. If I run it locally:

[['C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4001E0-PSG.edf',
 'C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4001EC-Hypnogram.edf'],
['C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4002E0-PSG.edf',
 'C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4002EC-Hypnogram.edf'],
['C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4011E0-PSG.edf',
 'C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4011EH-Hypnogram.edf'],
['C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4012E0-PSG.edf',
 'C:\\Users\\scheltie\\mne_data\\physionet-sleep-data\\SC4012EC-Hypnogram.edf']]

I get a list with 4 list inside, i.e. 4 elements to un-pack. This is probably because you are asking for recording 1 and 2 of ALICE and BOB.

To fix it, either store the entire list in a variable:

files = fetch_data(subjects=[ALICE, BOB], recording=[1,2])

Or unpack into 4 variables:

# Equivalent syntax
[files1, files2, files3, files4] = fetch_data(subjects=[ALICE, BOB], recording=[1,2])
(files1, files2, files3, files4) = fetch_data(subjects=[ALICE, BOB], recording=[1,2])
files1, files2, files3, files4 = fetch_data(subjects=[ALICE, BOB], recording=[1,2])

Mathieu

Hi Mathieu,

Thanks for your response. That answered my 2nd question. Could you please also take a look at my 1st question that comes with 4 sub question about the plotting?

Thank you,

Bin

Arf, multiple questions into one, you tricked me :wink:

When you plot with raw.plot(), the right bar (Q2) is the channel selection. In this case, you have all the channels already displayed, thus the bar is “fixed” and the up/down arrow keys don’t change anything. If you had an EEG recording with e.g. 64 channels, that bar and the arrow keys would let you scroll through the channels displayed.

The bottom bar is the time axis. You can navigate through time with this bar or with the right/left arrow keys.

The color stripes are the Sleep Stage W at the top are annotations. There is one very big one between 0 and 30 000-ish, named Sleep Stage W, then several smaller ones with other names (colored stripes) and finally one more big one at the end, from 55 000-ish to the end, which seems to be the same color as the first one, thus it should be labelled Sleep Stage W as well.

And finally Event marker is a channel while the Sleep Stage W and other color stripes are annotations. They are 2 different ways of marking “events of interest” in time (poor name, but I can’t figure out a better synonym at this hour). I do not know what information is encoded on the Event marker channel of this dataset.

Mathieu

Hi Mathieu,

Apology for the confusion. I will post my question more meaningful as i get more familiar with the forum.

Thanks for your response. i think my confusion was that the plot generated on my end has never interactive. I have tried multiple way to make it interactive. it turns out adding %matplotlib qt in vscode Jupter notebook and ‘plt.show()’ in vscode python file are the right way to do it. Once the plot is interactive, everything is self-explanatory now.

Bin