Skip to content

Commit

Permalink
adding step count to part 5 time series output to facilitate #1173 and
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Jul 30, 2024
1 parent 1ca4c0b commit 960d6a6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
34 changes: 23 additions & 11 deletions R/g.part5.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,21 @@ g.part5 = function(datadir = c(), metadatadir = c(), f0=c(), f1=c(),
# only include angle if angle is present
angleColName = grep(pattern = "angle", x = names(ts), value = TRUE)
if (lightpeak_available == TRUE) {
ts = aggregate(ts[, c("ACC","sibdetection", "diur", "nonwear", angleColName, "lightpeak", "lightpeak_imputationcode")],
by = list(ts$time_num), FUN = function(x) mean(x))
light_columns = c("lightpeak", "lightpeak_imputationcode")
} else {
ts = aggregate(ts[,c("ACC","sibdetection", "diur", "nonwear", angleColName)],
by = list(ts$time_num), FUN = function(x) mean(x))
light_columns = NULL
}

stepcount_available = ifelse("step_count" %in% names(ts), yes = TRUE, no = FALSE)

if (stepcount_available) {
step_count_tmp = aggregate(ts$step_count, by = list(ts$time_num), FUN = function(x) sum(x))
colnames(step_count_tmp)[2] = "step_count"
}
ts = aggregate(ts[,c("ACC","sibdetection", "diur", "nonwear", angleColName, light_columns)],
by = list(ts$time_num), FUN = function(x) mean(x))
if (stepcount_available) {
ts = merge(x = ts, y = step_count_tmp, by = "Group.1")
}
ts$sibdetection = round(ts$sibdetection)
ts$diur = round(ts$diur)
Expand Down Expand Up @@ -573,12 +583,10 @@ g.part5 = function(datadir = c(), metadatadir = c(), f0=c(), f1=c(),
}
if (params_output[["save_ms5rawlevels"]] == TRUE || params_247[["part6HCA"]] == TRUE || params_247[["part6CR"]] == TRUE) {
legendfile = paste0(metadatadir,ms5.outraw,"/behavioralcodes",as.Date(Sys.time()),".csv")
if (file.exists(legendfile) == FALSE) {
legendtable = data.frame(class_name = Lnames, class_id = 0:(length(Lnames) - 1), stringsAsFactors = FALSE)
data.table::fwrite(legendtable, file = legendfile, row.names = FALSE,
sep = params_output[["sep_reports"]],
dec = params_output[["dec_reports"]])
}
legendtable = data.frame(class_name = Lnames, class_id = 0:(length(Lnames) - 1), stringsAsFactors = FALSE)
data.table::fwrite(legendtable, file = legendfile, row.names = FALSE,
sep = params_output[["sep_reports"]],
dec = params_output[["dec_reports"]])
# I moved this bit of code to the end, because we want guider to be included (VvH April 2020)
rawlevels_fname = paste0(metadatadir, ms5.outraw, "/", TRLi, "_", TRMi, "_", TRVi, "/",
gsub(pattern = "[.]|rdata|csv|cwa|gt3x|bin",
Expand Down Expand Up @@ -609,10 +617,14 @@ g.part5 = function(datadir = c(), metadatadir = c(), f0=c(), f1=c(),
if (length(temperature_col) == 0) {
temperature_col = NULL
}
step_count_col = grep(pattern = "step_count", x = names(ts), value = TRUE)
if (length(step_count_col) == 0) {
step_count_col = NULL
}
g.part5.savetimeseries(ts = ts[, c("time", "ACC", "diur", "nonwear",
"guider", "window", "sibdetection", napNonwear_col,
lightpeak_col, selfreported_col,
angle_col, temperature_col)],
angle_col, temperature_col, step_count_col)],
LEVELS = LEVELS,
desiredtz = params_general[["desiredtz"]],
rawlevels_fname = rawlevels_fname,
Expand Down
5 changes: 5 additions & 0 deletions R/g.part5_initialise_ts.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ g.part5_initialise_ts = function(IMP, M, params_247, params_general, longitudina
ts = data.frame(time = IMP$metashort[,1], ACC = IMP$metashort[,params_general[["acc.metric"]]] * scale,
guider = rep("unknown", nrow(IMP$metashort)))
}
if ("step_count" %in% colnames(IMP$metashort)) {
ts$step_count = 0
ts$step_count = IMP$metashort$step_count
}

Nts = nrow(ts)
# add non-wear column
nonwear = IMP$rout[,5]
Expand Down

0 comments on commit 960d6a6

Please sign in to comment.