Skip to content

Commit

Permalink
Allow workflow to run independently of BETY database
Browse files Browse the repository at this point in the history
  • Loading branch information
Yinghao Sun committed Nov 22, 2024
1 parent fc7fd71 commit 6f2ab9f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 37 deletions.
48 changes: 27 additions & 21 deletions base/workflow/R/run.write.configs.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
run.write.configs <- function(settings, write = TRUE, ens.sample.method = "uniform",
posterior.files = rep(NA, length(settings$pfts)),
overwrite = TRUE) {
tryCatch({
con <- PEcAn.DB::db.open(settings$database$bety)
on.exit(PEcAn.DB::db.close(con), add = TRUE)
}, error = function(e) {
PEcAn.logger::logger.severe(
"Connection requested, but failed to open with the following error: ",
conditionMessage(e))
})
## Skip database connection if settings$database is NULL
if (write == FALSE) {
PEcAn.logger::logger.info("Database connection skipped: No database settings provided.")
con <- NULL # Set con to NULL to avoid errors in subsequent code
} else {
tryCatch({
con <- PEcAn.DB::db.open(settings$database$bety)
on.exit(PEcAn.DB::db.close(con), add = TRUE)
}, error = function(e) {
PEcAn.logger::logger.severe(
"Connection requested, but failed to open with the following error: ",
conditionMessage(e))
})
}

## Which posterior to use?
for (i in seq_along(settings$pfts)) {
Expand Down Expand Up @@ -81,7 +87,7 @@ run.write.configs <- function(settings, write = TRUE, ens.sample.method = "unifo
model <- settings$model$type
scipen <- getOption("scipen")
options(scipen = 12)

PEcAn.uncertainty::get.parameter.samples(settings, posterior.files, ens.sample.method)
samples.file <- file.path(settings$outdir, "samples.Rdata")
if (file.exists(samples.file)) {
Expand Down Expand Up @@ -109,8 +115,8 @@ run.write.configs <- function(settings, write = TRUE, ens.sample.method = "unifo
my.write.config <- paste0("write.config.",model)
if (!exists(my.write.config)) {
PEcAn.logger::logger.error(my.write.config,
"does not exist, please make sure that the model package contains a function called",
my.write.config)
"does not exist, please make sure that the model package contains a function called",
my.write.config)
}

## Prepare for model output. Clean up any old config files (if exists)
Expand All @@ -134,29 +140,29 @@ run.write.configs <- function(settings, write = TRUE, ens.sample.method = "unifo
### Write out SA config files
PEcAn.logger::logger.info("\n ----- Writing model run config files ----")
sa.runs <- PEcAn.uncertainty::write.sa.configs(defaults = settings$pfts,
quantile.samples = sa.samples,
settings = settings,
model = model,
write.to.db = write)
quantile.samples = sa.samples,
settings = settings,
model = model,
write.to.db = write)

# Store output in settings and output variables
runs.samples$sa <- sa.run.ids <- sa.runs$runs
settings$sensitivity.analysis$ensemble.id <- sa.ensemble.id <- sa.runs$ensemble.id

# Save sensitivity analysis info
fname <- PEcAn.uncertainty::sensitivity.filename(settings, "sensitivity.samples", "Rdata",
all.var.yr = TRUE, pft = NULL)
all.var.yr = TRUE, pft = NULL)
save(sa.run.ids, sa.ensemble.id, sa.samples, pft.names, trait.names, file = fname)

} ### End of SA

### Write ENSEMBLE
if ("ensemble" %in% names(settings)) {
ens.runs <- PEcAn.uncertainty::write.ensemble.configs(defaults = settings$pfts,
ensemble.samples = ensemble.samples,
settings = settings,
model = model,
write.to.db = write)
ensemble.samples = ensemble.samples,
settings = settings,
model = model,
write.to.db = write)

# Store output in settings and output variables
runs.samples$ensemble <- ens.run.ids <- ens.runs$runs
Expand All @@ -181,4 +187,4 @@ run.write.configs <- function(settings, write = TRUE, ens.sample.method = "unifo
options(scipen = scipen)

return(invisible(settings))
}
}
3 changes: 1 addition & 2 deletions base/workflow/R/runModule.run.write.configs.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ runModule.run.write.configs <- function(settings, overwrite = TRUE) {
}
return(PEcAn.settings::papply(settings, runModule.run.write.configs, overwrite = FALSE))
} else if (PEcAn.settings::is.Settings(settings)) {
write <- settings$database$bety$write
# double check making sure we have method for parameter sampling
if (is.null(settings$ensemble$samplingspace$parameters$method)) settings$ensemble$samplingspace$parameters$method <- "uniform"
ens.sample.method <- settings$ensemble$samplingspace$parameters$method
Expand All @@ -31,4 +30,4 @@ runModule.run.write.configs <- function(settings, overwrite = TRUE) {
} else {
stop("runModule.run.write.configs only works with Settings or MultiSettings")
}
}
}
25 changes: 14 additions & 11 deletions base/workflow/R/start_model_runs.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ start_model_runs <- function(settings, write = TRUE, stop.on.error = TRUE) {
# launch each of the jobs
for (run in run_list) {
run_id_string <- format(run, scientific = FALSE)
# write start time to database
PEcAn.DB::stamp_started(con = dbcon, run = run)
if(write){
# write start time to database
PEcAn.DB::stamp_started(con = dbcon, run = run)
}

# check to see if we use the model launcher
if (is_rabbitmq) {
Expand Down Expand Up @@ -170,9 +172,10 @@ start_model_runs <- function(settings, write = TRUE, stop.on.error = TRUE) {
)
}

# write finished time to database
PEcAn.DB::stamp_finished(con = dbcon, run = run)

if (write){
# write finished time to database
PEcAn.DB::stamp_finished(con = dbcon, run = run)
}
pbi <- pbi + 1
utils::setTxtProgressBar(pb, pbi)
}
Expand Down Expand Up @@ -236,12 +239,12 @@ start_model_runs <- function(settings, write = TRUE, stop.on.error = TRUE) {
}

} else {
out <- PEcAn.remote::start_serial(
run = run,
host = settings$host,
rundir = settings$rundir,
host_rundir = settings$host$rundir,
job_script = "launcher.sh")
out <- PEcAn.remote::start_serial(
run = run,
host = settings$host,
rundir = settings$rundir,
host_rundir = settings$host$rundir,
job_script = "launcher.sh")

# check output to see if an error occurred during the model run
PEcAn.remote::check_model_run(out = out, stop.on.error = TRUE)
Expand Down
2 changes: 2 additions & 0 deletions modules/uncertainty/R/ensemble.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ write.ensemble.configs <- function(defaults, ensemble.samples, settings, model,
}

# See if we need to write to DB
if (!is.null(settings$database) && !is.null(settings$database$bety) && !is.null(settings$database$bety$write)) {
write.to.db <- as.logical(settings$database$bety$write)

if (write.to.db) {
Expand All @@ -236,6 +237,7 @@ write.ensemble.configs <- function(defaults, ensemble.samples, settings, model,
PEcAn.logger::logger.warn("We were not able to successfully establish a connection with Bety ")
}
}
}



Expand Down
10 changes: 7 additions & 3 deletions modules/uncertainty/R/get.parameter.samples.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ get.parameter.samples <- function(settings,
num.pfts <- length(settings$pfts)
pft.names <- list()
outdirs <- list()
if (settings$database$bety$write == FALSE) {
PEcAn.logger::logger.info("Skipping database access in get.parameter.samples because database settings are NULL.")
con <- NULL
} else {
## Open database connection
con <- try(PEcAn.DB::db.open(settings$database$bety))
on.exit(try(PEcAn.DB::db.close(con), silent = TRUE), add = TRUE)

# If we fail to connect to DB then we set to NULL
if (inherits(con, "try-error")) {
con <- NULL
PEcAn.logger::logger.warn("We were not able to successfully establish a connection with Bety ")
}

}
for (i.pft in seq_along(pfts)) {
pft.names[i.pft] <- settings$pfts[[i.pft]]$name

Expand Down Expand Up @@ -202,7 +206,7 @@ get.parameter.samples <- function(settings,
env.samples, ens.sample.method, param.names)
}
}

#ensemble.samples <- NULL
save(ensemble.samples, trait.samples, sa.samples, runs.samples, env.samples,
file = file.path(settings$outdir, "samples.Rdata"))
} # get.parameter.samples

0 comments on commit 6f2ab9f

Please sign in to comment.