-
Notifications
You must be signed in to change notification settings - Fork 235
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
Hf landscape updates [WIP] #3193
base: develop
Are you sure you want to change the base?
Changes from 9 commits
19af737
fc6d693
2b86b1b
5512ae7
c5180ce
44e58ee
6da61c2
27ba92d
71cbfb9
7c39df4
5dc68f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
#' @param posterior.files Filenames for posteriors for drawing samples for ensemble and sensitivity | ||
#' analysis (e.g. post.distns.Rdata, or prior.distns.Rdata) | ||
#' @param overwrite logical: Replace output files that already exist? | ||
#' @param use.existing.samples FALSE (default), TRUE, or filename | ||
#' | ||
#' @details The default value for \code{posterior.files} is NA, in which case the | ||
#' most recent posterior or prior (in that order) for the workflow is used. | ||
|
@@ -24,7 +25,7 @@ | |
#' @author David LeBauer, Shawn Serbin, Ryan Kelly, Mike Dietze | ||
run.write.configs <- function(settings, write = TRUE, ens.sample.method = "uniform", | ||
posterior.files = rep(NA, length(settings$pfts)), | ||
overwrite = TRUE) { | ||
overwrite = TRUE,use.existing.samples=FALSE) { | ||
tryCatch({ | ||
con <- PEcAn.DB::db.open(settings$database$bety) | ||
on.exit(PEcAn.DB::db.close(con), add = TRUE) | ||
|
@@ -34,68 +35,82 @@ run.write.configs <- function(settings, write = TRUE, ens.sample.method = "unifo | |
conditionMessage(e)) | ||
}) | ||
|
||
## Which posterior to use? | ||
for (i in seq_along(settings$pfts)) { | ||
## if posterior.files is specified us that | ||
if (is.na(posterior.files[i])) { | ||
## otherwise, check to see if posteriorid exists | ||
if (!is.null(settings$pfts[[i]]$posteriorid)) { | ||
#TODO: sometimes `files` is a 0x0 tibble and other operations with it fail. | ||
files <- PEcAn.DB::dbfile.check("Posterior", | ||
settings$pfts[[i]]$posteriorid, | ||
con, settings$host$name, return.all = TRUE) | ||
pid <- grep("post.distns.*Rdata", files$file_name) ## is there a posterior file? | ||
if (length(pid) == 0) { | ||
pid <- grep("prior.distns.Rdata", files$file_name) ## is there a prior file? | ||
} | ||
if (length(pid) > 0) { | ||
posterior.files[i] <- file.path(files$file_path[pid], files$file_name[pid]) | ||
} ## otherwise leave posteriors as NA | ||
} | ||
## otherwise leave NA and get.parameter.samples will look for local | ||
} else { | ||
## does posterior.files point to a directory instead of a file? | ||
if(utils::file_test("-d",posterior.files[i])){ | ||
pfiles = dir(posterior.files[i],pattern = "post.distns.*Rdata",full.names = TRUE) | ||
if(length(pfiles)>1){ | ||
pid = grep("post.distns.Rdata",pfiles) | ||
if(length(pid > 0)){ | ||
pfiles = pfiles[grep("post.distns.Rdata",pfiles)] | ||
} else { | ||
PEcAn.logger::logger.error( | ||
"run.write.configs: could uniquely identify posterior files within", | ||
posterior.files[i]) | ||
} | ||
posterior.files[i] = pfiles | ||
} | ||
} | ||
## also, double check PFT outdir exists | ||
if (is.null(settings$pfts[[i]]$outdir) || is.na(settings$pfts[[i]]$outdir)) { | ||
## no outdir | ||
settings$pfts[[i]]$outdir <- file.path(settings$outdir, "pfts", settings$pfts[[i]]$name) | ||
} | ||
} ## end else | ||
} ## end for loop over pfts | ||
|
||
## Sample parameters | ||
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)) { | ||
samples <- new.env() | ||
load(samples.file, envir = samples) ## loads ensemble.samples, trait.samples, sa.samples, runs.samples, env.samples | ||
trait.samples <- samples$trait.samples | ||
ensemble.samples <- samples$ensemble.samples | ||
sa.samples <- samples$sa.samples | ||
runs.samples <- samples$runs.samples | ||
## env.samples <- samples$env.samples | ||
# PEcAn.uncertainty::get.parameter.samples(settings, posterior.files, ens.sample.method) | ||
# samples.file <- file.path(settings$outdir, "samples.Rdata") | ||
# if (file.exists(samples.file)) { | ||
# samples <- new.env() | ||
# load(samples.file, envir = samples) ## loads ensemble.samples, trait.samples, sa.samples, runs.samples, env.samples | ||
# trait.samples <- samples$trait.samples | ||
# ensemble.samples <- samples$ensemble.samples | ||
# sa.samples <- samples$sa.samples | ||
# runs.samples <- samples$runs.samples | ||
# ## env.samples <- samples$env.samples | ||
# } else { | ||
# PEcAn.logger::logger.error(samples.file, "not found, this file is required by the run.write.configs function") | ||
# } | ||
|
||
## Which posterior to use? | ||
if(is.logical(use.existing.samples)){ | ||
if(use.existing.samples & file.exists(file.path(settings$outdir, "samples.Rdata"))){ | ||
load(file.path(settings$outdir, "samples.Rdata")) ## loads ensemble.samples, trait.samples, sa.samples, runs.samples, env.samples | ||
} else { | ||
for (i in seq_along(settings$pfts)) { | ||
## if posterior.files is specified us that | ||
if (is.na(posterior.files[i])) { | ||
## otherwise, check to see if posteriorid exists | ||
if (!is.null(settings$pfts[[i]]$posteriorid)) { | ||
#TODO: sometimes `files` is a 0x0 tibble and other operations with it fail. | ||
files <- PEcAn.DB::dbfile.check("Posterior", | ||
settings$pfts[[i]]$posteriorid, | ||
con, settings$host$name, return.all = TRUE) | ||
pid <- grep("post.distns.*Rdata", files$file_name) ## is there a posterior file? | ||
if (length(pid) == 0) { | ||
pid <- grep("prior.distns.Rdata", files$file_name) ## is there a prior file? | ||
} | ||
if (length(pid) > 0) { | ||
posterior.files[i] <- file.path(files$file_path[pid], files$file_name[pid]) | ||
} ## otherwise leave posteriors as NA | ||
} | ||
## otherwise leave NA and get.parameter.samples will look for local | ||
} else { | ||
## does posterior.files point to a directory instead of a file? | ||
if(utils::file_test("-d",posterior.files[i])){ | ||
pfiles = dir(posterior.files[i],pattern = "post.distns.*Rdata",full.names = TRUE) | ||
if(length(pfiles)>1){ | ||
pid = grep("post.distns.Rdata",pfiles) | ||
if(length(pid > 0)){ | ||
pfiles = pfiles[grep("post.distns.Rdata",pfiles)] | ||
} else { | ||
PEcAn.logger::logger.error( | ||
"run.write.configs: could uniquely identify posterior files within", | ||
posterior.files[i]) | ||
} | ||
posterior.files[i] = pfiles | ||
} | ||
} | ||
## also, double check PFT outdir exists | ||
if (is.null(settings$pfts[[i]]$outdir) || is.na(settings$pfts[[i]]$outdir)) { | ||
## no outdir | ||
settings$pfts[[i]]$outdir <- file.path(settings$outdir, "pfts", settings$pfts[[i]]$name) | ||
} | ||
} ## end else | ||
} ## end for loop over pfts | ||
|
||
## Sample parameters | ||
PEcAn.uncertainty::get.parameter.samples(settings, posterior.files, ens.sample.method) | ||
load(file.path(settings$outdir, "samples.Rdata")) ## loads ensemble.samples, trait.samples, sa.samples, runs.samples, env.samples | ||
|
||
## end use.existing.samples == FALSE | ||
} ## is.logical(use.existing.samples) | ||
} else { | ||
PEcAn.logger::logger.error(samples.file, "not found, this file is required by the run.write.configs function") | ||
### use.existing.samples must be a filename | ||
load(use.existing.samples) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did a doubletake here ("wait, why is it calling load on a bool?") even after reading at the top of the function that Possible alternate design: Call this param |
||
} | ||
|
||
## remove previous runs.txt | ||
if (overwrite && file.exists(file.path(settings$rundir, "runs.txt"))) { | ||
PEcAn.logger::logger.warn("Existing runs.txt file will be removed.") | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Guessing this is temporarily commented out while developing the replacement, but I strongly recommend all replacement
load
calls copy the pattern shown here of loading all Rdata files into their own environment -- not only does it keep the package checks from complaining about variables with no visible binding, it means there's no ambiguity about what names were loaded from which file.The hack of reading the file into an environment and then immediately assigning back out to separate variables was just to minimize lines changed while quieting the check note. I'd support a further refactor to use all of these directly from thee loading environment instead (e.g. write all references to
sa.samples
assamples$sa.samples
)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.
to be honest, this was me trying to resolve a merge conflict and not having time to go back and check the code in more detail (it's been months since I worked on this PR). My plan had been to take a closer look at this function and once I had pulled in the latest code