Sleep, naps and sleep stages
Finds every sleep and nap in the strap's per-second accelerometer and heart rate, and estimates wake, light, deep and REM. Trained on open PSG datasets; checked against DREAMT and HMC.
Built from algorithms/sleep/README.md
These are the project's working notes, published as written. “You” and “your strap” mean the author and the author's own WHOOP strap; only summary numbers from it appear here, never the recordings.
Pulse's own sleep detection: finds every sleep and nap in the strap's data, and estimates wake / light / deep / REM
inside it. Built like the AF check (../rhythm/): open PSG datasets → features → small boosted trees → evaluation
held out by person → generated Swift (WhoopKit/SleepModel.swift) + test vectors checked by swift test.
uv run --with numpy python prepare.py # download + convert both datasets to 1 Hz (data/, gitignored)
uv run --with numpy --with scikit-learn python train.py --fresh # evaluate (≈ 9 min), then write model + Swift + vectors
uv run --with numpy --with scikit-learn python validate_strap.py /tmp/db/pulse.sqlite # your strap vs WHOOP
cp vectors.json ../../ios/WhoopKit/Tests/WhoopKitTests/Resources/sleep-vectors.json
cd ../../ios/WhoopKit && swift test
(train.py without --fresh reuses the cached held-out predictions in data/heldout.pkl.)
What the strap gives us
One history record per second: heart rate and the accelerometer (3 × f32 g at offset 37, stored in strap_acc).
The accelerometer value behaves like a 1-second mean, not a single sample: asleep, its second-to-second change
(median 0.0008 g) matches 1 s means of a 50 Hz watch (0.0012 g), not single samples (0.0038 g). So every dataset
is reduced to 1-second means before anything else, and the model sees exactly what the strap records.
Data (open access)
| Dataset | What | Used for |
|---|---|---|
| PhysioNet sleep-accel (Walch et al. 2019, ODC-BY) | Apple Watch accelerometer (~50 Hz) + PPG heart rate, lab PSG scored per 30 s, 31 adults. 5 nights dropped for missing accelerometer → 26 | sleep/wake with HR, stages |
| Newcastle PSG + accelerometer (van Hees et al. 2015, Zenodo 1160410) | GENEActiv on both wrists (85.7 Hz), PSG per 30 s, 28 sleep-clinic patients (insomnia, apnoea, narcolepsy…), no heart rate | accelerometer-only model; a second device and population as an external test |
Features (features.py, 30 s epochs, orientation-free)
- Movement:
act(mean second-to-second change of the acceleration vector),ang(degrees the gravity direction turned, from 5 s means: van Hees' idea, but between vectors so it doesn't depend on how the device is worn),sdof |a|; centred means over 2.5, 5.5, 10.5 and 20.5 min; the 5.5 min max ofang. - Stillness context: minutes since the last movement and until the next one (capped at 60), fraction of still epochs over 10.5 min. These carry most of the signal: sleep is long stillness.
- Heart rate relative to your sleeping heart rate (
hr/baseline − 1, baseline = 5th percentile of 30 s mean heart rate, as in training; in the app, the lower of the window's and the one remembered from your last main sleep, so a window without sleep doesn't make a calm evening look like sleep; for stages, the sleep ±1 h): the epoch, 2.5 and 10.5 min means, the 5.5 min spread, and the difference from the 20.5 min mean. - Stages only: hours since the sleep began and fraction of the sleep elapsed (deep sleep comes early, REM late). No clock time anywhere, so sleeping 8 AM → 4 PM is detected the same as 11 PM → 7 AM.
Models (train.py)
| Model | Trees | Trained on |
|---|---|---|
sleep: asleep vs awake, accelerometer + HR (the one the app uses) | 150, depth 3 | Walch |
sleepMotion: asleep vs awake, accelerometer only | 150, depth 3 | Walch + Newcastle |
stages: wake / light (N1+N2) / deep (N3) / REM | 4 × 120, depth 3 | Walch |
Sessions (sessions.py = SleepModel.sessions): probability smoothed over 2.5 min, asleep at ≥ 0.5, wake gaps
≤ 20 min bridged when the strap had data through them, ≥ 20 min asleep to count. Epochs where heart rate is missing
for half the epoch are "no data" (strap off or charging), never sleep. Main sleep vs nap (app,
SleepDetector.swift): the longest sleep with ≥ 3 h asleep ending on a date is that date's main sleep; the others
are naps. Stages are smoothed by a Viterbi pass with the stage-transition probabilities of the training
hypnograms; when WHOOP's stage totals are known, the totals are kept and the model only decides where each stage
goes (SleepModel.assign).
Results (held out by person)
| Sleep vs wake, Walch (acc + HR) | AUC 0.905, accuracy 93.8 %, sleep found 98.1 %, wake found 46.3 %, kappa 0.52 |
| Detected sleep vs PSG, per night | onset error median 4 min, final wake 0 min, total sleep time 18 min (median abs.) |
| Accelerometer only, Walch | AUC 0.905: heart rate adds nothing on this dataset (the watch's HR is sparse) |
| Accelerometer only, Walch-trained → Newcastle | AUC 0.752: a different device and sleep-clinic patients |
| Accelerometer only, Newcastle held out (trained on both) | AUC 0.805, kappa 0.49; total sleep time error 74 min (patients with fragmented sleep) |
| Stages, Walch (4 classes) | see table below |
| Stages (26 nights, 4 classes) | Accuracy | Kappa | Wake / light / deep / REM found | Minutes per night, median error (wake / light / deep / REM) |
|---|---|---|---|---|
| Trees, epoch by epoch | 65.4 % | 0.368 | 37 / 80 / 47 / 44 % | |
| + Viterbi (prior weight 0.5, used) | 65.6 % | 0.405 | 31 / 74 / 59 / 55 % | 7 / 53 / 21 / 43 |
| + Viterbi, prior weight 0 / 1 | 67.1 / 59.9 % | 0.375 / 0.387 |
Context: wrist actigraphy finds sleep easily and wake poorly (published wrist models sit at 40–60 % of wake epochs found); 4-stage accuracy of 60–70 % with kappa ≈ 0.35–0.45 is what accelerometer + heart-rate wearables reach against PSG. Consumer devices, WHOOP included, are in the same range.
Your strap, 29 Nov 2026 (validate_strap.py, 18 h of records, no tuning on it): Pulse found one sleep,
09:00 → 16:32, 434 min asleep; WHOOP scored 09:20 → 16:32, 417 min. 95 % of the epochs inside WHOOP's sleep
were asleep to Pulse, 2 % of the epochs outside it, and no false sleep or nap was reported in the 10 awake hours.
Stages: Pulse wake 44 / light 240 / deep 49 / REM 119 min; WHOOP 15 / 236 / 81 / 101. The stages move with the
baseline (WHOOP's resting HR of 56 instead of the sleep's own 52.5 turned 68 min of REM into light), which is why
the app uses the same 5th-percentile baseline as training.
Limits
- Nobody in either dataset was filmed lying awake watching a movie; the "awake but still" case is what heart rate
relative to your sleep HR and the user's Keep / Delete are for. Every correction is kept (
sleep_edits) and wins over the detector; later they can be used to fine-tune the model on your own nights. - Respiration and HRV (RR) aren't features yet: Walch has no beat intervals. DREAMT (PhysioNet, credentialed) has Empatica PPG + PSG and would allow RR-based staging.
DREAMT (research benchmark, not shipped)
DREAMT v2.2.0 (PhysioNet, Restricted Health Data License: research use only, so nothing trained on it ships):
100 sleep-clinic patients, most with sleep apnoea, Empatica E4 on the wrist, PSG stages. Run on the aws-silo box
with dreamt.py (download from PhysioNet's S3 access point, dreamt_get.sh as the slow fallback). Held out by
person (5 folds); 4 stages with the same Viterbi pass.
| Sleep/wake AUC | Wake found | Kappa | 4 stages accuracy | Stage kappa | |
|---|---|---|---|---|---|
| Shipped model (trained on Walch), untouched | 0.834 | 46 % | 0.44 | 55 % | 0.25 |
| Retrained on DREAMT: movement + HR | 0.843 | 49 % | 0.46 | 66 % | 0.31 |
| + HRV from E4 beats | 0.838 | 49 % | 0.45 | 65 % | 0.29 |
| + skin temperature | 0.845 | 50 % | 0.47 | 66 % | 0.31 |
| + HRV + temperature | 0.839 | 50 % | 0.46 | 65 % | 0.29 |
- The shipped sleep/wake model holds up on a different wristband and a much harder population (fragmented, apnoeic sleep): AUC 0.83 against 0.90 on Walch, close to what a model trained on DREAMT itself gets (0.84).
- Stages are where the population matters: 0.25 kappa untouched vs 0.31 retrained, mostly from deep sleep, which is rare and short in these patients.
- Beat-to-beat HRV and skin temperature don't help here. The E4's beat detection is known to drop most beats during sleep and movement, and these nights are broken by apnoea events, so this says little about the WHOOP strap's much denser beat intervals. Decision: no change to the shipped models; revisit HRV features with a dataset that has clean beats (e.g. ECG-based PSG sets such as MESA/SHHS via NSRR, which are free for research too).
HMC: does clean HRV help? (CC BY 4.0, usable in a product)
Haaglanden Medisch Centrum sleep staging database v1.1 (PhysioNet open access): 151 clinical PSG nights, lab ECG,
AASM stages. R-peaks with NeuroKit2 give clean beat intervals like the strap's. No accelerometer, so these models
are heart-only; the point is the gain from HRV (hmc.py, 5 folds held out by person, same Viterbi pass).
| Heart-only model | Sleep/wake AUC | Wake found | Kappa | 4 stages accuracy | Stage kappa | Wake / light / deep / REM found |
|---|---|---|---|---|---|---|
| Heart rate only | 0.720 | 16 % | 0.20 | 53 % | 0.24 | 26 / 72 / 50 / 24 % |
| + HRV from clean beats | 0.781 | 24 % | 0.28 | 55 % | 0.30 | 34 / 70 / 51 / 33 % |
| HRV only | 0.695 | 13 % | 0.15 | 52 % | 0.23 | 23 / 70 / 48 / 27 % |
With clean beats, HRV adds a lot on top of heart rate (stage kappa +22 %, REM found 24 → 33 %, wake 26 → 34 %), unlike DREAMT's sparse E4 beats. Whether it still adds once movement is in the model needs a dataset with both (MESA via NSRR: wrist actigraphy + ECG), which is the next step; HMC's licence allows training the shipped model on it.
Model files
Generated by this algorithm's train.py and copied from the repository at build time. More on the Our data page.
-
model.json 446 KBalgorithms/sleep/model.json
Three tree models (sleep, sleepMotion, stages), stage transition matrix, priors and the held-out results
sha256 9d0719a45838481e…
-
vectors.json 359 KBalgorithms/sleep/vectors.json
Test vectors: the first 2 h of one sleep-accel night (1 Hz acceleration + HR) → features → probabilities → stages
sha256 f8418a9bb0b6ff02…
References
- Walch O, Huang Y, Forger D, Goldstein C. Sleep stage prediction with raw acceleration and photoplethysmography heart rate data derived from a consumer wearable device. Sleep 42(12) (2019)
- Motion and heart rate from a wrist-worn wearable and labeled sleep from polysomnography (PhysioNet sleep-accel, ODC-BY 1.0)
- van Hees VT et al. A novel, open access method to assess sleep duration using a wrist-worn accelerometer. PLoS ONE 10(11):e0142533 (2015)
- van Hees V, Charman S, Anderson K. Newcastle polysomnography and accelerometer data (Zenodo, CC BY 4.0)
- Wang WK et al. Addressing wearable sleep tracking inequity: a new dataset and novel methods for a population with sleep disorders. CHIL 2024, PMLR 248:380–396 (DREAMT)
- DREAMT v2.2.0 (PhysioNet, Restricted Health Data License 1.5.0)
- Alvarez-Estevez D, Rijsman RM. Inter-database validation of a deep learning approach for automatic sleep scoring. PLoS ONE 16(8):e0256111 (2021)
- Haaglanden Medisch Centrum sleep staging database v1.1 (PhysioNet, CC BY 4.0)
- Makowski D et al. NeuroKit2: A Python toolbox for neurophysiological signal processing. Behav Res Methods 53:1689–1696 (2021)
- National Sleep Research Resource (MESA, SHHS)