paulhibbing / AGread

Read Accelerometer Files from ActiGraph Accelerometers
Other
15 stars 4 forks source link

ENH: Option for idle sleep imputation #22

Closed muschellij2 closed 4 years ago

muschellij2 commented 4 years ago

Would it be possible to either

  1. Have an option for the value to put for idle sleep mode, such as NA or 0? OR
  2. Have a flag to indicate that the device was in idle sleep?
paulhibbing commented 4 years ago

Option 2 would be much more straightforward, ideally as a follow-on to read_gt3x, or possibly as a new argument (flag_sleep = FALSE by default). Something like this:

flag_sleep <- function(RAW, EVENT) {

# Check that RAW and EVENT inherit from RAW and EVENT classes, respectively
# Initialize RAW$sleep <- FALSE
# If EVENT$idle_sleep_events has nrow == 0, return RAW
# Make lubridate intervals out of EVENT$idle_sleep_events[ ,c("sleep_ON", "sleep_OFF")]
# is_sleep <- sapply(RAW$Timestamp, function(x) any(lubridate::%within%(x, EVENT$idle_sleep_events$intervals))
# RAW$sleep <- ifelse(is_sleep, TRUE, FALSE)
# return RAW

}
muschellij2 commented 4 years ago

I agree - like it - may call it something other than sleep.

What about

#' @importFrom lubridate %within%
flag_sleep <- function(RAW, EVENT) {
  if (is.null(RAW)) {
    return(NULL)
  }
  if (is.null(EVENT)) {
    stop("need to read in EVENT data as well as ACTIVITY")
  }
  if (NROW(EVENT$idle_sleep_events) == 0) {
    return(RAW)
  }
  sleep_events = EVENT$idle_sleep_events[ ,c("sleep_ON", "sleep_OFF")]
  intervals = mapply(interval, 
                     sleep_events$sleep_ON,  
                     sleep_events$sleep_OFF,
                     SIMPLIFY = FALSE) 
  RAW$idle = RAW$Timestamp %within% intervals
  return(RAW)
}
paulhibbing commented 4 years ago

Concept looks good to me. A few considerations:

RAW$idle <- sapply(RAW$Timestamp, function(x) any(x %within% intervals))

Can you test this out and open a PR?