@@ -94,117 +94,8 @@ g.part5.analyseRest = function(sibreport = NULL, dsummary = NULL,
94
94
}
95
95
# -----------------------------------------------------------
96
96
# Consider using marker button data to aid nap detection
97
+ sibreport = markerButtonForRest(sibreport , params_sleep , ts )
97
98
98
- # nap_markerbutton_method:
99
- # 0 do not use marker button (default)
100
- # 1 do not require it and copy timing => use marker button to improve definition of nap
101
- # 2 require it and do not copy timing => use marker button as a condition for nap detection
102
- # 3 require it and copy timing => use marker button to improve definition and as a condition for nap detection
103
-
104
- # Explanation objects as used below
105
- # nap_copy_timing_mb: rely on marker for final timing of the nap?
106
- # if TRUE, only consider marker buttons before midtime of sib for start
107
- # and only consider marker buttons after midtime of sib for end
108
- # if FALSE, then consider any mark button, this uses the marker solely as
109
- # a confirmation that nap nearby
110
-
111
- # nap_require_mb: require availability of marker to consider
112
- # detection of a nap
113
- # if TRUE, only keep sibs that had marker button nearby
114
- # if FALSE, also keep sibs that may not have marker button but meet
115
- # criteria for another reason
116
- #
117
- sibreport $ ignore = FALSE # if no marker button than keep sibs
118
- if (params_sleep [[" nap_markerbutton_method" ]] > 0 ) {
119
- if (params_sleep [[" nap_markerbutton_method" ]] > 1 ) {
120
- sibreport $ ignore = TRUE # if no marker button then ignore all sibs
121
- }
122
- # maximum gap allowed between sib edge and marker
123
- nap_mb_max_dist = params_sleep [[" nap_markerbutton_max_distance" ]]
124
-
125
- # If marker button data available
126
- if (" marker" %in% colnames(ts )) {
127
- if (1 %in% ts $ marker ) {
128
- if (params_sleep [[" nap_markerbutton_method" ]] == 1 ) {
129
- nap_require_mb = FALSE
130
- nap_copy_timing_mb = TRUE
131
- } else if (params_sleep [[" nap_markerbutton_method" ]] == 2 ) {
132
- nap_require_mb = TRUE
133
- nap_copy_timing_mb = FALSE
134
- } else if (params_sleep [[" nap_markerbutton_method" ]] == 3 ) {
135
- nap_require_mb = TRUE
136
- nap_copy_timing_mb = TRUE
137
- }
138
- # Calculate time in minutes to nearest button press
139
- # that starts before and after midway the sib, for start and end respectively
140
- sibreport $ midtime = sibreport $ start + (sibreport $ end - sibreport $ start ) / 2
141
- marker_times = ts $ time [which(ts $ marker == 1 )]
142
- sibreport $ start_to_marker = NA
143
- sibreport $ end_to_marker = NA
144
-
145
- for (hi in 1 : nrow(sibreport )) {
146
- # assess duration marker buttons relative to sib midtime
147
- delta_times = as.numeric(sibreport $ midtime [hi ]) - as.numeric(marker_times )
148
- if (nap_copy_timing_mb == TRUE ) {
149
- # Plan is to use marker button timing, so only consider
150
- # for start the marker buttons that start before midtime
151
- end_delta_times = delta_times [which(delta_times < 0 )]
152
- # Plan is to use marker button timing, so only consider
153
- # for end the marker buttons that start after midtime
154
- start_delta_times = delta_times [which(delta_times > 0 )]
155
- } else {
156
- # Plan is NOT to use marker button timing, so any nearby
157
- # marker button is sufficient
158
- start_delta_times = end_delta_times = delta_times
159
- }
160
- nap_start_found = nap_end_found = FALSE
161
- half_sib_dur = sibreport $ duration [hi ] / 2
162
- # find marker for start nap
163
- if (length(start_delta_times ) > 0 ) {
164
- sibreport $ start_to_marker [hi ] = min(abs(start_delta_times )) / 60 - half_sib_dur
165
- if (any(sibreport $ start_to_marker [hi ] < nap_mb_max_dist )) {
166
- # marker button nearby
167
- if (nap_copy_timing_mb == TRUE ) {
168
- # use nearby marker button to overwrite sib start
169
- tmp_start = sibreport $ midtime [hi ] - start_delta_times [which.min(abs(start_delta_times ))]
170
- }
171
- nap_start_found = TRUE
172
- }
173
- }
174
- # find marker for end nap
175
- if (length(end_delta_times ) > 0 ) {
176
- sibreport $ end_to_marker [hi ] = min(abs(end_delta_times )) / 60 - half_sib_dur
177
- if (any(abs(sibreport $ end_to_marker [hi ]) < nap_mb_max_dist )) {
178
- # marker button nearby
179
- if (nap_copy_timing_mb == TRUE ) {
180
- # use nearby marker button to overwrite sib end
181
- tmp_end = sibreport $ midtime [hi ] + abs(end_delta_times [which.min(abs(end_delta_times ))])
182
- }
183
- nap_end_found = TRUE
184
- }
185
- }
186
- if (nap_require_mb == TRUE && (nap_start_found == FALSE || nap_end_found == FALSE )) {
187
- # if nearby marker button is required but none was available
188
- # ignore this sib
189
- sibreport $ ignore [hi ] = TRUE
190
- } else {
191
- # enable nap detection further down becaue marker button was found
192
- sibreport $ ignore [hi ] = FALSE
193
- # overwrite all other sibs that overlap such that they form duplicated rows
194
- sibreport $ start [hi ] = tmp_start
195
- sibreport $ start [which(sibreport $ start < sibreport $ midtime [hi ] & sibreport $ start > = tmp_start )] = sibreport $ start [hi ]
196
- sibreport $ end [hi ] = tmp_end
197
- sibreport $ end [which(sibreport $ end < sibreport $ midtime [hi ] & sibreport $ end > = tmp_end )] = sibreport $ end [hi ]
198
- # recalculate midtime
199
- sibreport $ midtime = sibreport $ start + (sibreport $ end - sibreport $ start ) / 2
200
- }
201
- }
202
- }
203
- }
204
- }
205
- # remove duplicate rows
206
- sibreport = sibreport [! duplicated(sibreport [, c(" start" , " end" )]), ]
207
-
208
99
sibreport $ startHour = as.numeric(format(sibreport $ start , " %H" ))
209
100
sibreport $ endHour = as.numeric(format(sibreport $ end , " %H" ))
210
101
0 commit comments