Skip to content

Commit

Permalink
476: be more robust against TMB version/order (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielinteractive authored Oct 11, 2024
1 parent df3418d commit 9de9a7e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

### Bug Fixes

- Previously, when compiling `mmrm` from source using a `TMB` version below 1.9.15, and installing a newer `TMB` of version 1.9.15 or above, would render the `mmrm` package unusable. This is fixed now, by checking in the dynamic library of `mmrm` whether the version of `TMB` has been sufficient.

# mmrm 0.3.14

### Bug Fixes

- In version 0.3.13, when the tape optimizer from `TMB` was switched on, a warning would be given by `fit_mmrm()`, instructing users to turn off the tape optimizer. However, this is not necessary for reproducible results. Instead, it is now checked whether the deterministic hash for the `TMB` tape optimizer is used, and a warning is issued otherwise.
- In version 0.3.13, the above described warning by `fit_mmrm()` was not visible to the user when calling `mmrm()` because it was caught internally, causing the first fit in each session to fail for the first tried optimizer and falling back to the other optimizers. The warning is now issued directly by `mmrm()`. This change ensures that the first model fit is consistent regarding the chosen optimizer (and thus numeric results) with subsequent model fits, avoiding discrepancies observed in version 0.3.13.

Expand Down
14 changes: 13 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ h_drop_levels <- function(data, subject_var, visit_var, except) {
data
}

#' Predicate if the TMB Version Used to Compile the Package is Sufficient
#'
#' @return Flag whether the TMB version is sufficient.
#' @keywords internal
h_tmb_version_sufficient <- function() {
# Note: There is no version information saved in the dynamic library, but
# we can check like this:
tmb_config <- TMB::config(DLL = "mmrm")
tape_deterministic <- tmb_config$tmbad_deterministic_hash
!is.null(tape_deterministic)
}

#' Warn if TMB is Configured to Use Non-Deterministic Hash for Tape Optimizer
#'
#' This function checks the TMB configuration for the `tmbad_deterministic_hash` setting
Expand All @@ -533,7 +545,7 @@ h_drop_levels <- function(data, subject_var, visit_var, except) {
#' @return No return value, called for side effects.
#' @keywords internal
h_tmb_warn_non_deterministic <- function() {
if (utils::packageVersion("TMB") < "1.9.15") {
if (!h_tmb_version_sufficient()) {
return()
}
tmb_config <- TMB::config(DLL = "mmrm")
Expand Down
9 changes: 7 additions & 2 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
#' @keywords internal
#' @noRd
.onLoad <- function(libname, pkgname) { # nolint
if (utils::packageVersion("TMB") < "1.9.15") {
warning("TMB version 1.9.15 or higher is required for reproducible model fits", call. = FALSE)
if (!h_tmb_version_sufficient()) {
msg <- paste(
"TMB below version 1.9.15 has been used to compile the mmrm package.",
"Reproducible model fits are not guaranteed.",
"Please consider recompiling the package with TMB version 1.9.15 or higher."
)
warning(msg, call. = FALSE)
}

register_on_load(
Expand Down
15 changes: 15 additions & 0 deletions man/h_tmb_version_sufficient.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ test_that("h_drop_levels works as expected", {
)
})

# h_tmb_version_sufficient ----

test_that("h_tmb_version_sufficient works as expected", {
skip_if(utils::packageVersion("TMB") < "1.9.15")
expect_true(h_tmb_version_sufficient())
})

# h_tmb_warn_non_deterministic ----

test_that("h_tmb_warn_non_deterministic works as expected", {
Expand Down

0 comments on commit 9de9a7e

Please sign in to comment.