Skip to content

Commit

Permalink
fix more warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
e-perl-NOAA committed Jun 5, 2024
1 parent 2942c60 commit 2f84d16
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 86 deletions.
52 changes: 27 additions & 25 deletions R/develtools.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,36 @@ test_no_par <- function(orig_mod_dir, new_mod_dir) {
# read in the 2 par files.
orig_par <- readLines(file.path(orig_mod_dir, "ss.par"))

dat_file <- list.files(file.path(new_mod_dir), pattern = "data.ss_new|data_echo.ss_new")
dat_file <- list.files(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)
orig_par_names <- grep("^# [^N]", orig_par, value = TRUE)
missing_vars <- setdiff(new_par_names, orig_par_names)
if (length(orig_par) < length(new_par)) {
msg <- " is missing the "
if (length(dat_file) > 0) {
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)
orig_par_names <- grep("^# [^N]", orig_par, value = TRUE)
missing_vars <- setdiff(new_par_names, orig_par_names)
if (length(orig_par) < length(new_par)) {
msg <- " is missing the "
}
if (length(orig_par) > length(new_par)) {
msg <- " has added "
}
stop(
"Problem with the ss.par file - different number of lines. ",
"The original par file in ", orig_mod_dir, msg, " parameters: ",
paste0(missing_vars, collapse = ", ")
)
} else {
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 ",
"par in ", new_mod_dir, ", so not sure what the issue is."
)
}
if (length(orig_par) > length(new_par)) {
msg <- " has added "
}
stop(
"Problem with the ss.par file - different number of lines. ",
"The original par file in ", orig_mod_dir, msg, " parameters: ",
paste0(missing_vars, collapse = ", ")
)
} else {
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 ",
"par in ", new_mod_dir, ", so not sure what the issue is."
)
# TODO: develop some more sophisticated ways to look at par file diffs.
}
# TODO: develop some more sophisticated ways to look at par file diffs.
} else {
# problem is not with the par file, but with some other model misspecification.
stop(
Expand Down
29 changes: 21 additions & 8 deletions R/initOM.R
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,30 @@ run_OM <- function(OM_dir,
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
)
if(data_filename == "data.ss_new"){
dat <- r4ss::SS_readdat(file.path(OM_dir, data_filename),
section = max_section, #bootstrap file in v3.30.21
verbose = FALSE
)
} else {
# for SS3 v3.30.21
dat <- r4ss::SS_readdat(file.path(OM_dir, "data_boot_001.ss"),
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_filename),
section = 2,
verbose = FALSE
)
if(data_filename == "data.ss_new"){
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, data_filename),
section = 2, #expected values data file in v3.30.21
verbose = FALSE
)
} else {
# for SS3 v3.30.21
exp_vals <- r4ss::SS_readdat(file.path(OM_dir, "data_expval.ss"),
verbose = FALSE
)
}
dat[["catch"]] <- exp_vals[["catch"]]
}
return(dat)
Expand Down
83 changes: 60 additions & 23 deletions R/results.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,45 @@ plot_index_sampling <- function(dir = getwd()) {
assertive.types::assert_is_a_string(om_name)

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]),
om_name, data_file
),
verbose = FALSE, section = 1
)
if (data_file == "data.ss_new") {
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, data_file
),
verbose = FALSE, section = 1
)
} else {
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, data_file
),
verbose = FALSE
)
}
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_file
),
if (data_file == "data.ss_new") {
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, data_file
),
verbose = FALSE, section = 2
)
)
} else {
tmp_dat_OM <- r4ss::SS_readdat(
file.path(
dir, as.character(iters[1]),
om_name, "data_expval.ss"
),
verbose = FALSE
)
}
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 @@ -172,13 +192,24 @@ 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(data_file == "data.ss_new"){
tmp_dat_EM <- r4ss::SS_readdat(
file.path(
dir, as.character(i),
em_name, data_file
),
verbose = FALSE, section = 1
)
} else {
tmp_dat_EM <- r4ss::SS_readdat(
file.path(
dir, as.character(i),
em_name, data_file
),
verbose = FALSE
)
}

tmp_dat_EM[["CPUE"]][["iteration"]] <- i
tmp_dat_EM[["CPUE"]][["scenario"]] <- scenario
tmp_dat_EM[["CPUE"]][["model_run"]] <- "sampled_dataset"
Expand Down Expand Up @@ -364,10 +395,16 @@ get_performance_metrics <- function(dir = getwd(),
)
}
om_mod_path <- tmp_mods[om_mod]
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
om_mod_dat <- list.files(om_mod_path, pattern = "data.ss_new|data_echo.ss_new")
if(om_mod_dat == "data.ss_new"){
dat <- r4ss::SS_readdat(file.path(om_mod_path, om_mod_dat),
section = 1, verbose = FALSE
)
} else {
dat <- r4ss::SS_readdat(file.path(om_mod_path, om_mod_dat),
verbose = FALSE
)
}
tmp_catch <- dat[["catch"]]
tmp_catch <- tmp_catch[, c("year", "fleet", "catch")]
colnames(tmp_catch) <- c("year", "fleet", "value")
Expand Down
6 changes: 3 additions & 3 deletions R/runSS.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ run_ss_model <- function(dir,
# note that this will exit on error.
} else {
stop(
"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"
"New data file (data.ss_new if using SS3 v3.30.18 or data_echo.ss_new ",
"if using SS3 v3.30.21) was not created during the model run, which ",
"suggests SS3 did not run correctly."
)
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions R/runSSMSE.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ run_SSMSE_scen <- function(scen_name = "scen_1",
if (!is.null(sample_struct_hist)) assertive.types::assert_is_list(sample_struct_hist)
assertive.types::assert_is_a_bool(verbose)


# create the out_dir to store all files for all iter in the scenario.
if (is.null(out_dir_scen)) {
out_dir_iter <- scen_name
Expand Down Expand Up @@ -537,7 +536,6 @@ 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
36 changes: 15 additions & 21 deletions tests/testthat/test-runSSMSE.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ test_that("run_SSMSE_iter runs with no EM", {

cod_OM_files <- list.files(file.path(new_temp_path, "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% cod_OM_files))
# expect_true(file.exists(file.path(new_temp_path, "1", "cod_OM", "data.ss_new")))
expect_true(result)
})

Expand Down Expand Up @@ -54,13 +53,11 @@ test_that("run_SSMSE runs with an EM, and works with summary funs", {
)) # Set a fixed integer seed that allows replication
expect_equivalent(result[["H-ctl"]][["errored_iterations"]], "No errored iterations")

H-ctl_cod_OM_files <- list.files(file.path(temp_path, "H-ctl", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-ctl_cod_OM_files))
# expect_true(file.exists(file.path(temp_path, "H-ctl", "1", "cod_OM", "data.ss_new")))
Hctl_cod_OM_files <- list.files(file.path(temp_path, "H-ctl", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hctl_cod_OM_files))

H-ctl_cod_EM_files <- list.files(file.path(temp_path, "H-ctl", "1", "cod_EM_106"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-ctl_cod_EM_files))
# expect_true(file.exists(file.path(temp_path, "H-ctl", "1", "cod_EM_106", "data.ss_new")))
Hctl_cod_EM_files <- list.files(file.path(temp_path, "H-ctl", "1", "cod_EM_106"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hctl_cod_EM_files))

expect_length(result, 1)
# some more specific values, specific to the scenario above.
Expand Down Expand Up @@ -117,10 +114,10 @@ test_that("run_SSMSE runs multiple iterations/scenarios and works with summary f
seed = 12345
)) # Set a fixed integer seed that allows replication
expect_equivalent(result[["H-ctl"]][["errored_iterations"]], "No errored iterations")
H-ctl_cod_OM_files <- list.files(file.path(new_temp_path, "H-ctl", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-ctl_cod_OM_files))
H-ctl_cod_EM_103_files <- list.files(file.path(new_temp_path, "H-ctl", "1", "cod_EM_103"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-ctl_cod_EM_103_files))
Hctl_cod_OM_files <- list.files(file.path(new_temp_path, "H-ctl", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hctl_cod_OM_files))
Hctl_cod_EM_103_files <- list.files(file.path(new_temp_path, "H-ctl", "1", "cod_EM_103"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hctl_cod_EM_103_files))
# this file should not exist b/c run_EM_last_yr is FALSE.
# expect_true(!file.exists(
# file.path(new_temp_path, "H-ctl", "1", "cod_EM_106", "data.ss_new")
Expand All @@ -129,15 +126,12 @@ test_that("run_SSMSE runs multiple iterations/scenarios and works with summary f
result[["H-scen-2"]][["errored_iterations"]],
"No errored iterations"
)
H-scen2_cod_OM_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-scen2_cod_OM_files))
H-scen2_cod_EM_103_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_EM_103"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% H-scen2_cod_EM_103_files))
H-scen2_cod_EM_106_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_EM_106"))
expect_true(!any(c("data.ss_new","data_echo.ss_new") %in% H-scen2_cod_EM_106_files))
# expect_true(!file.exists(
# file.path(new_temp_path, "H-scen-2", "1", "cod_EM_106", "data.ss_new")
# ))
Hscen2_cod_OM_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_OM"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hscen2_cod_OM_files))
Hscen2_cod_EM_103_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_EM_103"))
expect_true(any(c("data.ss_new","data_echo.ss_new") %in% Hscen2_cod_EM_103_files))
Hscen2_cod_EM_106_files <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_EM_106"))
expect_true(!any(c("data.ss_new","data_echo.ss_new") %in% Hscen2_cod_EM_106_files))
expect_length(result, 2)
# summarize results
summary <- SSMSE_summary_all(dir = new_temp_path, run_parallel = FALSE)
Expand All @@ -146,7 +140,7 @@ test_that("run_SSMSE runs multiple iterations/scenarios and works with summary f

# calculate performance metrics
# to use in the tests
dat_file <- list.files(file.path(temp_path, "H-scen-2", "1", "cod_OM"), pattern = "data.ss_new|data_echo.ss_new")
dat_file <- list.files(file.path(new_temp_path, "H-scen-2", "1", "cod_OM"), pattern = "data.ss_new|data_echo.ss_new")
tmp_dat <- r4ss::SS_readdat(
file.path(new_temp_path, "H-scen-2", "1", "cod_OM", dat_file)
)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_develtools.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ r4ss::SS_writestarter(start_orig,
# mock a bad par file by deleting a line
orig_par <- readLines(file.path(orig_mod_dir, "ss.par"))
writeLines(orig_par, file.path(temp_path, "good_ss.par"))
fcast_imp_line <- grep("^# Fcast_impl_error:$", orig_par)
bad_par <- orig_par[-c(fcast_imp_line, fcast_imp_line + 1)]
fcast_line <- grep("^# Fcast_impl_error:$|^# Fcast_recruitments:$", orig_par)
bad_par <- orig_par[-c(fcast_line, fcast_line + 1)]
writeLines(bad_par, file.path(orig_mod_dir, "ss.par"))
new_mod_dir <- file.path(temp_path, "new_mod_dir")

Expand All @@ -36,9 +36,9 @@ test_that("test_no_par works as expected", {
run_ss_model(orig_mod_dir, "-maxfn 0 -phase 50 -nohess",
verbose = FALSE
),
"data.ss_new was not created during the model run",
"New data file (data.ss_new if using SS3 v3.30.18 or data_echo.ss_new ",
fixed = TRUE
)
)
expect_error(
test_no_par(
orig_mod_dir = orig_mod_dir,
Expand Down

0 comments on commit 2f84d16

Please sign in to comment.