Skip to content

Commit

Permalink
Merge pull request #1207 from wadpac/issue1206_sptPart5
Browse files Browse the repository at this point in the history
Issue1206 spt part5
  • Loading branch information
vincentvanhees authored Oct 23, 2024
2 parents 0e98108 + ea94bad commit a1d6aa1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
12 changes: 8 additions & 4 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# 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 not correctly being added when
no SIBs are detected from the beginning of the recording until the first detected night. #1198

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

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 a1d6aa1

Please sign in to comment.