diff --git a/R/plot_spr.R b/R/plot_spr.R index 1acd96d1..01d786c0 100644 --- a/R/plot_spr.R +++ b/R/plot_spr.R @@ -39,7 +39,7 @@ plot_spr <- function( group = NULL, facet = NULL, ref_line = "target", - era = "time", + era = NULL, module = NULL, model = NULL, interactive = TRUE, @@ -55,8 +55,7 @@ plot_spr <- function( dat = dat, label_name = "spr", geom = geom, - # TODO: change this to era once stockplotr::example_data updated - era = NULL, + era = era, group = group, facet = facet, module = module, @@ -131,9 +130,7 @@ plot_spr <- function( ### Make RDA ---- if (make_rda) { - # TODO: Update caption, alt text, and quantities once plot is finalized # Obtain relevant key quantities for captions/alt text - spr.start.year <- min(prepared_data$year) spr.end.year <- max(prepared_data$year) spr.min <- min(prepared_data$estimate) |> round(digits = 3) diff --git a/R/utils.R b/R/utils.R index 551f8eaa..cc21df5e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -2,38 +2,6 @@ # General utility functions ############################################################################ -# Check end_year isn't past current year for non-projections plots -# make year character if not null -check_year <- function( - end_year = NULL, - topic = NULL, - fig_or_table = NULL -) { - if (!is.null(end_year)) { - # TODO: Update this to work for projections plots when developed - # stop if end year is past current year for non-projections plots - projections_plots <- c("proj.catch", "proj.biomass", "projection.ts", "sensitivity.runs") - - if (!is.null(topic)) { - if (topic %in% projections_plots == FALSE) { - if (!is.null(fig_or_table)) { - if (as.numeric(end_year) > format(Sys.Date(), "%Y")) { - cli::cli_abort("end_year is past the current year for a non-projections plot ({topic} {fig_or_table})", wrap = TRUE) - } - } else { - cli::cli_abort("fig_or_table is NULL") - } - } - } else { - cli::cli_abort("topic is NULL") - } - } else { - cli::cli_abort("end_year is NULL") - } -} - -#------------------------------------------------------------------------------ - # create notin operator `%notin%` <- Negate(`%in%`) diff --git a/R/utils_plot.R b/R/utils_plot.R index fab04df9..7e26d639 100644 --- a/R/utils_plot.R +++ b/R/utils_plot.R @@ -634,23 +634,6 @@ axis_breaks <- function(data_col) { scales::breaks_pretty()(x_range) } -# y_axis_breaks <- function(data){ -# # TODO: generalize this so we can input any column for the y-axis values -# y_n_breaks <- round(length(unique(data[["age"]]))) -# if (y_n_breaks > 80) { -# y_n_breaks <- round(length(unique(data[["age"]])) / 6) -# } else if (y_n_breaks > 40) { -# y_n_breaks <- round(length(unique(data[["age"]])) / 3) -# } -# y_n_breaks_minor <- as.vector(unique(data[["age"]])) -# if (length(y_n_breaks_minor) > 40) { -# y_n_breaks_minor <- NULL -# } else if (length(y_n_breaks_minor) > 20) { -# y_n_breaks_minor <- y_n_breaks_minor[c(TRUE, FALSE)] -# } -# list(y_n_breaks_minor, y_n_breaks) -# } - #------------------------------------------------------------------------------ cap_first_letter <- function(s) { @@ -719,7 +702,6 @@ filter_data <- function( selected_model <<- model_select$selected # TODO: add method to filter by units once added into the converter - # TODO: add option to scale data # Replace all spaces with underscore if not in proper format label_name <- gsub(" ", "_", tolower(label_name)) diff --git a/R/utils_rda.R b/R/utils_rda.R index cd15e956..56c36ac5 100644 --- a/R/utils_rda.R +++ b/R/utils_rda.R @@ -327,9 +327,6 @@ calc_kqs <- function(returned_kq, rm(selected_module) } -# TODO: update key quantities functions to work with a specified 'dir' instead of default 'getwd()'? -# TODO: update key quantities functions for plots that need to extract values from KQs csv to calculate their own, dependent KQs (none present in current plotting functions) - #' Substitute key quantities' values into template #' #' @param df Data frame. Dataframe created by importing "key_quantity_template.csv" or @@ -614,237 +611,6 @@ create_rda <- function( ) } -#------------------------------------------------------------------------------ -# NOTE: this code was taken from the former write_captions() function -# Leaving it here to pull from as we develop more figure and table functions -# which will have associated captions/alt text containing key quantities -# with the following object names and calculations - -# FIGURES----- - -## kobe plot- don't code quantities yet -# kobe.end.year <- -# value of B/B(MSY) at the end year -# B.BMSY.end.yr <- B/BMSY -# --B = time series of biomass, last year -# --BMSY = dplyr::filter(grepl('b_target', label) | grepl('b_msy', label) | c(grepl('fishing_mortality_msy', label) & is.na(year))) -# CHECK: if length > 1, then select b_target - -# value of F/F(MSY) at the end year -# F.FMSY.end.yr <- - -# object that should be "is" or "is not" and answers the question, -# "the stock overfishing status ... overfished" -# overfished.status.is.isnot <- - -# object that should be "is" or "is not" and answers the question, -# "the stock ... experiencing overfishing" -# overfishing.status.is.isnot <- - - -# TODO: uncomment and recode once we get clarity about how to extract this value properly -# R0 -# R0 <- dat |> -# dplyr::filter( -# # pull from BAM -# grepl('^recruitment$', label) & module_name == "parms" | -# grepl('^R0', label) | -# # pull from SS3 -# grepl('recruitment_virgin', label) -# ) |> -# dplyr::pull(estimate) |> -# unique() |> -# as.numeric() |> -# round(digits = 2) - -# Bend <- - -# TODO: uncomment and recode once we get clarity about how to extract this value properly -# Target biomass -# Btarg <- dat |> -# dplyr::filter(c(grepl('biomass', label) & grepl('target', label) & estimate >1) | label == 'biomass_msy') |> -# dplyr::pull(estimate) |> -# as.numeric() |> -# round(digits = 2) - -# Bmsy <- - -## vonB LAA (von Bertalanffy growth function + length at age)- don't code quantities yet -# vonb.age.min <- # minimum vonB age -# vonb.age.max <- # maximum vonB age - -# vonB length units (plural) -# vonb.length.units - -# vonb.length.min <- # minimum vonB length -# vonb.length.max <- # minimum vonB length - - -## length-type conversion plot- don't code quantities yet -# total length units (plural) -# total.length.units - -# total.length.min <- # minimum total length -# total.length.max <- # maximum total length -# fork length units (plural) -# fork.length.units - -# fork.length.min <- # minimum fork length -# fork.length.max <- # maximum fork length - - -## weight-length conversion plot- don't code quantities yet -# length units (plural) -# wl.length.units - -# wl.length.min <- # minimum length -# wl.length.max <- # maximum length - -# weight units (plural) -# wl.weight.units - -# wl.weight.min <- # minimum weight -# wl.weight.max <- # maximum weight - - -## maturity schedule (proportion mature)- don't code quantities yet -# length units (plural) -# prop.mat.length.units - -# prop.mat.length.min <- # minimum length -# prop.mat.length.max <- # maximum length - - -## fecundity at length- don't code quantities yet -# length units (plural) -# fecundity.length.units - -# fecundity.length.min <- # minimum length -# fecundity.length.max <- # maximum length - -# fecundity units (plural) -# fecundity.units - -# fecundity.min <- # minimum fecundity -# fecundity.max <- # maximum fecundity - -## CAL (catch at length)- don't code quantities yet -# cal.length.min <- # minimum length group -# cal.length.max <- # maximum length group -# fleet.or.survey.name <- # fleet or survey name - -## mod_fit_catch (model fit to catch ts)- don't code quantities yet -# mod.fit.catch.start.year <- # start year of model fit to catch ts plot -# mod.fit.catch.end.year <- # end year of model fit to catch ts plot - -# catch units (plural) -# mod.fit.catch.units - -# mod.fit.catch.min <- # minimum catch -# mod.fit.catch.max <- # maximum catch - - -## mod_fit_abun (model fit to abundance index plot)- don't code quantities yet -# start year of model fit to abundance index plot -# mod.fit.abun.start.year <- - -# end year of model fit to abundance index plot -# mod.fit.abun.end.year <- - -## mod_fit_discards- will be by fleet -## for ss3, obs discards not in output file -## -filter labels as discard_observed | discard_predicted | discard -## -then group_by(year, fleet, label), then summarize(estimate_y = sum(estimate)) -## ---then, get the following: -## -change alt text so that we add the line's min/max, but analyst has to describe further -# mod.fit.discards.start.year <- # start year of model fit to discards plot -# mod.fit.discards.end.year <- # end year of F -# mod.fit.discards.units <- # discards units (plural) -# mod.fit.discards.min <- # minimum discards -# mod.fit.discards.max <- # maximum discards - -## selectivity- don't code quantities yet -# selectivity.start.year <- # start year of selectivity plot -# selectivity.end.year <- # end year of selectivity plot -# selectivity.length.units <- # length units (plural) -# selectivity.length.min <- # minimum length -# selectivity.length.max <- # maximum length - -## tot_b (total biomass): same as B plot above - -# ssbtarg - -## spr (spawning potential ratio) -# minimum spr -# spr.min <- dat |> -# dplyr::filter(c(grepl("spr", label) | -# label == "spr") & -# !is.na(year) & -# !is.na(estimate)) |> -# dplyr::slice(which.min(estimate)) |> -# dplyr::select(estimate) |> -# as.numeric() |> -# round(digits = 2) -# -# # maximum spr -# spr.max <- dat |> -# dplyr::filter(c(grepl("spr", label) | -# label == "spr") & !is.na(year) & !is.na(estimate)) |> -# dplyr::slice(which.max(estimate)) |> -# dplyr::select(estimate) |> -# as.numeric() |> -# round(digits = 2) - -# TODO: uncomment and recode once we get clarity about how to extract this value properly -# spr reference point -# spr.ref.pt <- dat |> -# dplyr::filter(label == "spr_msy") |> -# dplyr::select(estimate) |> -# as.numeric()# |> -# round(digits = 2) - - -## proj_catch (projected catch) -# projected catch units (plural) -# proj.catch.units <- # probably mt, but wait until figure coded - -# start year of projected catch plot -# proj.catch.start.year <- landings.end.year + 1 - -# end year of projected catch plot -# proj.catch.end.year <- dat |> -# dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> -# dplyr::slice(which.max(year)) |> -# dplyr::select(year) |> -# as.numeric() - -# minimum projected catch -# proj.catch.min <- dat |> -# # no BAM file has catch; will be NA -# dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> -# dplyr::slice(which.min(estimate)) |> -# dplyr::select(estimate) |> -# as.numeric() - -# maximum projected catch -# proj.catch.max <- dat |> -# dplyr::filter(label == "catch" & module_name == "DERIVED_QUANTITIES") |> -# dplyr::slice(which.max(estimate)) |> -# dplyr::select(estimate) |> -# as.numeric() - -# TABLES----- - -## catch -# catch.fleet <- # fleet - -## discards -# discards.tbl.units <- # discards units - -## catchability -# catchability.fleet <- # fleet - - #------------------------------------------------------------------------------ #' Extract captions and alternative texts diff --git a/data/example_data.rda b/data/example_data.rda index 046324ed..53f8ffc5 100644 Binary files a/data/example_data.rda and b/data/example_data.rda differ diff --git a/data/ss3_simple.rda b/data/ss3_simple.rda index 91687838..3e005257 100644 Binary files a/data/ss3_simple.rda and b/data/ss3_simple.rda differ diff --git a/inst/resources/ss3_var_names.csv b/inst/resources/ss3_var_names.csv index 6580426e..f1d9df58 100644 --- a/inst/resources/ss3_var_names.csv +++ b/inst/resources/ss3_var_names.csv @@ -78,6 +78,7 @@ SPAWN_RECRUIT,bias_adjusted,, SPAWN_RECRUIT,pred_recr,predicted_recruitment, INDEX_2,dev,, SPAWN_RECRUIT,dev,recruitment_deviations, +SPAWN_RECRUIT_CURVE,dev,recruitment_deviations, SPAWN_RECRUIT,biasadjuster,, SPAWN_RECRUIT,mature_bio,mature_biomass,kg TIME_SERIES,mature_bio,mature_biomass,kg diff --git a/man/plot_spr.Rd b/man/plot_spr.Rd index fb577c25..d63d670d 100644 --- a/man/plot_spr.Rd +++ b/man/plot_spr.Rd @@ -10,7 +10,7 @@ plot_spr( group = NULL, facet = NULL, ref_line = "target", - era = "time", + era = NULL, module = NULL, model = NULL, interactive = TRUE, diff --git a/tests/testthat/test-convert_output.R b/tests/testthat/test-convert_output.R index 3761774e..7a17a196 100644 --- a/tests/testthat/test-convert_output.R +++ b/tests/testthat/test-convert_output.R @@ -20,7 +20,7 @@ test_that("convert_output works for SS3", { for (i in seq_along(models_to_test)) { # Ensure no errors occur while converting SS3 output expect_no_error(result <- convert_output( - file = r4ss::SS_output(file.path(models_to_test[i])) # file.path(models_to_test[i], "Report.sso") + files = r4ss::SS_output(file.path(models_to_test[i])) # file.path(models_to_test[i], "Report.sso") )) # Check that the result has exactly 35 columns @@ -29,7 +29,7 @@ test_that("convert_output works for SS3", { # Test saving the output in a global environment output <- convert_output( - file = r4ss::SS_output(file.path(all_models[1])) + files = r4ss::SS_output(file.path(all_models[1])) ) expect_equal(dim(output)[2], 36) @@ -38,7 +38,7 @@ test_that("convert_output works for SS3", { test_that("convert_output does not emit scalar case_when deprecation warning", { # NOTE: This does not output warnings when converted through r4ss::SS_output expect_no_warning(convert_output( - file = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.sso") + files = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.sso") )) }) @@ -47,7 +47,7 @@ test_that("convert_output saves model ss3 hake output file", { dir.create(fs::path("fixtures", "ss3_models_converted", "Hake_2018"), recursive = TRUE) convert_output( - file = r4ss::SS_output(fs::path("fixtures", "ss3_models", "models", "Hake_2018")), + files = r4ss::SS_output(fs::path("fixtures", "ss3_models", "models", "Hake_2018")), save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018", "std_output.rda") ) @@ -57,29 +57,28 @@ test_that("convert_output saves model ss3 hake output file", { }) test_that("missing arguments trigger warnings or errors", { - # TODO: Debug why this doesn't work - # expect_error( - # convert_output( - # model = "ss3", - # save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") - # ), - # "Missing `file`" - # ) - - # TODO: Debug why this doesn't work - # expect_error( - # convert_output( - # file = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.sso"), - # model = "fake_model", - # save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") - # ), - # "Missing `model`" - # ) + expect_error( + convert_output( + models = "ss3", + save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") + ), + "argument \"files\" is missing, with no default" + ) + + expect_error( + convert_output( + files = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.sso"), + models = "fake_model", + save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") + ), + "`file` not found." + ) + dir.create(fs::path("fixtures", "ss3_models_converted", "Hake_2018"), recursive = TRUE) expect_error( convert_output( - file = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.rdat"), + files = fs::path("fixtures", "ss3_models", "models", "Hake_2018", "Report.rdat"), models = "ss3", save_dir = fs::path("fixtures", "ss3_models_converted", "Hake_2018") ), @@ -126,7 +125,7 @@ test_that("URL input is compatible", { test_that("invalid URL input triggers an error", { expect_error( convert_output( - file = "https://invalid.invalid/Report.sso" + files = "https://invalid.invalid/Report.sso" ), "Invalid URL." ) @@ -166,11 +165,11 @@ test_that("convert_output works for Rceattle", { # Both documented input modes: a path, and an already-loaded fit. expect_no_error(from_path <- convert_output( - file = fit_path, + files = fit_path, models = "rceattle" )) expect_no_error(from_object <- convert_output( - file = readRDS(fit_path), + files = readRDS(fit_path), models = "rceattle" )) @@ -206,7 +205,7 @@ test_that("convert_output works for Rceattle", { test_that("convert_output places Rceattle selectivity and M at age", { fit <- readRDS(test_path("fixtures", "rceattle_goa_atf.rds")) - out <- convert_output(file = fit, models = "rceattle") + out <- convert_output(files = fit, models = "rceattle") # One row per fleet, sex, age and year of sel_at_age, and each cell lands # on its own fleet, sex, age and year. @@ -238,7 +237,7 @@ test_that("convert_output places Rceattle selectivity and M at age", { # The age dimension is the bin index, so the ages start at minage. fit[["data_list"]][["minage"]] <- 3 - shifted <- convert_output(file = fit, models = "rceattle") + shifted <- convert_output(files = fit, models = "rceattle") expect_equal( range(as.numeric(shifted[shifted[["label"]] == "natural_mortality", ][["age"]])), c(3, 23) @@ -250,7 +249,7 @@ test_that("convert_output places Rceattle selectivity and M at age", { test_that("convert_output gives Rceattle recruitment deviations a standard error", { fit <- readRDS(test_path("fixtures", "rceattle_goa_atf.rds")) - out <- convert_output(file = fit, models = "rceattle") + out <- convert_output(files = fit, models = "rceattle") # The standard error of a fixed effect is the square root of its diagonal # entry in cov.fixed. @@ -272,7 +271,7 @@ test_that("convert_output gives Rceattle recruitment deviations a standard error random_rec[["sdrep"]][["par.fixed"]] <- fit[["sdrep"]][["par.fixed"]][!is_rec_dev] random_rec[["sdrep"]][["cov.fixed"]] <- fit[["sdrep"]][["cov.fixed"]][!is_rec_dev, !is_rec_dev] - random_out <- convert_output(file = random_rec, models = "rceattle") + random_out <- convert_output(files = random_rec, models = "rceattle") random_rec_devs <- random_out[random_out[["label"]] == "recruitment_deviations", ] expect_equal(random_rec_devs[["estimate"]], rec_devs[["estimate"]]) expect_equal(random_rec_devs[["uncertainty"]], rec_dev_sd) @@ -285,7 +284,7 @@ test_that("convert_output accepts .RDS as well as .rds for Rceattle", { upper <- tempfile(fileext = ".RDS") on.exit(unlink(upper), add = TRUE) saveRDS(readRDS(test_path("fixtures", "rceattle_goa_atf.rds")), upper) - expect_no_error(convert_output(file = upper, models = "rceattle")) + expect_no_error(convert_output(files = upper, models = "rceattle")) }) test_that("convert_output says so when an Rceattle fit carries no sdrep", { @@ -297,7 +296,7 @@ test_that("convert_output says so when an Rceattle fit carries no sdrep", { # Matched on a single word, since cli wraps a message and a phrase can be # split across a line break. expect_warning( - out <- convert_output(file = no_sd, models = "rceattle"), + out <- convert_output(files = no_sd, models = "rceattle"), "sdrep" ) expect_false("biomass" %in% out[["label"]]) @@ -311,7 +310,7 @@ test_that("convert_output refuses an Rceattle fit of unknown species count", { no_nspp[["data_list"]][["nspp"]] <- NULL expect_error( - convert_output(file = no_nspp, models = "rceattle"), + convert_output(files = no_nspp, models = "rceattle"), "species count is unknown" ) }) @@ -325,7 +324,7 @@ test_that("convert_output refuses a multispecies Rceattle fit", { # Matched on a phrase short enough that cli's wrapping cannot split it. expect_error( - convert_output(file = multi, models = "rceattle"), + convert_output(files = multi, models = "rceattle"), "Rceattle model has 3 species" ) }) diff --git a/tests/testthat/test-plot_biomass.R b/tests/testthat/test-plot_biomass.R index 67506a40..a82116b2 100644 --- a/tests/testthat/test-plot_biomass.R +++ b/tests/testthat/test-plot_biomass.R @@ -35,12 +35,6 @@ test_that("plot_biomass generates plots without errors", { ) ) - # TODO: Update test - # expect plot with warning message if ref_point not indicated - # expect_message( - # plot_biomass(stockplotr::example_data) - # ) - # expect error-free plot with many arguments expect_no_error( plot_biomass( diff --git a/tests/testthat/test-plot_catch_at_age.R b/tests/testthat/test-plot_catch_at_age.R index 28c26dbd..3d4a57f2 100644 --- a/tests/testthat/test-plot_catch_at_age.R +++ b/tests/testthat/test-plot_catch_at_age.R @@ -1,4 +1,3 @@ -# TODO: Fix tests test_that("plot_catch_at_age generates plots without errors", { # expect error-free plot with minimal arguments expect_no_error( @@ -38,20 +37,6 @@ test_that("plot_catch_at_age generates plots without errors", { ) }) -# test_that("rda file made when indicated", { -# # export rda -# plot_catch_at_age( -# stockplotr::example_data, -# module = "CATCH_AT_AGE", -# interactive = FALSE, -# figures_dir = getwd(), -# make_rda = TRUE -# ) - -# # expect that both figures dir and the catch_at_age_figure.rda file exist -# check_and_remove_tmp_files("catch_at_age_figure.rda") -# }) - test_that("plot_catch_at_age generates error with scaling <1", { # expect error expect_error(