Skip to content

Commit

Permalink
some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
e-perl-NOAA committed May 30, 2024
1 parent 1f12b27 commit 149cfed
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
7 changes: 6 additions & 1 deletion R/MS_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,14 @@ Interim <- function(EM_out_dir = NULL, EM_init_dir = NULL,

run_EM(EM_dir = EM_out_dir, verbose = verbose, check_converged = TRUE)

data_filename <- if(file.exists(file.path(OM_dir, "data.ss_new"))) {
"data.ss_new"
} else {
"data_echo.ss_new"
}

Reference_dat <- SS_readdat(
file = file.path(EM_out_dir, "data.ss_new"),
file = file.path(EM_out_dir, data_filename),
version = 3.30, section = 2, verbose = FALSE
)

Expand Down
2 changes: 1 addition & 1 deletion R/develtools.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test_no_par <- function(orig_mod_dir, new_mod_dir) {
try(run_ss_model(new_mod_dir, "-maxfn 0 -phase 50 -nohess", verbose = FALSE))
# read in the 2 par files.
orig_par <- readLines(file.path(orig_mod_dir, "ss.par"))
if (file.exists(file.path(new_mod_dir, "data.ss_new"))) {
if (file.exists(file.path(new_mod_dir, "data.ss_new")) || file.exists(file.path(new_mod_dir, "data_echo.ss_new"))) {
new_par <- readLines(file.path(new_mod_dir, "ss.par"))
if (length(orig_par) != length(new_par)) {
new_par_names <- grep("^# [^N]", new_par, value = TRUE)
Expand Down
21 changes: 16 additions & 5 deletions R/initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,17 @@ create_OM <- function(OM_out_dir,
)
}
# check model runs without producing nans in the data file
tmp_new_dat <- readLines(file.path(OM_out_dir, "data.ss_new"))
data_filepath <- if(file.exists(file.path(OM_out_dir, "data.ss_new"))) {
file.path(OM_out_dir, "data.ss_new")
} else {
file.path(OM_out_dir, "data_echo.ss_new")
}
tmp_new_dat <- readLines(data_filepath)
nan_vals <- grep("nan", tmp_new_dat)
if (length(nan_vals) > 0) {
stop(
"NAN values present in the data.ss_new om file, suggesting an issue ",
"setting up the OM. See ", file.path(OM_out_dir, "data.ss_new")
"NAN values present in the ", basename(data_filepath)," om file, suggesting an issue ",
"setting up the OM. See ", data_filepath
)
}

Expand Down Expand Up @@ -487,14 +492,20 @@ run_OM <- function(OM_dir,
debug_par_run = debug_par_run
)

dat <- r4ss::SS_readdat(file.path(OM_dir, "data.ss_new"),
data_filename <- if(file.exists(file.path(OM_dir, "data.ss_new"))) {
"data.ss_new"
} else {
"data_echo.ss_new"
}

dat <- r4ss::SS_readdat(file.path(OM_dir, data_filename),
section = max_section,
verbose = FALSE
)
# replace with the expected catch values if sample_catch is FALSE and using
# bootstrap
if (boot == TRUE & sample_catch == FALSE) {
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, "data.ss_new"),
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, data_filename),
section = 2,
verbose = FALSE
)
Expand Down
11 changes: 9 additions & 2 deletions R/results.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,17 @@ plot_index_sampling <- function(dir = getwd()) {
full.names = FALSE
), value = TRUE)
assertive.types::assert_is_a_string(om_name)

data_file <- if(file.exists(file.path(dir, as.character(iters[1]), om_name, "data.ss_new"))){
"data.ss_new"
} else {
"data_echo.ss_new"
}

tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
om_name, data_file
),
verbose = FALSE, section = 1
)
Expand All @@ -154,7 +161,7 @@ plot_index_sampling <- function(dir = getwd()) {
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
om_name, data_file
),
verbose = FALSE, section = 2
)
Expand Down
32 changes: 20 additions & 12 deletions R/runSS.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

#' Run an operating or estimation model
#'
#' This function takes care of calling SS. Importantly, it parses whether the
#' This function takes care of calling SS3. Importantly, it parses whether the
#' user is on Unix or Windows and calls the binary correctly. This lower-level
#' function is meant to be called by higher level functions. Modified from
#' run_ss3model in \href{https://github.com/ss3sim/ss3sim}{ss3sim}.
#'
#'
#' @param dir The full or relative path to the model directory
#' @param admb_options Any options to pass to SS command. Should be
#' @param admb_options Any options to pass to SS3 command. Should be
#' of the form '-option'. Note that no checks are done to ensure this is a
#' valid ADMB command
#' @param ss_bin Name of the SS executable. Defaults to "ss"
#' @param ss3_bin Name of the SS3 executable. Defaults to "ss3"
#' @param ignore.stdout Passed to \code{system}. If \code{TRUE} then ADMB
#' output is not printed on screen. This will be slightly faster. Set to
#' \code{FALSE} to help with debugging.
Expand All @@ -39,7 +39,7 @@
#' @author Sean C. Anderson, Kathryn Doering
run_ss_model <- function(dir,
admb_options = "",
ss_bin = "ss",
ss3_bin = "ss3",
ignore.stdout = TRUE,
admb_pause = 0.05,
show.output.on.console = FALSE,
Expand All @@ -53,15 +53,23 @@ run_ss_model <- function(dir,
wd_orig <- getwd()
on.exit(setwd(wd_orig))
os <- .Platform[["OS.type"]]
bin <- get_bin(ss_bin)
bin <- get_bin(ss3_bin)
if (check_run == TRUE) {
ss_new_path <- file.path(dir, "data.ss_new")
# new data file named for ss3 v.3.30.18 and prior
if(file.exists(file.path(dir, "data.ss_new"))) {
ss_new_path <- file.path(dir, "data.ss_new")
}
# new data file named for ss3 v.3.30.19 and after
if(file.exists(file.path(dir, "data_echo.ss_new"))) {
ss_new_path <- file.path(dir, "data_echo.ss_new")
}

if (file.exists(ss_new_path)) {
file.remove(ss_new_path)
}
}

if (verbose) message("Running SS.")
if (verbose) message("Running SS3.")
if (os == "unix") {
system(
paste0(
Expand All @@ -88,7 +96,7 @@ run_ss_model <- function(dir,
} else {
stop(
"data.ss_new was not created during the model run, which suggests ",
"SS did not run correctly"
"SS3 did not run correctly"
)
}
} else {
Expand All @@ -104,8 +112,8 @@ run_ss_model <- function(dir,
#' Get the binary/executable location in the package SSMSE. This function
#' is from \href{https://github.com/ss3sim/ss3sim}{ss3sim}.
#'
#' @param bin_name Name of SS3 binary, defaults to "ss"
#' @return The path to an SS binary. If using the GitHub version of the
#' @param bin_name Name of SS3 binary, defaults to "ss3"
#' @return The path to an SS3 binary. If using the GitHub version of the
#' package, this will be an internal binary. Otherwise, this function
#' will search for a version of the binary in your path. See the
#' ss3sim vignette.
Expand All @@ -114,7 +122,7 @@ run_ss_model <- function(dir,
#' \dontrun{
#' get_bin()
#' }
get_bin <- function(bin_name = "ss") {
get_bin <- function(bin_name = "ss3") {
# code inspiration from glmmADMB package:
if (.Platform[["OS.type"]] == "windows") {
platform <- "Windows64"
Expand Down Expand Up @@ -161,7 +169,7 @@ get_bin <- function(bin_name = "ss") {
bin <- Sys.which(bin_name)[[1]]
if (bin == "") {
stop(
"The expected SS executable, ", bin_name, ", was not found in your",
"The expected SS3 executable, ", bin_name, ", was not found in your",
" path. See the ss3sim vignette and ?ss3sim::run_ss3model for ",
"instructions."
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 149cfed

Please sign in to comment.