| Title: | Estimate Sleep from 'Accelerometry' Data |
|---|---|
| Description: | Interfaces the 'asleep' python module <https://github.com/OxWearables/asleep> from Yuan (2024) <doi:10.1038/s41746-024-01148-y> to estimate sleep from 'accelerometry' data. |
| Authors: | John Muschelli [aut, cre] (ORCID: <https://orcid.org/0000-0001-6469-1750>) |
| Maintainer: | John Muschelli <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-09-18 08:55:50 UTC |
| Source: | https://github.com/jhuwit/asleep |
asleep Model on DataRun asleep Model on Data
Run asleep with Python
asleep( file, outdir = NULL, min_wear_hours = 22L, time_shift = "0", report_light_and_temp = FALSE, pytorch_device = c("cpu", "cuda:0"), sample_rate = NULL, verbose = TRUE, force_download = FALSE ) py_asleep( ..., pyenv_function = function() { asleep::py_require_asleep() }, show = FALSE )asleep( file, outdir = NULL, min_wear_hours = 22L, time_shift = "0", report_light_and_temp = FALSE, pytorch_device = c("cpu", "cuda:0"), sample_rate = NULL, verbose = TRUE, force_download = FALSE ) py_asleep( ..., pyenv_function = function() { asleep::py_require_asleep() }, show = FALSE )
file |
accelerometry file to process, including CSV,
CWA, GT3X, and |
outdir |
output directory for CSVs and outputs |
min_wear_hours |
Min wear time in hours to be eligible for summary statistics computation. The sleepnet paper uses 22 |
time_shift |
The number hours to shift forward or backward from the current device time. e.g. +1 or -1 |
report_light_and_temp |
If true, it adds mean temp, and light columns to the predictions |
pytorch_device |
device to use for prediction for PyTorch. |
sample_rate |
Sample rate for the data, currently not used. |
verbose |
print diagnostic messages |
force_download |
force a download of the model, passed to |
... |
arguments to pass to asleep |
pyenv_function |
function that loads the forest Python package.
By default, it uses |
show |
Logical, whether to show the standard output on the
screen while the child process is running, passed to |
A list of outputs, including summaries, paths, and dataframes.
asleep_check_result = function() { res = try({suppressWarnings(asleep_check())}) if (inherits(res, "try-error")) { res = FALSE } res } file = system.file("extdata/example_sleep.csv.gz", package = "asleep") stopifnot(file.exists(file)) if (asleep_check_result()) { out = try({asleep(file = file, verbose = 2L)}) if (inherits(out, "try-error")) { message(out) if (interactive()) { reticulate::py_last_error() } } else { pred = out$predictions } } file = system.file("extdata/example_sleep.csv.gz", package = "asleep") df = readr::read_csv(file) if (asleep_check_result()) { out = try({asleep(file = df)}) if (inherits(out, "try-error")) { message(out) } else { st = out$predictions if (requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("tidyr", quietly = TRUE) && requireNamespace("dplyr", quietly = TRUE)) { d = st[1:250,] |> dplyr::mutate( time = lubridate::as_datetime(time), time_end = dplyr::lead(time) ) |> dplyr::filter(!is.na(time_end)) raw = df |> dplyr::filter(time >= min(d$time) & time <= max(d$time)) dat = raw |> tidyr::gather(axis, value, -time) d = d |> dplyr::mutate(activity_y = as.numeric(sleep_wake == "sleep")) dat |> ggplot2::ggplot(ggplot2::aes(x = time, y = value, colour = axis)) + ggplot2::geom_step() + ggplot2::geom_segment( data = d, ggplot2::aes( x = time, xend = time_end, y = activity_y, yend = activity_y, linetype = sleep_wake ), colour = "black", linewidth = 1, inherit.aes = FALSE ) + ggplot2::labs(linetype = "Sleep/wake") } } }asleep_check_result = function() { res = try({suppressWarnings(asleep_check())}) if (inherits(res, "try-error")) { res = FALSE } res } file = system.file("extdata/example_sleep.csv.gz", package = "asleep") stopifnot(file.exists(file)) if (asleep_check_result()) { out = try({asleep(file = file, verbose = 2L)}) if (inherits(out, "try-error")) { message(out) if (interactive()) { reticulate::py_last_error() } } else { pred = out$predictions } } file = system.file("extdata/example_sleep.csv.gz", package = "asleep") df = readr::read_csv(file) if (asleep_check_result()) { out = try({asleep(file = df)}) if (inherits(out, "try-error")) { message(out) } else { st = out$predictions if (requireNamespace("ggplot2", quietly = TRUE) && requireNamespace("tidyr", quietly = TRUE) && requireNamespace("dplyr", quietly = TRUE)) { d = st[1:250,] |> dplyr::mutate( time = lubridate::as_datetime(time), time_end = dplyr::lead(time) ) |> dplyr::filter(!is.na(time_end)) raw = df |> dplyr::filter(time >= min(d$time) & time <= max(d$time)) dat = raw |> tidyr::gather(axis, value, -time) d = d |> dplyr::mutate(activity_y = as.numeric(sleep_wake == "sleep")) dat |> ggplot2::ggplot(ggplot2::aes(x = time, y = value, colour = axis)) + ggplot2::geom_step() + ggplot2::geom_segment( data = d, ggplot2::aes( x = time, xend = time_end, y = activity_y, yend = activity_y, linetype = sleep_wake ), colour = "black", linewidth = 1, inherit.aes = FALSE ) + ggplot2::labs(linetype = "Sleep/wake") } } }
Get SSL Net -
get_sslnet() sl_get_sslnet()get_sslnet() sl_get_sslnet()
A keras model representing the SSL Net.
asleep Python ModuleCheck the asleep Python Module
have_asleep() asleep_check(...) asleep_version(...)have_asleep() asleep_check(...) asleep_version(...)
... |
additional arguments to pass to reticulate::py_list_packages |
A logical value indicating whether the asleep Python module is available.
if (have_asleep()) { asleep_version() }if (have_asleep()) { asleep_version() }
py_require for asleep
Command for py_require for asleep
py_require_asleep(...)py_require_asleep(...)
... |
arguments to pass to |
A logical value indicating whether the package is available.
asleep models to CacheDownload asleep models to Cache
sl_download_models(force_download = FALSE)sl_download_models(force_download = FALSE)
force_download |
force a download of the model, even if it already exists at |
A model from Python. sl_download_model returns a model file path.
asleep
Read a Data Set for asleep
sl_read(file, resample_hz = "uniform", keep_pandas = FALSE)sl_read(file, resample_hz = "uniform", keep_pandas = FALSE)
file |
path to the file for reading |
resample_hz |
Target frequency (Hz) to resample the signal. If
"uniform", use the implied frequency (use this option to fix any device
sampling errors). Pass |
keep_pandas |
do not convert the data to a |
A list of the data and information about the data
The data P30_wrist100 is from
https://ora.ox.ac.uk/objects/uuid:19d3cb34-e2b3-4177-91b6-1bad0e0163e7,
where we took the first 180,000 rows, the first 30 minutes of data
from that participant as an example.
file = system.file("extdata/example_sleep.csv.gz", package = "asleep") if (asleep_check()) { out = sl_read(file, resample_hz = FALSE) }file = system.file("extdata/example_sleep.csv.gz", package = "asleep") if (asleep_check()) { out = sl_read(file, resample_hz = FALSE) }
asleep
Rename data for asleep
sl_rename_data(data) sl_write_csv(data, path = tempfile(fileext = ".csv"))sl_rename_data(data) sl_write_csv(data, path = tempfile(fileext = ".csv"))
data |
a |
path |
path to the CSV output file |
A data.frame of renamed columns