Open a real night

Before any model: what a sleep recording actually is, what comes inside the file, and how a screen turns it into lines you can read.

Not yet verified. The sources on this page haven't been checked against the originals yet. What this means

Until someone checks them, treat the claims and figures here as unverified.

How we verify: each cited claim is an exact sentence on this page plus the source that should back it. A reviewer reads the source itself, not a summary, and records per claim whether it supports the sentence and where. Three independent Claude runs will check the claims too; anything they don't all agree on goes to a person. The label turns green only when every claim is supported and a named reviewer has signed off this exact text; when the text changes, the label goes back to red until someone checks it again. How to verify a source.

  • A scored sleep recording comes as two files: the signal (voltages from electrodes, all night) and the hypnogram (a trained person’s answer: which stage each 30-second page is).
  • There is no model on this page. The hypnogram was made by a human. Every sleep-staging model, from DreamentoScorer to ezscore-f, is trained on pairs like these: a signal plus a human’s answer.
  • Your files never leave your computer. The page reads them in your browser; nothing is uploaded anywhere.

Educational, not medical advice.

New to this? Start with Lab 8a: From electrode to bytes: what one electrode records, how it becomes numbers and bytes, and how you see alpha or deep sleep in those numbers. This lab then opens the whole file.

1. Walk through a real file

Is a sleep recording JSON? A spreadsheet? Neither. Walk through one real night, byte by byte, in eight short steps. Every tile and number in it comes from the actual file you download in step 2.

Inside the file

Loading the 3D file…

In short: an EDF file is binary. It starts with a fixed block of plain text (256 bytes, plus 256 per signal) that says what is in it, and after that come only numbers, each stored in 2 bytes, record after record. There are no field names, brackets or commas: a number’s position in the file says what it is. You can’t read it in a text editor, but every program that knows the rules can.

2. Get the files

We use one night from Sleep-EDF Expanded, a free database on PhysioNet. Download these two files (about 48 MB and 5 kB):

SC4001E0-PSG.edf

The signal. "PSG" is polysomnography: brain, eyes, muscle and more, recorded together.

SC4001EC-Hypnogram.edf

The answer. A technician looked at the signal and wrote down a sleep stage for every 30 seconds.

The name is a code: SC is the Sleep Cassette study, 00 the subject number and 1 the night. In the hypnogram’s name, the eighth letter (C here) tells you which technician scored it. Any other .edf file works too, for example a different night from the same database.

3. Open them

Try this

  • Press first N3. Look at the two EEG lines: big, slow waves. Then press first REM: small, fast EEG, the EOG line moves in sharp jumps, and the EMG line stays low.
  • Switch to 1 hour and step through the night. Watch the stage colours above change while the EEG changes shape below.
  • Look at the EMG line in Sleep-EDF: it moves in steps. That is not a bug. This channel is stored at 1 Hz, so you see one value per second.
  • Under the channels is the spectrum of the two EEG channels for the window you’re looking at, with the share of each band (Lab 8a explains how it’s computed). Click page 1750 in the morning (06:47, scored W): Pz-Oz at the back of the head has 54% alpha, Fpz-Cz on the forehead only 5%. Then press first N3 and watch delta take over on both.
  • Look at the whole hypnogram: the night is a small part of the recording. These recordings were made at home over about 20 hours, so most of the file is daytime wake. Before training anything, you first have to find the night.

4. What came inside the files

An EDF file (European Data Format, 1992) is simple on purpose: first a block of plain text that describes the recording, then the numbers. Below is exactly what your files say, field by field.

The header: the label on the box

Open the files above and your file's header appears here, field by field.

The channels: what each line on the screen is

Each signal has its own small description. The two things that decide what you see are the sampling rate (how many measurements per second) and the unit.

Open the files above to list their channels.

The hypnogram: the human’s answer

Open the hypnogram file to see the technician's notes.

5. How the picture is made

Step 1: numbers become volts. Inside the file every measurement is stored as a whole number between a digital minimum and maximum (for 2-byte integers, at most −32768 to 32767). The header also gives the matching physical minimum and maximum. In SC4001E0, the Fpz-Cz EEG is stored as whole numbers from −2048 to 2047, and those stand for −192 to +192 µV. Those four numbers set the offset and amplification, so the page maps each stored number on a straight line between them:

volts = (stored − digital_min) × (physical_max − physical_min) / (digital_max − digital_min) + physical_min

Step 2: the data is in blocks. The file is a row of data records, each the same length (30 s in Sleep-EDF). Each record holds the samples of signal 1, then signal 2, and so on. The sampling rate is not written down directly: it is the number of samples per record divided by the record length. That is why the EEG comes out at 3000 / 30 s = 100 Hz and the chin EMG at 30 / 30 s = 1 Hz.

Step 3: samples become pixels. A screen is only about a thousand pixels wide. For one 30-second page of EEG that is 3000 samples, so each pixel column covers about 3 samples; for one hour it is 360,000 samples. Drawing only every n-th sample would hide short spikes, so for each pixel column the page draws a vertical line from the lowest to the highest sample in it. The line under the viewer gives the exact numbers for the window you’re looking at.

Step 4: one scale for the whole night. Each channel gets its height from the whole recording (twice the range between the 1st and 99th percentile of its values, so the largest waves still fit), not from the current window. The ± number next to each label is how far the row reaches up and down. A fixed scale lets you see the EEG grow large and slow in N3 and stay small and fast in REM. A scale that re-fits every window would make everything look the same size.

6. Why there is no model here, and why that matters

Nothing on this page predicts anything. The colours in the hypnogram are the technician’s decisions, read straight from the second file. That file is what researchers call the ground truth: the answer a model is trained on and later judged against.

So the quality of every sleep-staging model starts here: how good is the signal, and how good is the human answer? Two people scoring the same night don’t always agree, and a recording full of bad contact or movement gives a model nothing to learn from. The lab tools in lesson 9 deal with exactly those two problems: staging (DreamentoScorer, ezscore-f) and checking whether a stretch of signal is usable at all (eegFloss).

Coming next: cut out only the night and draw the hypnogram yourself in Python (Lab 9b), turn each 30-second page into numbers (9c), and train a first small model and test it on a night it hasn’t seen (9d).

What this doesn’t show

  • Only one database, scored by the 1968 rules. Newer data is scored by the AASM rules, and other files can label stages differently; the page recognises the common labels and ignores the rest.
  • The channel explanations are matched on the channel’s name. A channel this page doesn’t recognise says so.
  • The page draws what is in the file. It does not filter the signal, remove artifacts or check quality.

Sources

  • The data. Sleep-EDF Expanded, PhysioNet, Open Data Commons Attribution License v1.0: https://doi.org/10.13026/C2X676. Created by Bob Kemp. Original paper: Kemp, Zwinderman, Tuk, Kamphuisen and Oberyé (2000), Analysis of a sleep-dependent neuronal feedback loop: the slow-wave microcontinuity of the EEG, IEEE Transactions on Biomedical Engineering 47(9):1185–1194.
  • The Sleep Cassette study (from the PhysioNet page, https://physionet.org/content/sleep-edfx/1.0.0/): recorded 1987–1991 to study age effects on sleep, in healthy people aged 25–101 without sleep-related medication. Two recordings of about 20 hours each, at the subjects’ homes, on a modified Walkman-like cassette recorder. EEG and EOG sampled at 100 Hz; chin EMG stored as an envelope at 1 Hz; airflow, rectal temperature and the event marker at 1 Hz.
  • The scoring (same page): by well-trained technicians, following the 1968 Rechtschaffen and Kales manual, but using the Fpz-Cz and Pz-Oz EEG instead of the usual C4-A1/C3-A2. Labels W, R, 1, 2, 3, 4, M (movement time) and ? (not scored). The AASM rules merge stages 3 and 4 into N3, and this page does the same. The 30-second page length is not written on the PhysioNet page; we checked it in the file: every annotation lasts a whole multiple of 30 s.
  • The file format. EDF: Kemp, Värri, Rosa, Nielsen and Gade (1992), A simple format for exchange of digitized polygraphic recordings, Electroencephalography and Clinical Neurophysiology 82:391–393. EDF+ (adds annotations such as the hypnogram): Kemp and Olivan (2003), European data format ‘plus’ (EDF+), an EDF alike standard format for the exchange of physiological data, Clinical Neurophysiology 114:1755–1761. Field-by-field specs: https://www.edfplus.info/specs/edf.html and https://www.edfplus.info/specs/edfplus.html. The volts formula above is the straight-line mapping the spec describes in words.
  • Our own checks on the file (the game in step 1): the byte tiles are the real first 2,048 bytes of SC4001E0-PSG.edf and bytes 513–624 of SC4001EC-Hypnogram.edf. We checked that 2,048 + 2,650 × 18,240 = 48,338,048 bytes is the file’s exact size, and that the first two data bytes (35 00) read as 53, which the header turns into 5.02 µV. The “about 144 MB as text” figure is our own count: every value of the night written out rounded to 0.01, separated by commas.