Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions R/conout_fims.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
#' object, or the path to an .rds file containing either.
#' @param out_new The empty standard output data frame from
#' [convert_output()].
#' @param start_year Number. First year of the model. FIMS reports years as
#' indices (`year_i`, 1 = first model year), which are converted to years as
#' `year_i + start_year - 1`. NULL keeps the indices.
#'
#' @returns A data frame with the columns of `out_new`.
#'
Expand All @@ -34,7 +31,7 @@
#' ends) have `era` "fore"; other rows with a year have `era` "time".
#'
#' @noRd
conout_fims <- function(file, out_new, start_year = NULL) {
conout_fims <- function(file, out_new) {
dat <- read_fims_estimates(file)
names(dat) <- tolower(names(dat))
if (!all(c("label", "estimated") %in% names(dat))) {
Expand All @@ -46,23 +43,15 @@ conout_fims <- function(file, out_new, start_year = NULL) {
col <- function(name) {
if (name %in% names(dat)) dat[[name]] else rep(NA, nrow(dat))
}
year_i <- suppressWarnings(as.numeric(col("year_i")))
if (is.null(start_year)) {
cli::cli_alert_info(
"FIMS reports years as indices (1 = first model year); use {.arg start_year} to convert them to years."
)
year <- year_i
} else {
year <- year_i + start_year - 1
}
year <- suppressWarnings(as.numeric(col("year")))
observed <- suppressWarnings(as.numeric(col("observed")))
observed[observed %in% -999] <- NA
# The last year of data; later years are after the model ends
data_years <- year_i[!is.na(observed)]
last_year <- if (length(data_years) > 0) max(data_years) else max(year_i, na.rm = TRUE)
data_years <- year[!is.na(observed)]
last_year <- if (length(data_years) > 0) max(data_years) else max(year, na.rm = TRUE)
era <- dplyr::case_when(
is.na(year_i) ~ NA_character_,
year_i > last_year ~ "fore",
is.na(year) ~ NA_character_,
year > last_year ~ "fore",
TRUE ~ "time"
)
uncertainty <- dplyr::na_if(suppressWarnings(as.numeric(col("uncertainty"))), -999)
Expand Down
10 changes: 2 additions & 8 deletions R/convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#' in the file path provided.
#'
#' Default: NULL
#' @param start_year Number. First year of the model, for FIMS output only.
#' FIMS reports years as indices (1 = first model year), which are converted
#' to years when `start_year` is given.
#'
#' Default: NULL (years are left as indices)
#'
#' @author Samantha Bredeck
#'
Expand Down Expand Up @@ -77,8 +72,7 @@ convert_output <- function(
files,
models = NULL,
fleet_names = NULL,
save_dir = NULL,
start_year = NULL
save_dir = NULL
) {

# Is user inputs a single model file and doesn't wrap it as a list object
Expand Down Expand Up @@ -274,7 +268,7 @@ convert_output <- function(
cli::cli_abort("JABBA output not currently compatible.")
#### FIMS ####
} else if (model == "fims") {
out_new <- conout_fims(file = file, out_new = out_new, start_year = start_year)
out_new <- conout_fims(file = file, out_new = out_new)
#### Rceattle ####
} else if (model == "rceattle") {
out_new <- conout_rceattle(file = file, out_new = out_new, fleet_names = fleet_names)
Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/test-convert_output_fims.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ convert_fims <- function(dat = fims_estimates(), ...) {

test_that("FIMS data points are split into observed and predicted rows", {
est <- fims_estimates()
out <- convert_fims(est, start_year = 2001)
out <- convert_fims(est)
counts <- table(out$label[grepl("_(observed|predicted)$", out$label)])
expect_equal(
as.vector(counts[c("index_observed", "index_predicted", "landings_observed", "landings_predicted")]),
Expand All @@ -31,7 +31,7 @@ test_that("FIMS data points are split into observed and predicted rows", {
expect_equal(unique(obs$uncertainty_label), "cv")
expect_equal(obs$likelihood, -rows$likelihood)
expect_equal(unique(obs$fleet), "survey1")
expect_equal(obs$year, rows$year_i + 2000)
expect_equal(obs$year, 1:30)

# Age compositions keep their bins and counts
comp <- out[out$label == "age_composition_observed", ]
Expand Down Expand Up @@ -86,22 +86,18 @@ test_that("FIMS estimates that could not be calculated are NA", {

test_that("FIMS estimates are relabeled and get years and eras", {
est <- fims_estimates()
out <- convert_fims(est, start_year = 2001)
out <- convert_fims(est)
ssb <- out[out$label == "spawning_biomass", ]
expect_equal(ssb$estimate, est$estimated[est$label == "spawning_biomass"])
expect_equal(ssb$uncertainty, est$uncertainty[est$label == "spawning_biomass"])
expect_equal(range(ssb$year), c(2001, 2031))
expect_equal(range(ssb$year), c(1, 31))
# Spawning biomass at the start of the year after the model ends
expect_equal(ssb$era[ssb$year == 2031], "fore")
expect_true(all(ssb$era[ssb$year <= 2030] == "time"))
expect_equal(ssb$era[ssb$year == 31], "fore")
expect_true(all(ssb$era[ssb$year <= 30] == "time"))

expect_equal(sum(out$label == "recruitment"), sum(est$label == "expected_recruitment"))
expect_equal(sum(out$label == "log_recruitment_deviations"), sum(est$label == "log_devs"))
expect_false(any(c("expected_recruitment", "log_devs") %in% out$label))

# Without start_year, years are the FIMS year indices
expect_message(convert_output(est, model = "fims"), "start_year")
expect_equal(range(convert_fims(est)$year[convert_fims(est)$label == "spawning_biomass"]), c(1, 31))
})

test_that("FIMS output is read from an rds file", {
Expand All @@ -121,11 +117,11 @@ test_that("a FIMSFit object needs FIMS", {
})

test_that("plots work with converted FIMS output", {
out <- convert_fims(start_year = 2001)
out <- convert_fims()
p <- suppressMessages(plot_index(out, interactive = FALSE))
b <- ggplot2::ggplot_build(p)
expect_equal(nrow(b$data[[1]]), 30)
expect_equal(range(b$data[[1]]$x), c(2001, 2030))
expect_equal(range(b$data[[1]]$x), c(1, 30))
for (f in list(plot_landings, plot_spawning_biomass, plot_biomass, plot_recruitment, plot_recruitment_deviations)) {
p <- suppressMessages(suppressWarnings(f(out, interactive = FALSE)))
expect_s3_class(p, "ggplot")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-plot_data_availability.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("plot_data_availability works with each example model output", {
pollock = stockplotr::pollock,
ss3_simple = stockplotr::ss3_simple,
vermilion_snapper = stockplotr::vermilion_snapper,
fims = suppressMessages(convert_output(est, model = "fims", start_year = 2001))
fims = suppressMessages(convert_output(est, model = "fims"))
)
for (name in names(datasets)) {
p <- plot_data_availability(datasets[[name]])
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-plot_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test_that("plot_index can use a log scale", {

test_that("plot_index intervals work for FIMS output", {
est <- readRDS(test_path("fixtures", "fims_output", "fims_estimates.rds"))
fims <- suppressMessages(convert_output(est, model = "fims", start_year = 2001))
fims <- suppressMessages(convert_output(est, model = "fims"))
p <- suppressMessages(plot_index(fims, interval = 0.95, interactive = FALSE))
ints <- p$layers[[1]]$data
rows <- est[est$label == "index_expected" & !is.na(est$observed), ]
Expand Down Expand Up @@ -131,7 +131,7 @@ test_that("plot_index can plot all indices relative to their means", {
expect_no_error(ggplot2::ggplot_build(p))
expect_no_error(ggplot2::ggplot_build(suppressMessages(plot_index(stockplotr::ss3_simple, relative = TRUE, interactive = FALSE))))
est <- readRDS(test_path("fixtures", "fims_output", "fims_estimates.rds"))
fims <- suppressMessages(convert_output(est, model = "fims", start_year = 2001))
fims <- suppressMessages(convert_output(est, model = "fims"))
p <- suppressMessages(plot_index(fims, relative = TRUE, interactive = FALSE))
expect_equal(mean(p$layers[[2]]$data$estimate[p$layers[[2]]$data$label == "index_observed"]), 1)
})
2 changes: 1 addition & 1 deletion tests/testthat/test-plot_joint_residuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("plot_joint_residuals works with each example model output and option"
pollock = stockplotr::pollock,
ss3_simple = stockplotr::ss3_simple,
vermilion_snapper = stockplotr::vermilion_snapper,
fims = suppressMessages(convert_output(est, model = "fims", start_year = 2001))
fims = suppressMessages(convert_output(est, model = "fims"))
)
for (name in names(datasets)) {
# ss3_simple also has catch labeled as an index in the "catch" module
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-plot_runs_test.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fims_converted <- function() {
est <- readRDS(test_path("fixtures", "fims_output", "fims_estimates.rds"))
suppressMessages(convert_output(est, model = "fims", start_year = 2001))
suppressMessages(convert_output(est, model = "fims"))
}

test_that("residual_control estimates limits and the runs test", {
Expand Down
Loading