Skip to content

Commit

Permalink
Merge branch 'master' into issue1212_modify_nonwear_window
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Oct 24, 2024
2 parents a9a1b50 + 03d89f6 commit cf32bcd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
13 changes: 9 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# CHANGES IN GGIR VERSION 3.1-?
# CHANGES IN GGIR VERSION 3.1-5

- Part 5: Fixed minor bug in g.part5.addfirstwake causing the first wake is not correctly added when
no SIBs are detected from the beginning of the recording until the first detected night. #1198
- Part 5:

- Part 5: Add parameters require_complete_lastnight_part5 to control whether last window is included if last night is incomplete. #1196
- Add parameters require_complete_lastnight_part5 to control whether last window is included if last night is incomplete. #1196

- Adjust sleeplog times to recording length in part 5 when classifying a night that was not detected in part 4. #1206

- Fixed minor bug in g.part5.addfirstwake causing the first wake is not correctly added when no SIBs are detected from the beginning of the recording until the first detected night. #1198

- Revise MM window definition in daylight saving time days, as it assumed fixed day duration of 24 hours #1211

- General: GGIR version look-up in .onattach() no longer crashes when computer is offline, fixes #1203.

Expand Down
30 changes: 11 additions & 19 deletions R/g.part5.definedays.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,14 @@ g.part5.definedays = function(nightsi, wi, indjump, nightsi_bu,
# Check that it is possible to find both windows (WW and MM)
# in the data for this day.
if (timewindowi == "MM") {
NepochPerDay = ((24*3600) / epochSize)
# offset from prev midnight
t0 = format(ts$time[1], "%H:%M:%S")
hms = as.numeric(unlist(strsplit(t0, ":")))
NepochFromPrevMidnight = (hms[1]*60*60 + hms[2]*60 + hms[3]) / epochSize
NepochFromDayborder2Midnight = (-dayborder*60*60) / epochSize
NepochFromPrevNight = NepochFromPrevMidnight + NepochFromDayborder2Midnight
if (NepochFromPrevNight < 0) {
NepochFromPrevNight = NepochPerDay + NepochFromPrevNight
}
if (wi == 1) {
qqq[1] = 1
qqq[2] = NepochPerDay - NepochFromPrevNight
} else {
qqq[1] = ((wi - 1) * NepochPerDay) - NepochFromPrevNight + 1
qqq[2] = (wi * NepochPerDay) - NepochFromPrevNight
}
# include first and last partial days in MM
if (nightsi[1] > 1) nightsi = c(1, nightsi)
if (nightsi[length(nightsi)] < nrow(ts)) nightsi = c(nightsi, nrow(ts))
# define window
qqq[1] = nightsi[wi]
qqq[2] = nightsi[wi + 1] - 1
# is this the last day?
if (qqq[2] >= Nts) {
if (qqq[2] >= Nts - 1) {
qqq[2] = Nts
lastDay = TRUE
}
Expand Down Expand Up @@ -88,7 +77,10 @@ g.part5.definedays = function(nightsi, wi, indjump, nightsi_bu,
breaks = qwindow2timestamp(qwindow, epochSize)
if (24 %in% qwindow) {
# 24:00:00: probably does not exist, replace by last timestamp in a day
latest_time_in_day = max(format(ts$time[1:pmin(Nts, NepochPerDay)], format = "%H:%M:%S"))
# here, we consider N epochs per day plus 1 hour just in case we are deriving this in
# a 25-hour daylight saving time day
NepochPerDayPlusOneHr = ((25*3600) / epochSize)
latest_time_in_day = max(format(ts$time[1:pmin(Nts, NepochPerDayPlusOneHr)], format = "%H:%M:%S"))
breaks = gsub(pattern = "24:00:00", replacement = latest_time_in_day, x = breaks)
}
breaks_i = c()
Expand Down
28 changes: 24 additions & 4 deletions R/g.part5.wakesleepwindows.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,24 @@ g.part5.wakesleepwindows = function(ts, part4_output, desiredtz, nightsi,
s1 = findIndex(timeChar, wc = format(as.POSIXlt(w1[k], tz = desiredtz)))

if (is.na(s0) == TRUE) {
s0 = 1
if (format(as.POSIXlt(w0[k], tz = desiredtz)) < timeChar[1]) {
# safe check, only turn s0 to 1 when the part4 wake up is before the first
# timestamp. Otherwise, if wake up is after the last timestamp, then the
# whole time series is set to sleep period time (only occurs in nights with
# cleaningcode = 5 in part 4 (no accelerometer data available, but spt extracted from sleeplog))
s0 = 1
}
}
if (is.na(s1) == TRUE) {
# might still be NA if the timestamps is not in ts (expanded time from expand_tail)
# if so, we assume the participant is sleeping at the end of the recording, this night will be disregarded later on
s1 = nrow(ts)
if (format(as.POSIXlt(w1[k], tz = desiredtz)) > timeChar[length(timeChar)]) {
# safe check, only turn s1 to nrow(ts) when the part4 wake up is after the last
# timestamp. Otherwise, if sleep onset is before the first timestamp, then the
# whole time series is set to sleep period time (only occurs in nights with
# cleaningcode = 5 in part 4 (no accelerometer data available, but spt extracted from sleeplog))
s1 = nrow(ts)
}
}

if (length(s1) != 0 & length(s0) != 0 & is.na(s0) == FALSE & is.na(s1) == FALSE && length(nightsi) > 0) {
Expand All @@ -95,7 +107,6 @@ g.part5.wakesleepwindows = function(ts, part4_output, desiredtz, nightsi,
if (noon1 > Nts) noon1 = Nts
nonwearpercentage = mean(ts$nonwear[noon0:noon1])
if ((length(sleeplog) > 0 & (nonwearpercentage > 0.33) | part4_output$sleeponset_ts[k] == "")) { # added condition to detect nights that are not detected in part 4

# If non-wear is high for this day and if sleeplog is available
sleeplogonset = sleeplog$sleeponset[which(sleeplog$ID == ID & sleeplog$night == part4_output$night[k])]
sleeplogwake = sleeplog$sleepwake[which(sleeplog$ID == ID & sleeplog$night == part4_output$night[k])]
Expand Down Expand Up @@ -124,10 +135,19 @@ g.part5.wakesleepwindows = function(ts, part4_output, desiredtz, nightsi,
s0 = 1
}
s1 = closestmidnight + round(sleeplogwake_hr * Nepochsinhour)
# if sleeplog indicates a time after the ending of the recording,
# then set the nrow(ts) + 1, so that the next line will set as
# SPT all the time from sleep onset until the end of the recording.
if (s1 > nrow(ts) + 1) s1 = nrow(ts) + 1
}
}
}
ts$diur[s0:(s1 - 1)] = 1
# it might also be that both the sleeplog reported sleeponset and
# wakeup goes beyond recording length, in that case, s1 < s0,
# so only assign SPT if s0 < s1
if (s0 < s1) {
ts$diur[s0:(s1 - 1)] = 1
}
}
}
return(ts)
Expand Down

0 comments on commit cf32bcd

Please sign in to comment.