-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reactive check_config, add regexp check for cfg$… values
- Loading branch information
Showing
15 changed files
with
288 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
checkFixCfg <- function(cfg, remindPath = ".", testmode = FALSE) { | ||
refcfg <- gms::readDefaultConfig(remindPath) | ||
gms::check_config(cfg, reference_file = refcfg, modulepath = file.path(remindPath, "modules"), | ||
settings_config = file.path(remindPath, "config", "settings_config.csv"), | ||
extras = c("backup", "remind_folder", "pathToMagpieReport", "cm_nash_autoconverge_lastrun", | ||
"gms$c_expname", "restart_subsequent_runs", "gms$c_GDPpcScen", | ||
"gms$cm_CES_configuration", "gms$c_description", "model")) | ||
|
||
errorsfound <- 0 | ||
|
||
code <- system(paste0("grep regexp ", file.path(remindPath, "main.gms")), intern = TRUE) | ||
grepisnum <- "((\\+|-)?[0-9]*([0-9]\\.?|\\.?[0-9])[0-9]*)" | ||
|
||
for (n in names(cfg$gms)) { | ||
errormsg <- NULL | ||
paramdef <- paste0("^([ ]*", n, "[ ]*=|\\$setglobal[ ]+", n, " )") | ||
filtered <- grep(paste0(paramdef, ".*regexp[ ]*=[ ]*"), code, value = TRUE) | ||
if (length(filtered) == 1) { | ||
# search for string '!! regexp = whatever', potentially followed by '!! otherstuff' and extract 'whatever' | ||
regexp <- paste0("^(", trimws(gsub("!!.*", "", gsub("^.*regexp[ ]*=", "", filtered))), ")$") | ||
useregexp <- gsub("is.numeric", grepisnum, regexp, fixed = TRUE) | ||
if (! grepl(useregexp, cfg$gms[[n]])) { | ||
errormsg <- paste0("Parameter cfg$gms$", n, "=", cfg$gms[[n]], " does not fit this regular expression: ", regexp) | ||
} | ||
} else if (length(filtered) > 1) { | ||
errormsg <- paste0("More than one regexp found for ", n, ". These are the code lines:\n", paste(filtered, collapse = "\n")) | ||
} | ||
if (! is.null(errormsg)) { | ||
errorsfound <- errorsfound + 1 | ||
if (testmode) warning(errormsg) else message(errormsg) | ||
} | ||
} | ||
|
||
# Check for compatibility with subsidizeLearning | ||
if ((cfg$gms$optimization != "nash") && (cfg$gms$subsidizeLearning == "globallyOptimal") ) { | ||
message("Only optimization='nash' is compatible with subsidizeLearning='globallyOptimal'. Switching subsidizeLearning to 'off' now.\n") | ||
cfg$gms$subsidizeLearning <- "off" | ||
} | ||
|
||
# reportCEScalib only works with the calibrate module | ||
if (! isTRUE(cfg$gms$CES_parameters == "calibrate")) { | ||
cfg$output <- setdiff(cfg$output, "reportCEScalib") | ||
} | ||
if (errorsfound > 0) { | ||
if (testmode) warning(errorsfound, " errors found.") | ||
else stop(errorsfound, " errors found, see above. Either adapt the parameter choice or the regexp in main.gms") | ||
} | ||
|
||
return(cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# | (C) 2006-2023 Potsdam Institute for Climate Impact Research (PIK) | ||
# | authors, and contributors see CITATION.cff file. This file is part | ||
# | of REMIND and licensed under AGPL-3.0-or-later. Under Section 7 of | ||
# | AGPL-3.0, you are granted additional permissions described in the | ||
# | REMIND License Exception, version 1.0 (see LICENSE file). | ||
# | Contact: [email protected] | ||
test_that("checkFixCfg works", { | ||
remind_folder <- "../.." | ||
savecfg <- cfg <- gms::readDefaultConfig(remind_folder) | ||
# regexp = NA means: no warning | ||
expect_warning(checkFixCfg(cfg, remind_folder, testmode = TRUE), regexp = NA) | ||
|
||
wrongsetting <- c( | ||
"cm_NDC_version" = "2004_cond", | ||
"cm_emiscen" = "123", | ||
"cm_nash_autoconverge" = "NA", | ||
"cm_co2_tax_2020" = "2.2.2", | ||
"cm_co2_tax_growth" = "333++", | ||
"c_macscen" = "-1", | ||
"cm_keep_presolve_gdxes" = "1.1", | ||
"cm_startyear" = "1985", | ||
"cm_netZeroScen" = "NöööGFS_v4", | ||
"cm_rcp_scen" = "apocalypse", | ||
NULL) | ||
|
||
cfg <- savecfg | ||
cfg$gms[names(wrongsetting)] <- wrongsetting | ||
w <- capture_warnings(checkFixCfg(cfg, remind_folder, testmode = TRUE)) | ||
for (n in names(wrongsetting)) { | ||
expect_match(w, paste0(n, "=", wrongsetting[[n]]), all = FALSE, fixed = TRUE) | ||
} | ||
expect_match(w, paste0(length(wrongsetting), " errors found"), all = FALSE, fixed = TRUE) | ||
expect_equal(length(w), length(wrongsetting) + 1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,48 @@ | |
# | AGPL-3.0, you are granted additional permissions described in the | ||
# | REMIND License Exception, version 1.0 (see LICENSE file). | ||
# | Contact: [email protected] | ||
test_that("start.R --gamscompile startgroup=AMT titletag=AMT config/scenario_config.csv works", { | ||
test_that("start.R --gamscompile startgroup=AMT config/scenario_config.csv works", { | ||
unlink(paste0("../../output/gamscompile/*TESTTHAT", c(".gms", ".lst"))) | ||
csvfile <- "config/scenario_config.csv" | ||
titletag <- paste0("titletag=TESTTHAT-", gsub(".csv$", "", basename(csvfile))) | ||
with_mocked_bindings({ | ||
skipIfPreviousFailed() | ||
output <- localSystem2("Rscript", | ||
c("start.R", "--gamscompile", "startgroup=AMT", titletag, csvfile)) | ||
printIfFailed(output) | ||
expectSuccessStatus(output) | ||
}, | ||
getLine = function() stop("getLine should not called."), | ||
.package = "gms" | ||
) | ||
unlink("../../*TESTTHAT.RData") | ||
}) | ||
|
||
test_that("start.R --gamscompile works on all configs and scenarios", { | ||
skipIfFast() | ||
skipIfPreviousFailed() | ||
output <- localSystem2("Rscript", | ||
c("start.R", "--gamscompile", "startgroup=AMT", "titletag=AMT", "config/scenario_config.csv")) | ||
printIfFailed(output) | ||
expectSuccessStatus(output) | ||
csvfiles <- system("git ls-files ../../config/scenario_config*.csv ../../config/*/scenario_config*.csv", intern = TRUE) | ||
if (length(csvfiles) == 0) { | ||
csvfiles <- Sys.glob(c(file.path("../../config/scenario_config*.csv"), | ||
file.path("../../config", "*", "scenario_config*.csv"))) | ||
} | ||
csvfiles <- normalizePath(grep("^scenario_config_coupled.*", csvfiles, invert = TRUE, value = TRUE)) | ||
skipfiles <- c("scenario_config_21_EU11_ECEMF", "scenario_config_EDGE-T_NDC_NPi_pkbudget", | ||
"scenario_config_NAVIGATE_300", "21_regions_EU11", "scenario_config_tradeCap_standalone") | ||
csvfiles <- grep(paste(skipfiles, collapse = "|"), csvfiles, invert = TRUE, value = TRUE) | ||
expect_true(length(csvfiles) > 0) | ||
with_mocked_bindings( | ||
for (csvfile in csvfiles) { | ||
test_that(paste("perform start.R --gamscompile with", basename(csvfile)), { | ||
titletag <- paste0("titletag=TESTTHAT-", gsub(".csv$", "", basename(csvfile))) | ||
output <- localSystem2("Rscript", | ||
c("start.R", "--gamscompile", "startgroup=*", titletag, csvfile)) | ||
printIfFailed(output) | ||
expectSuccessStatus(output) | ||
}) | ||
}, | ||
getLine = function() stop("getLine should not called."), | ||
.package = "gms" | ||
) | ||
unlink("../../*TESTTHAT.RData") | ||
}) |
Oops, something went wrong.