Skip to content

Commit

Permalink
add mergeFitbitData and allign with mergePHBdata #68
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees committed Sep 30, 2024
1 parent 3eab460 commit 671a663
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 60 deletions.
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
22 changes: 22 additions & 0 deletions R/mergeFitbitData.R
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion R/mergePHBfilePairs.R → R/mergePHBdata.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mergePHBfilePairs = function(filenames = NULL,
mergePHBdata = function(filenames = NULL,
timeformat = "%m/%d/%Y %H:%M:%S",
desiredtz = "", configtz = NULL,
timeformatName = "timeformat") {
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
94 changes: 47 additions & 47 deletions inst/testfiles/calories-1995-06-23_Fitbit.json
Original file line number Diff line number Diff line change
@@ -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"
}]
35 changes: 35 additions & 0 deletions man/mergeFitbitData.Rd
Original file line number Diff line number Diff line change
@@ -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 <v.vanhees@accelting.com>
}
6 changes: 3 additions & 3 deletions man/mergePHBfilePairs.Rd → man/mergePHBdata.Rd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
\name{mergePHBfilePairs}
\alias{mergePHBfilePairs}
\name{mergePHBdata}
\alias{mergePHBdata}
\title{
Merge Philips Health Band xlsx file pairs into one csv file
}
Expand All @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test_mergeFitbitData.R
Original file line number Diff line number Diff line change
@@ -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*")
})

Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test_readFitbit.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down

0 comments on commit 671a663

Please sign in to comment.