tests/testthat/test-daily_measures.R

local({
  bouts <- data.frame(
    TimeUTC = c(1619417001, 1619417016, 1619417030, 1619417043, 1619417055, 1619417066, 1619417076, 1619417085, 1619417092, 1619417100, 1619417107),
    Duration = c(15, 14, 13, 12, 11, 10, 9, 7, 8, 7, 6),
    AGSAMean = c(0.01, 0.05, 0.02, 0.023, 0.06, 0.019, 0.002, 0.05, 0.02, 0.04, 0.03),
    NonWear = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE),
    Rest = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
    Sleep = c(TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE),
    Ambulatory = rep(FALSE, 11),
    RestSeconds = c(14, 14, 13, 12, 11, 10, 9, 7, 1, 0, 0),
    StepMean = rep(1, 11),
    Classification = c(
      "sleep", "sedentary", "sleep", "sleep", "sedentary",
      "sleep", "non_wear", "sedentary", "sleep", "sedentary", "sleep"
    ),
    stringsAsFactors = FALSE
  )

  TEMPMPI <- list()
  TEMPMPI$non_movement$non_wear <- data.frame(start_time = 1619417076, duration = 9)
  TEMPMPI$non_movement$rest_intervals <- data.frame(
    day_number = 1,
    start_time = 1619417002,
    duration = 91
  )
  TEMPMPI$file_data$NumberDays <- 1
  TEMPMPI$file_data$TimeOffset <- 3600
  TEMPMPI$file_data$CutTime24Hr <- "15:00"
  TEMPMPI$file_data[["MeasurementStartTimeUTC"]] <- 1619416801
  TEMPMPI$file_data[["MeasurementEndTimeUTC"]] <- 1619417501
  TEMPMPI$file_data[["FirstLocalMidnightTimeUTC"]] <- 1619478000
  TEMPMPI$file_data[["ParticipantWeight"]] <- ""

  test_dir <- file.path(tempdir(), "sleep_test")
  dir.create(test_dir, showWarnings = FALSE)
  UniqueBinFileIdentifier <- "TEST123"
  dir.create(file.path(test_dir, "GENEAbout"))
  saveRDS(TEMPMPI, file.path(test_dir, paste0(UniqueBinFileIdentifier, "_MPI.rds")))
  saveRDS(bouts, file.path(test_dir, "GENEAbout", paste0(UniqueBinFileIdentifier, "_day1_bouts.rds")))

  expected_measures <- data.frame(
    NightofDate = "25-Apr-2021",
    RecordedDataDay = 112,
    WearDurationDay = 103,
    NonWearDurationDay = 9,
    RestStartTime = "07:03:21",
    RestEndTime = "07:04:52",
    RestIntervalDuration = 91,
    SleepOnsetTime = "07:03:21",
    SleepEndTime = "07:04:36",
    SleepIntervalDuration = 75,
    TotalSleepDuration = 50,
    SleepEfficiency = 54.9,
    SleepOnsetLatency = 0,
    SleepOffsetDuration = 16,
    WakeAfterSleepOnsetDuration = 25,
    WakeAfterSleepOnsetCount = 2,
    NapsCountDay = 2,
    TotalNapDuration = 14,
    MidsleepTime = "07:03:58",
    MeanNapDuration = 7,
    NumberBoutsDay = 11,
    Notes = ""
  )

  actual_measures <- single_file_measures(
    output_folder = test_dir,
    UniqueBinFileIdentifier = UniqueBinFileIdentifier,
    minimum_valid_hours = 0,
    measure_type = "sleep"
  )

  test_that("Daily sleep measures are aggregated correctly.", {
    expect_equal(actual_measures, expected_measures)
  })

  actual_measures_2 <- single_file_measures(
    output_folder = test_dir,
    UniqueBinFileIdentifier = UniqueBinFileIdentifier,
    minimum_valid_hours = 1,
    measure_type = "sleep"
  )

  expected_measures_2 <- data.frame(
    NightofDate = "25-Apr-2021",
    RecordedDataDay = 112,
    WearDurationDay = 103,
    NonWearDurationDay = 9,
    RestStartTime = "07:03:21",
    RestEndTime = "07:04:52",
    RestIntervalDuration = 91,
    SleepOnsetTime = "07:03:21",
    SleepEndTime = "07:04:36",
    SleepIntervalDuration = 75,
    TotalSleepDuration = 50,
    SleepEfficiency = 54.9,
    SleepOnsetLatency = 0,
    SleepOffsetDuration = 16,
    WakeAfterSleepOnsetDuration = 25,
    WakeAfterSleepOnsetCount = 2,
    NapsCountDay = 2,
    TotalNapDuration = 14,
    MidsleepTime = "07:03:58",
    MeanNapDuration = 7,
    NumberBoutsDay = 11,
    Notes = "Less than 1 hours of wear time."
  )

  test_that("Daily sleep measures are aggregated correctly when minimum_valid_hours not met.", {
    expect_equal(actual_measures_2, expected_measures_2)
  })

  actual_measures_3 <- single_file_measures(
    output_folder = test_dir,
    UniqueBinFileIdentifier = UniqueBinFileIdentifier,
    minimum_valid_hours = 0,
    measure_type = "activity"
  )

  expected_measures_3 <- data.frame(
    Date = "25-Apr-2021",
    DayNumber = 1,
    RecordedDataDay = 112,
    WearDurationDay = 103,
    NonWearDurationDay = 9,
    ActiveDurationDay = 0,
    SleepDurationDay = 64,
    SedentaryDurationDay = 39,
    LightDurationDay = 0,
    ModerateDurationDay = 0,
    VigorousDurationDay = 0,
    MVPADurationDay = 0,
    ActiveVolumeDay = 0,
    ActiveVolumeDayMET = 0,
    ActiveIntensityDay = NA_real_,
    M6MinIntensity = 0.035,
    METMinutesDay = 2,
    TotalStepsDay = 0,
    SlowStepsDay = 0,
    FastStepsDay = 0,
    WalkingDurationDay = 0,
    MeanCadenceDay = NA_real_,
    Cadence95Day = NA_real_,
    RunningStepsDay = 0,
    RunningDurationDay = 0,
    SitStandTransitionsDay = 0,
    NumberBoutsDay = 11,
    Notes = "No active bouts in day."
  )

  test_that("Daily activity measures are aggregated correctly.", {
    expect_equal(actual_measures_3, expected_measures_3)
  })
})


local({
  bouts <- data.frame(
    TimeUTC = c(
      1762035695, 1762035715, 1762037791, 1762037810, 1762041077, 1762041100,
      1762041153, 1762041167, 1762041174, 1762041344, 1762042130, 1762043459,
      1762044929, 1762044938, 1762045050, 1762045123, 1762045645, 1762046282,
      1762046417, 1762046423, 1762046851, 1762046860, 1762046897, 1762047223
    ),
    Duration = c(
      20, 2076, 19, 3267, 23, 53, 14, 7, 170, 786, 1329,
      1470, 9, 112, 73, 522, 637, 135, 6, 428, 9, 37, 326, 2143
    ),
    RestSeconds = c(
      0, 200, 19, 3267, 23, 53, 14, 7, 170, 786, 1329,
      1470, 9, 112, 73, 522, 637, 135, 6, 428, 9, 37, 326, 200
    ),
    NonWear = rep(FALSE, 24),
    Rest = c(F, F, rep(T, 21), F),
    Sleep = c(F, T, F, T, F, F, F, F, F, T, T, T, F, T, F, T, T, F, F, T, F, F, T, T),
    Classification = c(
      "active", "sleep", "sedentary", "sleep", rep("sedentary", 5),
      rep("sleep", 3), "sedentary", "sleep", "sedentary", "sleep",
      "sleep", "active", "active", "sleep", "active", "active", "sleep", "sleep"
    ),
    stringsAsFactors = FALSE
  )

  TEMPMPI <- list()
  TEMPMPI$non_movement$non_wear <- data.frame()
  TEMPMPI$non_movement$rest_intervals <- data.frame(
    day_number = 1,
    start_time = 1762037591,
    duration = 9832
  )
  TEMPMPI$file_data$NumberDays <- 1
  TEMPMPI$file_data$TimeOffset <- 0
  TEMPMPI$file_data$CutTime24Hr <- "15:00"
  TEMPMPI$file_data[["MeasurementStartTimeUTC"]] <- 1762035695
  TEMPMPI$file_data[["MeasurementEndTimeUTC"]] <- 1762049366
  TEMPMPI$file_data[["FirstLocalMidnightTimeUTC"]] <- 1762041600

  test_dir <- file.path(tempdir(), "sleep_test")
  dir.create(test_dir, showWarnings = FALSE)
  UniqueBinFileIdentifier <- "TEST123"
  dir.create(file.path(test_dir, "GENEAbout"))
  saveRDS(TEMPMPI, file.path(test_dir, paste0(UniqueBinFileIdentifier, "_MPI.rds")))
  saveRDS(bouts, file.path(test_dir, "GENEAbout", paste0(UniqueBinFileIdentifier, "_day1_bouts.rds")))

  expected_measures <- data.frame(
    NightofDate = "01-Nov-2025",
    RecordedDataDay = 13671,
    WearDurationDay = 13671,
    NonWearDurationDay = 0,
    RestStartTime = "22:56:31",
    RestEndTime = "01:33:43",
    RestIntervalDuration = 9432,
    SleepOnsetTime = "22:56:50",
    SleepEndTime = "01:33:43",
    SleepIntervalDuration = 9413,
    TotalSleepDuration = 8877,
    SleepEfficiency = 94.1,
    SleepOnsetLatency = 19,
    SleepOffsetDuration = 0,
    WakeAfterSleepOnsetDuration = 536,
    WakeAfterSleepOnsetCount = 5,
    NapsCountDay = 2,
    TotalNapDuration = 4219,
    MidsleepTime = "00:15:16",
    MeanNapDuration = 2110,
    NumberBoutsDay = 24,
    Notes = ""
  )

  actual_measures <- single_file_measures(
    output_folder = test_dir,
    UniqueBinFileIdentifier = UniqueBinFileIdentifier,
    minimum_valid_hours = 0,
    measure_type = "sleep"
  )

  test_that("Daily sleep measures are aggregated correctly.", {
    expect_equal(actual_measures, expected_measures)
  })

  bouts$RestSeconds[c(2, 24)] <- 1500
  bouts$Rest[c(2, 24)] <- T
  TEMPMPI$non_movement$rest_intervals <- data.frame(
    day_number = 1,
    start_time = 1762036291,
    duration = 12432
  )
  saveRDS(TEMPMPI, file.path(test_dir, paste0(UniqueBinFileIdentifier, "_MPI.rds")))
  saveRDS(bouts, file.path(test_dir, "GENEAbout", paste0(UniqueBinFileIdentifier, "_day1_bouts.rds")))

  expected_measures <- data.frame(
    NightofDate = "01-Nov-2025",
    RecordedDataDay = 13671,
    WearDurationDay = 13671,
    NonWearDurationDay = 0,
    RestStartTime = "22:21:55",
    RestEndTime = "02:09:26",
    RestIntervalDuration = 13651,
    SleepOnsetTime = "22:21:55",
    SleepEndTime = "02:09:26",
    SleepIntervalDuration = 13651,
    TotalSleepDuration = 13096,
    SleepEfficiency = 95.9,
    SleepOnsetLatency = 0,
    SleepOffsetDuration = 0,
    WakeAfterSleepOnsetDuration = 555,
    WakeAfterSleepOnsetCount = 6,
    NapsCountDay = 0,
    TotalNapDuration = 0,
    MidsleepTime = "00:15:40",
    MeanNapDuration = 0,
    NumberBoutsDay = 24,
    Notes = ""
  )

  actual_measures <- single_file_measures(
    output_folder = test_dir,
    UniqueBinFileIdentifier = UniqueBinFileIdentifier,
    minimum_valid_hours = 0,
    measure_type = "sleep"
  )

  test_that("Daily sleep measures are aggregated correctly.", {
    expect_equal(actual_measures, expected_measures)
  })
})

Try the GENEAcore package in your browser

Any scripts or data that you put into this service are public.

GENEAcore documentation built on Oct. 22, 2026, 5:09 p.m.