Sleep statistics¶
yousleep_common.utils.sleep_statistics computes the thirteen sleep statistics
of a scored hypnogram: TST, TIB, sleep efficiency, sleep onset latency, WASO,
REM latency, the SOREMP indicator, the four sleep stages' shares of TST, wake's
share of TIB, and the sleep fragmentation index. It is the one implementation:
the platform's reports and a model's validation against an expert both call
it, so the same scoring gives the same number in both. Each statistic's
definition is the description of its field on Biomarkers.
It needs numpy, which the package does not otherwise install:
The input¶
A hypnogram is three parallel arrays, one entry per scored interval, in time
order: its start and its end in milliseconds, and its stage as a StageCode.
StageCode |
Value |
|---|---|
WAKE |
0 |
N1 |
1 |
N2 |
2 |
N3 |
3 |
REM |
4 |
Any other integer is no stage (UNSCORED is -1): an unscored epoch, a label in
no stage group, or movement time. Like a gap between intervals, it counts
toward time in bed and nothing else, and it neither opens nor closes the sleep
period. stage_codes(labels) turns staging event labels into codes through
SLEEP_STAGE_GROUPS, which folds Rechtschaffen & Kales stages 3 and 4 into N3.
from yousleep_common.utils.sleep_statistics import sleep_statistics, stage_codes
biomarkers = sleep_statistics(
[e.start_time_ms for e in staging],
[e.end_time_ms for e in staging],
stage_codes(e.label for e in staging),
)
print(biomarkers.tst_min, biomarkers.sleep_efficiency_pct)
A hypnogram of equal epochs is the same call, epoch i spanning
i × length to (i + 1) × length.
Time in bed¶
Time in bed spans the first interval's start to the last one's end, unless
time_in_bed_ms=(start, end) gives it. Choosing the night is the caller's:
to compute lights off to lights on, pass the intervals inside it and its bounds.
Sleep onset latency is measured from the start of time in bed.
What a night does not define¶
A statistic a night does not define is None, never 0: WASO, the
fragmentation index and the sleep stages' shares without any sleep, REM latency
without REM. Without sleep, sleep onset latency equals time in bed.
The SOREMP threshold is 15 minutes unless soremp_threshold_min sets another;
a first REM exactly at the threshold counts.
What belongs here¶
This package holds a statistic's definition only when two components must compute exactly the same number from it, as the platform's reports and a model's validation do for these thirteen. Everything else about an analysis stays with the component that uses it: which of its events count, how probabilistic staging is resolved, which part of a recording is in scope, every other metric, and what a report shows. A further statistic moves here when a second component needs the same number. Changing a definition here changes it everywhere it is called.