Skip to content

Commit

Permalink
Merge pull request #17 from bhelsel/development
Browse files Browse the repository at this point in the history
Merge development to master for agcounts 0.6.2
  • Loading branch information
bhelsel authored May 12, 2023
2 parents e545a8f + 1289c31 commit a7e19a3
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: agcounts
Type: Package
Title: R Package for Extracting Actigraphy Counts from Accelerometer Data
Version: 0.6.1
Version: 0.6.2
Authors@R: c(
person(c("Brian", "C."), "Helsel", email = "bhelsel80@gmail.com",
role = c("aut", "cre")),
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# agcounts 0.6.2
* Update the Shiny rawDataModule UI and Server to filter by AM or PM to make plotting faster
* Improved test coverage from 19 to 56% by adding tests for the Shiny UI, `gcalibrateC`, `get_counts`, `agread`.
* Updated the test_actilife_counts.R file using a new data set that was uploaded for `gcalibrateC` testing.

# agcounts 0.6.1

* Added `start_time` and `stop_time` attributes to data read by the `pygt3x` calibrated reader.
Expand Down
15 changes: 13 additions & 2 deletions R/rawDataModule.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rawDataModuleUI <- function(id) {
shiny::fileInput(ns("gt3xFile"), "Choose GT3X File", multiple = FALSE, accept = c(".gt3x")),
shiny::radioButtons(ns("parser"), "Select your parser", choices = c("pygt3x", "ggir", "uncalibrated", "agcalibrate"), inline = TRUE, selected = 0),
shiny::uiOutput(ns("dateAccessed")),
shiny::uiOutput(ns("timeSlot")),
shiny::selectInput(ns("axisRaw"), "Raw Axis", choices = c("X", "Y", "Z", "Vector.Magnitude"), selected = "Vector.Magnitude"),
shiny::uiOutput(ns("applyRaw")),
shiny::uiOutput(ns("applyEpoch")),
Expand Down Expand Up @@ -58,6 +59,11 @@ rawDataModuleServer <- function(id) {
shiny::selectInput(session$ns("dateAccessed"), "Choose a date", choices = dates, selected = dates[1])
})

output$timeSlot <- shiny::renderUI({
shiny::req(input$gt3xFile, input$parser)
shiny::radioButtons(session$ns("timeSlot"), "Choose AM or PM", choices = c("AM", "PM"), selected = "AM")
})

output$applyRaw <- shiny::renderUI({
shiny::req(input$axisRaw == "Vector.Magnitude")
shiny::radioButtons(session$ns("applyRaw"),
Expand Down Expand Up @@ -129,9 +135,14 @@ rawDataModuleServer <- function(id) {

# Filter data by dateAccessed to make processing faster
filteredData <- shiny::reactive({
shiny::req(calibratedData(), input$dateAccessed)
shiny::req(calibratedData(), input$dateAccessed, input$timeSlot)
date2filter <- as.Date(input$dateAccessed, "%B %d, %Y")
data <- calibratedData()[as.Date(calibratedData()$time) == date2filter, ]

if(input$timeSlot == "AM"){
data <- calibratedData()[as.Date(calibratedData()$time) == date2filter & as.numeric(format(calibratedData()$time, "%H") < 12), ]
} else{
data <- calibratedData()[as.Date(calibratedData()$time) == date2filter & as.numeric(format(calibratedData()$time, "%H") >= 12), ]
}
})

processedData <- shiny::reactive({
Expand Down
Binary file added inst/extdata/calibration1sec.agd
Binary file not shown.
Binary file added inst/extdata/calibrationXaxis.RData
Binary file not shown.
Binary file added inst/extdata/calibrationYaxis.RData
Binary file not shown.
Binary file added inst/extdata/calibrationZaxis.RData
Binary file not shown.
Binary file added inst/extdata/ggirCalibrate.RData
Binary file not shown.
Binary file removed inst/extdata/testAGD.RData
Binary file not shown.
Binary file removed inst/extdata/testGT3X.RData
Binary file not shown.
71 changes: 71 additions & 0 deletions tests/testthat/_snaps/compareCountsModule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# shiny compareCountsModuleUI creates expected HTML

Code
.testHTML_compareCountsModuleUI("compareCountsModule")
Output
<div class="row">
<div class="col-sm-4">
<form class="well" role="complementary">
<div class="form-group shiny-input-container">
<label class="control-label" id="compareCountsModule-agdFile-label" for="compareCountsModule-agdFile">Choose the Matching AGD File</label>
<div class="input-group">
<label class="input-group-btn input-group-prepend">
<span class="btn btn-default btn-file">
Browse...
<input id="compareCountsModule-agdFile" name="compareCountsModule-agdFile" type="file" style="position: absolute !important; top: -99999px !important; left: -99999px !important;" accept=".agd"/>
</span>
</label>
<input type="text" class="form-control" placeholder="No file selected" readonly="readonly"/>
</div>
<div id="compareCountsModule-agdFile_progress" class="progress active shiny-file-input-progress">
<div class="progress-bar"></div>
</div>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="compareCountsModule-axisCounts2-label" for="compareCountsModule-axisCounts2">Count Axis</label>
<div>
<select id="compareCountsModule-axisCounts2"><option value="Axis1">Axis1</option>
<option value="Axis2">Axis2</option>
<option value="Axis3">Axis3</option>
<option value="Vector.Magnitude" selected>Vector.Magnitude</option></select>
<script type="application/json" data-for="compareCountsModule-axisCounts2" data-nonempty="">{"plugins":["selectize-plugin-a11y"]}</script>
</div>
</div>
<div class="form-group shiny-input-container">
<div class="checkbox">
<label>
<input id="compareCountsModule-agdBlandAltmanPlot" type="checkbox"/>
<span>Bland Altman Plot?</span>
</label>
</div>
</div>
<div id="compareCountsModule-rangeYBlandAltman" class="shiny-html-output"></div>
<div class="form-group shiny-input-container">
<label class="control-label" id="compareCountsModule-agdPlotColor-label" for="compareCountsModule-agdPlotColor">Plot Color (accepts color name or hex code)</label>
<input id="compareCountsModule-agdPlotColor" type="text" class="form-control" value="#000000"/>
</div>
</form>
</div>
<div class="col-sm-8" role="main">
<div class="tabbable">
<ul class="nav nav-tabs shiny-tab-input" id="comparisonTabset" data-tabsetid="4785">
<li class="active">
<a href="#tab-4785-1" data-toggle="tab" data-bs-toggle="tab" data-value="Visualization">Visualization</a>
</li>
<li>
<a href="#tab-4785-2" data-toggle="tab" data-bs-toggle="tab" data-value="Data">Data</a>
</li>
</ul>
<div class="tab-content" data-tabsetid="4785">
<div class="tab-pane active" data-value="Visualization" id="tab-4785-1">
<div class="shiny-plot-output html-fill-item" id="compareCountsModule-comparisonPlot" style="width:100%;height:400px;"></div>
</div>
<div class="tab-pane" data-value="Data" id="tab-4785-2">
<h5>Differences between ActiGraph counts and agcounts</h5>
<div class="reactable html-widget html-widget-output shiny-report-size html-fill-item-overflow-hidden html-fill-item" data-reactable-output="compareCountsModule-comparisonReactableTable" id="compareCountsModule-comparisonReactableTable" style="width:auto;height:auto;"></div>
</div>
</div>
</div>
</div>
</div>

76 changes: 76 additions & 0 deletions tests/testthat/_snaps/countsModule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# shiny countsModuleUI creates expected HTML

Code
.testHTML_countsModuleUI("countsModule")
Output
<div class="row">
<div class="col-sm-4">
<form class="well" role="complementary">
<div class="form-group shiny-input-container">
<label class="control-label" id="countsModule-epoch-label" for="countsModule-epoch">What epoch level?</label>
<input class="js-range-slider" id="countsModule-epoch" data-skin="shiny" data-min="1" data-max="60" data-from="30" data-step="1" data-grid="false" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-postfix=" seconds" data-keyboard="true" data-data-type="number"/>
</div>
<div class="form-group shiny-input-container">
<div class="checkbox">
<label>
<input id="countsModule-lfe" type="checkbox"/>
<span>Add a low frequency extension filter?</span>
</label>
</div>
</div>
<h5><b>Plot Settings for Calculate Counts</b></h5>
<div class="form-group shiny-input-container">
<label class="control-label" id="countsModule-axisCounts-label" for="countsModule-axisCounts">Counts Axis</label>
<div>
<select id="countsModule-axisCounts"><option value="Axis1">Axis1</option>
<option value="Axis2">Axis2</option>
<option value="Axis3">Axis3</option>
<option value="Vector.Magnitude" selected>Vector.Magnitude</option></select>
<script type="application/json" data-for="countsModule-axisCounts" data-nonempty="">{"plugins":["selectize-plugin-a11y"]}</script>
</div>
</div>
<div class="form-group shiny-input-container">
<div class="checkbox">
<label>
<input id="countsModule-excludeZeros" type="checkbox"/>
<span>Exclude zeros from the plot?</span>
</label>
</div>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="countsModule-binwidthCounts-label" for="countsModule-binwidthCounts">Select a frequency polygon binwidth</label>
<input id="countsModule-binwidthCounts" type="number" class="form-control" value="30" step="10"/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="countsModule-countsPlotColor-label" for="countsModule-countsPlotColor">Plot Color (accepts color name or hex code)</label>
<input id="countsModule-countsPlotColor" type="text" class="form-control" value="#000000"/>
</div>
<div class="form-group shiny-input-container">
<label class="control-label" id="countsModule-rangeCounts-label" for="countsModule-rangeCounts">Select a range for the X axis</label>
<input class="js-range-slider" id="countsModule-rangeCounts" data-skin="shiny" data-type="double" data-min="0" data-max="10000" data-from="0" data-to="2000" data-step="1" data-grid="true" data-grid-num="10" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-postfix=" counts" data-keyboard="true" data-drag-interval="true" data-data-type="number"/>
</div>
</form>
</div>
<div class="col-sm-8" role="main">
<div class="tabbable">
<ul class="nav nav-tabs shiny-tab-input" id="countsTabset" data-tabsetid="4785">
<li class="active">
<a href="#tab-4785-1" data-toggle="tab" data-bs-toggle="tab" data-value="Visualization">Visualization</a>
</li>
<li>
<a href="#tab-4785-2" data-toggle="tab" data-bs-toggle="tab" data-value="Data">Data</a>
</li>
</ul>
<div class="tab-content" data-tabsetid="4785">
<div class="tab-pane active" data-value="Visualization" id="tab-4785-1">
<div class="shiny-plot-output html-fill-item" id="countsModule-countsPlot" style="width:100%;height:400px;"></div>
</div>
<div class="tab-pane" data-value="Data" id="tab-4785-2">
<h5>Total and average accelerometer counts from agcounts</h5>
<div class="reactable html-widget html-widget-output shiny-report-size html-fill-item-overflow-hidden html-fill-item" data-reactable-output="countsModule-countsReactableTable" id="countsModule-countsReactableTable" style="width:auto;height:auto;"></div>
</div>
</div>
</div>
</div>
</div>

97 changes: 97 additions & 0 deletions tests/testthat/_snaps/rawDataModule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# shiny rawDataModuleUI creates expected HTML

Code
.testHTML_rawDataModuleUI("rawDataModule")
Output
<div class="row">
<div class="col-sm-4">
<form class="well" role="complementary">
<div class="form-group shiny-input-container">
<label class="control-label" id="rawDataModule-gt3xFile-label" for="rawDataModule-gt3xFile">Choose GT3X File</label>
<div class="input-group">
<label class="input-group-btn input-group-prepend">
<span class="btn btn-default btn-file">
Browse...
<input id="rawDataModule-gt3xFile" name="rawDataModule-gt3xFile" type="file" style="position: absolute !important; top: -99999px !important; left: -99999px !important;" accept=".gt3x"/>
</span>
</label>
<input type="text" class="form-control" placeholder="No file selected" readonly="readonly"/>
</div>
<div id="rawDataModule-gt3xFile_progress" class="progress active shiny-file-input-progress">
<div class="progress-bar"></div>
</div>
</div>
<div id="rawDataModule-parser" class="form-group shiny-input-radiogroup shiny-input-container shiny-input-container-inline" role="radiogroup" aria-labelledby="rawDataModule-parser-label">
<label class="control-label" id="rawDataModule-parser-label" for="rawDataModule-parser">Select your parser</label>
<div class="shiny-options-group">
<label class="radio-inline">
<input type="radio" name="rawDataModule-parser" value="pygt3x"/>
<span>pygt3x</span>
</label>
<label class="radio-inline">
<input type="radio" name="rawDataModule-parser" value="ggir"/>
<span>ggir</span>
</label>
<label class="radio-inline">
<input type="radio" name="rawDataModule-parser" value="uncalibrated"/>
<span>uncalibrated</span>
</label>
<label class="radio-inline">
<input type="radio" name="rawDataModule-parser" value="agcalibrate"/>
<span>agcalibrate</span>
</label>
</div>
</div>
<div id="rawDataModule-dateAccessed" class="shiny-html-output"></div>
<div id="rawDataModule-timeSlot" class="shiny-html-output"></div>
<div class="form-group shiny-input-container">
<label class="control-label" id="rawDataModule-axisRaw-label" for="rawDataModule-axisRaw">Raw Axis</label>
<div>
<select id="rawDataModule-axisRaw"><option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option>
<option value="Vector.Magnitude" selected>Vector.Magnitude</option></select>
<script type="application/json" data-for="rawDataModule-axisRaw" data-nonempty="">{"plugins":["selectize-plugin-a11y"]}</script>
</div>
</div>
<div id="rawDataModule-applyRaw" class="shiny-html-output"></div>
<div id="rawDataModule-applyEpoch" class="shiny-html-output"></div>
<h5><b>Plot Settings for Raw Data</b></h5>
<div class="form-group shiny-input-container">
<label class="control-label" id="rawDataModule-gt3xPlotColor-label" for="rawDataModule-gt3xPlotColor">Plot Color (accepts color name or hex code)</label>
<input id="rawDataModule-gt3xPlotColor" type="text" class="form-control" value="#000000"/>
</div>
<div id="rawDataModule-rangeXraw" class="shiny-html-output"></div>
<div id="rawDataModule-rangeYraw" class="shiny-html-output"></div>
</form>
</div>
<div class="col-sm-8" role="main">
<div class="tabbable">
<ul class="nav nav-tabs shiny-tab-input" id="rawTabset" data-tabsetid="4785">
<li class="active">
<a href="#tab-4785-1" data-toggle="tab" data-bs-toggle="tab" data-value="Visualization">Visualization</a>
</li>
<li>
<a href="#tab-4785-2" data-toggle="tab" data-bs-toggle="tab" data-value="Data">Data</a>
</li>
<li>
<a href="#tab-4785-3" data-toggle="tab" data-bs-toggle="tab" data-value="Notes">Notes</a>
</li>
</ul>
<div class="tab-content" data-tabsetid="4785">
<div class="tab-pane active" data-value="Visualization" id="tab-4785-1">
<div class="shiny-plot-output html-fill-item" id="rawDataModule-gt3xPlot" style="width:100%;height:400px;"></div>
</div>
<div class="tab-pane" data-value="Data" id="tab-4785-2">
<h5> Average Raw Acceleration Data by Hour </h5>
<div class="reactable html-widget html-widget-output shiny-report-size html-fill-item-overflow-hidden html-fill-item" data-reactable-output="rawDataModule-rawReactableTable" id="rawDataModule-rawReactableTable" style="width:auto;height:auto;"></div>
</div>
<div class="tab-pane" data-value="Notes" id="tab-4785-3">
<div id="rawDataModule-sampleFrequency" class="shiny-text-output"></div>
<div id="rawDataModule-calibrationMethod" class="shiny-html-output"></div>
</div>
</div>
</div>
</div>
</div>

45 changes: 31 additions & 14 deletions tests/testthat/test_actilife_counts.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@

testthat::test_that("agcounts and Actilife counts are the same",{

gt3x.file <- system.file("extdata/testGT3X.RData", package = "agcounts")
load(gt3x.file)
testGT3X %<>% calculate_counts(raw = ., epoch = 1)

agd.file <- system.file("extdata/testAGD.RData", package = "agcounts")
load(agd.file)

expect_equal(nrow(testAGD), nrow(testGT3X))
compare(testAGD$` Axis1`, testGT3X$Axis1, tolerance = 0.05)
compare(testAGD$Axis2, testGT3X$Axis2, tolerance = 0.05)
compare(testAGD$Axis3, testGT3X$Axis3, tolerance = 0.05)
compare(round(testAGD$`Vector Magnitude`), testGT3X$Vector.Magnitude, tolerance = 0.05)


# Load Data
load(system.file("extdata/calibrationXaxis.RData", package = "agcounts"))
load(system.file("extdata/calibrationYaxis.RData", package = "agcounts"))
load(system.file("extdata/calibrationZaxis.RData", package = "agcounts"))
agdPath <- system.file("extdata/calibration1sec.agd", package = "agcounts")

# Recreate time stamps
sf = 30
start <- as.POSIXct("2023-04-24 15:02:00", tz = "UTC")
end <- as.POSIXct("2023-04-25 11:46:13", tz = "UTC") + 1
time <- seq(from = start, to = end - 0.001, by = 1/sf)

# Merge X, Y, and Z data together in a matrix
rawData <- data.frame(time, X, Y, Z)

# Calculate Counts
gt3xData <- calculate_counts(raw = rawData, epoch = 1)

# Read in agd data
agdData <- .read_agd(agdPath)$data

# Remove rows that are not in the gt3xData file
agdData <- agdData[-which(!agdData$time %in% gt3xData$time), ]

# Tests
expect_equal(nrow(agdData), nrow(gt3xData))
expect_equal(agdData$Axis1, gt3xData$Axis1)
expect_equal(agdData$Axis2, gt3xData$Axis2)
expect_equal(agdData$Axis3, gt3xData$Axis3)
expect_equal(agdData$Vector.Magnitude, gt3xData$Vector.Magnitude)
})
25 changes: 25 additions & 0 deletions tests/testthat/test_agread.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


testthat::test_that("Check that data can be read into agcounts using each agread method", {

file <- system.file("extdata/example.gt3x", package = "agcounts")

pygt3x <- agread(path = file, parser = "pygt3x")
expect_equal(nrow(pygt3x), 16200)
expect_equal(ncol(pygt3x), 4)

ggir <- agread(path = file, parser = "ggir")
expect_equal(nrow(ggir), 16200)
expect_equal(ncol(ggir), 4)

uncalibrated <- agread(path = file, parser = "uncalibrated")
expect_equal(nrow(uncalibrated), 16200)
expect_equal(ncol(uncalibrated), 4)

raw <- read.gt3x(path = file, asDataFrame = TRUE)
sf <- .get_frequency(raw)
agcalibrated <- agcalibrate(raw)
expect_equal(nrow(agcalibrated), 16201) # Looks like agcalibrate produces 1 additional row of data
expect_equal(ncol(agcalibrated), 4)

})
Loading

0 comments on commit a7e19a3

Please sign in to comment.