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..0597b0f 100644 --- a/R/findStartData.R +++ b/R/findStartData.R @@ -1,6 +1,26 @@ -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 + if (!is.null(blockname)) { + # 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) + 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(block_start) != 0) return(block_start) + startindex = block_start + } + # 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/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({ 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