Skip to content

Commit

Permalink
more updates for v3.30.21; still not working yet though
Browse files Browse the repository at this point in the history
  • Loading branch information
e-perl-NOAA committed Jun 4, 2024
1 parent 149cfed commit 2942c60
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 91 deletions.
6 changes: 1 addition & 5 deletions R/MS_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,7 @@ 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"
}
data_filename <- list.files(file.path(OM_dir), pattern = "data.ss_new|data_echo.ss_new")

Reference_dat <- SS_readdat(
file = file.path(EM_out_dir, data_filename),
Expand Down
6 changes: 6 additions & 0 deletions R/develop_OMs.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ develop_OMs <- function(OM_name = NULL, OM_in_dir = NULL, out_dir = getwd(), par
if (!file.exists(file.path(tmp_mod_path, "control.ss_new"))) {
warning("Problem refitting model in ", tmp_mod_path)
}
if (file.exists(file.path(tmp_mod_path,"data.ss_new")) && file.exists(file.path(tmp_mod_path,"data_echo.ss_new"))) {
file.remove(file.path(tmp_mod_path,"data.ss_new"))
}
} else {
# run with no estimation
run_ss_model(
Expand All @@ -114,6 +117,9 @@ develop_OMs <- function(OM_name = NULL, OM_in_dir = NULL, out_dir = getwd(), par
if (!file.exists(file.path(tmp_mod_path, "control.ss_new"))) {
warning("Problem running model without estimation in ", tmp_mod_path)
}
if (file.exists(file.path(tmp_mod_path,"data.ss_new")) && file.exists(file.path(tmp_mod_path,"data_echo.ss_new"))) {
file.remove(file.path(tmp_mod_path,"data.ss_new"))
}
# add back original recdevs into the model (b/c not specified through the ctl file)
new_parfile <- r4ss::SS_readpar_3.30(
parfile = file.path(tmp_mod_path, "ss.par"),
Expand Down
7 changes: 5 additions & 2 deletions R/develtools.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ 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")) || file.exists(file.path(new_mod_dir, "data_echo.ss_new"))) {

dat_file <- list.files(file.path(new_mod_dir), pattern = "data.ss_new|data_echo.ss_new")

if (file.exists(file.path(new_mod_dir, dat_file))) {
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 All @@ -49,7 +52,7 @@ test_no_par <- function(orig_mod_dir, new_mod_dir) {
stop(
"Problem with the ss.par file - same number of lines. ",
"The original par file in ", orig_mod_dir,
" has the same number of values as the new ",
"has the same number of values as the new ",
"par in ", new_mod_dir, ", so not sure what the issue is."
)
}
Expand Down
26 changes: 10 additions & 16 deletions R/initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ create_OM <- function(OM_out_dir,
# read in files to use ----
dat <- r4ss::SS_readdat(
file = file.path(OM_out_dir, start[["datfile"]]),
verbose = FALSE, section = 1
verbose = FALSE
)
forelist <- r4ss::SS_readforecast(
file = file.path(OM_out_dir, "forecast.ss"),
Expand Down Expand Up @@ -333,8 +333,6 @@ create_OM <- function(OM_out_dir,
if (!is.null(dat[["age_info"]])) dat[["age_info"]][["mintailcomp"]] <- -1
}



# write all files
r4ss::SS_writectl(
ctllist = ctl, outfile = file.path(OM_out_dir, start[["ctlfile"]]),
Expand Down Expand Up @@ -365,6 +363,9 @@ create_OM <- function(OM_out_dir,
verbose = verbose,
debug_par_run = TRUE
)
if (file.exists(file.path(OM_out_dir,"data.ss_new")) && file.exists(file.path(OM_out_dir,"data_echo.ss_new"))) {
file.remove(file.path(OM_out_dir,"data.ss_new"))
}
# TODO: maybe add the following check into the debug par run arg of run_ss_model?
check_par <- readLines(file.path(OM_out_dir, "ss.par"))
check_sum_val <- check_par[grep("checksum999", check_par) + 1]
Expand All @@ -384,11 +385,7 @@ create_OM <- function(OM_out_dir,
)
}
# check model runs without producing nans in the data file
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")
}
data_filepath <- list.files(file.path(OM_out_dir), pattern = "data.ss_new|data_echo.ss_new", full.names = TRUE)
tmp_new_dat <- readLines(data_filepath)
nan_vals <- grep("nan", tmp_new_dat)
if (length(nan_vals) > 0) {
Expand Down Expand Up @@ -476,7 +473,7 @@ run_OM <- function(OM_dir,
if (is.null(seed)) {
seed <- stats::runif(1, 1, 9999999)
}

# browser()
start <- r4ss::SS_readstarter(file.path(OM_dir, "starter.ss"),
verbose = FALSE
)
Expand All @@ -491,13 +488,10 @@ run_OM <- function(OM_dir,
verbose = verbose,
debug_par_run = debug_par_run
)

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

if (file.exists(file.path(OM_dir,"data.ss_new")) && file.exists(file.path(OM_dir,"data_echo.ss_new"))) {
file.remove(file.path(OM_dir,"data.ss_new"))
}
data_filename <- list.files(file.path(OM_dir), pattern = "data.ss_new|data_echo.ss_new")
dat <- r4ss::SS_readdat(file.path(OM_dir, data_filename),
section = max_section,
verbose = FALSE
Expand Down
10 changes: 3 additions & 7 deletions R/results.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,7 @@ plot_index_sampling <- function(dir = getwd()) {
), 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"
}

data_file <- list.files(file.path(dir,as.character(iters[1]), om_name), pattern = "data.ss_new|data_echo.ss_new")
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
Expand Down Expand Up @@ -369,7 +364,8 @@ 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"),
om_mod_dat <- list.files(file.path(om_mod_path), pattern = "data.ss_new|data_echo.ss_new")
dat <- r4ss::SS_readdat(file.path(om_mod_path, om_mod_dat),
section = 1, verbose = FALSE
)
tmp_catch <- dat[["catch"]]
Expand Down
26 changes: 12 additions & 14 deletions R/runSS.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ run_ss_model <- function(dir,
bin <- get_bin(ss3_bin)
if (check_run == TRUE) {
# 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(file.path(dir,"data.ss_new")) && file.exists(file.path(dir,"data_echo.ss_new"))) {
file.remove(file.path(dir,"data.ss_new"))
}

if (file.exists(ss_new_path)) {
file.remove(ss_new_path)
ss_new_path <- list.files(dir, pattern = "data.ss_new|data_echo.ss_new", full.names = TRUE)
if (length(ss_new_path) > 0) {
if (file.exists(ss_new_path)) {
file.remove(ss_new_path)
}
}
}

if (verbose) message("Running SS3.")
if (os == "unix") {
system(
Expand All @@ -86,7 +83,7 @@ run_ss_model <- function(dir,
)
}
if (check_run == TRUE) {
if (!file.exists(ss_new_path)) {
if (!file.exists(file.path(dir, "data.ss_new")) && !file.exists(file.path(dir, "data_echo.ss_new"))) {
if (debug_par_run) {
test_no_par(
orig_mod_dir = dir,
Expand All @@ -95,12 +92,13 @@ 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 ",
"SS3 did not run correctly"
"new data file (data.ss_new if using SS3 3.30.18 or data_echo.ss_new",
"if using SS3 3.30.21) was not created during the model run, which",
"suggests SS3 did not run correctly"
)
}
} else {
if (verbose) "data.ss_new created during model run."
if (verbose) "new data file (data.ss_new if using SS3 3.30.18 or data_echo.ss_new if using SS3 3.30.21) created during model run."
}
}
Sys.sleep(admb_pause)
Expand Down
4 changes: 1 addition & 3 deletions R/runSSMSE.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ run_SSMSE <- function(scen_name_vec,
if (!is.null(custom_MS_source)) {
source(custom_MS_source)
}

# input checks
if (!all(MS_vec %in% c("EM", "no_catch", "Interim"))) {
invalid_MS <- MS_vec[unlist(lapply(MS_vec, function(x) !exists(x)))]
Expand Down Expand Up @@ -538,7 +537,7 @@ run_SSMSE_iter <- function(out_dir = NULL,
}

message("Starting iteration ", niter, ".")

# browser()
set.seed((iter_seed[["iter"]][1] + 123))
# get and create directories, copy model files ----
# assign or reassign OM_dir and OM_in_dir in case they weren't specified
Expand Down Expand Up @@ -584,7 +583,6 @@ run_SSMSE_iter <- function(out_dir = NULL,
sample_struct <- convert_to_r4ss_names(sample_struct)
sample_struct_hist <- convert_to_r4ss_names(sample_struct_hist)
}

# Convert the user input parameter modifications into vectors of annual additive deviations
future_om_dat <- convert_future_om_list_to_devs_df(future_om_list = future_om_list, scen_name = scen_name, niter = niter, om_mod_path = OM_out_dir, nyrs = nyrs, global_seed = (iter_seed[["iter"]][1] + 1234))

Expand Down
3 changes: 2 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,9 @@ copy_model_files <- function(OM_in_dir = NULL,
verbose = FALSE) {
# checks
if (!is.null(OM_in_dir)) {
dat_file <- list.files(OM_in_dir, pattern = "data.ss_new|data_echo.ss_new")
if (!all(c(
"control.ss_new", "data.ss_new", "starter.ss_new",
"control.ss_new", dat_file, "starter.ss_new",
"forecast.ss_new", "ss.par"
) %in% list.files(OM_in_dir))) {
stop(
Expand Down
Binary file modified inst/bin/Linux64/ss3
Binary file not shown.
Binary file modified inst/bin/MacOS/ss3
Binary file not shown.
Binary file modified inst/bin/Windows64/ss3.exe
Binary file not shown.
8 changes: 6 additions & 2 deletions tests/testthat/test-develop_OMs.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ test_that("develop_OMs works as expected", {
)
expect_true(file.exists(file.path(temp_path, "cod_SR_BH_steep_0.4", "control.ss_new")))
expect_true(file.exists(file.path(temp_path, "cod_SR_BH_steep_0.8", "control.ss_new")))
dat <- r4ss::SS_readdat(file.path(temp_path, "cod_SR_BH_steep_0.4", "data.ss_new"),

dat_file <- list.files(file.path(temp_path, "cod_SR_BH_steep_0.4"), pattern = "data.ss_new|data_echo.ss_new")
dat <- r4ss::SS_readdat(file.path(temp_path, "cod_SR_BH_steep_0.4", dat_file),
verbose = FALSE
)
ctl <- r4ss::SS_readctl(
Expand Down Expand Up @@ -58,7 +60,9 @@ test_that("develop_OMs works as expected when renaming the model", {
)
expect_true(file.exists(file.path(temp_path_mod_rename, "rename-mod-test_SR_BH_steep_0.4", "control.ss_new")))
expect_true(file.exists(file.path(temp_path_mod_rename, "rename-mod-test_SR_BH_steep_0.8", "control.ss_new")))
dat <- r4ss::SS_readdat(file.path(temp_path_mod_rename, "rename-mod-test_SR_BH_steep_0.4", "data.ss_new"),

dat_file <- list.files(file.path(temp_path_mod_rename, "rename-mod-test_SR_BH_steep_0.4"), pattern = "data.ss_new|data_echo.ss_new")
dat <- r4ss::SS_readdat(file.path(temp_path_mod_rename, "rename-mod-test_SR_BH_steep_0.4", dat_file),
verbose = FALSE
)
ctl <- r4ss::SS_readctl(
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-future_om.R
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ test_that("Tests a model with env link using historical values", {
om_mod_path = om_path, nyrs = 10
)
expect_length(unique(devs_list[["dev_vals"]][["SR_LN(R0)"]]), 1)
dat <- r4ss::SS_readdat(file.path(om_path, "data.ss_new"))
OM_dat_file <- list.files(om_path, pattern = "data.ss_new|data_echo.ss_new")
dat <- r4ss::SS_readdat(file.path(om_path, OM_dat_file))
dat <- dat[["envdat"]]
env_vals <- dat[dat[["Yr"]] >= tmp_list[[1]][["input"]][["first_yr_averaging"]] &
dat[["Yr"]] <= tmp_list[[1]][["input"]][["last_yr_averaging"]], "Value"]
Expand Down
21 changes: 13 additions & 8 deletions tests/testthat/test-interim.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,19 @@ test_that("run_SSMSE runs with interim assessment workflow", {
) # Set a fixed integer seed that allows replication

expect_true(result[["base"]][["errored_iterations"]] == "No errored iterations")
expect_true(file.exists(file.path(
temp_path, "base", "1", "cod_OM",
"data.ss_new"
)))
expect_true(file.exists(file.path(
temp_path, "base", "1", "cod_EM_init",
"data.ss_new"
)))

cod_OM_files <- list.files(file.path(temp_path,"base","1","cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% cod_OM_files))
# expect_true(file.exists(file.path(
# temp_path, "base", "1", "cod_OM",
# "data.ss_new"
# )))
cod_EM_files <- list.files(file.path(temp_path, "base", "1", "cod_EM_init"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% cod_EM_files))
# expect_true(file.exists(file.path(
# temp_path, "base", "1", "cod_EM_init",
# "data.ss_new"
# )))
expect_length(result, 1)
# could add some better tests to make sure this works.
})
Loading

0 comments on commit 2942c60

Please sign in to comment.