Skip to content

Commit

Permalink
remove count aggregation (will do it in GGIR) and by that simplify re…
Browse files Browse the repository at this point in the history
…ad functionality #68,
  • Loading branch information
vincentvanhees committed Sep 28, 2024
1 parent 207d712 commit 32f4460
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 157 deletions.
15 changes: 2 additions & 13 deletions R/readActiGraphCount.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
readActiGraphCount = function(filename = NULL, desiredEpochSize = NULL,
readActiGraphCount = function(filename = NULL,
timeformat = "%m/%d/%Y %H:%M:%S", tz = "", timeformatName = "timeformat") {
# In GGIR set timeformatName to extEpochData_timeformat
deviceSerialNumber = NULL
Expand Down Expand Up @@ -168,18 +168,7 @@ readActiGraphCount = function(filename = NULL, desiredEpochSize = NULL,
checkTimeFormat(timestamp_POSIX = timestamp_POSIX, rawValue = timestamp[1],
timeformat = timeformat,
timeformatName = timeformatName)


# If requested, aggregate data to lower resolution to match desired
# epoch size in argument windowsizes
if (!is.null(desiredEpochSize)) {
if (desiredEpochSize > epSizeShort) {
step = desiredEpochSize %/% epSizeShort
D = matAggregate(D, step)
epSizeShort = epSizeShort * step
}
checkEpochMatch(desiredEpochSize, epSizeShort)
}

invisible(list(data = D, epochSize = epSizeShort,
startTime = timestamp_POSIX,
deviceSerialNumber = deviceSerialNumber))
Expand Down
13 changes: 2 additions & 11 deletions R/readActicalCount.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
readActicalCount = function(filename = NULL, desiredEpochSize = NULL,
readActicalCount = function(filename = NULL,
timeformat = "%m/%d/%Y %H:%M:%S", tz = "",
timeformatName = "timeformat") {
# In GGIR set timeformatName to extEpochData_timeformat
Expand Down Expand Up @@ -51,16 +51,7 @@ readActicalCount = function(filename = NULL, desiredEpochSize = NULL,
D = D[, -which(colnames(D) %in% c("date", "time"))]
D = as.matrix(D, drop = FALSE)
if (quote == "") D = apply(D, 2, as.numeric)
# If requested, aggregate data to lower resolution to match desired
# epoch size in argument windowsizes
if (!is.null(desiredEpochSize)) {
if (desiredEpochSize > epSizeShort) {
step = desiredEpochSize %/% epSizeShort
D = matAggregate(D, step)
epSizeShort = epSizeShort * step
}
checkEpochMatch(desiredEpochSize, epSizeShort)
}

invisible(list(data = D, epochSize = epSizeShort,
startTime = timestamp_POSIX))
}
14 changes: 2 additions & 12 deletions R/readActiwatchCount.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
readActiwatchCount = function(filename = NULL, desiredEpochSize = NULL,
readActiwatchCount = function(filename = NULL,
timeformat = "%m/%d/%Y %H:%M:%S", tz = "",
timeformatName = "timeformat") {
# In GGIR set timeformatName to extEpochData_timeformat
Expand Down Expand Up @@ -86,17 +86,7 @@ readActiwatchCount = function(filename = NULL, desiredEpochSize = NULL,

}
D = as.matrix(D, drop = FALSE)
if (quote == "") D = apply(D, 2, as.numeric)
# If requested, aggregate data to lower resolution to match desired
# epoch size in argument windowsizes
if (!is.null(desiredEpochSize)) {
if (desiredEpochSize > epSizeShort) {
step = desiredEpochSize %/% epSizeShort
D = matAggregate(D, step)
epSizeShort = epSizeShort * step
}
checkEpochMatch(desiredEpochSize, epSizeShort)
}
if (quote == "") D = apply(D, 2, as.numeric)
invisible(list(data = D, epochSize = epSizeShort,
startTime = timestamp_POSIX))
}
30 changes: 1 addition & 29 deletions R/utils_for_countdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ 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",
" epoch size we see in the data (", epSizeShort,
" seconds). Please correct."), call. = FALSE)
}
return()
}

detectQuote = function(fn, index) {
# data.table::fread has argument quote.
# On some computers the quotes in the files are
Expand All @@ -62,24 +51,7 @@ getExtension <- function(filename){
return(ex[-1])
}

matAggregate = function(mat, step) {
# Aggregate matrix mat by taking over step number of rows
# as sum unless column names is sleep or nonwear in that case
# we take the rounded mean.
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)
# Correct non incremental variables
for (niv in c("sleep", "nonwear")) {
if (niv %in% colnames(D)) D[, niv] = round(D[, niv] / step)
}
return(mat)
}


findStartData = function(filename, quote, startindex) {
# Function used to find start of time series in Actiwatch and Actical data
Expand Down
6 changes: 1 addition & 5 deletions man/readActiGraphCount.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
Reads ActiGraph Count data file. Currently a variety of csv format are facilitated.
}
\usage{
readActiGraphCount(filename = NULL, desiredEpochSize = NULL,
readActiGraphCount(filename = NULL,
timeformat = "\%m/\%d/\%Y \%H:\%M:\%S", tz = "",
timeformatName = "timeformat")
}
\arguments{
\item{filename}{
filename (required)
}
\item{desiredEpochSize}{
Numeric, desired epoch size in seconds. If set aggregate data by summation to
this epochsize.
}
\item{timeformat}{
Character, timestemp format.
}
Expand Down
6 changes: 1 addition & 5 deletions man/readActicalCount.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
Reads Actical Count data file.
}
\usage{
readActicalCount(filename = NULL, desiredEpochSize = NULL,
readActicalCount(filename = NULL,
timeformat = "\%m/\%d/\%Y \%H:\%M:\%S", tz = "",
timeformatName = "timeformat")
}
\arguments{
\item{filename}{
filename (required)
}
\item{desiredEpochSize}{
Numeric, desired epoch size in seconds. If set aggregate data by summation to
this epochsize.
}
\item{timeformat}{
Character, timestemp format.
}
Expand Down
6 changes: 1 addition & 5 deletions man/readActiwatchCount.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
Reads Actiwatch Count data file. Currently a variety of csv format are facilitated.
}
\usage{
readActiwatchCount(filename = NULL, desiredEpochSize = NULL,
readActiwatchCount(filename = NULL,
timeformat = "\%m/\%d/\%Y \%H:\%M:\%S", tz = "",
timeformatName = "timeformat")
}
\arguments{
\item{filename}{
filename (required)
}
\item{desiredEpochSize}{
Numeric, desired epoch size in seconds. If set aggregate data by summation to
this epochsize.
}
\item{timeformat}{
Character, timestemp format.
}
Expand Down
39 changes: 3 additions & 36 deletions tests/testthat/test_readActiGraphCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ library(GGIRread)
context("read ActiGraph csv files")
test_that("ActiGraph61 is correctly read", {
file = system.file("testfiles/ActiGraph61.csv", package = "GGIRread")
D = readActiGraphCount(filename = file, desiredEpochSize = 10, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "MOS2D16160581")
expect_equal(D$epochSize, 10)
expect_equal(format(D$startTime), "2016-08-15 21:35:00")
expect_equal(nrow(D$data), 495)
expect_equal(ncol(D$data), 5)
expect_equal(sum(D$data[, c("y", "x", "z", "vm")]), 63952.33)
expect_equal(sum(D$data[, c("steps")]), 253)

D = readActiGraphCount(filename = file, desiredEpochSize = 5, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
D = readActiGraphCount(filename = file, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "MOS2D16160581")
expect_equal(D$epochSize, 5)
expect_equal(format(D$startTime), "2016-08-15 21:35:00")
Expand All @@ -23,55 +14,31 @@ test_that("ActiGraph61 is correctly read", {

test_that("ActiGraph31 is correctly read", {
file = system.file("testfiles/ActiGraph13.csv", package = "GGIRread")
D = readActiGraphCount(filename = file, desiredEpochSize = 15, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
D = readActiGraphCount(filename = file, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "CLE2A2123456")
expect_equal(D$epochSize, 15)
expect_equal(format(D$startTime), "2013-08-26 09:00:00")
expect_equal(nrow(D$data), 990)
expect_equal(ncol(D$data), 5)
expect_equal(sum(D$data[, c("y", "x", "z", "vm")]), 272870.6, tol = 0.1)
expect_equal(sum(D$data[, c("steps")]), 1118)

D = readActiGraphCount(filename = file, desiredEpochSize = 30, timeformat = "%m/%d/%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "CLE2A2123456")
expect_equal(D$epochSize, 30)
expect_equal(format(D$startTime), "2013-08-26 09:00:00")
expect_equal(nrow(D$data), 495)
expect_equal(ncol(D$data), 5)
expect_equal(sum(D$data[, c("y", "x", "z", "vm")]), 272870.6, tol = 0.1)
expect_equal(sum(D$data[, c("steps")]), 1118)

})

test_that("ActiGraph13_timestamps_headers.csv is correctly read", {
file = system.file("testfiles/ActiGraph13_timestamps_headers.csv", package = "GGIRread")
D = readActiGraphCount(filename = file, desiredEpochSize = 1, timeformat = "%d-%m-%Y %H:%M:%S", tz = "")
D = readActiGraphCount(filename = file, timeformat = "%d-%m-%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "TAS1D48140206")
expect_equal(D$epochSize, 1)
expect_equal(format(D$startTime), "2017-12-09 15:00:00")
expect_equal(nrow(D$data), 1000)
expect_equal(ncol(D$data), 5)
expect_equal(sum(D$data[, c("y", "x", "z", "vm")]), 255707.4, tol = 0.1)
expect_equal(sum(D$data[, c("steps")]), 442)

D = readActiGraphCount(filename = file, desiredEpochSize = 5, timeformat = "%d-%m-%Y %H:%M:%S", tz = "")
expect_equal(D$deviceSerialNumber, "TAS1D48140206")
expect_equal(D$epochSize, 5)
expect_equal(format(D$startTime), "2017-12-09 15:00:00")
expect_equal(nrow(D$data), 200)
expect_equal(ncol(D$data), 5)
expect_equal(sum(D$data[, c("y", "x", "z", "vm")]), 255707.4, tol = 0.1)
expect_equal(sum(D$data[, c("steps")]), 442)
})

test_that("Actiwatch csv error correctly", {
file = system.file("testfiles/ActiGraph13_timestamps_headers.csv", package = "GGIRread")
expect_error(readActiGraphCount(filename = file,
desiredEpochSize = 5,
timeformat = "%m/%d/%Y %H:%M:%S"),
regexp = "Time format*")
expect_error(readActiGraphCount(filename = file,
desiredEpochSize = 0.5,
timeformat = "%d-%m-%Y %H:%M:%S"),
regexp = "The short*")
})
15 changes: 1 addition & 14 deletions tests/testthat/test_readActicalCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@ library(GGIRread)
context("read Actical files")
test_that("Actical csv is correctly read", {
file = system.file("testfiles/Actical.csv", package = "GGIRread")
D = readActicalCount(filename = file, desiredEpochSize = 60, timeformat = "%d-%b-%y %H:%M", tz = "")
D = readActicalCount(filename = file, timeformat = "%d-%b-%y %H:%M", tz = "")
expect_equal(D$epochSize, 60)
expect_equal(format(D$startTime), "2021-05-13")
expect_equal(nrow(D$data), 501)
expect_equal(ncol(D$data), 2)
expect_equal(sum(D$data, na.rm = TRUE), 8436)

D = readActicalCount(filename = file, desiredEpochSize = 120, timeformat = "%d-%b-%y %H:%M", tz = "")
expect_equal(D$epochSize, 120)
expect_equal(format(D$startTime), "2021-05-13")
expect_equal(nrow(D$data), 250)
expect_equal(ncol(D$data), 2)
expect_equal(sum(D$data, na.rm = TRUE), 8436)
})

test_that("Actical csv error correctly", {
file = system.file("testfiles/Actical.csv", package = "GGIRread")
expect_error(readActicalCount(filename = file,
desiredEpochSize = 60,
timeformat = "%d/%m/%Y %H:%M", tz = ""),
regexp = "Time format*")

expect_error(readActicalCount(filename = file,
desiredEpochSize = 5,
timeformat = "%d-%b-%y %H:%M", tz = ""),
regexp = "The short*")
})
30 changes: 3 additions & 27 deletions tests/testthat/test_readActiwatchCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,39 @@ library(GGIRread)
context("read Actiwatch files")
test_that("Actiwatch csv is correctly read", {
file = system.file("testfiles/Actiwatch.csv", package = "GGIRread")
D = readActiwatchCount(filename = file, desiredEpochSize = 15, timeformat = "%d/%m/%Y %H:%M:%S", tz = "")
D = readActiwatchCount(filename = file, timeformat = "%d/%m/%Y %H:%M:%S", tz = "")
expect_equal(D$epochSize, 15)
expect_equal(format(D$startTime), "2019-11-23 06:00:00")
expect_equal(nrow(D$data), 860)
expect_equal(ncol(D$data), 3)
expect_equal(sum(D$data[, "counts"], na.rm = TRUE), 4589)
expect_equal(sum(D$data[, "sleep"], na.rm = TRUE), 55)
expect_equal(sum(D$data[, "nonwear"], na.rm = TRUE), 797)

D = readActiwatchCount(filename = file, desiredEpochSize = 30, timeformat = "%d/%m/%Y %H:%M:%S", tz = "")
expect_equal(D$epochSize, 30)
expect_equal(format(D$startTime), "2019-11-23 06:00:00")
expect_equal(nrow(D$data), 430)
expect_equal(ncol(D$data), 3)
expect_equal(sum(D$data[, "counts"], na.rm = TRUE), 4569)
expect_equal(sum(D$data[, "sleep"], na.rm = TRUE), 54)
expect_equal(sum(D$data[, "nonwear"], na.rm = TRUE), 797)
})
test_that("Actiwatch awd is correctly read", {
file = system.file("testfiles/Actiwatch.AWD", package = "GGIRread")
D = readActiwatchCount(filename = file, desiredEpochSize = 60, timeformat = "%d-%b-%Y %H:%M:%S", tz = "")
D = readActiwatchCount(filename = file, timeformat = "%d-%b-%Y %H:%M:%S", tz = "")
expect_equal(D$epochSize, 60)
expect_equal(format(D$startTime), "2009-10-01 17:00:00")
expect_equal(nrow(D$data), 329)
expect_equal(ncol(D$data), 2)
expect_equal(sum(D$data[, "counts"], na.rm = TRUE), 108864)
expect_equal(sum(D$data[, "light"], na.rm = TRUE), 0)

D = readActiwatchCount(filename = file, desiredEpochSize = 300, timeformat = "%d-%b-%Y %H:%M:%S", tz = "")
expect_equal(D$epochSize, 300)
expect_equal(format(D$startTime), "2009-10-01 17:00:00")
expect_equal(nrow(D$data), 65)
expect_equal(ncol(D$data), 2)
expect_equal(sum(D$data[, "counts"], na.rm = TRUE), 108713)
expect_equal(sum(D$data[, "light"], na.rm = TRUE), 0)
})

test_that("Actiwatch awd error correctly", {
file = system.file("testfiles/Actiwatch.AWD", package = "GGIRread")
expect_error(readActiwatchCount(filename = file,
desiredEpochSize = 60,
timeformat = "%d-%m-%Y %H:%M:%S"),
regexp = "Time format*")

expect_error(readActiwatchCount(filename = file,
desiredEpochSize = 5,
timeformat = "%d-%b-%Y %H:%M:%S"),
regexp = "The short*")

expect_error(readActiwatchCount(filename = "",
desiredEpochSize = 60,
timeformat = "%d-%b-%Y %H:%M:%S"),
regexp = "Cannot")

})

test_that("checkTimeFormat also detect implausible year", {
test_that("checkTimeFormat also detects implausible year", {
rawValue = "6/28/21 10:10:10"
timeformat = "%m/%d/%y %H:%M:%S"
timestamp_POSIX = as.POSIXlt("6/28/21 10:10:10", format = "%m/%d/%Y %H:%M:%S")
Expand Down

0 comments on commit 32f4460

Please sign in to comment.