-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reactive check_config, add regexp check for cfg$… values #1356
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
53e56e6
reactive check_config, add regexp check for cfg$… values
orichters a4d4be8
comments for checkFixCfg, in test-coupled send mails only for fails
orichters 1b9d214
some more regexp checks, add is.share
orichters 9e196c6
explain checkFixCfg()
orichters File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
title;start;qos;sbatch;magpie_scen;magpie_empty;no_ghgprices_land_until;max_iterations;oldrun;path_gdx;path_gdx_ref;path_gdx_bau;path_report;cm_nash_autoconverge_lastrun;path_mif_ghgprice_land | ||
TESTTHAT-SSP2EU-Base;1;auto;--wait;SSP2|NPI;TRUE;y2150;2;;;;;;2; | ||
TESTTHAT-SSP2EU-NDC;1;auto;--wait;SSP2|NDC;TRUE;y2150;2;;;;;;2;TESTTHAT-SSP2EU-Base | ||
TESTTHAT-SSP2EU-Policy;2;auto;--wait;SSP2|NDC;TRUE;y2150;2;TESTTHAT-SSP2EU-Base;;;;;;output/C_TESTTHAT-SSP2EU-Base-rem-1/REMIND_generic_C_TESTTHAT-SSP2EU-Base-rem-1.mif | ||
TESTTHAT-SSP2EU-Base;1;auto;--wait --mail-type=FAIL;SSP2|NPI;TRUE;y2150;2;;;;;;2; | ||
TESTTHAT-SSP2EU-NDC;1;auto;--wait --mail-type=FAIL;SSP2|NDC;TRUE;y2150;2;;;;;;2;TESTTHAT-SSP2EU-Base | ||
TESTTHAT-SSP2EU-Policy;2;auto;--wait --mail-type=FAIL;SSP2|NDC;TRUE;y2150;2;TESTTHAT-SSP2EU-Base;;;;;;output/C_TESTTHAT-SSP2EU-Base-rem-1/REMIND_generic_C_TESTTHAT-SSP2EU-Base-rem-1.mif |
Large diffs are not rendered by default.
Oops, something went wrong.
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,77 @@ | ||
#' take a REMIND cfg, runs some consistency checks and automatically fix some wrong settings | ||
#' The regexp check loads the code from main.gms and looks for 'regexp = ' patterns. | ||
#' It then checks whether the current cfg matches those patterns. | ||
#' | ||
#' @param cfg list with REMIND setting | ||
#' @param remindPath path to REMIND directory containing the main.gms | ||
#' @param testmode boolean. Default is FALSE which fails on errors, in testmode only raise warnings | ||
#' @author Oliver Richters | ||
#' @return updated cfg | ||
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 | ||
|
||
## regexp check | ||
# extract all instances of 'regexp' from main.gms | ||
code <- system(paste0("grep regexp ", file.path(remindPath, "main.gms")), intern = TRUE) | ||
# this is used to replace all 'regexp = is.numeric' | ||
grepisnum <- "((\\+|-)?[0-9]*([0-9]\\.?|\\.?[0-9])[0-9]*)" | ||
grepisshare <- "(\\+?0?\\.[0-9]+|0|0\\.0*|1|1\\.0*)" | ||
# some simple tests | ||
if (testmode) { | ||
stopifnot(all( grepl(paste0("^", grepisnum, "$"), c("2", "2.2", "32.", "+32.", "+.05", "-0.5", "-.5", "-5", "-7.")))) | ||
stopifnot(all(! grepl(paste0("^", grepisnum, "$"), c("2.2.", "0a", "1e1", ".2.", "ab", "2.3a", "--a", "++2")))) | ||
stopifnot(all( grepl(paste0("^", grepisshare, "$"), c("0", "0.0", ".000", "1.0", "1.", "1", "0.12341234")))) | ||
stopifnot(all(! grepl(paste0("^", grepisshare, "$"), c("1.1", "-0.3", "-0", ".")))) | ||
} | ||
|
||
for (n in names(cfg$gms)) { | ||
errormsg <- NULL | ||
# how parameter n is defined in main.gms | ||
paramdef <- paste0("^([ ]*", n, "[ ]*=|\\$setglobal[ ]+", n, " )") | ||
# filter fitting parameter definition from code snippets containing regexp | ||
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))), ")$") | ||
# replace is.numeric by pattern defined above | ||
useregexp <- gsub("is.numeric", grepisnum, regexp, fixed = TRUE) | ||
useregexp <- gsub("is.share", grepisshare, useregexp, fixed = TRUE) | ||
# check whether parameter value fits regular expression | ||
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) { | ||
# fail if more than one regexp found for parameter | ||
errormsg <- paste0("More than one regexp found for ", n, ". These are the code lines:\n", paste(filtered, collapse = "\n")) | ||
} | ||
# count errors | ||
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
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,36 @@ | ||
# | (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", | ||
"c_testOneRegi_region" = "LOONG", | ||
"c_shGreenH2" = "1.5", | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,13 @@ | |
# | Contact: [email protected] | ||
test_that("combine_slurmConfig works", { | ||
teststring <- "--qos=priority --time=03:30:00" | ||
expect_identical(combine_slurmConfig(teststring, teststring), teststring) | ||
expect_identical(combine_slurmConfig(teststring, NULL), teststring) | ||
expect_identical(combine_slurmConfig(NULL, NULL), "") | ||
expect_identical(combine_slurmConfig(teststring, ""), teststring) | ||
expect_identical(combine_slurmConfig(teststring, "--qos=standby"), "--qos=standby --time=03:30:00") | ||
expect_identical(combine_slurmConfig(teststring, "--bla=blub"), paste("--bla=blub", teststring)) | ||
expect_identical(combine_slurmConfig(teststring, "--wait"), paste("--wait", teststring)) | ||
teststring <- "--qos=priority --wait" | ||
expect_identical(combine_slurmConfig(teststring, "--qos=standby"), "--qos=standby --wait") | ||
}) |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't test-coupled be removed from make commands so you can only run coupled test with SLURM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. The model runs are on slurm anyway, the difference is whether also the test itself is on slurm. I think normally you should not do that (because it reserves a full CPU basically for waiting), but sometimes if the internet connection is unstable (train…) it is a helpful option because then the tests do not fail just because your console lost connection.