Skip to content

Commit

Permalink
fix: add data_echo.ss_new checks
Browse files Browse the repository at this point in the history
SS updated data.ss_new files to be called data_echo.ss_new in version 3.19 this commit adds capacity to read these new file names.
  • Loading branch information
nathanvaughan-NOAA committed Nov 30, 2023
1 parent d1e7714 commit 23df01f
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 60 deletions.
19 changes: 14 additions & 5 deletions R/MS_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,20 @@ Interim <- function(EM_out_dir = NULL, EM_init_dir = NULL,

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


Reference_dat <- SS_readdat(
file = file.path(EM_out_dir, "data.ss_new"),
version = 3.30, section = 2, verbose = FALSE
)
if(file.exists(file.path(EM_out_dir, "data.ss_new"))){
Reference_dat <- SS_readdat(
file = file.path(EM_out_dir, "data.ss_new"),
version = 3.30, section = 2, verbose = FALSE
)
}else if(file.exists(file.path(EM_out_dir, "data_echo.ss_new"))){
Reference_dat <- SS_readdat(
file = file.path(EM_out_dir, "data_echo.ss_new"),
version = 3.30, section = 2, verbose = FALSE
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}



### This is test code that calculates the standard error of index residuals that could be
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
64 changes: 49 additions & 15 deletions R/initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,28 @@ 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"))
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")
)
if(file.exists(file.path(OM_out_dir, "data.ss_new"))){
tmp_new_dat <- readLines(file.path(OM_out_dir, "data.ss_new"))
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")
)
}
}else if(file.exists(file.path(OM_out_dir, "data_echo.ss_new"))){
tmp_new_dat <- readLines(file.path(OM_out_dir, "data.ss_new"))
nan_vals <- grep("nan", tmp_new_dat)
if (length(nan_vals) > 0) {
stop(
"NAN values present in the data_echo.ss_new om file, suggesting an issue ",
"setting up the OM. See ", file.path(OM_out_dir, "data_echo.ss_new")
)
}
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}


# check the names of F parameters in the Parameters section of the report
# file.
Expand Down Expand Up @@ -482,17 +496,37 @@ run_OM <- function(OM_dir,
debug_par_run = debug_par_run
)

dat <- r4ss::SS_readdat(file.path(OM_dir, "data.ss_new"),
section = max_section,
verbose = FALSE
)
if(file.exists(file.path(OM_dir, "data.ss_new"))){
dat <- r4ss::SS_readdat(file.path(OM_dir, "data.ss_new"),
section = max_section,
verbose = FALSE
)
}else if(file.exists(file.path(OM_dir, "data_echo.ss_new"))){
dat <- r4ss::SS_readdat(file.path(OM_dir, "data_echo.ss_new"),
section = max_section,
verbose = FALSE
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}

# 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"),
section = 2,
verbose = FALSE
)
if(file.exists(file.path(OM_dir, "data.ss_new"))){
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, "data.ss_new"),
section = 2,
verbose = FALSE
)
}else if(file.exists(file.path(OM_dir, "data_echo.ss_new"))){
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, "data_echo.ss_new"),
section = 2,
verbose = FALSE
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}

dat[["catch"]] <- exp_vals[["catch"]]
}
return(dat)
Expand Down
101 changes: 77 additions & 24 deletions R/results.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,54 @@ plot_index_sampling <- function(dir = getwd()) {
full.names = FALSE
), value = TRUE)
assertive.types::assert_is_a_string(om_name)
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
),
verbose = FALSE, section = 1
)
if(file.exists(file.path(dir, as.character(iters[1]),
om_name, "data.ss_new"))){
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
),
verbose = FALSE, section = 1
)
}else if(file.exists(file.path(dir, as.character(iters[1]),
om_name, "data_echo.ss_new"))){
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data_echo.ss_new"
),
verbose = FALSE, section = 1
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}

tmp_dat_OM[["CPUE"]][["iteration"]] <- as.character(iters[1])
tmp_dat_OM[["CPUE"]][["scenario"]] <- scenario
tmp_dat_OM[["CPUE"]][["model_run"]] <- "historical_values"
index_dat <- tmp_dat_OM[["CPUE"]]
# get the OM expected values
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
),
verbose = FALSE, section = 2
)
if(file.exists(file.path(dir, as.character(iters[1]),
om_name, "data.ss_new"))){
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data.ss_new"
),
verbose = FALSE, section = 2
)
}else if(file.exists(file.path(dir, as.character(iters[1]),
om_name, "data_echo.ss_new"))){
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data_echo.ss_new"
),
verbose = FALSE, section = 2
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}
tmp_dat_OM[["CPUE"]][["iteration"]] <- as.character(iters[1])
tmp_dat_OM[["CPUE"]][["scenario"]] <- scenario
tmp_dat_OM[["CPUE"]][["model_run"]] <- "OM_expected_values"
Expand All @@ -170,13 +199,28 @@ plot_index_sampling <- function(dir = getwd()) {
), value = TRUE)
assertive.types::assert_is_a_string(em_name)
for (i in iters) {
tmp_dat_EM <- r4ss::SS_readdat(
file.path(
dir, as.character(i),
em_name, "data.ss_new"
),
verbose = FALSE, section = 1
)
if(file.exists(file.path(dir, as.character(i),
em_name, "data.ss_new"))){
tmp_dat_EM <- r4ss::SS_readdat(
file.path(
dir, as.character(i),
em_name, "data.ss_new"
),
verbose = FALSE, section = 1
)
}else if(file.exists(file.path(dir, as.character(i),
em_name, "data_echo.ss_new"))){
tmp_dat_EM <- r4ss::SS_readdat(
file.path(
dir, as.character(i),
em_name, "data_echo.ss_new"
),
verbose = FALSE, section = 1
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}

tmp_dat_EM[["CPUE"]][["iteration"]] <- i
tmp_dat_EM[["CPUE"]][["scenario"]] <- scenario
tmp_dat_EM[["CPUE"]][["model_run"]] <- "sampled_dataset"
Expand Down Expand Up @@ -362,9 +406,18 @@ get_performance_metrics <- function(dir = getwd(),
)
}
om_mod_path <- tmp_mods[om_mod]
dat <- r4ss::SS_readdat(file.path(om_mod_path, "data.ss_new"),
section = 1, verbose = FALSE
)
if(file.exists(file.path(om_mod_path, "data.ss_new"))){
dat <- r4ss::SS_readdat(file.path(om_mod_path, "data.ss_new"),
section = 1, verbose = FALSE
)
}else if(file.exists(file.path(om_mod_path, "data_echo.ss_new"))){
dat <- r4ss::SS_readdat(file.path(om_mod_path, "data_echo.ss_new"),
section = 1, verbose = FALSE
)
}else{
stop("Error: No data.ss_new file or data_echo.ss_new file was found.")
}

tmp_catch <- dat[["catch"]]
tmp_catch <- tmp_catch[, c("year", "fleet", "catch")]
colnames(tmp_catch) <- c("year", "fleet", "value")
Expand Down
17 changes: 11 additions & 6 deletions R/runSS.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#' @param show.output.on.console Logical: passed on to
#' \code{\link[base]{system}}.
#' @param check_run Should it be checked that the model ran by deleting the
#' data.ss_new file if one exists and then checking if one was created?
#' Defaults to TRUE.
#' data.ss_new or data_echo.ss_new file if one exists and then checking if one
#' was created? Defaults to TRUE.
#' @param debug_par_run If set to TRUE, and the run fails, a new folder called
#' error_check will be created, and the model will be run from control start
#' values instead of ss.par. The 2 par files are then compared to help debug
Expand Down Expand Up @@ -59,6 +59,11 @@ run_ss_model <- function(dir,
if (file.exists(ss_new_path)) {
file.remove(ss_new_path)
}

ss_new_path2 <- file.path(dir, "data_echo.ss_new")
if (file.exists(ss_new_path2)) {
file.remove(ss_new_path2)
}
}

if (verbose) message("Running SS.")
Expand All @@ -78,7 +83,7 @@ run_ss_model <- function(dir,
)
}
if (check_run == TRUE) {
if (!file.exists(ss_new_path)) {
if (!file.exists(ss_new_path) & !file.exists(ss_new_path2)) {
if (debug_par_run) {
test_no_par(
orig_mod_dir = dir,
Expand All @@ -87,12 +92,12 @@ run_ss_model <- function(dir,
# note that this will exit on error.
} else {
stop(
"data.ss_new was not created during the model run, which suggests ",
"SS did not run correctly"
"data.ss_new or data_echo.ss_new were not created during the model ",
"run, which suggests SS did not run correctly"
)
}
} else {
if (verbose) "data.ss_new created during model run."
if (verbose) "data.ss_new or data_echo.ss_new created during model run."
}
}
Sys.sleep(admb_pause)
Expand Down
39 changes: 30 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -589,17 +589,38 @@ copy_model_files <- function(OM_in_dir = NULL,
EM_out_dir = NULL,
verbose = FALSE) {
# checks
if (!is.null(OM_in_dir)) {
if (!all(c(
"control.ss_new", "data.ss_new", "starter.ss_new",
"forecast.ss_new", "ss.par"
) %in% list.files(OM_in_dir))) {
stop(
".ss_new files not found in the original OM directory ",
OM_in_dir, ". Please run the model to make the .ss_new files available."
)
if(file.exists(file.path(OM_in_dir, "data.ss_new"))){
if (!is.null(OM_in_dir)) {
if (!all(c(
"control.ss_new", "data.ss_new", "starter.ss_new",
"forecast.ss_new", "ss.par"
) %in% list.files(OM_in_dir))) {
stop(
".ss_new files not found in the original OM directory ",
OM_in_dir, ". Please run the model to make .ss_new files available."
)
}
}
}else if(file.exists(file.path(OM_in_dir, "data_echo.ss_new"))){
if (!is.null(OM_in_dir)) {
if (!all(c(
"control.ss_new", "data_echo.ss_new", "starter.ss_new",
"forecast.ss_new", "ss.par"
) %in% list.files(OM_in_dir))) {
stop(
".ss_new files not found in the original OM directory ",
OM_in_dir, ". Please run the model to make .ss_new files available."
)
}
}
}else{
stop(
".ss_new files not found in the original OM directory ",
OM_in_dir, ". Please run the model to make the .ss_new files available."
)
}


# copy over OM ----
if (!is.null(OM_in_dir) & !is.null(OM_out_dir)) {
if (verbose == TRUE) {
Expand Down

0 comments on commit 23df01f

Please sign in to comment.