From 671a663c356c0c6224328a28d14e71568c67afd9 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Mon, 30 Sep 2024 14:28:17 +0200 Subject: [PATCH] add mergeFitbitData and allign with mergePHBdata #68 --- NAMESPACE | 4 +- R/mergeFitbitData.R | 22 +++++ R/{mergePHBfilePairs.R => mergePHBdata.R} | 2 +- README.md | 10 +- .../testfiles/calories-1995-06-23_Fitbit.json | 94 +++++++++---------- man/mergeFitbitData.Rd | 35 +++++++ man/{mergePHBfilePairs.Rd => mergePHBdata.Rd} | 6 +- tests/testthat/test_mergeFitbitData.R | 22 +++++ ...ergePHBfilePairs.R => test_mergePHBdata.R} | 3 +- tests/testthat/test_readFitbit.R | 2 +- 10 files changed, 140 insertions(+), 60 deletions(-) create mode 100644 R/mergeFitbitData.R rename R/{mergePHBfilePairs.R => mergePHBdata.R} (96%) create mode 100644 man/mergeFitbitData.Rd rename man/{mergePHBfilePairs.Rd => mergePHBdata.Rd} (94%) create mode 100644 tests/testthat/test_mergeFitbitData.R rename tests/testthat/{test_mergePHBfilePairs.R => test_mergePHBdata.R} (88%) diff --git a/NAMESPACE b/NAMESPACE index 2be517a..4e8b528 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,9 @@ export(readGenea, readAxivity, readGENEActiv, GENEActivReader, resample, readWav, readActiGraphCount, readActiwatchCount, - readActicalCount, mergePHBfilePairs) + readActicalCount, readPHBCount, + readFitbit, mergePHBdata, + mergeFitbitData) useDynLib(GGIRread, .registration = TRUE) importFrom(Rcpp, sourceCpp) importFrom(data.table, fread) diff --git a/R/mergeFitbitData.R b/R/mergeFitbitData.R new file mode 100644 index 0000000..7163b5e --- /dev/null +++ b/R/mergeFitbitData.R @@ -0,0 +1,22 @@ +mergeFitbitData = function(filenames = NULL, desiredtz = "", configtz = NULL) { + if (length(filenames) < 2) { + stop("Provide at least two filenames") + } + cnt = 1 + while (cnt <= length(filenames)) { + D = readFitbit(filename = filenames[cnt], desiredtz = desiredtz, configtz = configtz) + if (cnt == 1) { + data = D + } else { + if (length(intersect(x = data$dateTime, D$dateTime)) == 0) { + warning(paste0("Time series do not intersect for files ", + basename(filenames[cnt]), " and ", basename(filenames[cnt - 1])), + call. = FALSE) + } + + data = merge(data, D, by = "dateTime", all = TRUE) + } + cnt = cnt + 1 + } + return(data) +} \ No newline at end of file diff --git a/R/mergePHBfilePairs.R b/R/mergePHBdata.R similarity index 96% rename from R/mergePHBfilePairs.R rename to R/mergePHBdata.R index 2333217..fc802de 100644 --- a/R/mergePHBfilePairs.R +++ b/R/mergePHBdata.R @@ -1,4 +1,4 @@ -mergePHBfilePairs = function(filenames = NULL, +mergePHBdata = function(filenames = NULL, timeformat = "%m/%d/%Y %H:%M:%S", desiredtz = "", configtz = NULL, timeformatName = "timeformat") { diff --git a/README.md b/README.md index 8cc1940..5db9a59 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ Brand | Device name | File extension | Data type | GGIRread function Axivity Ltd https://axivity.com/ | AX3 and AX6 | .cwa | raw gravitational units |readAxivity ActivInsights Ltd https://activinsights.com/ | GENEActiv Original and Sleep | .bin | raw gravitational units | readGENEActiv Unilever Discover Ltd | Genea (no longer manufactured) | .bin | raw gravitational units | readGenea -ActiGraph | ??? | .csv | count data | readActigraph -Actiwatch | ??? | .csv and .awd | count data | readActiwatch -Actical | ??? | .csv | count data | readActical.R -Philips Health Band | ??? | .xlsx | count data | mergePHBfilePairs.R -Fitbit | ??? | .json | sleep, steps or calories data | readFitbit.R +ActiGraph | ??? | .csv | count data | readActigraphCount +Actiwatch | ??? | .csv and .awd | count data | readActiwatchCount +Actical | ??? | .csv | count data | readActicalCount +Philips Health Band | ??? | .xlsx | count data | readPHBCount +Fitbit | ??? | .json | sleep, steps or calories data | readFitbit diff --git a/inst/testfiles/calories-1995-06-23_Fitbit.json b/inst/testfiles/calories-1995-06-23_Fitbit.json index 81f1294..29103bb 100644 --- a/inst/testfiles/calories-1995-06-23_Fitbit.json +++ b/inst/testfiles/calories-1995-06-23_Fitbit.json @@ -1,142 +1,142 @@ [{ - "dateTime" : "06/23/95 00:00:00", + "dateTime" : "06/24/95 16:00:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:01:00", + "dateTime" : "06/24/95 16:01:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:02:00", + "dateTime" : "06/24/95 16:02:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:03:00", + "dateTime" : "06/24/95 16:03:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:04:00", + "dateTime" : "06/24/95 16:04:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:05:00", + "dateTime" : "06/24/95 16:05:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:06:00", + "dateTime" : "06/24/95 16:06:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:07:00", + "dateTime" : "06/24/95 16:07:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:08:00", + "dateTime" : "06/24/95 16:08:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:09:00", + "dateTime" : "06/24/95 16:09:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:10:00", + "dateTime" : "06/24/95 16:10:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:11:00", + "dateTime" : "06/24/95 16:11:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:12:00", + "dateTime" : "06/24/95 16:12:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:13:00", + "dateTime" : "06/24/95 16:13:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:14:00", + "dateTime" : "06/24/95 16:14:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:15:00", + "dateTime" : "06/24/95 16:15:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:16:00", + "dateTime" : "06/24/95 16:16:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:17:00", + "dateTime" : "06/24/95 16:17:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:18:00", + "dateTime" : "06/24/95 16:18:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:19:00", + "dateTime" : "06/24/95 16:19:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:20:00", + "dateTime" : "06/24/95 16:20:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:21:00", + "dateTime" : "06/24/95 16:21:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:22:00", + "dateTime" : "06/24/95 16:22:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:23:00", + "dateTime" : "06/24/95 16:23:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:24:00", + "dateTime" : "06/24/95 16:24:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:25:00", + "dateTime" : "06/24/95 16:25:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:26:00", + "dateTime" : "06/24/95 16:26:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:27:00", + "dateTime" : "06/24/95 16:27:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:28:00", + "dateTime" : "06/24/95 16:28:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:29:00", + "dateTime" : "06/24/95 16:29:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:30:00", + "dateTime" : "06/24/95 16:30:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:31:00", + "dateTime" : "06/24/95 16:31:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:32:00", + "dateTime" : "06/24/95 16:32:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:33:00", + "dateTime" : "06/24/95 16:33:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:34:00", + "dateTime" : "06/24/95 16:34:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:35:00", + "dateTime" : "06/24/95 16:35:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:36:00", + "dateTime" : "06/24/95 16:36:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:37:00", + "dateTime" : "06/24/95 16:37:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:38:00", + "dateTime" : "06/24/95 16:38:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:39:00", + "dateTime" : "06/24/95 16:39:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:40:00", + "dateTime" : "06/24/95 16:40:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:41:00", + "dateTime" : "06/24/95 16:41:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:42:00", + "dateTime" : "06/24/95 16:42:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:43:00", + "dateTime" : "06/24/95 16:43:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:44:00", + "dateTime" : "06/24/95 16:44:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:45:00", + "dateTime" : "06/24/95 16:45:00", "value" : "1.48" },{ - "dateTime" : "06/23/95 00:46:00", + "dateTime" : "06/24/95 16:46:00", "value" : "1.48" }] \ No newline at end of file diff --git a/man/mergeFitbitData.Rd b/man/mergeFitbitData.Rd new file mode 100644 index 0000000..bae6f14 --- /dev/null +++ b/man/mergeFitbitData.Rd @@ -0,0 +1,35 @@ +\name{mergeFitbitData} +\alias{mergeFitbitData} +\title{ + Merge Fitbit json files into one data.frame +} +\description{ + Read and merge Fitbit json files into one data.frame, and + give warning if time series do not intersect. +} +\usage{ + mergeFitbitData(filenames = NULL, + desiredtz = "", configtz = NULL) +} +\arguments{ + \item{filenames}{ + Character vector, specifying the filenames (at least two) to be merged. + } + \item{desiredtz}{ + Character, timezone name where the accelerometer was worn. Timezone names are + expected to be the timezone database names, e.g. Europe/London. See also: + https://en.wikipedia.org/wiki/List_of_tz_database_time_zones + } + \item{configtz}{ + Character, timezone name where the accelerometer was configured. Leave NULL + if equal to desiredtz. Timezones name are expected to be the timezone database + names, e.g. Europe/London. See also: + https://en.wikipedia.org/wiki/List_of_tz_database_time_zones + } +} +\value{ + Merged data from the two or more files. +} +\author{ + Vincent T van Hees +} \ No newline at end of file diff --git a/man/mergePHBfilePairs.Rd b/man/mergePHBdata.Rd similarity index 94% rename from man/mergePHBfilePairs.Rd rename to man/mergePHBdata.Rd index b10f129..7770da4 100644 --- a/man/mergePHBfilePairs.Rd +++ b/man/mergePHBdata.Rd @@ -1,5 +1,5 @@ -\name{mergePHBfilePairs} -\alias{mergePHBfilePairs} +\name{mergePHBdata} +\alias{mergePHBdata} \title{ Merge Philips Health Band xlsx file pairs into one csv file } @@ -9,7 +9,7 @@ xlsx file to csv. } \usage{ - mergePHBfilePairs(filenames = NULL, + mergePHBdata(filenames = NULL, timeformat = "\%m/\%d/\%Y \%H:\%M:\%S", desiredtz = "", configtz = NULL, timeformatName = "timeformat") diff --git a/tests/testthat/test_mergeFitbitData.R b/tests/testthat/test_mergeFitbitData.R new file mode 100644 index 0000000..d5d8144 --- /dev/null +++ b/tests/testthat/test_mergeFitbitData.R @@ -0,0 +1,22 @@ +library(GGIRread) +context("merge PHB files") +test_that("merging of PHB files goes correctly", { + file1 = system.file("testfiles/steps-1995-06-23_Fitbit.json", package = "GGIRread") + file2 = system.file("testfiles/calories-1995-06-23_Fitbit.json", package = "GGIRread") + file3 = system.file("testfiles/sleep-1995-06-23_Fitbit.json", package = "GGIRread") + + # apply function to merge the files + D = mergeFitbitData(filenames = c(file1, file2), + desiredtz = "Europe/Amsterdam") + expect_equal(nrow(D), 47) + expect_equal(ncol(D), 3) + expect_equal(sum(D$steps, na.rm = TRUE), 607) + expect_equal(sum(D$calories, na.rm = TRUE), 69.56) + expect_equal(format(D$dateTime[1]), "1995-06-24 16:00:00") + + # apply function to merge the files + expect_warning(mergeFitbitData(filenames = c(file1, file2, file3), + desiredtz = "Europe/Amsterdam"), + regexp = "Time series*") +}) + \ No newline at end of file diff --git a/tests/testthat/test_mergePHBfilePairs.R b/tests/testthat/test_mergePHBdata.R similarity index 88% rename from tests/testthat/test_mergePHBfilePairs.R rename to tests/testthat/test_mergePHBdata.R index 39c1b3b..43ed9a1 100644 --- a/tests/testthat/test_mergePHBfilePairs.R +++ b/tests/testthat/test_mergePHBdata.R @@ -1,11 +1,10 @@ library(GGIRread) context("merge PHB files") test_that("merging of PHB files goes correctly", { - # prepare folder with test files file1 = system.file("testfiles/DataList_AH1234567890_PhilipsHealthBand.xlsx", package = "GGIRread") file2 = system.file("testfiles/Sleep_Wake_AH1234567890_PhilipsHealthBand.xlsx", package = "GGIRread") # apply function to merge the files - D = mergePHBfilePairs(filenames = c(file1, file2), + D = mergePHBdata(filenames = c(file1, file2), timeformat = "%m-%d-%Y %H:%M:%S", desiredtz = "Europe/Amsterdam") expect_equal(nrow(D$data), 246) diff --git a/tests/testthat/test_readFitbit.R b/tests/testthat/test_readFitbit.R index b95d500..5601c32 100644 --- a/tests/testthat/test_readFitbit.R +++ b/tests/testthat/test_readFitbit.R @@ -24,7 +24,7 @@ test_that("Fitbit json is correctly read", { D = readFitbit(filename = file, desiredtz = "Europe/Amsterdam") expect_equal(nrow(D), 47) expect_equal(ncol(D), 2) - expect_equal(format(D$dateTime[1]), "1995-06-23") + expect_equal(format(D$dateTime[1]), "1995-06-24 16:00:00") expect_equal(sum(D$calories), 69.56) })