diff --git a/R/checkTimeFormat.R b/R/checkTimeFormat.R new file mode 100644 index 0000000..d50f4e3 --- /dev/null +++ b/R/checkTimeFormat.R @@ -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) + } + } +} \ No newline at end of file diff --git a/R/detectQuote.R b/R/detectQuote.R new file mode 100644 index 0000000..7329a76 --- /dev/null +++ b/R/detectQuote.R @@ -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) +} \ No newline at end of file diff --git a/R/findStartData.R b/R/findStartData.R new file mode 100644 index 0000000..cb9c4fe --- /dev/null +++ b/R/findStartData.R @@ -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) +} \ No newline at end of file diff --git a/R/getExtension.R b/R/getExtension.R new file mode 100644 index 0000000..6e9e6bc --- /dev/null +++ b/R/getExtension.R @@ -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]) +} + + + diff --git a/R/readActicalCount.R b/R/readActicalCount.R index 404ff66..ed1bcfd 100644 --- a/R/readActicalCount.R +++ b/R/readActicalCount.R @@ -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 diff --git a/R/readActiwatchCount.R b/R/readActiwatchCount.R index f7efb04..b5caf6e 100644 --- a/R/readActiwatchCount.R +++ b/R/readActiwatchCount.R @@ -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 @@ -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, diff --git a/R/utils_for_countdata.R b/R/utils_for_countdata.R deleted file mode 100644 index ff471b1..0000000 --- a/R/utils_for_countdata.R +++ /dev/null @@ -1,87 +0,0 @@ -# Collection of short function used in functions -# - readActigraphCount -# - readActiwatchCount -#----------------------------------------------------------------------------------------- -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) - } - } -} - -detectQuote = function(fn, index) { - # 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 = fn, - header = FALSE, sep = ",", skip = index, - nrows = 20, quote = quote)}, silent = TRUE) - if (length(Dtest) == 0) { - quote = "" - } else { - if (nrow(Dtest) <= 1) { - quote = "" - } - } - return(quote) -} - -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]) -} - - - -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) -} \ No newline at end of file diff --git a/man/checkTimeFormat.Rd b/man/checkTimeFormat.Rd new file mode 100644 index 0000000..266e3ee --- /dev/null +++ b/man/checkTimeFormat.Rd @@ -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 +} \ No newline at end of file diff --git a/man/detectQuote.Rd b/man/detectQuote.Rd new file mode 100644 index 0000000..d78d11c --- /dev/null +++ b/man/detectQuote.Rd @@ -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 +} \ No newline at end of file diff --git a/man/findStartData.Rd b/man/findStartData.Rd new file mode 100644 index 0000000..04a8943 --- /dev/null +++ b/man/findStartData.Rd @@ -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 +} \ No newline at end of file diff --git a/man/getExtension.Rd b/man/getExtension.Rd new file mode 100644 index 0000000..01b3371 --- /dev/null +++ b/man/getExtension.Rd @@ -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 +} \ No newline at end of file