@@ -88,7 +88,6 @@ aggregate_per_date = function(tsDir, epoch, classifier, classes,
8888 dsnames [ci ] = paste(" dur" , " total" , " nighttime" , " min" , sep = " _" )
8989 ci = ci + 1
9090 }
91-
9291 # total minutes in classes
9392 ci2 = ci + length(classes ) - 1
9493 time_in_classes = aggregate(activity ~ date , data = ts , FUN = min_in_class , epoch = epoch )
@@ -105,32 +104,40 @@ aggregate_per_date = function(tsDir, epoch, classifier, classes,
105104 }
106105 if (" nighttime.awake" %in% classes ) {
107106 noons = which(ts $ time == " 12:00:00" )
108- start_end_nighttime = find_start_end(ts , column = " activity" ,
109- class = c(" nighttime.awake" , " nighttime.sleep" ))
110- start_end_nighttime_dates = NULL
111- for (ni in 1 : length(start_end_nighttime $ ends )) {
112- next_noon = which(noons > start_end_nighttime $ ends [ni ])[1 ]
113- if (is.na(next_noon )) {
114- # if there is not a next_noon, meaning that recording finished before 12pm
115- # following the last wake up
116- prev_noon = max(which(noons < start_end_nighttime $ ends [ni ]))
117- start_end_nighttime_dates [ni ] = as.character(as.Date(ts $ date [noons [prev_noon ]]) + 1 )
118- } else {
119- start_end_nighttime_dates [ni ] = ts $ date [noons [next_noon ]]
107+ if (sum(grepl(" nighttime" , ts $ activity )) > 0 ) {
108+ # if sleep periods have been detected...
109+ start_end_nighttime = find_start_end(ts , column = " activity" ,
110+ class = c(" nighttime.awake" , " nighttime.sleep" ))
111+ start_end_nighttime_dates = NULL
112+ for (ni in 1 : length(start_end_nighttime $ ends )) {
113+ next_noon = which(noons > start_end_nighttime $ ends [ni ])[1 ]
114+ if (is.na(next_noon )) {
115+ # if there is not a next_noon, meaning that recording finished before 12pm
116+ # following the last wake up
117+ prev_noon = max(which(noons < start_end_nighttime $ ends [ni ]))
118+ start_end_nighttime_dates [ni ] = as.character(as.Date(ts $ date [noons [prev_noon ]]) + 1 )
119+ } else {
120+ start_end_nighttime_dates [ni ] = ts $ date [noons [next_noon ]]
121+ }
120122 }
123+ # start_end_nighttime_dates = ts$date[start_end_nighttime$ends] # dates based on wakeup
124+ rows2fill = which(availableDates %in% start_end_nighttime_dates )
125+ ds [rows2fill , ci ] = as.character(ts $ timestamp [start_end_nighttime $ starts ])
126+ ds [rows2fill , ci + 1 ] = as.character(ts $ timestamp [start_end_nighttime $ ends ])
127+ dsnames [ci : (ci + 1 )] = paste(" timestamp" , c(" sleepOnset" , " wakeup" ), sep = " _" )
128+ ci = ci + 2
129+ } else {
130+ # sleep periods have not been detected (e.g., participant removed devices all nights)
131+ # only store names to be consistent in columns in the full dataset,
132+ # but leave all data as NA
133+ dsnames [ci : (ci + 1 )] = paste(" timestamp" , c(" sleepOnset" , " wakeup" ), sep = " _" )
134+ ci = ci + 2
121135 }
122- # start_end_nighttime_dates = ts$date[start_end_nighttime$ends] # dates based on wakeup
123- rows2fill = which(availableDates %in% start_end_nighttime_dates )
124- ds [rows2fill , ci ] = as.character(ts $ timestamp [start_end_nighttime $ starts ])
125- ds [rows2fill , ci + 1 ] = as.character(ts $ timestamp [start_end_nighttime $ ends ])
126- dsnames [ci : (ci + 1 )] = paste(" timestamp" , c(" sleepOnset" , " wakeup" ), sep = " _" )
127- ci = ci + 2
128136 }
129-
130137 # bouts of behaviors
131138 boutdur = sort(boutdur , decreasing = TRUE )
132139 for (classi in classes ) {
133- if (grepl(" ^nighttime|^nonwear" , classes [ classi ] )) break
140+ if (grepl(" ^nighttime|^nonwear" , classi )) break
134141 for (boutduri in 1 : length(boutdur )) {
135142 look4bouts = ifelse(ts $ activity == classi , 1 , 0 )
136143 # getBout is a copy of GGIR::g.getbout with which we are experimenting
0 commit comments