README.md

Sleepr

Build Status codecov License: MIT

Sleep analysis with R.

Installation

SleepR latest version can be directly installed from Github using the devtools package.

devtools::install_github("boupetch/sleepr")

Usage

File manipulation

In SleepR, write_mdf() and read_mdf() functions are used to write and read records on disk. Files are converted from the European Data Format (EDF) to Morpheo Data Format1 (MDF). MDF is a simple, efficient and interoperable file format for biological timeseries. The format supports raw signal and metadata storage. MDF uses binary files for signals and JSON for metadata.

Sleep visualization

Hypnogram

plot_hypnogram() function plots a hypnogram using ggplot2, higlighting REM sleep in red. The function takes as argument a dataframe containing labels, start and end times of events. AASM stages2 with labels AWA,REM,N1,N2,N3 by default. Labels can be redefined using the labels argument.

hypnogram

Spectrogram

A spectrogram visually represents the frenquencies spectrum of a signal. Spectrogram are widely used to visualize and analyse EEG. plot_spectrogram() conveniently wrappers phonTools library spectrogram function to plot spectrograms.

spectrogram

Spectral powers

record <- read_mdf("mdf/isruc-1-89-1/")

powers <- sleepr::hypnogram_band_powers(record = record,
                                        channel = "C3-M2")
powers$colors <- NA
powers$colors[powers$stage == "AWA"] <- "#1D60E7"
powers$colors[powers$stage == "REM"] <- "#D900F0"
powers$colors[powers$stage == "N1"] <- "#FD9509"
powers$colors[powers$stage == "N2"] <- "#F70007"
powers$colors[powers$stage == "N3"] <- "#339F03"
threejs::scatterplot3js(powers$delta, powers$theta,
                        as.integer(powers$epoch), size = 0.25,
                        color= powers$colors)
spectral powers

Transitions

Statistics

h <- hypnogram(read_mdf("mdf/1/89/",channels = NA)[["events"]])

tm <- transition_matrix(h)

knitr::kable(round(tm, digits=3))

| | AWA| N1| N2| N3| REM| |:---|-----:|-----:|-----:|-----:|-----:| |AWA | 0.911| 0.065| 0.019| 0.000| 0.002| |N1 | 0.149| 0.368| 0.391| 0.000| 0.092| |N2 | 0.052| 0.067| 0.810| 0.059| 0.011| |N3 | 0.029| 0.012| 0.047| 0.907| 0.006| |REM | 0.044| 0.088| 0.011| 0.000| 0.857|

Plot

h <- hypnogram(read_mdf("mdf/1/89/",channels = NA)[["events"]])

sleepr::plot_transitions(h)
transitions graph

Statistics computing

Various statistics can be computed from the polysomnographic data, signals and events. Durations are expressed in minutes, indexes in hours.

compute_all_stats() function call all the following functions over a vector of records paths. It returns a dataframe with a line for each record. EEG bands can be specified at this point to the bands argument.

# Write record to disk from EDF to MDF
write_mdf("path/to/edf", "path/to/mdf")

# Compute all stats from the record
compute_all_stats("path/to/mdf", bands = list(
  delta = c(0.5,3.5), theta = c(3.5,8), alpha = c(8,12),
  beta = c(12,30), gamma1 = c(30,40), gamma2 = c(40,60)))

Stages & scoring

These functions compute statistics based on stage scoring.

Position & activity

Snoring

Respiratory indexes

Micro-arousals

Rapid eye movements

Cycles

Events nomenclature

SleepR uses the following nomenclature for annotated events labels:

Stages

Cycles

Position

Other events

Data download

SleepR can download open databases with simple function calls.

ISRUC Sleep3

A collection of sleep records from the Institute for Systems and Robotics (ISR) of the University of Coimbra, Portugal.

download_isruc("./")

The Sleep-EDF Database Expanded4

61 polysomnograms hosted on Physionet5.

download_sleepedfx("./")

Dreams Subjects6

20 whole-night PSG recordings coming from healthy subjects.

download_dreams_subjects("./")

CAP Sleep Database7

A collection of 108 polysomnographic recordings registered at the Sleep Disorders Center of the Ospedale Maggiore of Parma, Italy and hosted on Physionet5.

download_capslpdb("./")

Examples

Sleepviewer.com is an online application to visualize sleep records from various open data sources. It uses SleepR to manage and process records.

Package Testing

Testing use testthat.

Generating Sleepr package reference manual

R CMD Rd2pdf . && mv ..pdf sleepr.pdf && rm -r .Rd2pdf*

References

  1. P. Bouchequet, D. Jin, G. Solelhac, M. Chennaoui, D. Leger, «Morpheo Data Format (MDF), un nouveau format de données simple, robuste et performant pour stocker et analyser les enregistrements de sommeil», Médecine du Sommeil, vol. 15, n 1, p. 48‑49, march 2018.

  2. R.B. Berry, R. Brooks, C.E. Gamaldo, S.M. Harding, C.L. Marcus, B.V. Vaughn, The AASM Manual for the Scoring of Sleep and Associated Events, 2013.

  3. Khalighi Sirvan, Teresa Sousa, José Moutinho Santos, and Urbano Nunes. ISRUC-Sleep: A comprehensive public dataset for sleep researchers. Computer methods and programs in biomedicine 124 (2016): 180-192.

  4. B Kemp, AH Zwinderman, B Tuk, HAC Kamphuisen, JJL Oberyé. Analysis of a sleep-dependent neuronal feedback loop: the slow-wave microcontinuity of the EEG. IEEE-BME 47(9):1185-1194 (2000).

  5. Goldberger AL, Amaral LAN, Glass L, Hausdorff JM, Ivanov PCh, Mark RG, Mietus JE, Moody GB, Peng C-K, Stanley HE. PhysioBank, PhysioToolkit, and PhysioNet: Components of a New Research Resource for Complex Physiologic Signals. Circulation 101(23):e215-e220 Circulation Electronic Pages; 2000 (June 13).

  6. S. Devuyst, T. Dutoit, P. Stenuit, M. Kerkhofs, E. Stanus, "Canceling ECG Artifacts in EEG using a Modified Independent Component Analysis Approach", EURASIP Journal on Advances in Signal Processing, Volume 2008, Article ID 747325, Accepted 31 July 2008.

  7. MG Terzano, L Parrino, A Sherieri, R Chervin, S Chokroverty, C Guilleminault, M Hirshkowitz, M Mahowald, H Moldofsky, A Rosa, R Thomas, A Walters. Atlas, rules, and recording techniques for the scoring of cyclic alternating pattern (CAP) in human sleep. Sleep Med 2001 Nov; 2(6):537-553.



boupetch/sleepr documentation built on June 26, 2019, 9:08 a.m.