From ab0b6f9b4799e370c929bebe9873df4e41eb9d69 Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Wed, 22 Mar 2023 12:35:41 -0400 Subject: [PATCH 1/7] check lp_solver setting --- .../delphiBackfillCorrection/R/utils.R | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/backfill_corrections/delphiBackfillCorrection/R/utils.R b/backfill_corrections/delphiBackfillCorrection/R/utils.R index 9cf7d581f..fa82e418f 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/utils.R +++ b/backfill_corrections/delphiBackfillCorrection/R/utils.R @@ -67,9 +67,24 @@ read_params <- function(path = "params.json", template_path = "params.json.templ # Model parameters if (!("taus" %in% names(params))) {params$taus <- TAUS} if (!("lambda" %in% names(params))) {params$lambda <- LAMBDA} - if (!("lp_solver" %in% names(params))) {params$lp_solver <- LP_SOLVER} if (!("lag_pad" %in% names(params))) {params$lag_pad <- LAG_PAD} + if ("lp_solver" %in% names(params)) { + params$lp_solver <- match.arg(params$lp_solver, c("gurobi", "glpk")) + } else { + params$lp_solver <- LP_SOLVER + } + if (params$lp_solver == "gurobi" && ( + !("gurobi" %in% names(params)) || + !params_element_exists_and_valid(params$gurobi, "GRB_LICENSEID") || + !params_element_exists_and_valid(params$gurobi, "GRB_WLSACCESSID") || + !params_element_exists_and_valid(params$gurobi, "GRB_WLSSECRET") + )) { + warning("gurobi solver was requested but license information was ", + "not available; using glpk instead") + params$lp_solver <- "glpk" + } + # Data parameters if (!("num_col" %in% names(params))) {params$num_col <- "num"} if (!("denom_col" %in% names(params))) {params$denom_col <- "denom"} From cd7c4c0ade1dfdf5066f1dd4c7c08835e29dbe8f Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Wed, 22 Mar 2023 13:16:08 -0400 Subject: [PATCH 2/7] test lp_solver selection logic --- .../testthat/params-glpk_direct.json.template | 4 ++ .../testthat/params-grb.json.template | 9 ++++ .../params-grb_noparams.json.template | 4 ++ .../params-grb_notallparams.json.template | 5 ++ .../unit-tests/testthat/test-utils.R | 46 +++++++++++++++++-- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template create mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template create mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template create mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template new file mode 100644 index 000000000..c92fd1276 --- /dev/null +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template @@ -0,0 +1,4 @@ +{ + "input_dir": "./test.temp", + "lp_solver": "glpk" +} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template new file mode 100644 index 000000000..1a21af670 --- /dev/null +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template @@ -0,0 +1,9 @@ +{ + "input_dir": "./test.temp", + "lp_solver": "gurobi", + "gurobi": { + "GRB_LICENSEID": "1", + "GRB_WLSACCESSID": "2", + "GRB_WLSSECRET": "3" + } +} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template new file mode 100644 index 000000000..274503ea9 --- /dev/null +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template @@ -0,0 +1,4 @@ +{ + "input_dir": "./test.temp", + "lp_solver": "gurobi" +} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template new file mode 100644 index 000000000..2d45b5bdf --- /dev/null +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template @@ -0,0 +1,5 @@ +{ + "input_dir": "./test.temp", + "lp_solver": "gurobi", + "gurobi": {"GRB_WLSACCESSID": ""} +} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R index 6529e4e5e..320d94a01 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R @@ -81,9 +81,11 @@ test_that("testing read parameters", { # Create input file path = "test.temp" create_dir_not_exist(path) - expect_silent(params <- read_params(path = "params-test.json", + expect_warning(params <- read_params(path = "params-test.json", template_path = "params-test.json.template", - train_models = TRUE, make_predictions = TRUE)) + train_models = TRUE, make_predictions = TRUE), + "gurobi solver was requested but license information was not available" + ) unlink(path, recursive = TRUE) @@ -125,7 +127,7 @@ test_that("testing read parameters", { expect_true(all(params$taus == TAUS)) expect_true(params$lambda == LAMBDA) - expect_true(params$lp_solver == LP_SOLVER) + expect_true(params$lp_solver == "glpk") expect_true(params$lag_pad == LAG_PAD) expect_true(params$num_col == "num") @@ -148,6 +150,44 @@ test_that("testing read parameters", { expect_silent(file.remove("params-test.json")) }) +test_that("lp_solver selection works", { + # GLPK selected explicitly. + path = "test.temp" + create_dir_not_exist(path) + expect_silent(params <- read_params(path = "params-glpk_direct.json", + template_path = "params-glpk_direct.json.template", + train_models = TRUE, make_predictions = TRUE)) + expect_true(params$lp_solver == "glpk") + expect_silent(file.remove("params-glpk_direct.json")) + + # gurobi selected explicitly, but without gurobi params subsection + expect_warning(params <- read_params(path = "params-grb_noparams.json", + template_path = "params-grb_noparams.json.template", + train_models = TRUE, make_predictions = TRUE), + "gurobi solver was requested but license information was not available" + ) + expect_true(params$lp_solver == "glpk") + expect_silent(file.remove("params-grb_noparams.json")) + + # gurobi selected explicitly, with gurobi params subsection, but without license information + expect_warning(params <- read_params(path = "params-grb_notallparams.json", + template_path = "params-grb_notallparams.json.template", + train_models = TRUE, make_predictions = TRUE), + "gurobi solver was requested but license information was not available" + ) + expect_true(params$lp_solver == "glpk") + expect_silent(file.remove("params-grb_notallparams.json")) + + # gurobi selected explicitly + expect_silent(params <- read_params(path = "params-grb.json", + template_path = "params-grb.json.template", + train_models = TRUE, make_predictions = TRUE)) + expect_true(params$lp_solver == "gurobi") + expect_silent(file.remove("params-grb.json")) + + unlink(path, recursive = TRUE) +}) + test_that("validity_checks alerts appropriately", { time_value = as.Date(c("2022-01-01", "2022-01-02", "2022-01-03")) issue_date = as.Date(c("2022-01-05", "2022-01-05", "2022-01-05")) From 023f54fa4d02e2dd46e555a0032aff3c39e40abc Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Wed, 22 Mar 2023 18:08:03 -0400 Subject: [PATCH 3/7] capture license expiration error --- backfill_corrections/delphiBackfillCorrection/R/model.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backfill_corrections/delphiBackfillCorrection/R/model.R b/backfill_corrections/delphiBackfillCorrection/R/model.R index 93fc5a2bd..e9b1c4685 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/model.R +++ b/backfill_corrections/delphiBackfillCorrection/R/model.R @@ -131,7 +131,14 @@ model_training_and_testing <- function(train_data, test_data, taus, covariates, success = success + 1 }, - error=function(e) {msg_ts("Training failed for ", model_path)} + error=function(e) { + if (e$message == "Error 10032: License has expired\n") { + stop("The gurobi license has expired. Please renew or switch to ", + "using glpk. lp_solver can be specified in params.json.") + } + msg_ts("Training failed for ", model_path, ". Check that your gurobi ", + "license is valid and being passed properly to the program.") + } ) } if (success < length(taus)) {return (NULL)} From b793fe12d924ff5615904b280fe018176365bd96 Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Thu, 23 Mar 2023 14:29:02 -0400 Subject: [PATCH 4/7] use glpk explicitly in tests; default to glpk if gurobi license missing --- .../delphiBackfillCorrection/R/utils.R | 31 +++++++++--- .../delphiBackfillCorrection/man/run_cli.Rd | 14 ++++++ ...son.template => params-glpk.json.template} | 0 .../testthat/params-grb.json.template | 9 ---- .../params-grb_notallparams.json.template | 5 -- ...n.template => params-gurobi.json.template} | 0 .../testthat/params-run.json.template | 3 +- .../testthat/params-test.json.template | 3 +- .../testthat/test-beta_prior_estimation.R | 4 +- .../unit-tests/testthat/test-model.R | 6 +-- .../unit-tests/testthat/test-utils.R | 50 +++++++------------ 11 files changed, 66 insertions(+), 59 deletions(-) create mode 100644 backfill_corrections/delphiBackfillCorrection/man/run_cli.Rd rename backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/{params-glpk_direct.json.template => params-glpk.json.template} (100%) delete mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template delete mode 100644 backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template rename backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/{params-grb_noparams.json.template => params-gurobi.json.template} (100%) diff --git a/backfill_corrections/delphiBackfillCorrection/R/utils.R b/backfill_corrections/delphiBackfillCorrection/R/utils.R index fa82e418f..b85613e9b 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/utils.R +++ b/backfill_corrections/delphiBackfillCorrection/R/utils.R @@ -74,14 +74,24 @@ read_params <- function(path = "params.json", template_path = "params.json.templ } else { params$lp_solver <- LP_SOLVER } - if (params$lp_solver == "gurobi" && ( - !("gurobi" %in% names(params)) || - !params_element_exists_and_valid(params$gurobi, "GRB_LICENSEID") || - !params_element_exists_and_valid(params$gurobi, "GRB_WLSACCESSID") || - !params_element_exists_and_valid(params$gurobi, "GRB_WLSSECRET") - )) { + # Make call to gurobi CLI to check license. Returns a status of `0` if + # license can be found and is valid. + tryCatch( + expr = { + license_status <- run_cli("gurobi_cl") + }, + error=function(e) { + if (e$message == "Error 10032: License has expired\n") { + stop("The gurobi license has expired. Please renew or switch to ", + "using glpk. lp_solver can be specified in params.json.") + } + msg_ts(e$message) + license_status <- 1 + } + ) + if (params$lp_solver == "gurobi" && license_status != 0) { warning("gurobi solver was requested but license information was ", - "not available; using glpk instead") + "not available or not valid; using glpk instead") params$lp_solver <- "glpk" } @@ -124,6 +134,13 @@ read_params <- function(path = "params.json", template_path = "params.json.templ return(params) } +#' Wrapper for `base::system2` for testing convenience +#' +#' @param command string to run as command +run_cli <- function(command) { + system2(command) +} + #' Create directory if not already existing #' #' @param path string specifying a directory to create diff --git a/backfill_corrections/delphiBackfillCorrection/man/run_cli.Rd b/backfill_corrections/delphiBackfillCorrection/man/run_cli.Rd new file mode 100644 index 000000000..a5301d87b --- /dev/null +++ b/backfill_corrections/delphiBackfillCorrection/man/run_cli.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{run_cli} +\alias{run_cli} +\title{Wrapper for `base::system2` for testing convenience} +\usage{ +run_cli(command) +} +\arguments{ +\item{command}{string to run as command} +} +\description{ +Wrapper for `base::system2` for testing convenience +} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk.json.template similarity index 100% rename from backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk_direct.json.template rename to backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-glpk.json.template diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template deleted file mode 100644 index 1a21af670..000000000 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb.json.template +++ /dev/null @@ -1,9 +0,0 @@ -{ - "input_dir": "./test.temp", - "lp_solver": "gurobi", - "gurobi": { - "GRB_LICENSEID": "1", - "GRB_WLSACCESSID": "2", - "GRB_WLSSECRET": "3" - } -} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template deleted file mode 100644 index 2d45b5bdf..000000000 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_notallparams.json.template +++ /dev/null @@ -1,5 +0,0 @@ -{ - "input_dir": "./test.temp", - "lp_solver": "gurobi", - "gurobi": {"GRB_WLSACCESSID": ""} -} diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-gurobi.json.template similarity index 100% rename from backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-grb_noparams.json.template rename to backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-gurobi.json.template diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-run.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-run.json.template index f2224855a..3b6f13ae6 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-run.json.template +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-run.json.template @@ -4,5 +4,6 @@ "ref_lag": 3, "input_dir": "./input", "export_dir": "./output", - "cache_dir": "./cache" + "cache_dir": "./cache", + "lp_solver": "glpk" } diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-test.json.template b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-test.json.template index d158d7b54..274503ea9 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-test.json.template +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/params-test.json.template @@ -1,3 +1,4 @@ { - "input_dir": "./test.temp" + "input_dir": "./test.temp", + "lp_solver": "gurobi" } diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-beta_prior_estimation.R b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-beta_prior_estimation.R index 87a638e3a..134ab36dd 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-beta_prior_estimation.R +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-beta_prior_estimation.R @@ -70,7 +70,7 @@ test_that("testing the squared error objection function given the beta prior", { test_that("testing the prior estimation", { dw <- "Sat_ref" priors <- est_priors(train_data, prior_test_data, geo, value_type, dw, TAUS, - covariates, response, LP_SOLVER, lambda, + covariates, response, "glpk", lambda, indicator, signal, geo_level, signal_suffix, training_end_date, training_start_date, model_save_dir) alpha <- priors[2] @@ -110,7 +110,7 @@ test_that("testing the main beta prior adjustment function", { indicator, signal, geo_level, signal_suffix, lambda, value_type, geo, training_end_date, training_start_date, model_save_dir, - taus = TAUS, lp_solver = LP_SOLVER) + taus = TAUS, lp_solver = "glpk") updated_train_data <- updated_data[[1]] updated_test_data <- updated_data[[2]] diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-model.R b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-model.R index e7911569c..62d25c677 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-model.R +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-model.R @@ -113,13 +113,13 @@ test_that("testing generating or loading the model", { # Generate the model and check again obj <- get_model(model_path, train_data, covariates, tau, - lambda, LP_SOLVER, train_models=TRUE) + lambda, "glpk", train_models=TRUE) expect_true(file.exists(model_path)) created <- file.info(model_path)$ctime # Check that the model was not generated again. obj <- get_model(model_path, train_data, covariates, tau, - lambda, LP_SOLVER, train_models=FALSE) + lambda, "glpk", train_models=FALSE) expect_equal(file.info(model_path)$ctime, created) expect_silent(file.remove(model_path)) @@ -127,7 +127,7 @@ test_that("testing generating or loading the model", { test_that("testing model training and testing", { result <- model_training_and_testing(train_data, test_data, taus=TAUS, covariates=covariates, - lp_solver=LP_SOLVER, lambda=lambda, test_lag=test_lag, + lp_solver="glpk", lambda=lambda, test_lag=test_lag, geo=geo, value_type=value_type, model_save_dir=model_save_dir, indicator=indicator, signal=signal, geo_level=geo_level, signal_suffix=signal_suffix, diff --git a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R index 320d94a01..0f2052db5 100644 --- a/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R +++ b/backfill_corrections/delphiBackfillCorrection/unit-tests/testthat/test-utils.R @@ -1,7 +1,13 @@ library(stringr) +library(mockr) context("Testing utils helper functions") +# Make it look like we have a valid gurobi license for testing purposes. +mock_system2 <- function(...) { + return(0) +} + test_that("testing create directory if not exist", { # If not exists path = "test.test" @@ -55,14 +61,9 @@ test_that("testing read parameters", { expect_true(!("parallel_max_cores" %in% names(params))) - expect_true(!("taus" %in% names(params))) - expect_true(!("lambda" %in% names(params))) - expect_true(!("lp_solver" %in% names(params))) expect_true(!("lag_pad" %in% names(params))) - expect_true(!("taus" %in% names(params))) expect_true(!("lambda" %in% names(params))) - expect_true(!("lp_solver" %in% names(params))) expect_true(!("num_col" %in% names(params))) expect_true(!("denom_col" %in% names(params))) @@ -94,12 +95,7 @@ test_that("testing read parameters", { expect_true("parallel" %in% names(params)) expect_true("parallel_max_cores" %in% names(params)) - - - expect_true("taus" %in% names(params)) - expect_true("lambda" %in% names(params)) - expect_true("lp_solver" %in% names(params)) - + expect_true("taus" %in% names(params)) expect_true("lambda" %in% names(params)) expect_true("lp_solver" %in% names(params)) @@ -154,36 +150,28 @@ test_that("lp_solver selection works", { # GLPK selected explicitly. path = "test.temp" create_dir_not_exist(path) - expect_silent(params <- read_params(path = "params-glpk_direct.json", - template_path = "params-glpk_direct.json.template", + expect_silent(params <- read_params(path = "params-glpk.json", + template_path = "params-glpk.json.template", train_models = TRUE, make_predictions = TRUE)) expect_true(params$lp_solver == "glpk") - expect_silent(file.remove("params-glpk_direct.json")) - - # gurobi selected explicitly, but without gurobi params subsection - expect_warning(params <- read_params(path = "params-grb_noparams.json", - template_path = "params-grb_noparams.json.template", - train_models = TRUE, make_predictions = TRUE), - "gurobi solver was requested but license information was not available" - ) - expect_true(params$lp_solver == "glpk") - expect_silent(file.remove("params-grb_noparams.json")) + expect_silent(file.remove("params-glpk.json")) - # gurobi selected explicitly, with gurobi params subsection, but without license information - expect_warning(params <- read_params(path = "params-grb_notallparams.json", - template_path = "params-grb_notallparams.json.template", + # gurobi selected explicitly, but without gurobi license file + expect_warning(params <- read_params(path = "params-gurobi.json", + template_path = "params-gurobi.json.template", train_models = TRUE, make_predictions = TRUE), "gurobi solver was requested but license information was not available" ) expect_true(params$lp_solver == "glpk") - expect_silent(file.remove("params-grb_notallparams.json")) + expect_silent(file.remove("params-gurobi.json")) - # gurobi selected explicitly - expect_silent(params <- read_params(path = "params-grb.json", - template_path = "params-grb.json.template", + # gurobi selected explicitly, with gurobi license file mocked to appear available and valid + local_mock("delphiBackfillCorrection::run_cli" = mock_system2) + expect_silent(params <- read_params(path = "params-gurobi.json", + template_path = "params-gurobi.json.template", train_models = TRUE, make_predictions = TRUE)) expect_true(params$lp_solver == "gurobi") - expect_silent(file.remove("params-grb.json")) + expect_silent(file.remove("params-gurobi.json")) unlink(path, recursive = TRUE) }) From 8126b219416d87ee982f6ca5047b2d22ba46ad77 Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Thu, 23 Mar 2023 14:41:37 -0400 Subject: [PATCH 5/7] fuzzy check on expiration errors --- backfill_corrections/delphiBackfillCorrection/R/model.R | 2 +- backfill_corrections/delphiBackfillCorrection/R/utils.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backfill_corrections/delphiBackfillCorrection/R/model.R b/backfill_corrections/delphiBackfillCorrection/R/model.R index e9b1c4685..838b7b29a 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/model.R +++ b/backfill_corrections/delphiBackfillCorrection/R/model.R @@ -132,7 +132,7 @@ model_training_and_testing <- function(train_data, test_data, taus, covariates, success = success + 1 }, error=function(e) { - if (e$message == "Error 10032: License has expired\n") { + if (grepl("Error 10032: License has expired", e$message, fixed=TRUE)) { stop("The gurobi license has expired. Please renew or switch to ", "using glpk. lp_solver can be specified in params.json.") } diff --git a/backfill_corrections/delphiBackfillCorrection/R/utils.R b/backfill_corrections/delphiBackfillCorrection/R/utils.R index b85613e9b..0ebd37664 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/utils.R +++ b/backfill_corrections/delphiBackfillCorrection/R/utils.R @@ -81,7 +81,7 @@ read_params <- function(path = "params.json", template_path = "params.json.templ license_status <- run_cli("gurobi_cl") }, error=function(e) { - if (e$message == "Error 10032: License has expired\n") { + if (grepl("Error 10032: License has expired", e$message, fixed=TRUE)) { stop("The gurobi license has expired. Please renew or switch to ", "using glpk. lp_solver can be specified in params.json.") } From db55f0865b351ac4290765b4fda93ea710bcca46 Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Thu, 23 Mar 2023 14:41:57 -0400 Subject: [PATCH 6/7] list mockr as needed package --- backfill_corrections/delphiBackfillCorrection/DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backfill_corrections/delphiBackfillCorrection/DESCRIPTION b/backfill_corrections/delphiBackfillCorrection/DESCRIPTION index b68d27cef..bcf44b3f1 100644 --- a/backfill_corrections/delphiBackfillCorrection/DESCRIPTION +++ b/backfill_corrections/delphiBackfillCorrection/DESCRIPTION @@ -30,6 +30,7 @@ Suggests: knitr (>= 1.15), rmarkdown (>= 1.4), testthat (>= 1.0.1), - covr (>= 2.2.2) + covr (>= 2.2.2), + mockr RoxygenNote: 7.2.0 Encoding: UTF-8 From e3daff2477106ac3192d80d308b786322653b108 Mon Sep 17 00:00:00 2001 From: Nat DeFries <42820733+nmdefries@users.noreply.github.com> Date: Thu, 23 Mar 2023 15:55:56 -0400 Subject: [PATCH 7/7] check gurobi license validity only if lp_solver is gurobi --- .../delphiBackfillCorrection/R/model.R | 4 -- .../delphiBackfillCorrection/R/utils.R | 37 ++++++++++--------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/backfill_corrections/delphiBackfillCorrection/R/model.R b/backfill_corrections/delphiBackfillCorrection/R/model.R index 838b7b29a..aed7839f0 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/model.R +++ b/backfill_corrections/delphiBackfillCorrection/R/model.R @@ -132,10 +132,6 @@ model_training_and_testing <- function(train_data, test_data, taus, covariates, success = success + 1 }, error=function(e) { - if (grepl("Error 10032: License has expired", e$message, fixed=TRUE)) { - stop("The gurobi license has expired. Please renew or switch to ", - "using glpk. lp_solver can be specified in params.json.") - } msg_ts("Training failed for ", model_path, ". Check that your gurobi ", "license is valid and being passed properly to the program.") } diff --git a/backfill_corrections/delphiBackfillCorrection/R/utils.R b/backfill_corrections/delphiBackfillCorrection/R/utils.R index 0ebd37664..1737f2a42 100644 --- a/backfill_corrections/delphiBackfillCorrection/R/utils.R +++ b/backfill_corrections/delphiBackfillCorrection/R/utils.R @@ -74,25 +74,28 @@ read_params <- function(path = "params.json", template_path = "params.json.templ } else { params$lp_solver <- LP_SOLVER } - # Make call to gurobi CLI to check license. Returns a status of `0` if - # license can be found and is valid. - tryCatch( - expr = { - license_status <- run_cli("gurobi_cl") - }, - error=function(e) { - if (grepl("Error 10032: License has expired", e$message, fixed=TRUE)) { - stop("The gurobi license has expired. Please renew or switch to ", - "using glpk. lp_solver can be specified in params.json.") + if (params$lp_solver == "gurobi") { + # Make call to gurobi CLI to check license. Returns a status of `0` if + # license can be found and is valid. + tryCatch( + expr = { + license_status <- run_cli("gurobi_cl") + }, + error=function(e) { + if (grepl("Error 10032: License has expired", e$message, fixed=TRUE)) { + stop("The gurobi license has expired. Please renew or switch to ", + "using glpk. lp_solver can be specified in params.json.") + } + msg_ts(e$message) + license_status <- 1 } - msg_ts(e$message) - license_status <- 1 + ) + + if (license_status != 0) { + warning("gurobi solver was requested but license information was ", + "not available or not valid; using glpk instead") + params$lp_solver <- "glpk" } - ) - if (params$lp_solver == "gurobi" && license_status != 0) { - warning("gurobi solver was requested but license information was ", - "not available or not valid; using glpk instead") - params$lp_solver <- "glpk" } # Data parameters