Skip to content

Commit 26f2dcc

Browse files
committed
update version number and minor fixes on warnings and checks for sleep detection
1 parent bf355ea commit 26f2dcc

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: actimetric
22
Type: Package
33
Title: Classifies Accelerometer Data Into Physical Activity Types
4-
Version: 0.1.4.9000
4+
Version: 0.1.4.9002
55
Authors@R: c(person("Jairo H","Migueles", role = c("aut","cre"),
66
email = "[email protected]",
77
comment = c(ORCID = "0000-0003-0366-6935")),

R/aggregate_per_date.R

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

R/runActimetric.R

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,8 @@ runActimetric = function(input_directory = NULL, output_directory = NULL, studyn
133133
if (grepl("thigh", classifier)) body_attachment_site = "thigh"
134134
# check on do.sleep and body attachment site
135135
if (do.sleep == TRUE & body_attachment_site != "wrist") {
136-
warning("Sleep detection only available for wrist attachment site for now.\n
137-
Setting do.sleep to FALSE and n_valid_hours_nighttime = 0")
138-
do.sleep = FALSE
139-
n_valid_hours_nighttime = 0
140-
}
141-
# redefine classes
142-
if (do.sleep == TRUE) {
143-
classes = c(classes, "nighttime", "sleep")
144-
}
145-
if (do.nonwear == TRUE) {
146-
classes = c(classes, "nonwear")
147-
}
148-
# Welcome message
149-
if (verbose) {
150-
cat('\n')
151-
cat(paste0(rep('_', options()$width), collapse = ''))
152-
cat(paste0("\n\nWelcome to the actimetric R package!\n",
153-
"\nIf you experiment any technical issue or wish to contribute to actimetric,",
154-
"\nplease contact Jairo at [email protected] or Matthew at [email protected]\n\n"))
155-
cat(paste0(rep('_', options()$width), collapse = ''))
156-
cat(paste0("\n\nClassifier: ", classifier, "\n\n"))
157-
cat(paste0(rep('_', options()$width), collapse = ''))
158-
cat("\n")
136+
warning(paste("The sleep detection method was developed and validated for wrist-worn devices.",
137+
"Accuracy may be compromised when used with other attachment sites."))
159138
}
160139
# -------------------------------------------------------------------------
161140
# Check directories and list files
@@ -202,6 +181,28 @@ runActimetric = function(input_directory = NULL, output_directory = NULL, studyn
202181
data.table::fwrite(config, file = file.path(output_directory, "configuration.csv"),
203182
na = "", row.names = FALSE)
204183
# -------------------------------------------------------------------------
184+
# redefine classes
185+
if (do.sleep == TRUE) {
186+
classes = c(classes, "nighttime", "sleep")
187+
}
188+
if (do.nonwear == TRUE) {
189+
classes = c(classes, "nonwear")
190+
}
191+
# Welcome message
192+
if (verbose) {
193+
cat('\n')
194+
cat(paste0(rep('_', options()$width), collapse = ''))
195+
cat(paste0("\n\nWelcome to the actimetric R package!",
196+
"\nactimetric version ", actimetricversion,
197+
"\nactimetricModels version ", actimetricModelsversion, "\n",
198+
"\nIf you experiment any technical issue or wish to contribute to actimetric,",
199+
"\nplease contact Jairo at [email protected] or Matthew at [email protected]\n\n"))
200+
cat(paste0(rep('_', options()$width), collapse = ''))
201+
cat(paste0("\n\nClassifier: ", classifier, "\n\n"))
202+
cat(paste0(rep('_', options()$width), collapse = ''))
203+
cat("\n")
204+
}
205+
# -------------------------------------------------------------------------
205206
# Load files...
206207
for (file in files) {
207208
# file format and ID

0 commit comments

Comments
 (0)