From 9e2ea7ce77659e7c57c3252cb257f8e39183be8e Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Thu, 26 Sep 2024 17:08:09 +0200 Subject: [PATCH] reduce code duplication #68 --- R/readActiGraphCount.R | 14 +++----------- R/readActiwatchCount.R | 9 +-------- R/utils_for_countdata.R | 21 ++++++++++++++++++++- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/R/readActiGraphCount.R b/R/readActiGraphCount.R index 20060f4..1455c73 100644 --- a/R/readActiGraphCount.R +++ b/R/readActiGraphCount.R @@ -107,7 +107,7 @@ readActiGraphCount = function(filename = file, desiredEpochSize = NULL, keep = c(acccol, vmcol)[!is.na(c(acccol, vmcol))] D = D[, keep, drop = FALSE] if (ncol(D) == 3 & is.na(vmcol)) { - D$vm = sqrt(D[,1]^2 + D[,2]^2 + D[,3]^2) + D$vm = sqrt(D[, 1] ^ 2 + D[, 2] ^ 2 + D[, 3] ^ 2) } # Extract information from header, if present if (headerAvailable == TRUE) { @@ -129,12 +129,7 @@ readActiGraphCount = function(filename = file, desiredEpochSize = NULL, if (colnames == TRUE) { datecol = grep("date", colnames(tmp), ignore.case = TRUE) timecol = grep("time|epoch", colnames(tmp), ignore.case = TRUE) - time = tmp[, timecol] - date = tmp[, datecol] - - starttime = time[1] - starttime = date[1] - timestamp = paste0(date, " ", time) + timestamp = paste0(tmp[, datecol], " ", tmp[1, timecol]) format = timeformat timestamp_POSIX = as.POSIXlt(timestamp, tz = tz, format = format) if (all(is.na(timestamp_POSIX))) { @@ -159,10 +154,7 @@ readActiGraphCount = function(filename = file, desiredEpochSize = NULL, if (!is.null(desiredEpochSize)) { if (desiredEpochSize > epSizeShort) { step = desiredEpochSize %/% epSizeShort - D = rbind(rep(0, ncol(D)), D) - D = apply(D, 2, cumsum) - D = D[seq(1, nrow(D), by = step), , drop = FALSE] - D = apply(D, 2, diff) + D = sumAggregate(D, step) epSizeShort = epSizeShort * step } checkEpochMatch(desiredEpochSize, epSizeShort) diff --git a/R/readActiwatchCount.R b/R/readActiwatchCount.R index 0a351bf..e5e8416 100644 --- a/R/readActiwatchCount.R +++ b/R/readActiwatchCount.R @@ -111,14 +111,7 @@ readActiwatchCount = function(filename = file, desiredEpochSize = NULL, if (!is.null(desiredEpochSize)) { if (desiredEpochSize > epSizeShort) { step = desiredEpochSize %/% epSizeShort - D = rbind(rep(0, ncol(D)), D) - cumsum2 = function(x) { - x = cumsum(ifelse(is.na(x), 0, x)) + x*0 - return(x) - } - D = apply(D, 2, cumsum2) - D = D[seq(1, nrow(D), by = step), , drop = FALSE] - D = apply(D, 2, diff) + D = sumAggregate(D, step) epSizeShort = epSizeShort * step } checkEpochMatch(desiredEpochSize, epSizeShort) diff --git a/R/utils_for_countdata.R b/R/utils_for_countdata.R index 34ce7ce..49a778a 100644 --- a/R/utils_for_countdata.R +++ b/R/utils_for_countdata.R @@ -1,6 +1,10 @@ -# Collection of short function used in functions readActigraphCount, readActiwatchCount +# Collection of short function used in functions +# - readActigraphCount +# - readActiwatchCount +#----------------------------------------------------------------------------------------- checkTimeFormat = function(timestamp_POSIX, rawValue = " ?? ", timeformat = " ?? ", timeformatName = NULL) { + # If timestamp_POSIX is NA gieve error message to inform user that something went wrong. if (is.na(timestamp_POSIX)) { stop(paste0("\nTime format in data ", rawValue, " does not match with time format ", timeformat, @@ -10,6 +14,7 @@ checkTimeFormat = function(timestamp_POSIX, rawValue = " ?? ", timeformat = " ?? } checkEpochMatch = function(desiredEpochSize, epSizeShort) { + # Check whether desired and derived epoch size match if (!is.null(desiredEpochSize) && epSizeShort != desiredEpochSize) { stop(paste0("\nThe short epoch size as specified by the user (", desiredEpochSize, " seconds) does NOT match the short", @@ -39,7 +44,21 @@ detectQuote = function(fn, index) { } getExtension <- function(filename){ + # Extract file extension ex <- unlist(strsplit(basename(filename), split = "[.]")) if (length(ex) < 2) stop(paste0("Cannot recognise extension from '", filename, "' as filename, please check"), call. = FALSE) return(ex[-1]) +} + +sumAggregate = function(mat, step) { + # Aggregate matrix mat by taking the sum over step number of rows + mat = rbind(rep(0, ncol(mat)), mat) + cumsum2 = function(x) { + x = cumsum(ifelse(is.na(x), 0, x)) + x*0 + return(x) + } + mat = apply(mat, 2, cumsum2) + mat = mat[seq(1, nrow(mat), by = step), , drop = FALSE] + mat = apply(mat, 2, diff) + return(mat) } \ No newline at end of file