| Title: | Hypnogram Handling, Plotting, and Sleep Architecture Metrics |
|---|---|
| Description: | Provides a staging-agnostic layer for hypnogram ingestion, sleep architecture metric computation, cycle segmentation, and transition analysis. Accepts full AASM-staged hypnograms (W / N1 / N2 / N3 / REM) from mrpheus and coarser actigraphy-derived hypnograms (W / Sleep / Quiet sleep) from zeitR; all metric functions degrade gracefully depending on staging resolution. Includes publication-ready plotting via theme_circadia(). Designed as the hypnogram layer of the Circadia Lab ecosystem, feeding into syncR::sync(). |
| Authors: | Lucas França [aut, cre] (ORCID: <https://orcid.org/0000-0003-0853-1319>), Mario Leocadio-Miguel [aut] (ORCID: <https://orcid.org/0000-0002-7248-3529>) |
| Maintainer: | Lucas França <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-07-01 23:57:11 UTC |
| Source: | https://github.com/circadia-bio/hypnoR |
Segments a hypnogram into NREM/REM cycles using either Feinberg & Floyd (1979) rules or a simplified AASM-compatible algorithm. Only applicable to full AASM hypnograms; returns an informative error for coarse actigraphy-derived hypnograms.
compute_cycles( hypnogram, method = "feinberg_floyd", epoch_sec = 30L, min_rem_epochs = 5L )compute_cycles( hypnogram, method = "feinberg_floyd", epoch_sec = 30L, min_rem_epochs = 5L )
hypnogram |
A tibble returned by |
method |
|
epoch_sec |
Epoch duration in seconds. Default |
min_rem_epochs |
Minimum number of consecutive REM epochs to qualify
as a REM period. Default |
A tibble with one row per detected cycle:
Integer cycle index.
First and last epoch of the cycle.
Duration of NREM and REM portions (minutes).
Total cycle duration (minutes).
Feinberg, I., & Floyd, T. C. (1979). Systematic trends across the night in human sleep cycles. Psychophysiology, 16(3), 283–291.
## Not run: hyp <- read_hypnogram("night_001.csv") compute_cycles(hyp) ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") compute_cycles(hyp) ## End(Not run)
Derives standard sleep architecture summary statistics from a staged hypnogram. All metrics are resolution-agnostic: the function computes every metric that is possible given the available staging levels and silently omits metrics that require stages not present in the hypnogram (e.g. REM latency requires a full AASM hypnogram).
compute_sleep_architecture( hypnogram, epoch_sec = 30L, lights_off = NULL, lights_on = NULL )compute_sleep_architecture( hypnogram, epoch_sec = 30L, lights_off = NULL, lights_on = NULL )
hypnogram |
A tibble returned by |
epoch_sec |
Epoch duration in seconds. Default |
lights_off, lights_on
|
Optional |
A one-row tibble with columns:
Total sleep time (minutes).
Time in bed (minutes). NA if lights_off/lights_on
not supplied.
Sleep efficiency (percent). NA if TIB unavailable.
Sleep onset latency (minutes).
Wake after sleep onset (minutes).
REM latency (minutes). NA for coarse hypnograms.
SWS (N3) latency (minutes). NA for coarse
hypnograms.
Stage percentages of TST. NA
for stages absent in the hypnogram.
Stage percentages for coarse
hypnograms. NA for full AASM hypnograms.
"aasm" or "coarse".
## Not run: hyp <- read_hypnogram("night_001.csv") compute_sleep_architecture(hyp) ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") compute_sleep_architecture(hyp) ## End(Not run)
Builds a stage-to-stage transition probability matrix and derives fragmentation indices from a staged hypnogram. Works with both full AASM and coarse actigraphy-derived staging.
compute_transitions(hypnogram, normalise = TRUE)compute_transitions(hypnogram, normalise = TRUE)
hypnogram |
A tibble returned by |
normalise |
If |
A list with two elements:
A square tibble (stages × stages) of transition probabilities or counts. Row = from stage, column = to stage.
A one-row tibble with:
Total number of stage transitions.
Proportion of epochs that are followed by a different stage.
Number of transitions to Wake (proxy for arousal burden).
## Not run: hyp <- read_hypnogram("night_001.csv") trans <- compute_transitions(hyp) trans$matrix trans$fragmentation ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") trans <- compute_transitions(hyp) trans$matrix trans$fragmentation ## End(Not run)
Renders stage durations or percentages as a horizontal bar chart using
ggplot2 and theme_circadia().
plot_architecture( architecture, metric = "duration", colours = NULL, title = NULL )plot_architecture( architecture, metric = "duration", colours = NULL, title = NULL )
architecture |
A one-row tibble returned by
|
metric |
|
colours |
Named character vector of stage colours. See
|
title |
Optional plot title. |
A ggplot object.
## Not run: hyp <- read_hypnogram("night_001.csv") arch <- compute_sleep_architecture(hyp) plot_architecture(arch) ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") arch <- compute_sleep_architecture(hyp) plot_architecture(arch) ## End(Not run)
Renders a classic stage-over-time hypnogram using ggplot2 and
theme_circadia(). Accepts both full AASM and coarse staging; the
y-axis order and colour mapping are set automatically from the staging
resolution detected in hypnogram.
plot_hypnogram( hypnogram, epoch_sec = 30L, colours = NULL, show_cycles = FALSE, title = NULL )plot_hypnogram( hypnogram, epoch_sec = 30L, colours = NULL, show_cycles = FALSE, title = NULL )
hypnogram |
A tibble returned by |
epoch_sec |
Epoch duration in seconds, used to construct the
time axis. Default |
colours |
Named character vector mapping stage labels to hex colours.
Defaults to the Circadia Lab palette via |
show_cycles |
If |
title |
Optional plot title. |
A ggplot object.
## Not run: hyp <- read_hypnogram("night_001.csv") plot_hypnogram(hyp) ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") plot_hypnogram(hyp) ## End(Not run)
Renders the transition probability (or count) matrix returned by
compute_transitions() as a heatmap using ggplot2 and
theme_circadia().
plot_transition_matrix( transitions, label_values = TRUE, digits = 2L, title = NULL )plot_transition_matrix( transitions, label_values = TRUE, digits = 2L, title = NULL )
transitions |
The |
label_values |
If |
digits |
Number of decimal places for cell labels. Default |
title |
Optional plot title. |
A ggplot object.
## Not run: hyp <- read_hypnogram("night_001.csv") trans <- compute_transitions(hyp) plot_transition_matrix(trans$matrix) ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") trans <- compute_transitions(hyp) plot_transition_matrix(trans$matrix) ## End(Not run)
Imports a staged hypnogram from common formats and returns a tidy tibble
with one row per epoch. The resulting object is the standard input for all
other hypnoR functions.
read_hypnogram( path, format = "auto", epoch_sec = 30L, tz = "UTC", quiet = FALSE )read_hypnogram( path, format = "auto", epoch_sec = 30L, tz = "UTC", quiet = FALSE )
path |
Path to the hypnogram file. |
format |
One of |
epoch_sec |
Epoch duration in seconds. Default |
tz |
Time zone string passed to |
quiet |
Suppress informational messages. Default |
hypnoR distinguishes two staging resolutions:
Full AASM (5-state): supplied by mrpheus.
Coarse (3-state): supplied by zeitR.
All downstream metric functions are resolution-agnostic: they compute what is possible given the available stages and document which metrics require full AASM staging.
A tibble with columns:
Integer epoch index (1-based).
POSIXct timestamp for the start of each epoch.
Ordered factor. Level set depends on staging resolution:
full AASM (W, N1, N2, N3, REM) or coarse actigraphy-derived
(W, Sleep, Quiet sleep).
Character — the originating device / scorer label, if available in the file header.
## Not run: hyp <- read_hypnogram("night_001.csv") hyp <- read_hypnogram("night_001.edf", format = "edf_annotations") ## End(Not run)## Not run: hyp <- read_hypnogram("night_001.csv") hyp <- read_hypnogram("night_001.edf", format = "edf_annotations") ## End(Not run)