From 8ec7f44844074cca81644b0db6eb92c4f457f750 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 22 Jan 2025 17:26:29 +0100 Subject: [PATCH 1/5] fixes #1245 --- R/g.part5.wakesleepwindows.R | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/R/g.part5.wakesleepwindows.R b/R/g.part5.wakesleepwindows.R index 09c5abafd..1ffe16f36 100644 --- a/R/g.part5.wakesleepwindows.R +++ b/R/g.part5.wakesleepwindows.R @@ -23,8 +23,9 @@ g.part5.wakesleepwindows = function(ts, part4_output, desiredtz, nightsi, # Round seconds to integer number of epoch lengths (needed if cleaningcode = 5). round_seconds_to_epochSize = function(x, epochSize) { if (length(as.numeric(unlist(strsplit(x,":")))) == 3) { - xPOSIX = as.POSIXct(x, format = "%H:%M:%S") - xPOSIX_rounded = as.POSIXct(round(as.numeric(xPOSIX) / epochSize) * epochSize) + xPOSIX = as.POSIXct(x, format = "%H:%M:%S", origin = "1970-01-01") + xPOSIX_rounded = as.POSIXct(round(as.numeric(xPOSIX) / epochSize) * epochSize, + origin = "1970-01-01") x = format(xPOSIX_rounded, format = "%H:%M:%S") } else { x = "" @@ -67,7 +68,8 @@ g.part5.wakesleepwindows = function(ts, part4_output, desiredtz, nightsi, w1[k] = paste(tt[3],"-", tt[2], "-", tt[1], " ", as.character(defWA_ts), sep = "") # if time is beyond 24 then change the date if (defSO >= 24) { - w0[k] = as.character(as.POSIXlt(w0[k],tz = desiredtz) + (24*3600)) + tmp_w0 = as.POSIXlt(w0[k],tz = desiredtz, origin = "1970-01-01") + w0[k] = as.character(tmp_w0 + (24*3600)) } if (defWA >= 24 | (part4_output$daysleeper[k] == 1 & defWA < 18)) { From cf9b0484d69dc6b75ef6dd5d9b0477aff2fc2d5a Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 22 Jan 2025 17:26:52 +0100 Subject: [PATCH 2/5] fixes #1181 --- R/g.imputeTimegaps.R | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/R/g.imputeTimegaps.R b/R/g.imputeTimegaps.R index 8bf8ac4af..8930910a7 100644 --- a/R/g.imputeTimegaps.R +++ b/R/g.imputeTimegaps.R @@ -124,14 +124,28 @@ g.imputeTimegaps = function(x, sf, k=0.25, impute = TRUE, x$next_epoch_delay = 0 longEpochDayCut = seq(0, 24 * 60^2, by = longEpochSize) x$imputation = 0; imp = 0 # keep track of the imputation to organize data later on + Rversion_lt_420 = as.numeric(R.Version()$major) >= 4 && as.numeric(R.Version()$minor) > 2 for (i in gap90i) { imp = imp + 1 + # In the next bit of code we use data.table to extract hour and minute from timestamps + # however this does not work for older version of R + if (Rversion_lt_420) { + time_i = x$time[i] + time_ip1 = x$time[i + 1] + } else { + # In R 4.2 and early numeric time cannot be provide to data.table + # In GMT because at this point we do not care about the specific time + # because this is removed at the end of the function, we only use it + # to identify time gaps and their size + time_i = as.POSIXct(x$time[i], origin = "1970-01-01", tz = "GMT") + time_ip1 = as.POSIXct(x$time[i + 1], origin = "1970-01-01", tz = "GMT") + } # short epochs to add to fill up to next long epoch cut - seconds = data.table::hour(x$time[i]) * 60^2 + data.table::minute(x$time[i]) * 60 + data.table::second(x$time[i]) + 1 + seconds = data.table::hour(time_i) * 60^2 + data.table::minute(time_i) * 60 + data.table::second(time_i) + 1 seconds_from_prevCut = seconds - max(longEpochDayCut[which(longEpochDayCut <= seconds)]) shortEpochs2add_1 = (longEpochSize - seconds_from_prevCut) / shortEpochSize # short epochs to add after time gap - seconds = data.table::hour(x$time[i + 1]) * 60^2 + data.table::minute(x$time[i + 1]) * 60 + data.table::second(x$time[i + 1]) + 1 + seconds = data.table::hour(time_ip1) * 60^2 + data.table::minute(time_ip1) * 60 + data.table::second(time_ip1) + 1 seconds_from_prevCut = seconds - max(longEpochDayCut[which(longEpochDayCut <= seconds)]) shortEpochs2add_2 = (seconds_from_prevCut - 1) / shortEpochSize # short epochs to add - total From 2d22dd9f8b77ca946cffb6b5f85674c58affe1b6 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 22 Jan 2025 17:38:17 +0100 Subject: [PATCH 3/5] add origin to make GGIR compatible with R 4.2.0, see also #1181 and #1245 --- NEWS.md | 6 +++++- R/g.part6.R | 9 ++++++--- tests/testthat/test_part6.R | 6 +++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1e5f715f7..e5c759b49 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# CHANGES IN GGIR VERSION 3.1-?? +# CHANGES IN GGIR VERSION 3.1-11 - Part 2: @@ -21,6 +21,10 @@ - Added warning when there are two accelerometer recordings with the same ID. - Change extraction of imputation code from sleep diary, which is now assumed to correspond to preceding night. This is now also documented. #1251 + +- Part 1, 5 and 6: + + - Update code to be backward compatible with R 4.2.0 # CHANGES IN GGIR VERSION 3.1-10 diff --git a/R/g.part6.R b/R/g.part6.R index 3023a2c06..76b22ed51 100644 --- a/R/g.part6.R +++ b/R/g.part6.R @@ -169,7 +169,9 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(), mdat$window[invalid] = 9999 mdat$invalid_sleepperiod[invalid] = 100 mdat$invalid_wakinghours[invalid] = 100 - mdat$time = mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]]) + mdat$time = mdat$timestamp = as.POSIXct(mdat$timenum, + tz = params_general[["desiredtz"]], + origin = "1970-01-01") return(mdat) } mdat = imputeTimeGaps(mdat, epochSize) @@ -238,7 +240,8 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(), summary[fi] = unlist(strsplit(fnames.ms5raw[i], "_"))[1] s_names[fi] = "ID" fi = fi + 1 - starttime = as.POSIXlt(ts$time[1], tz = params_general[["desiredtz"]]) + starttime = as.POSIXlt(ts$time[1], tz = params_general[["desiredtz"]], + origin = "1970-01-01") summary[fi] = format(starttime) s_names[fi] = "starttime" fi = fi + 1 @@ -291,7 +294,7 @@ g.part6 = function(datadir = c(), metadatadir = c(), f0 = c(), f1 = c(), threshold = as.numeric(unlist(strsplit( params_phyact[["part6_threshold_combi"]], "_"))[1]) # extract nightsi again - tempp = unclass(as.POSIXlt(acc4cos$time, tz = params_general[["desiredtz"]])) + tempp = unclass(as.POSIXlt(acc4cos$time, tz = params_general[["desiredtz"]], origin = "1970-01-01")) sec = tempp$sec min = tempp$min hour = tempp$hour diff --git a/tests/testthat/test_part6.R b/tests/testthat/test_part6.R index e5d428efc..02ea7a91f 100644 --- a/tests/testthat/test_part6.R +++ b/tests/testthat/test_part6.R @@ -39,15 +39,15 @@ test_that("Part 6 with household co-analysis", { "day_IN_bts_10_30", "day_LIG_bts_10") mdat$timenum = mdat$timenum - (5 * 60) - mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]]) + mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]], origin = "1970-01-01") filename = "800-900-001_left wrist.bin" save(mdat, filename, Lnames, file = paste0(dn, "/800-900-001_left wrist.RData")) mdat$timenum = mdat$timenum + (7 * 60) - mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]]) + mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]], origin = "1970-01-01") filename = "800-900-002_left wrist.bin" save(mdat, filename, Lnames, file = paste0(dn, "/800-900-002_left wrist.RData")) mdat$timenum = mdat$timenum + (14 * 60) - mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]]) + mdat$timestamp = as.POSIXct(mdat$timenum, tz = params_general[["desiredtz"]], origin = "1970-01-01") filename = "800-900-003_left wrist.bin" save(mdat, filename, Lnames, file = paste0(dn, "/800-900-003_left wrist.RData")) mdat_file3 = mdat From 6685a743301bbac1e7433908fa1b08b4053358d6 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 22 Jan 2025 17:40:28 +0100 Subject: [PATCH 4/5] prepare 3.1-11 release --- DESCRIPTION | 4 ++-- man/GGIR-package.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 89742e8da..3194cb898 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: GGIR Type: Package Title: Raw Accelerometer Data Analysis -Version: 3.1-10 -Date: 2025-01-10 +Version: 3.1-11 +Date: 2025-01-23 Authors@R: c(person("Vincent T","van Hees",role=c("aut","cre"), email="v.vanhees@accelting.com"), person("Jairo H","Migueles",role="aut", diff --git a/man/GGIR-package.Rd b/man/GGIR-package.Rd index e959a4682..0f7eb1221 100755 --- a/man/GGIR-package.Rd +++ b/man/GGIR-package.Rd @@ -21,8 +21,8 @@ \tabular{ll}{ Package: \tab GGIR\cr Type: \tab Package\cr - Version: \tab 3.1-10\cr - Date: \tab 2025-01-10\cr + Version: \tab 3.1-11\cr + Date: \tab 2025-01-23\cr License: \tab Apache License (== 2.0)\cr Discussion group: \tab https://groups.google.com/forum/#!forum/rpackageggir\cr } From cf9c3e7c6775db86137b715089d7cf8c4149f4e2 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Wed, 22 Jan 2025 17:55:57 +0100 Subject: [PATCH 5/5] tidy up code comment --- R/g.imputeTimegaps.R | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/R/g.imputeTimegaps.R b/R/g.imputeTimegaps.R index 8930910a7..f15462738 100644 --- a/R/g.imputeTimegaps.R +++ b/R/g.imputeTimegaps.R @@ -127,16 +127,16 @@ g.imputeTimegaps = function(x, sf, k=0.25, impute = TRUE, Rversion_lt_420 = as.numeric(R.Version()$major) >= 4 && as.numeric(R.Version()$minor) > 2 for (i in gap90i) { imp = imp + 1 - # In the next bit of code we use data.table to extract hour and minute from timestamps - # however this does not work for older version of R if (Rversion_lt_420) { time_i = x$time[i] time_ip1 = x$time[i + 1] } else { - # In R 4.2 and early numeric time cannot be provide to data.table - # In GMT because at this point we do not care about the specific time - # because this is removed at the end of the function, we only use it - # to identify time gaps and their size + # Further down data.table is used to extract hour and minute from time stamps + # In R 4.2.0 and earlier numeric time cannot be provided to data.table + # For those older version of R convert timestamp format + # Tz is set to GMT because at this point we do not care about the specific time + # as time is removed at the end of the function, we only use it + # to identify time gaps and their size. time_i = as.POSIXct(x$time[i], origin = "1970-01-01", tz = "GMT") time_ip1 = as.POSIXct(x$time[i + 1], origin = "1970-01-01", tz = "GMT") }