Skip to content

Commit

Permalink
Merge pull request #21 from habitus-eu/issue20_redundantcolumn
Browse files Browse the repository at this point in the history
Fixes: redundant column and trip duration one epoch too short
  • Loading branch information
vincentvanhees authored Mar 20, 2024
2 parents 5287df8 + f1f73d7 commit 3bfaa30
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
^\.github
prepareNewRelease.R
LICENSE
codecov.yml
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: hbGPS
Type: Package
Title: Process GPS Data For Human Behaviour Research
Version: 0.0.4
Date: 2023-09-28
Version: 0.0.6
Date: 2024-03-20
Authors@R: c(person("Vincent T","van Hees",role=c("aut","cre"),
email="[email protected]"),
person("SDU Denmark", role = c("cph", "fnd")))
Expand Down
2 changes: 1 addition & 1 deletion R/deriveTrips.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ deriveTrips = function(df, tz, minTripDur, minTripDist_m) {
s1 = as.POSIXct(Segs$time_t1[tripEnd[j]], tz = tz, origin = "1970-01-01")
tripInd = which(df$time >= s0 & df$time <= s1)
# trip duration in minutes
tripDur = (Segs$time_t1[tripEnd[j]] - Segs$time_t0[tripStart[j]])
tripDur = diff(as.numeric(range(df$time[c(tripInd, tripInd[length(tripInd)] + 1)])))
tripDist_m = sum(abs(df$distance_m[tripInd]))
maxTimeGap = max(df$deltaTime[tripInd])
tripIncl = mean(df$inclination_deg[tripInd])
Expand Down
6 changes: 4 additions & 2 deletions R/mergeGGIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mergeGGIR = function(GGIRpath, GPSdf, ID, verbose) {

# If user by accident specified GGIR output folder then attempt
# to update path to specify the ms5.outraw subfolder inside it
if (length(grep(pattern = "ms5.outraw", x = GGIRpath)) != 0) {
if (length(grep(pattern = "ms5.outraw", x = GGIRpath)) == 0) {
newGGIRpath = paste0(GGIRpath, "/meta/ms5.outraw")
if (dir.exists(newGGIRpath)) {
GGIRpath = newGGIRpath
Expand Down Expand Up @@ -47,7 +47,9 @@ mergeGGIR = function(GGIRpath, GPSdf, ID, verbose) {
# Linearly interpolate acceleration
GPSdf$GGIR_ACC = GGIRread::resample(raw = as.matrix(G$ACC), rawTime = G$timenum, time = GPSdf$time, stop = nrow(G), type = 1)
# Nearest neigbour interpolate other GGIR output columns
col2impute = c("SleepPeriodTime", "invalidepoch", "guider", "window", "class_id")
if ("class_id" %in% colnames(G) == FALSE) stop("GGIR time series misses class_id column")
if ("invalidepoch" %in% colnames(G) == FALSE) stop("GGIR time series misses invalidepoch column")
col2impute = colnames(G)[which(colnames(G) %in% c("SleepPeriodTime", "invalidepoch", "window", "class_id") == TRUE)]
GS = as.data.frame(GGIRread::resample(raw = as.matrix(G[, col2impute]),
rawTime = G$timenum,
time = GPSdf$time, stop = nrow(G), type = 2))
Expand Down
7 changes: 7 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
\name{NEWS}
\title{News for Package \pkg{GGIRread}}
\newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
\section{Changes in version 0.0.6 (release date:20-03-2024)}{
\itemize{
\item Simplify mergeGGIR to no longer expect window column, see #20
\item Allow mergeGGIR to handle both full path to ms5.outraw folder or
only the GGIR output folder
}
}
\section{Changes in version 0.0.5 (release date:05-03-2024)}{
\itemize{
\item More formats for GPS satillitate info now supported, see #12
Expand Down
16 changes: 9 additions & 7 deletions tests/testthat/test_hbGPS.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ test_that("hbGPS pipeline process file 4 correctly", {
return_object = TRUE)

expect_equal(nrow(D), 20029)
expect_equal(ncol(D), 44)
expect_equal(ncol(D), 43)

expect_equal(length(unique(D$trip)), 39)
expect_equal(length(unique(D$trip)), 40)
expect_equal(mean(D$GGIR_ACC), 26.19259, tolerance = 0.0001)
expect_equal(mean(D$snr), 256.9834, tolerance = 0.0001)
expect_equal(mean(D$snr_ratio), 78.81412, tolerance = 0.0001)
Expand All @@ -43,11 +43,13 @@ test_that("hbGPS pipeline process file 4 correctly", {
expect_equal(mean(D$bearing_deg), 10.62006, tolerance = 0.00001)
expect_equal(mean(D$deltaBearing), 65.73111, tolerance = 0.00001)

expect_equal(mean(D$tripMaxTimegGap), 0.7691348, tolerance = 0.000001)
expect_equal(mean(D$tripEleSpeed_kmh), 0.03796242, tolerance = 0.000001)
expect_equal(mean(D$tripMaxTimegGap), 0.7728793, tolerance = 0.000001)
expect_equal(mean(D$tripEleSpeed_kmh), 0.03658415, tolerance = 0.000001)

# Finally a crude global check to see whether anything has changed
expect_equal(sum(rowSums(D[,c(11:15, 17:41)]), na.rm = TRUE), 26828374, tolerance = 0.1)
num_cols = which(unlist(lapply(D, is.numeric), use.names = FALSE) == TRUE)
num_cols = num_cols[num_cols %in% which(colnames(D) == "timenum") == FALSE]
expect_equal(sum(rowSums(D[, num_cols], na.rm = TRUE)), 27142209, tolerance = 0.1)

# Check that it also works with a configFile
DC = hbGPS(gps_file = gps_file,
Expand All @@ -59,8 +61,8 @@ test_that("hbGPS pipeline process file 4 correctly", {
configFile = system.file("testfiles/config_hbGPS.csv", package = "hbGPS"),
return_object = TRUE)
expect_equal(nrow(DC), 20029)
expect_equal(ncol(DC), 44)
expect_equal(length(unique(DC$trip)), 39)
expect_equal(ncol(DC), 43)
expect_equal(length(unique(DC$trip)), 40)
expect_equal(mean(DC$GGIR_ACC), 26.19259, tolerance = 0.0001)
expect_equal(mean(DC$speed_ms), 0.6386111, tolerance = 0.0001)

Expand Down

0 comments on commit 3bfaa30

Please sign in to comment.