Skip to content

Commit

Permalink
reduce code duplication #68
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Sep 26, 2024
1 parent 5149a61 commit 9e2ea7c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
14 changes: 3 additions & 11 deletions R/readActiGraphCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))) {
Expand All @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions R/readActiwatchCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion R/utils_for_countdata.R
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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)
}

0 comments on commit 9e2ea7c

Please sign in to comment.