Sleep EEG and staging
Sleep stages and graphoelements, a hypnogram with yasa, and spectra across the night.
Prerequisites: L4.6 · Is it really an oscillation?
5 claims on this page are unverified. TODO(confirm) marks a specific statement the author has not yet checked against a
primary source. Everything else on this page has been reviewed. Treat a marked claim as
provisional and go to the cited source rather than quoting the sentence.
Objectives
- Recognize sleep stages and graphoelements (spindles, K-complexes, slow waves)
- Compute a hypnogram with yasa
- Analyze spectra across the night
Why this matters
Sleep is the one area of EEG where a whole night of recording is routinely reduced to a single staircase of five or six labels, and where nearly every downstream number — sleep efficiency, time in N3, REM latency, spindle density, the slope of overnight delta decline — is computed from that staircase rather than from the signal. So the staging is not a preliminary step. It is the measurement, and everything after it inherits its conventions, its disagreements and its scoring manual.
This lesson stages one night automatically, compares the result against the technologist who scored it, and spends most of its care on the thing that makes that comparison meaningful or meaningless: the mapping between two scoring manuals.
Concepts
Two manuals, and a merge that cannot be undone
The recording this lesson uses was scored to the 1968 Rechtschaffen and Kales manual. Modern automatic stagers, yasa among them, produce AASM stages. The vocabularies do not match:
| R&K stage | AASM stage | Note |
|---|---|---|
| W | W | unchanged |
| 1 | N1 | unchanged |
| 2 | N2 | unchanged |
| 3 | N3 | R&K 3 and 4 merge |
| 4 | N3 | the merge is not reversible |
| R | R | unchanged |
| Movement time | — | an absence of a stage, not a stage |
? (unscored) | — | likewise |
That table is shipped with the data, stated and unapplied — the site’s asset carries the hypnogram’s own labels verbatim and collapses nothing silently, so that the choice stays yours and stays visible.
Two consequences follow, and they are the reason this section comes before the agreement statistic rather than after it.
The merge is one-way. Once stage 3 and stage 4 are both called N3 you cannot recover which was which, so any analysis that wants the R&K depth distinction has to be done before the mapping, and any claim about “N3” in a paper that scored under R&K is a claim about 3 and 4 together.
An agreement statistic is undefined until the mapping is stated. Cohen’s κ compares two labellings of the same epochs. If one labelling has seven categories and the other has five, κ is not a number until you say how the seven became five and what you did with the epochs that have no stage at all — Movement time and ?. Exclude them, and κ is computed over fewer epochs than the night contains; map them to Wake, and you have invented a label. This lesson excludes them and says so wherever the number appears. A κ quoted without its mapping is not reproducible, which is the whole reason the exercise below states the mapping in its prompt.
The graphoelements, and why this site cannot point at one
Three transient waveforms carry most of the staging rules:
- a sleep spindle, a short burst of waxing-and-waning activity in the sigma range, most visible over central sites, and one of the two features that define N2;
- a K-complex, a large, sharp, biphasic deflection that stands out from the background, also defining for N2, and often followed by a spindle;
- a slow wave, a high-amplitude low-frequency wave; when enough of an epoch is made of them, the epoch is N3 (R&K 3 and 4 differ in how much of the epoch).
TODO(confirm): the numeric criteria — the sigma band edges, the minimum spindle duration, the amplitude and frequency thresholds for a slow wave, and the percentage of an epoch each manual requires — are deliberately not stated here. Nobody on this project has read them from the AASM manual or from Rechtschaffen and Kales (1968), and a staging criterion quoted from memory is the kind of number this site does not print. The notebook prints the criteria its detector actually used; quote those, with the detector’s version.
No spindle, K-complex or slow wave is marked in anything this site ships. The sleep asset carries the hypnogram and nothing else: no graphoelement annotations exist, because no detector was installable in the environment that built the assets and the asset track would not hand-place labels it could not detect. So this lesson can teach you what the three look like and where in the night to expect them — spindles and K-complexes in N2, slow waves concentrated in the early NREM periods, REM periods lengthening towards morning — and it cannot show you a marked one. The scroller will take you to an N2 epoch; what you see there is your call, not the site’s. That gap is served by the notebook, which can install a detector, and it is stated here rather than papered over.
What a hypnogram is, and what it is not
A hypnogram is one label per scoring epoch — 30 seconds, by both manuals — drawn as a staircase with wake at the top and the deepest stage at the bottom. It is worth being clear about what it is not:
- It is not continuous. A stage is assigned to a whole epoch by rules about what occupies the majority of it, so a 40-second REM period and a 20-second one can both score as one epoch of REM or as none.
- It is not a measurement of the brain alone. Staging rules use EEG, EOG and chin EMG together; REM is defined partly by eye movements and by muscle atonia, not by the EEG alone.
- It does not carry lights-off and lights-on. Sleep efficiency, sleep-onset latency and wake-after-sleep-onset are all defined against those two clock times, and a stage sequence does not contain them. Deriving them from the first non-wake epoch is a guess dressed as a number, which is why the widget shows stage durations and shares and stops there. If your notebook reports sleep efficiency, it must state the two times it used.
The whole-night spectrogram
Stack a power spectrum per epoch, put frequency on one axis and the whole night on the other, and the staging becomes visible without the labels: slow-wave-rich periods appear as low-frequency bands early in the night, REM periods as a different texture, wake as broadband. It is the single most useful plot in sleep EEG because it shows the continuous quantity the staircase discretises, and it shows drift the staircase cannot — a gradual decline in overnight delta power, for instance, which is a real phenomenon measured on the spectrum and not on the stages.
Read it with L4.6’s caution attached: a band in a spectrogram is band-limited power, and band-limited power changes when the aperiodic background tilts as well as when an oscillation appears.
The recording this lesson ships, with its four caveats
ds-sleep-edf is the Sleep-EDF Database Expanded — 197 polysomnograms from 100 subjects across two studies, ODC-By 1.0, open, and per-file downloadable, so a notebook fetches only the nights it needs. Snippets are allowed with attribution, which is why the scroller below has real signal in it.
The night shipped here is sleep-cassette subject index 0, and four things about it must travel with every number computed from it:
- It is not a night. It is 22.1 hours long, because the sleep-cassette study recorded a whole day-and-night period at the subject’s own home with a portable recorder. 75.4 % of its scored epochs are wake. That is the recording, not a defect, and any sentence that calls it “a night” is wrong.
- Eleven minutes of signal ship, against a hypnogram for all 22.1 hours. The §4.5 size cap and the 60-second paging rule permit no more: the raw night is about 64 MB. So the overview is complete and the traces are a sample of it — one page per stage present, plus a five-minute descent across the fall into sleep. The widget shows exactly where the excerpts are and disables stage jumps that would land where no signal ships.
- Two bipolar derivations, not a montage. Fpz-Cz and Pz-Oz at 100 Hz, and the scoring was done on those rather than on the clinical C4-A1 / C3-A2. A spindle detector tuned to central derivations is not being asked the same question here. The EMG channel is not EMG in the usual sense either: it is a rectified, filtered envelope sampled at 1 Hz, as are respiration and temperature. Only EEG and EOG are at 100 Hz.
- “Expert” means the dataset’s technologist, not this site’s author. The hypnogram was “manually scored by well-trained technicians” and ships with the recording, so the labels carry
label_source: expertwith asourcefield naming the hypnogram file. Everywhere else on this siteexpertmeans the author has reviewed the item; here it does not, and nobody on this project has reviewed these labels. The widget prints that distinction verbatim rather than paraphrasing it.
Two fields on the dataset entry are honestly unknown and are worth knowing about before you interpret a spectrum: mains_hz is TODO(confirm) (the recordings are Dutch but no primary source read states the mains frequency) and the EEG online_filters are TODO(confirm) (the landing page documents the EMG processing chain in detail and says nothing about EEG filtering). A 50 Hz artifact in this data is therefore not attributable from the catalog alone.
The cross-species aside
§6 offers an optional aside on ds-mouse, high-density mouse EEG under chronic sleep restriction with its own wake / NREM / REM labels, CC BY 4.0 and open. It is a genuinely different test of the same detectors: different anatomy, different frequencies, different sleep architecture. It is not run here — the upstream corpus is roughly 90–100 GB and the machine that builds this site had a few gigabytes free — so nothing on this site derives from it and the aside is a pointer, not a result. TODO(confirm): whether a small enough subset can be fetched for a notebook to run the comparison has not been established.
Explore
Four things to do with it, in this order:
- Look at the shape of the hypnogram before you look at any epoch. Wake dominates, the sleep periods are islands in it, and the staircase runs 2650 epochs wide in a few hundred pixels. This is what a 22.1-hour home recording looks like, and it is why the stage table reports shares of scored epochs rather than “sleep efficiency”.
- Read the spectrogram against the staircase. The transitions the technologist marked mostly have a visible counterpart in the continuous picture — and some of the texture changes have no step in the staircase at all, because the rules quantise.
- Use the descent segment, not the isolated stage pages. Five consecutive minutes crossing wake → stage 1 → stage 2 shows what an isolated page cannot: alpha giving way, the trace slowing, and the point at which a scorer has to commit.
- Read the provenance line and then disbelieve the word “expert”. The widget prints what
expertmeans for these labels — the dataset’s technologist, not a review by this site. That is the difference between a reference standard and a verified one, and κ measures agreement with the former. - Notice what is not marked. No spindle, K-complex or slow wave is annotated anywhere. Find an N2 page and decide for yourself what is a spindle; the site will not confirm or deny it.
Practice
Sleep staging against an expert hypnogram: the irreversible Rechtschaffen & Kales to AASM mapping stated first, then Cohen's kappa over the whole recording and over the sleep period, with the confusion matrix and four sensitivity runs nb-7-2-sleep
Downloads from ds-sleep-edf.
The notebook fetches one night of ds-sleep-edf (about 48 MB for the PSG plus a few kB for the hypnogram), stages it with yasa (
(Vallat, 2021)
), applies the R&K → AASM mapping explicitly and in one place, excludes Movement-time and unscored epochs from the comparison, and computes Cohen’s κ against the technologist’s hypnogram. It then builds the whole-night spectrogram, and — if a detector can be installed — runs spindle and slow-wave detection so that the graphoelements this site cannot ship can at least be found. Its run log records the detector, its version and every threshold it used.
Exercises
Exercise ex-7-2-wake-fraction
NumericThe shipped night has 2650 scored 30-second epochs and none unscored. 1997 of them are scored Wake. What percentage of the scored epochs is wake, to one decimal place?
Exercise ex-7-2-rk-merge
Multiple choiceThe shipped hypnogram is scored to Rechtschaffen and Kales and contains 101 epochs of stage 3 and 119 of stage 4. You map it to AASM so that you can compare it with an automatic stager. What have you lost, and what can you still do?
Exercise ex-7-2-graphoelements-available
Multiple choiceYou want to show a learner a sleep spindle using what this site ships. What can you actually do?
Exercise ex-7-2-kappa
NumericCohen's κ between the automatic staging and the technologist's hypnogram for the shipped night, computed after mapping R&K to AASM (W→W, 1→N1, 2→N2, 3 and 4 both→N3, R→R) and excluding Movement-time and unscored epochs from the comparison rather than mapping them. Give κ to two decimal places.
Pitfalls
No pitfall entry is listed for this lesson in spec §6, and the frontmatter carries the §5.3 exemption. The two failure modes this lesson does warn about live elsewhere: reading a band in a spectrogram as an oscillation is pf-band-power-slope and L4.6, and quoting an agreement statistic without the mapping that made it computable is the reproducibility problem L6.6 treats generally.
In other tools
In other toolsEEGLAB — names only
The equivalents of what this lesson does, for a reader who works in another toolbox. Function names only: their own documentation is the place to learn how to call them.
EEGLAB
pop_countingSheepPSGCountSheepPSG plugin (install separately)pop_detect_spindlesdetect_spindles plugin (install separately)
Names checked 2026-09-18 against EEGLAB 2026.0.0 (plugins at the versions in EEGLAB’s own plugin list).
Reading
- Vallat & Walker (2021). YASA. unverified