From 889efa8d045eb2bbe5afbd105756a90b1abda836 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Tue, 15 Jul 2025 09:56:00 +0200 Subject: [PATCH 1/3] improve detection Actical start, fixes #85 --- NEWS.md | 4 ++++ R/findStartData.R | 25 +++++++++++++++++++++++-- R/readActicalCount.R | 4 ++-- man/findStartData.Rd | 6 +++++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/NEWS.md b/NEWS.md index 812de5e..32e0ed4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# Changes in version 1.0.6 (release date:??-??-2025) + +- Actical: Improve detection of recording start #85 + # Changes in version 1.0.5 (release date:09-05-2025) - Fitbit: Now also loads heart rate data #78 diff --git a/R/findStartData.R b/R/findStartData.R index f7b8d8a..fee5720 100644 --- a/R/findStartData.R +++ b/R/findStartData.R @@ -1,6 +1,27 @@ -findStartData = function(filename, quote, startindex) { +findStartData = function(filename, quote, startindex, blockname = NULL) { # Function used to find start of time series in Actiwatch and Actical data - # ! Assumptions that timeseries start before line 1000 + # ! Assumption that time series start in first 3000 lines + # ! Assumption count data are preceded by a block header with name blockname + if (!is.null(blockname)) { + quote = detectQuote(filename = filename, skip = startindex) + testraw = data.table::fread(input = filename, + header = FALSE, sep = ",", + nrows = 3000, data.table = FALSE, + quote = quote, fill = TRUE) + + startindex_temp = grep(pattern = blockname, x = testraw[,1], ignore.case = TRUE) + if (length(startindex_temp) != 0) { + temp = testraw[(startindex_temp + 1):(startindex_temp + 20),1] + temp = unlist(lapply(temp, FUN = function(x) unlist(strsplit(x, ","))[1])) + epochnumbers = suppressWarnings(as.numeric(temp)) + startindex_temp = startindex_temp + which(!is.na(epochnumbers))[1] + } + if (length(startindex_temp) != 0) return(startindex_temp) + startindex = startindex_temp + } + # Original approach: + # ! Assumption that timeseries start before line 1000 + # ! Assumption that epoch column start with 1 while (startindex > 0) { testraw = data.table::fread(input = filename, header = FALSE, sep = ",", skip = startindex, diff --git a/R/readActicalCount.R b/R/readActicalCount.R index ed1bcfd..e2c08f0 100644 --- a/R/readActicalCount.R +++ b/R/readActicalCount.R @@ -8,7 +8,7 @@ readActicalCount = function(filename = NULL, # ! Assumptions that timeseries start before line 1000 startindex = 300 quote = detectQuote(filename = filename, skip = startindex) - startindex = findStartData(filename, quote, startindex) + startindex = findStartData(filename, quote, startindex, blockname = "epoch-by-epoch") # -1 because Actical starts at epoch 0 while function looks for epoch 1 startindex = startindex - 1 D = data.table::fread(input = filename, sep = ",", skip = startindex, @@ -29,7 +29,7 @@ readActicalCount = function(filename = NULL, colnames = data.table::fread(input = filename, data.table = FALSE, header = FALSE, sep = ",", skip = dashedLineIndex + 1, - nrows = (startindex - dashedLineIndex) - 2, quote = quote) + nrows = (startindex - dashedLineIndex) - 2, quote = quote, fill = TRUE) collapse = function(x) { return(paste0(x, collapse = "_")) } diff --git a/man/findStartData.Rd b/man/findStartData.Rd index 04a8943..eb15baf 100644 --- a/man/findStartData.Rd +++ b/man/findStartData.Rd @@ -8,7 +8,7 @@ Actiwatch and Actical data. } \usage{ - findStartData(filename, quote, startindex) + findStartData(filename, quote, startindex, blockname = NULL) } \arguments{ \item{filename}{ @@ -21,6 +21,10 @@ Start index where to start searching. For Actical we start at 300 while for Actiwatch we start at 1000. } + \item{blockname}{ + Character with name of data block to search for. + For Actical we use "epoch-by-epoch". + } } \value{ Start index From b0743b47eabac6902db8e2549aa44a350074ce3c Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Tue, 15 Jul 2025 13:09:11 +0200 Subject: [PATCH 2/3] tidying up code revisions for #85 --- R/findStartData.R | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/R/findStartData.R b/R/findStartData.R index fee5720..0597b0f 100644 --- a/R/findStartData.R +++ b/R/findStartData.R @@ -1,23 +1,22 @@ findStartData = function(filename, quote, startindex, blockname = NULL) { # Function used to find start of time series in Actiwatch and Actical data - # ! Assumption that time series start in first 3000 lines - # ! Assumption count data are preceded by a block header with name blockname if (!is.null(blockname)) { - quote = detectQuote(filename = filename, skip = startindex) - testraw = data.table::fread(input = filename, + # Default approach when blockname is specified (used for Actical) + # ! Assumption that time series start in first 3000 lines + # ! Assumption count data are preceded by a block header with name blockname + data_head = data.table::fread(input = filename, header = FALSE, sep = ",", nrows = 3000, data.table = FALSE, quote = quote, fill = TRUE) - - startindex_temp = grep(pattern = blockname, x = testraw[,1], ignore.case = TRUE) - if (length(startindex_temp) != 0) { - temp = testraw[(startindex_temp + 1):(startindex_temp + 20),1] - temp = unlist(lapply(temp, FUN = function(x) unlist(strsplit(x, ","))[1])) - epochnumbers = suppressWarnings(as.numeric(temp)) - startindex_temp = startindex_temp + which(!is.na(epochnumbers))[1] + block_start = grep(pattern = blockname, x = data_head[, 1], ignore.case = TRUE) + if (length(block_start) != 0) { + block_head = data_head[(block_start + 1):(block_start + 20), 1] + block_head = unlist(lapply(block_head, FUN = function(x) unlist(strsplit(x, ","))[1])) + epochnumbers = suppressWarnings(as.numeric(block_head)) + block_start = block_start + which(!is.na(epochnumbers))[1] } - if (length(startindex_temp) != 0) return(startindex_temp) - startindex = startindex_temp + if (length(block_start) != 0) return(block_start) + startindex = block_start } # Original approach: # ! Assumption that timeseries start before line 1000 From 07e1d423bfdbb3757e574768bb3ff8fdfab32a0f Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Tue, 15 Jul 2025 13:12:08 +0200 Subject: [PATCH 3/3] fixes #81 --- R/readAxivity.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/readAxivity.R b/R/readAxivity.R index 9ff9b77..acf83f4 100755 --- a/R/readAxivity.R +++ b/R/readAxivity.R @@ -361,7 +361,7 @@ readAxivity = function(filename, start = 0, end = 0, progressBar = FALSE, desire stop("At least file must be specified") } # Get file size in data blocks - numDBlocks = round(file.info(filename)$size / blockBytes) - 2 + numDBlocks = round(file.size(filename) / blockBytes) - 2 # Open file fid = file(filename,"rb") on.exit({