Skip to content

Commit

Permalink
Merge branch 'master' of github.com:walterwilliamson/iglu
Browse files Browse the repository at this point in the history
adding recent iglu updates
walterwilliamson committed Oct 9, 2024
2 parents 0709ea8 + 5acb6d0 commit 8a2b937
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/heatmap.R
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
#' @param clustering_distance_subjects the distance measure for subjects clustering, accepts same values as \code{\link{dist}}, default value is 'correlation' distance
#' @param tz \strong{Default: "".} A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning.
#'
#' @return A heatmap of metrics by subjects generated via \code{\link{pheatmap}}
#' @return A heatmap of metrics by subjects generated via \code{\link[pheatmap]{pheatmap}}
#' @export
#'
#' @examples
2 changes: 1 addition & 1 deletion R/hyper_index.R
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
#' a column for the Hyperglycemia Index values is returned. NA glucose values are
#' omitted from the calculation of the Hyperglycemia Index values.
#'
#' Hyperglycemia Index is calculated by \eqn{\frac{1}{n \cdot c} \sum \left( \text{hyperBG}_j - \text{ULTR} \right)^a}.
#' Hyperglycemia Index is calculated by \eqn{\frac{1}{n \cdot c} \sum \left(hyperBG_j - ULTR \right)^a}.
#' Here n is the total number of Glucose measurements (excluding NA values), \eqn{hyperBG_j}
#' is the jth Glucose measurement above the ULTR cutoff, a is an exponent, and c is a scaling factor.
#'
2 changes: 1 addition & 1 deletion R/hypo_index.R
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
#' a column for the Hypoglycemia Index values is returned. NA glucose values are
#' omitted from the calculation of the Hypoglycemia Index values.
#'
#' Hypoglycemia Index is calculated by \eqn{\frac{1}{n \cdot d} \sum \left( \text{ULTR} - \text{hyperBG}_j \right)^b}.
#' Hypoglycemia Index is calculated by \eqn{\frac{1}{n \cdot d} \sum \left(ULTR - hyperBG_j \right)^b}.
#' Here n is the total number of Glucose measurements (excluding NA values),
#' and \eqn{hypoBG_j} is the jth Glucose measurement below the LLTR cutoff, b is an exponent, and d is a scaling factor.
#'
2 changes: 1 addition & 1 deletion R/lasagna_plots.R
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
#'
#' @inheritParams conga
#' @param lasagnatype \strong{Default: "unsorted".} String corresponding to plot type, currently supported options are 'unsorted' for an unsorted single-subject lasagna plot, 'timesorted' for a lasagna plot with glucose values sorted within each time point across days, and 'daysorted' for a lasagna plot with glucose values sorted within each day across time points.
#' @param limits The minimal and maximal glucose values for coloring grid which is gradient from blue (minimal) to red (maximal), see \code{\link{scale_fill_gradient2}})
#' @param limits The minimal and maximal glucose values for coloring grid which is gradient from blue (minimal) to red (maximal), see \code{\link[ggplot2]{scale_fill_gradient2}})
#' @param midpoint The glucose value serving as midpoint of the diverging gradient scale (see \code{\link{scale_fill_gradient2}}). The default value is 105 mg/dL. The values above are colored in red, and below in blue in the default color_scheme, which can be adjusted.
#' @param dt0 The time frequency for interpolated aligned grid in minutes, the default will match the CGM meter's frequency (e.g. 5 min for Dexcom).
#' @param inter_gap The maximum allowable gap (in minutes) for interpolation of
33 changes: 33 additions & 0 deletions tests/testthat/test-cogi.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Test on one subject data
test1 = iglu::example_data_1_subject
test2 = iglu::example_data_1_subject[1:300, ]
test3 = iglu::example_data_5_subject

out = iglu::cogi(iglu::example_data_5_subject)$COGI


test_cogi = function(data, targets = c(70, 180), weights = c(.5, .35, .15)){
ir = in_range_percent(data, list(targets))[[2]] * weights[1]
br = max(min(((-20/3) * below_percent(data, targets_below = targets[1])[[2]] + 100), 100), 0) * weights[2]
st = max(min(((-10/9) * sd_glu(data)[[2]] + 120), 100), 0) * weights[3]
cogi = ir + br + st
return(cogi)
}

testthat::test_that("cogi works", {
testthat::expect_equal(iglu::cogi(test1)$COGI[1], test_cogi(test1), tolerance = 0.0001)
testthat::expect_equal(iglu::cogi(test1, targets = c(80, 150), weights = c(0.2,0.6,0.2))$COGI[1], test_cogi(test1, targets = c(80, 150), weights = c(0.2,0.6,0.2)), tolerance = 0.0001)
testthat::expect_equal(iglu::cogi(test2)$COGI[1], test_cogi(test2), tolerance = 0.0001)
testthat::expect_equal(out[1], test_cogi(test3[test3$id=='Subject 1',]), tolerance = 0.0001)
testthat::expect_equal(out[2], test_cogi(test3[test3$id=='Subject 2',]), tolerance = 0.0001)
testthat::expect_equal(out[3], test_cogi(test3[test3$id=='Subject 3',]), tolerance = 0.0001)
testthat::expect_equal(out[4], test_cogi(test3[test3$id=='Subject 4',]), tolerance = 0.0001)
testthat::expect_equal(out[5], test_cogi(test3[test3$id=='Subject 5',]), tolerance = 0.0001)
})







32 changes: 32 additions & 0 deletions tests/testthat/test-modd.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Test on one subject data
test1 = iglu::example_data_1_subject
test2 = iglu::example_data_1_subject[1:300, ]
test3 = iglu::example_data_5_subject

out = iglu::modd(iglu::example_data_5_subject)$MODD


test_modd = function(data, lag = 1){
data_ip <- CGMS2DayByDay(data)
gl_by_day <- data_ip$gd2d
modd <- mean(abs(diff(gl_by_day, lag = lag)), na.rm = TRUE)
return(modd)
}

testthat::test_that("modd works", {
testthat::expect_equal(iglu::modd(test1)$MODD[1], test_modd(test1), tolerance = 0.0001)
testthat::expect_equal(iglu::modd(test1, lag = 2)$MODD[1], test_modd(test1, lag = 2), tolerance = 0.0001)
testthat::expect_equal(iglu::modd(test2)$MODD[1], test_modd(test2), tolerance = 0.0001)
testthat::expect_equal(out[1], test_modd(test3[test3$id=='Subject 1',]), tolerance = 0.0001)
testthat::expect_equal(out[2], test_modd(test3[test3$id=='Subject 2',]), tolerance = 0.0001)
testthat::expect_equal(out[3], test_modd(test3[test3$id=='Subject 3',]), tolerance = 0.0001)
testthat::expect_equal(out[4], test_modd(test3[test3$id=='Subject 4',]), tolerance = 0.0001)
testthat::expect_equal(out[5], test_modd(test3[test3$id=='Subject 5',]), tolerance = 0.0001)
})







0 comments on commit 8a2b937

Please sign in to comment.