Skip to content

Commit

Permalink
migrate utils functions to their own function files #68
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Sep 30, 2024
1 parent 9b1c1e7 commit 3a3d588
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 90 deletions.
22 changes: 22 additions & 0 deletions R/checkTimeFormat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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,
" as specified by argument ", timeformatName,
", please correct.\n"), call. = FALSE)
} else {
year = as.numeric(format(timestamp_POSIX, format = "%Y"))
if (year < 1980 || year > 2500) {
# Assumption that after 2500 no new ActiGraph data will be collected!
stop(paste0("\nTimestamp recognised as ", format(timestamp_POSIX),
" with year identified as ", year,
". This does not seem to be correct. Raw timestamp value is stored as ",
rawValue, ". please change specification of ",
"argument ", timeformatName, " (currently ",
timeformat, ") to ensure correct interpretation of timestamp.\n"),
call. = FALSE)
}
}
}
18 changes: 18 additions & 0 deletions R/detectQuote.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
detectQuote = function(filename, skip) {
# data.table::fread has argument quote.
# On some computers the quotes in the files are
# not recognised, to catch this first try to check whether this is the case:
quote = "\""
Dtest = NULL
try(expr = {Dtest = data.table::fread(input = filename,
header = FALSE, sep = ",", skip = skip,
nrows = 20, quote = quote)}, silent = TRUE)
if (length(Dtest) == 0) {
quote = ""
} else {
if (nrow(Dtest) <= 1) {
quote = ""
}
}
return(quote)
}
32 changes: 32 additions & 0 deletions R/findStartData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
findStartData = function(filename, quote, startindex) {
# Function used to find start of time series in Actiwatch and Actical data
# ! Assumptions that timeseries start before line 1000
while (startindex > 0) {
testraw = data.table::fread(input = filename,
header = FALSE, sep = ",", skip = startindex,
nrows = 2, data.table = FALSE, quote = quote)
if (length(testraw) > 0) {
if (nrow(testraw) == 2) {
if (testraw$V1[2] == testraw$V1[1] + 1) {
break
}
}
}
startindex = startindex - 100
}
# ! Assumption that first column are the epoch numbers
delta = 1 - testraw$V1[1]
startindex = startindex + delta
startFound = FALSE
while (startFound == FALSE) {
Dtest = data.table::fread(input = filename, sep = ",", skip = startindex, quote = quote, nrows = 1)
if (Dtest$V1[1] == 1) {
startFound = TRUE
} else {
# This happens when file is has an empty row between each measurement point is stored
startindex = startindex - 1
if (startindex < 1) stop("Could not find start of recording", call. = FALSE)
}
}
return(startindex)
}
9 changes: 9 additions & 0 deletions R/getExtension.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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])
}



2 changes: 1 addition & 1 deletion R/readActicalCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readActicalCount = function(filename = NULL,
if (length(configtz) == 0) configtz = desiredtz
# ! Assumptions that timeseries start before line 1000
startindex = 300
quote = detectQuote(fn = filename, index = startindex)
quote = detectQuote(filename = filename, skip = startindex)
startindex = findStartData(filename, quote, startindex)
# -1 because Actical starts at epoch 0 while function looks for epoch 1
startindex = startindex - 1
Expand Down
4 changes: 2 additions & 2 deletions R/readActiwatchCount.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readActiwatchCount = function(filename = NULL,
#=========================================================
# ! Assumptions that timeseries start before line 1000
startindex = 1000
quote = detectQuote(fn = filename, index = startindex)
quote = detectQuote(filename = filename, skip = startindex)
index = findStartData(filename, quote, startindex)
D = data.table::fread(input = filename, sep = ",", skip = index, quote = quote, data.table = FALSE)
# ! Assumption that column names are present 2 lines prior to timeseries
Expand Down Expand Up @@ -55,7 +55,7 @@ readActiwatchCount = function(filename = NULL,
# ! Assumption that first data row equals the first row with 3 columns
index = 0

quote = detectQuote(fn = filename, index = 50)
quote = detectQuote(filename = filename, skip = 50)
NC = 1
while (NC >= 3) {
testraw = data.table::fread(input = filename,
Expand Down
87 changes: 0 additions & 87 deletions R/utils_for_countdata.R

This file was deleted.

36 changes: 36 additions & 0 deletions man/checkTimeFormat.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
\name{checkTimeFormat}
\alias{checkTimeFormat}
\title{
Check timestamp format
}
\description{
Helper function used internally. Generate informative error message if timestamp
could not be recognisede.
}
\usage{
checkTimeFormat(timestamp_POSIX, rawValue = " ?? ", timeformat = " ?? ",
timeformatName = NULL)
}
\arguments{
\item{timestamp_POSIX}{
POSIX object with timestamp
}
\item{rawValue}{
Timestamp value as encounterd in the data before processing
}
\item{timeformat}{
Timestap format used for converting timestamp
}
\item{timeformatName}{
Character with the argument name to specify the timeformat.
If used as dependency of GGIR then this argument name will be different.
}
}
\value{
No output value is generated, only an error message if timestamp could not be
recognised.
}
\keyword{internal}
\author{
Vincent T van Hees <v.vanhees@accelting.com>
}
27 changes: 27 additions & 0 deletions man/detectQuote.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
\name{detectQuote}
\alias{detectQuote}
\title{
Detect quote
}
\description{
Helper function used internally, used to find out what value should
be used for argument quote in function fread.
}
\usage{
detectQuote(filename, skip)
}
\arguments{
\item{filename}{
Character
}
\item{skip}{
skip
}
}
\value{
Character with quote object to be used for fread
}
\keyword{internal}
\author{
Vincent T van Hees <v.vanhees@accelting.com>
}
31 changes: 31 additions & 0 deletions man/findStartData.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
\name{findStartData}
\alias{findStartData}
\title{
Find start of data inside text file.
}
\description{
Helper function used internally. Finds variables start of the data in
Actiwatch and Actical data.
}
\usage{
findStartData(filename, quote, startindex)
}
\arguments{
\item{filename}{
Character
}
\item{quote}{
Quote as extracted with \link{detectQuote}
}
\item{startindex}{
Start index where to start searching. For Actical we start at 300 while
for Actiwatch we start at 1000.
}
}
\value{
Start index
}
\keyword{internal}
\author{
Vincent T van Hees <v.vanhees@accelting.com>
}
23 changes: 23 additions & 0 deletions man/getExtension.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
\name{getExtension}
\alias{getExtension}
\title{
Get File Extension
}
\description{
Helper function used internally to get file extension.
}
\usage{
getExtension(filename)
}
\arguments{
\item{filename}{
Character
}
}
\value{
Character with file extension
}
\keyword{internal}
\author{
Vincent T van Hees <v.vanhees@accelting.com>
}

0 comments on commit 3a3d588

Please sign in to comment.