From dd59a5760d97ebf446e6827fc16c31e58c4c543f Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Fri, 15 Nov 2024 13:14:24 +0100 Subject: [PATCH] fixes #1225 --- NEWS.md | 4 ++++ R/g.part5.definedays.R | 2 +- R/g.sib.det.R | 11 ++++++++++- tests/testthat/test_chainof5parts.R | 4 ++-- tests/testthat/test_recordingEndSleepHour.R | 8 ++++---- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 03543895f..d99023644 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# CHANGES IN GGIR VERSION 3.1-? + +- Part 3: Improved handling of DST, #1225 + # CHANGES IN GGIR VERSION 3.1-6 - Part 6: diff --git a/R/g.part5.definedays.R b/R/g.part5.definedays.R index 18e4b3492..78ba4383b 100644 --- a/R/g.part5.definedays.R +++ b/R/g.part5.definedays.R @@ -86,7 +86,7 @@ g.part5.definedays = function(nightsi, wi, indjump, nightsi_bu, breaks_i = c() for (bi in 1:length(breaks)) { if (any(grepl(breaks[bi], ts$time[fullQqq]))) { - breaks_i[bi] = fullQqq[grep(breaks[bi], ts$time[fullQqq])] + breaks_i[bi] = fullQqq[grep(breaks[bi], ts$time[fullQqq])][1] } else { breaks_i[bi] = qqq[1] } diff --git a/R/g.sib.det.R b/R/g.sib.det.R index d74d88749..9da6935cb 100644 --- a/R/g.sib.det.R +++ b/R/g.sib.det.R @@ -224,9 +224,18 @@ g.sib.det = function(M, IMP, I, twd = c(-12, 12), qqq1 = 1 # preceding noon (not available in recording) qqq2 = midnightsi[1] + (twd[1] * (3600 / ws3))# first noon in recording } else { - qqq1 = midnightsi[j] + (twd[1] * (3600 / ws3)) #preceding noon + qqq1 = midnightsi[j] + (twd[1] * (3600 / ws3)) + 1 #preceding noon qqq2 = midnightsi[j] + (twd[2] * (3600 / ws3)) #next noon } + # twd assumed 24 hour window, which is not the case for DST + if (qqq2 < length(time)) { + qqq2_hour = as.numeric(format(iso8601chartime2POSIX(time[qqq2], tz = desiredtz), "%H")) + if (qqq2_hour == 11) { + qqq2 = qqq2 + (3600 / ws3) + } else if (qqq2_hour == 13) { + qqq2 = qqq2 - (3600 / ws3) + } + } sptei = sptei + 1 if (qqq2 - qqq1 < 60) next # skip night if it has less than 60 epochs if (qqq2 > length(time)) qqq2 = length(time) diff --git a/tests/testthat/test_chainof5parts.R b/tests/testthat/test_chainof5parts.R index d63f206d7..90fbe1427 100644 --- a/tests/testthat/test_chainof5parts.R +++ b/tests/testthat/test_chainof5parts.R @@ -298,7 +298,7 @@ test_that("chainof5parts", { rn = dir(dirname,full.names = TRUE) load(rn[1]) expect_true(file.exists(vis_sleep_file)) - expect_that(round(nightsummary$SptDuration[1], digits = 4), equals(18.075)) + expect_equal(round(nightsummary$SptDuration[1], digits = 4), 18.0792, tolerance = 0.0005) expect_true(as.logical(nightsummary$acc_available[1])) expect_false(as.logical(nightsummary$sleeplog_used[1])) @@ -386,7 +386,7 @@ test_that("chainof5parts", { load(rn[1]) expect_true(dir.exists(dirname)) expect_that(round(nightsummary$number_sib_wakinghours[1], digits = 4), equals(6)) - expect_that(round(nightsummary$SptDuration[1], digits = 4), equals(13.075)) + expect_equal(round(nightsummary$SptDuration[1], digits = 4), 13.0792) #--------------------- # Part 1 with external function: exampleExtFunction = function(data=c(), parameters=c()) { diff --git a/tests/testthat/test_recordingEndSleepHour.R b/tests/testthat/test_recordingEndSleepHour.R index cf322bd89..c099ae365 100644 --- a/tests/testthat/test_recordingEndSleepHour.R +++ b/tests/testthat/test_recordingEndSleepHour.R @@ -58,8 +58,8 @@ test_that("recordingEndSleepHour works as expected", { expect_equal(nrow(p4full), 3) # Night 3 is not in the part 4 reports expect_equal(sum(p4$sleeponset), 62.77) expect_equal(sum(p4$wakeup), 93.505) - expect_equal(sum(p4$guider_onset), 62.778) - expect_equal(sum(p4$guider_wakeup), 93.511) + expect_equal(sum(p4$guider_onset), 62.775) + expect_equal(sum(p4$guider_wakeup), 93.506) expect_equal(sum(p4$number_sib_sleepperiod), 114) expect_true(all(is.na(p4$longitudinal_axis))) @@ -95,8 +95,8 @@ test_that("recordingEndSleepHour works as expected", { expect_equal(nrow(p4full), 3) # Night 3 is not in the part 4 reports expect_equal(sum(p4$sleeponset), 62.1) expect_equal(sum(p4$wakeup), 81.096) - expect_equal(sum(p4$guider_inbedStart), 53.171) - expect_equal(sum(p4$guider_inbedEnd), 86.853) + expect_equal(sum(p4$guider_inbedStart), 53.17, tolerance = 0.005) + expect_equal(sum(p4$guider_inbedEnd), 86.85, tolerance = 0.005) expect_equal(sum(p4$number_sib_sleepperiod), 24) expect_equal(sum(p4$sleepefficiency), 0.501) expect_equal(sum(p4$longitudinal_axis), 9)