Skip to content

Commit

Permalink
Merge pull request #148 from Nathaniel-Fernandes/master
Browse files Browse the repository at this point in the history
Minor updates
  • Loading branch information
irinagain authored Apr 13, 2024
2 parents cca3e8e + 752fe6b commit db465af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# iglu 4.0.2
* Fixed broken whiplash plot in MAGE documentation
* Added MAGEmax plotting functionality

# iglu 4.0.0
* CRAN release to match upcoming paper summarizing updated MAGE algorithm, episode calculation functionality, meal metrics, clustering and new example data inclusion

Expand Down
27 changes: 13 additions & 14 deletions R/mage_ma_single.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mage_ma_single <- function(data,

## 0. Calculates MAGE on 1 segment of CGM trace
mage_atomic <- function(.data) {
nmeasurements = list_cross = types = count = crosses = num_extrema = minmax = indexes = s1 = s2 = standardD = heights = nadir2peak = idx = peak_or_nadir = plus_or_minus = first_excursion = NULL
nmeasurements = list_cross = types = count = crosses = num_extrema = minmax = indexes = s1 = s2 = standardD = heights = nadir2peak = idx = peak_or_nadir = plus_or_minus = first_excursion = max_direction = NULL
rm(list=c('nmeasurements', 'list_cross', 'types', 'count', 'crosses', 'num_extrema', 'minmax', 'indexes', 's1', 's2', 'standardD', 'heights', 'nadir2peak', 'idx', 'peak_or_nadir', 'plus_or_minus', 'first_excursion'))

if (all(is.na(.data$gl))) {
Expand Down Expand Up @@ -224,29 +224,30 @@ mage_ma_single <- function(data,
return(data.frame(start=utils::head(.data$time, 1), end=utils::tail(.data$time, 1), mage=NA, plus_or_minus=NA, first_excursion=NA))
}

plus_first = ifelse((length(mage_plus_heights) > 0) && (length(mage_minus_heights) == 0 || mage_plus_tp_pairs[[1]][2] <= mage_minus_tp_pairs[[1]][1]), TRUE, FALSE)
mage_plus = data.frame(start=utils::head(.data$time, 1), end=utils::tail(.data$time, 1), mage=ifelse(length(mage_plus_heights), mean(mage_plus_heights, na.rm = TRUE), NA), plus_or_minus="PLUS", first_excursion=plus_first)
mage_minus = data.frame(start=utils::head(.data$time, 1), end=utils::tail(.data$time, 1), mage=ifelse(length(mage_minus_heights), abs(mean(mage_minus_heights, na.rm = TRUE)), NA), plus_or_minus="MINUS", first_excursion=!plus_first)
is_plus_first = ifelse((length(mage_plus_heights) > 0) && (length(mage_minus_heights) == 0 || mage_plus_tp_pairs[[1]][2] <= mage_minus_tp_pairs[[1]][1]), TRUE, FALSE)
mage_plus = data.frame(start=utils::head(.data$time, 1), end=utils::tail(.data$time, 1), mage=ifelse(length(mage_plus_heights), mean(mage_plus_heights, na.rm = TRUE), NA), plus_or_minus="PLUS", first_excursion=is_plus_first)
mage_minus = data.frame(start=utils::head(.data$time, 1), end=utils::tail(.data$time, 1), mage=ifelse(length(mage_minus_heights), abs(mean(mage_minus_heights, na.rm = TRUE)), NA), plus_or_minus="MINUS", first_excursion=!is_plus_first)
is_plus_max = ifelse(mage_plus$mage >= mage_minus$mage, TRUE, FALSE)

# save data for plotting
pframe = parent.frame()
pframe$all_data = base::rbind(pframe$all_data, .data)

for (e in mage_plus_tp_pairs) {
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[1]], peak_or_nadir="NADIR", plus_or_minus="PLUS", first_excursion=plus_first))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[2]], peak_or_nadir="PEAK", plus_or_minus="PLUS", first_excursion=plus_first))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[1]], peak_or_nadir="NADIR", plus_or_minus="PLUS", first_excursion=is_plus_first, max_direction=is_plus_max))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[2]], peak_or_nadir="PEAK", plus_or_minus="PLUS", first_excursion=is_plus_first, max_direction=is_plus_max))
}

for (e in mage_minus_tp_pairs) {
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[1]], peak_or_nadir="PEAK", plus_or_minus="MINUS", first_excursion=!plus_first))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[2]], peak_or_nadir="NADIR", plus_or_minus="MINUS", first_excursion=!plus_first))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[1]], peak_or_nadir="PEAK", plus_or_minus="MINUS", first_excursion=!is_plus_first, max_direction=!is_plus_max))
pframe$all_tp_indexes = base::rbind(pframe$all_tp_indexes, data.frame(idx=indexes[e[2]], peak_or_nadir="NADIR", plus_or_minus="MINUS", first_excursion=!is_plus_first, max_direction=!is_plus_max))
}

return(base::rbind(mage_plus, mage_minus))
}

## 1. Preprocessing
MA_Short = MA_Long = DELTA_SHORT_LONG = TP = id = .xmin = .xmax = gap = x = y = xend = yend = hours = weight = idx = peak_or_nadir = plus_or_minus = first_excursion = NULL
MA_Short = MA_Long = DELTA_SHORT_LONG = TP = id = .xmin = .xmax = gap = x = y = xend = yend = hours = weight = idx = peak_or_nadir = plus_or_minus = first_excursion = max_direction = NULL
rm(list = c("MA_Short", "MA_Long", "DELTA_SHORT_LONG", "TP", ".xmin", ".xmax", "id", "gap", "x", "y", "xend", "yend", "hours", "weight", "idx", "peak_or_nadir", "plus_or_minus", "first_excursion"))

data = check_data_columns(data)
Expand Down Expand Up @@ -372,14 +373,12 @@ mage_ma_single <- function(data,
direction = match.arg(direction, c('avg', 'service', 'max', 'plus', 'minus'))
plot_type = match.arg(plot_type, c('ggplot', 'plotly'))

if (direction == "max") {
stop("Plotting functionality for MAGEmax is not possible right now. Please request this feature on GitHub if you'd like it. Thank you for your patience.")
}

if (direction == "avg") {
if (direction == 'avg') {
tp_indexes <- dplyr::select(all_tp_indexes, idx, peak_or_nadir, plus_or_minus)
} else if (direction == 'service') {
tp_indexes <- dplyr::filter(all_tp_indexes, first_excursion==TRUE) %>% dplyr::select(idx, peak_or_nadir, plus_or_minus)
} else if (direction == 'max') {
tp_indexes <- dplyr::filter(all_tp_indexes, max_direction==TRUE) %>% dplyr::select(idx, peak_or_nadir, plus_or_minus)
} else {
tp_indexes <- dplyr::filter(all_tp_indexes, plus_or_minus==ifelse(direction == 'plus', "PLUS", "MINUS")) %>% dplyr::select(idx, peak_or_nadir, plus_or_minus)
}
Expand Down
4 changes: 2 additions & 2 deletions vignettes/MAGE.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Service et al. [3] by two of the authors, who were unaware of the
outcome of the proposed algorithm's outputs.

| Dataset | Diabetes Type | Age group | Subject ID | Number of days used (CGM traces) |
|:------------------------:|:----------:|:----------:|:----------:|:----------:|
|:------------------:|:-----------:|:-----------:|:-----------:|:-----------:|
| [Hall et al. (2018)](https://doi.org/10.1371/journal.pbio.2005143) | None | Adults (\> 18) | 1636-69-001 | 5 |
| | | | 1636-69-026 | 6 |
| [Tsalikian et al. (2005)](https://doi.org/10.1016/j.jpeds.2005.04.065) | Type 1 | Children (10 - 18) | 2 | 1 |
Expand Down Expand Up @@ -551,7 +551,7 @@ correction for whiplash. Notice the true peak is properly identified for
$tp_{12}$.

```{r, echo=FALSE, fig.width=10, fig.height=7, out.width="700px", out.height ="400px"}
knitr::include_graphics("mage_figures/boxplot-of-errors.png")
knitr::include_graphics("mage_figures/whiplash2.png")
```

**Figure 6.** Plot of Subject 1636-69-026 via MAGE iglu v3.5.0 without
Expand Down
2 changes: 1 addition & 1 deletion vignettes/iglu.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Multiple CGM-derived metrics have been developed to assess the quality of glycem

The iglu package is designed to work with Continuous Glucose Monitor (CGM) data in the form of a data frame with the following three columns present:

* Blood glucose level measurement [in mg/dL] (`"gl"`)
* Glucose level measurement [in mg/dL] (`"gl"`)

* Timestamp for glucose measurement (`"time"`)

Expand Down

0 comments on commit db465af

Please sign in to comment.