<jupyter_start><jupyter_text>Automatic sleep stagingThis notebook demonstrates how to perform automatic sleep staging of polysomnography data in YASA.Please make sure to install the latest version of YASA first with: `pip install --upgrade yasa`.<jupyter_code>import mne
import yasa
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Data loadingThe automatic sleep staging function requires that the data are loaded using the [MNE Python package](https://mne.tools/stable/index.html). For instance, if your polysomnography data are stored in the standard European Data Fomat (.edf), you can use [this function](https://mne.tools/stable/generated/mne.io.read_raw_edf.html?highlight=read_raw_edfmne.io.read_raw_edf) to load the data into Python.<jupyter_code># Let's load a nap recording, directly as an MNE Raw object
raw = mne.io.read_raw_fif('sub-02_mne_raw.fif', preload=True, verbose=False)
print('The channels are:', raw.ch_names)
print('The sampling frequency is:', raw.info['sfreq'])
raw
# Let's now load the human-scored hypnogram, where each value represents a 30-sec epoch.
hypno = np.loadtxt('sub-02_hypno_30s.txt', dtype=str)
hypno<jupyter_output><empty_output><jupyter_text>Automatic sleep stagingAutomatic sleep stages classification can be done since YASA 0.4.0 using the [SleepStaging](https://raphaelvallat.com/yasa/build/html/generated/yasa.SleepStaging.htmlyasa.SleepStaging) class. Make sure to read the [documentation](https://raphaelvallat.com/yasa/build/html/generated/yasa.SleepStaging.htmlyasa.SleepStaging), which explains how the algorithm works.<jupyter_code># We first need to specify the channel names and, optionally, the age and sex of the participant
# - "raw" is the name of the variable containing the polysomnography data loaded with MNE.
# - "eeg_name" is the name of the EEG channel, preferentially a central derivation (e.g. C4-M1). This is always required to run the sleep staging algorithm.
# - "eog_name" is the name of the EOG channel (e.g. LOC-M1). This is optional.
# - "eog_name" is the name of the EOG channel (e.g. EMG1-EMG3). This is optional.
# - "metadata" is a dictionary containing the age and sex of the participant. This is optional.
sls = yasa.SleepStaging(raw, eeg_name="C4", eog_name="EOG1", emg_name="EMG1", metadata=dict(age=21, male=False))
# Getting the predicted sleep stages is now as easy as:
y_pred = sls.predict()
y_pred
# What is the accuracy of the prediction, compared to the human scoring
accuracy = (hypno == y_pred).sum() / y_pred.size
print("The overall agreement is %.3f" % accuracy)<jupyter_output>The overall agreement is 0.827<jupyter_text>**Stage probabilities and confidence of the algorithm at each epoch**<jupyter_code># What are the predicted probabilities of each sleep stage at each epoch?
sls.predict_proba()
# Plot the predicted probabilities
sls.plot_predict_proba();
# From the probabilities, we can extract a confidence level (ranging from 0 to 1) for each epoch.
confidence = sls.predict_proba().max(1)
confidence<jupyter_output><empty_output><jupyter_text>**Exporting to a CSV file**<jupyter_code># Let's first create a dataframe with the predicted stages and confidence
df_pred = pd.DataFrame({'Stage': y_pred, 'Confidence': confidence})
df_pred.head(6)

# Now export to a CSV file
# df_pred.to_csv("my_hypno.csv")<jupyter_output><empty_output><jupyter_text>**Applying the detection using only a single EEG derivation**<jupyter_code># Using just an EEG channel (= no EOG or EMG)
y_pred = yasa.SleepStaging(raw, eeg_name="C4").predict()
y_pred<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09674751929437707, 'fraction_numerical': 0.008544652701212789, 'mean_word_length': 5.8601134215500945, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '84253', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1134, 'n_words': 395, 'path': 'notebooks/14_automatic_sleep_staging.ipynb', 'repo_name': 'HKH515/yasa', 'size': '3628'}