Skip to content

Commit 4dfe37b

Browse files
Merge pull request #86 from wadpac/issue85_actical_csv_start
improve detection of Actical start
2 parents 28997cd + 07e1d42 commit 4dfe37b

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Changes in version 1.0.6 (release date:??-??-2025)
2+
3+
- Actical: Improve detection of recording start #85
4+
15
# Changes in version 1.0.5 (release date:09-05-2025)
26

37
- Fitbit: Now also loads heart rate data #78

R/findStartData.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1-
findStartData = function(filename, quote, startindex) {
1+
findStartData = function(filename, quote, startindex, blockname = NULL) {
22
# Function used to find start of time series in Actiwatch and Actical data
3-
# ! Assumptions that timeseries start before line 1000
3+
if (!is.null(blockname)) {
4+
# Default approach when blockname is specified (used for Actical)
5+
# ! Assumption that time series start in first 3000 lines
6+
# ! Assumption count data are preceded by a block header with name blockname
7+
data_head = data.table::fread(input = filename,
8+
header = FALSE, sep = ",",
9+
nrows = 3000, data.table = FALSE,
10+
quote = quote, fill = TRUE)
11+
block_start = grep(pattern = blockname, x = data_head[, 1], ignore.case = TRUE)
12+
if (length(block_start) != 0) {
13+
block_head = data_head[(block_start + 1):(block_start + 20), 1]
14+
block_head = unlist(lapply(block_head, FUN = function(x) unlist(strsplit(x, ","))[1]))
15+
epochnumbers = suppressWarnings(as.numeric(block_head))
16+
block_start = block_start + which(!is.na(epochnumbers))[1]
17+
}
18+
if (length(block_start) != 0) return(block_start)
19+
startindex = block_start
20+
}
21+
# Original approach:
22+
# ! Assumption that timeseries start before line 1000
23+
# ! Assumption that epoch column start with 1
424
while (startindex > 0) {
525
testraw = data.table::fread(input = filename,
626
header = FALSE, sep = ",", skip = startindex,

R/readActicalCount.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ readActicalCount = function(filename = NULL,
88
# ! Assumptions that timeseries start before line 1000
99
startindex = 300
1010
quote = detectQuote(filename = filename, skip = startindex)
11-
startindex = findStartData(filename, quote, startindex)
11+
startindex = findStartData(filename, quote, startindex, blockname = "epoch-by-epoch")
1212
# -1 because Actical starts at epoch 0 while function looks for epoch 1
1313
startindex = startindex - 1
1414
D = data.table::fread(input = filename, sep = ",", skip = startindex,
@@ -29,7 +29,7 @@ readActicalCount = function(filename = NULL,
2929
colnames = data.table::fread(input = filename, data.table = FALSE,
3030
header = FALSE, sep = ",",
3131
skip = dashedLineIndex + 1,
32-
nrows = (startindex - dashedLineIndex) - 2, quote = quote)
32+
nrows = (startindex - dashedLineIndex) - 2, quote = quote, fill = TRUE)
3333
collapse = function(x) {
3434
return(paste0(x, collapse = "_"))
3535
}

R/readAxivity.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ readAxivity = function(filename, start = 0, end = 0, progressBar = FALSE, desire
361361
stop("At least file must be specified")
362362
}
363363
# Get file size in data blocks
364-
numDBlocks = round(file.info(filename)$size / blockBytes) - 2
364+
numDBlocks = round(file.size(filename) / blockBytes) - 2
365365
# Open file
366366
fid = file(filename,"rb")
367367
on.exit({

man/findStartData.Rd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Actiwatch and Actical data.
99
}
1010
\usage{
11-
findStartData(filename, quote, startindex)
11+
findStartData(filename, quote, startindex, blockname = NULL)
1212
}
1313
\arguments{
1414
\item{filename}{
@@ -21,6 +21,10 @@
2121
Start index where to start searching. For Actical we start at 300 while
2222
for Actiwatch we start at 1000.
2323
}
24+
\item{blockname}{
25+
Character with name of data block to search for.
26+
For Actical we use "epoch-by-epoch".
27+
}
2428
}
2529
\value{
2630
Start index

0 commit comments

Comments
 (0)