-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate utils functions to their own function files #68
- Loading branch information
1 parent
9b1c1e7
commit 3a3d588
Showing
11 changed files
with
201 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |