The SleepCycles package Blume and Cajochen (2021) has been specifically developed to identify sleep cycles Feinberg and Floyd (1979) and their corresponding NREM and REM components (known as (N)REM periods) from data that has been categorized based on AASM criteria for sleep staging (“AASM Scoring Manual - American Academy of Sleep Medicine,” n.d.).

In the other hand, the rsleep package reads and analyze sleep data in various formats.

This vignette describes how to combine SleepCycles and rsleep packages to identify sleep cycles in sleep data and then leverage this material in sleep data analysis pipelines.

Hypnogram

Sleep cycles can be identified from hypnograms. 15012016HD.csv contains a hypnogram scored by a sleep expert using Noxturnal software published by ResMed. The rsleep package provides the read_events_noxturnal() function to read hypnograms in this format.

library(rsleep)

if(!file.exists("15012016HD.csv")){
  download.file(
  url = "https://rsleep.org/data/15012016HD.csv",
  destfile = "15012016HD.csv")}

events <- rsleep::read_events_noxturnal("15012016HD.csv")

unlink("15012016HD.csv")

events = hypnogram(events)

rsleep::plot_hypnogram(events)

Formatting

Tne SleepCycles package only reads directories and files in specific format. Hypnograms must be converted in the appropriate arrangement before being written on disk in an explicit folder:

events.vmrk = data.frame(Description = as.character(events$event))
events.vmrk$Description[events.vmrk$Description == "AWA"] = 0
events.vmrk$Description[events.vmrk$Description == "N1"] = 1
events.vmrk$Description[events.vmrk$Description == "N2"] = 2
events.vmrk$Description[events.vmrk$Description == "N3"] = 3
events.vmrk$Description[events.vmrk$Description == "REM"] = 5
events.vmrk$Description = as.integer(events.vmrk$Description)
events.vmrk$Type = "SleepStage"
events.vmrk = events.vmrk[,c(2,1)]

newdir <- file.path(
  tempdir(),"SleepCycles")

dir.create(newdir, showWarnings = FALSE)

write.table(
  events.vmrk, 
  file = paste(
    newdir,
    "events.txt", sep = "/"),
  row.names=FALSE,
  col.names = TRUE, 
  quote = FALSE, 
  sep = ",")

Detection

The SleepCycles() function can now read the created directory and detect sleep cycles in the saved hypnograms. The original version of the function interactively asks the file format to the user and writes the result to a file in the same directory. The forked version boupetch/SleepCycles modifies this behaviour to take the format as parameters and return directly the results as a dataframe, making the pipeline easier to automate.


devtools::install_github("boupetch/SleepCycles")

cycles = SleepCycles::SleepCycles(
  p = newdir, 
  filetype = "txt", 
  plot = FALSE)

unlink(newdir, recursive=TRUE)

head(cycles)

Indicators

Binding the resulting dataframe to the original hypnogram events and performing aggregations provides valuable sleep macrostructure indicators:


hypnogram.full = cbind(events, cycles)

# Number of cycles
max(hypnogram.full$SleepCycle, na.rm = TRUE)
  
# Duration of each cycle
hypnogram.agg = aggregate(
  event ~ SleepCycle, 
  data = hypnogram.full, 
  FUN = length)
hypnogram.agg$minutes = hypnogram.agg$event/2
hypnogram.agg

# Composition of each cycle
cycles.comp = aggregate(
  SleepStages ~ SleepCycle + event, 
  data = hypnogram.full, 
  FUN = length)
cycles.comp = reshape(
  data = cycles.comp, 
  direction = "wide", 
  timevar  = "event",
  idvar  = "SleepCycle")
cycles.comp

References

“AASM Scoring Manual - American Academy of Sleep Medicine.” n.d. American Academy of Sleep Medicine Association for Sleep Clinicians and Researchers. https://aasm.org/clinical-resources/scoring-manual/.

Blume, Christine, and Christian Cajochen. 2021. “‘SleepCycles’ Package for R - a Free Software Tool for the Detection of Sleep Cycles from Sleep Staging.” MethodsX 8: 101318. https://doi.org/https://doi.org/10.1016/j.mex.2021.101318.

Feinberg, I., and T. C. Floyd. 1979. “Systematic Trends Across the Night in Human Sleep Cycles.” Psychophysiology. Wiley. https://doi.org/10.1111/j.1469-8986.1979.tb02991.x.