Skip to content

Commit

Permalink
+ script checking if fixing to ref run worked, allows to fix that in …
Browse files Browse the repository at this point in the history
…post-processing
  • Loading branch information
orichters committed Sep 13, 2023
1 parent a522f3d commit ace7fe1
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ cfg$logoption <- 2

# Just list the name of the output scripts that should be used by output.R
# At the moment there are several R-scripts located in scripts/output/
cfg$output <- c("reporting","reportCEScalib","rds_report") #,"validation","emulator","reportCEScalib","validationSummary","dashboard")
cfg$output <- c("reporting","reportCEScalib","rds_report","fixOnRef") #,"validation","emulator","reportCEScalib","validationSummary","dashboard")

# Set the format for the results folder, type string :date: in order to use the current time stamp in the folder name (e.g. "results:date:") use :title: to use the current title in the folder name
cfg$results_folder <- "output/:title::date:"
Expand Down
12 changes: 7 additions & 5 deletions output.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ library(lucode2)
library(gms)
require(stringr, quietly = TRUE)

flags <- NULL
### Define arguments that can be read from command line
if (!exists("source_include")) {
# if this script is not being sourced by another script but called from the command line via Rscript read the command
# line arguments and let the user choose the slurm options
flags <- readArgs("outputdir", "output", "comp", "remind_dir", "slurmConfig", "filename_prefix",
.flags = c(t = "--test", h = "--help"))
} else {
flags <- NULL
}

if ("--help" %in% flags) {
Expand Down Expand Up @@ -125,7 +124,7 @@ if (! exists("output")) {
# Select output directories if not defined by readArgs
if (! exists("outputdir")) {
modulesNeedingMif <- c("compareScenarios2", "xlsx_IIASA", "policyCosts", "Ariadne_output",
"plot_compare_iterations", "varListHtml")
"plot_compare_iterations", "varListHtml", "fixOnRef")
needingMif <- any(modulesNeedingMif %in% output)
dir_folder <- if (exists("remind_dir")) c(file.path(remind_dir, "output"), remind_dir) else "./output"
dirs <- dirname(Sys.glob(file.path(dir_folder, "*", "fulldata.gdx")))
Expand Down Expand Up @@ -198,11 +197,14 @@ if (comp %in% c("comparison", "export")) {
}
} else { # comp = single
# define slurm class or direct execution
outputUsingDirect <- c("plotIterations")
outputInteractive <- c("plotIterations", "fixOnRef")
if (! exists("source_include")) {
# for selected output scripts, only slurm configurations matching these regex are available
slurmExceptions <- if ("reporting" %in% output) "--mem=[0-9]*[0-9]{3}" else NULL
if (any(output %in% outputUsingDirect)) slurmConfig <- "direct"
if (any(output %in% outputInteractive)) {
slurmConfig <- "direct"
flags <- c(flags, "--interactive") # to tell scripts they can run in interactive mode
}
# if this script is not being sourced by another script but called from the command line via Rscript let the user
# choose the slurm options
if (!exists("slurmConfig")) {
Expand Down
117 changes: 117 additions & 0 deletions scripts/output/single/fixOnRef.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# | (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: remind@pik-potsdam.de

# if you want to change the reference run for yourrun, you can run:
# Rscript scripts/output/single/fixRefOn.R outputdir=yourrun,newreferencerun

suppressPackageStartupMessages(library(tidyverse))

if(! exists("source_include")) {
# Define arguments that can be read from command line
outputdir <- "."
flags <- readArgs("outputdir", .flags = c(i = "--interactive"))
}

findRefMif <- function(outputdir, envi) {
stopifnot(length(outputdir) == 1)
inputref <- try(envi$cfg$files2export$start[["input_ref.gdx"]], silent = TRUE)
if (inherits(inputref, "try-error") || is.na(inputref) || isTRUE(inputref == "NA") || length(inputref) == 0) {
message("No input_ref.gdx found in config.")
return(NULL)
}
refdir <- dirname(inputref)
if (! file.exists(file.path(refdir, "config.Rdata"))) {
message("Config in reference directory '", refdir, "' not found.")
return(NULL)
}
refscen <- lucode2::getScenNames(refdir)
refmif <- file.path(refdir, paste0("REMIND_generic_", refscen, ".mif"))
if (! file.exists(refmif)) {
message("Reference mif '", refmif, "' not found, run reporting!")
return(NULL)
}
return(refmif)
}

fixOnMif <- function(outputdir) {

gdxs <- file.path(outputdir, "fulldata.gdx")
configs <- file.path(outputdir, "config.Rdata")
message("### Checking if mif is correctly fixed on reference run for ", outputdir)
if (! all(file.exists(gdxs, configs))) stop("gdx or config.Rdata not found!")
scens <- lucode2::getScenNames(outputdir)
mifs <- file.path(outputdir, paste0("REMIND_generic_", scens, ".mif"))
if (! all(file.exists(mifs))) stop("mif file not found, run reporting!")

envi <- new.env()
load(configs[[1]], env = envi)
title <- envi$cfg$title
stopifnot(title == scens[[1]])
startyear <- envi$cfg$gms$cm_startyear

if (length(outputdir) == 1) {
refmif <- findRefMif(outputdir, envi)
if (is.null(refmif)) return(NULL)
} else if (length(outputdir) == 2) {
refmif <- mifs[[2]]
} else {
stop("length(outputdir)=", length(outputdir), ", is bigger than 2.")
}
refname <- basename(dirname(refmif))
d <- quitte::as.quitte(mifs)
dref <- quitte::as.quitte(refmif)
if (identical(levels(d$scenario), levels(dref$scenario))) {
levels(dref$scenario) <- paste0(levels(dref$scenario), "_ref")
}

message("Comparing ", title, " with reference run ", refname, " for t < ", startyear)
mismatches <- rbind(d, dref) %>%
filter(period < startyear, ! grepl("Moving Avg$", variable)) %>%
group_by(model, region, variable, unit, period) %>%
filter(0 != var(value)) %>%
ungroup() %>%
distinct(variable, period) %>%
group_by(variable) %>%
summarise(period = paste(sort(period), collapse = ', '))
if (nrow(mismatches) == 0) {
message("# Run is perfectly fixed on reference run!")
return(TRUE)
}
showrows <- 50
theserows <- match(unique(gsub("\\|.*$", "", mismatches$variable)), gsub("\\|.*$", "", mismatches$variable))
# extract some representative variables that differ in the first two parts of A|B|C…
first2elements <- gsub("(\\|.*?)\\|.*$", "\\1", mismatches$variable)
theserows <- match(unique(first2elements), first2elements)
theserows <- theserows[seq(min(length(theserows), showrows))]
rlang::with_options(width = 160, print(mismatches[theserows, ], n = showrows))
if (length(theserows) < nrow(mismatches)) {
message("The variables above are representative, avoiding repetition after A|B. ",
"Additional ", (nrow(mismatches) - length(theserows)), " variables differ.")
}
filename <- file.path(outputdir, "log_fixOnRef.csv")
message("Find failing variables in '", filename, "'.")
csvdata <- rbind(d, dref) %>%
filter(! grepl("Moving Avg$", variable), period < startyear) %>%
pivot_wider(names_from = scenario) %>% # order them such that ref is after run is missing
droplevels()
csvdata <- filter(csvdata, abs(csvdata[, levels(d$scenario)] - csvdata[, levels(dref$scenario)]) > 0.0000001) # ugly!
write.table(csvdata, filename, sep = ",", quote = FALSE, dec = ".", row.names = FALSE)
if (exists("flags") && isTRUE("--interactive" %in% flags)) {
message("\nDo you want to fix that by overwriting ", title, " mif with reference run ", refname, " for t < ", startyear, "? y/N")
if (tolower(gms::getLine()) %in% c("y", "yes")) {
di <- rbind(
filter(d, period >= startyear),
mutate(filter(dref, period < startyear), scenario = title)
)
quitte::write.mif(di, paste0(mifs[[1]], "test"))
remind2::deletePlus(mifs[[1]], writemif = TRUE)
}
}
return(mismatches)
}

invisible(fixOnMif(outputdir))

0 comments on commit ace7fe1

Please sign in to comment.