Skip to content
Open
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ VignetteBuilder:
Remotes:
NOAA-FIMS/FIMS,
r4ss/r4ss
Config/roxygen2/version: 8.1.1
Config/roxygen2/version: 8.1.2
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/testthat/start-first: save_all_plots*, convert_output*
Expand Down
18 changes: 12 additions & 6 deletions R/conout_bam.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,25 @@ conout_bam <- function(
if ("age" %in% colnames(df2)) {
if (all(is.na(df2$age))) {
df2 <- df2 |>
dplyr::mutate(age = age_a,
age_n = NA)
dplyr::mutate(
age = age_a,
age_n = NA
)
}
# check if age column values are equal to age_a
if (all(df2$age == df2$age_a, na.rm = TRUE)) {
df2 <- df2 |>
dplyr::mutate(age = age_a,
age_n = NA)
dplyr::mutate(
age = age_a,
age_n = NA
)
}
} else {
df2 <- df2 |>
dplyr::mutate(age = age_a,
age_a = NA)
dplyr::mutate(
age = age_a,
age_a = NA
)
}
if (all(is.na(df2$age_a))) df2 <- dplyr::select(df2, -age_a)
}
Expand Down
4 changes: 2 additions & 2 deletions R/conout_rceattle.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ conout_rceattle <- function(
module_name = module_name,
len_bins = as.numeric(stringr::str_remove(len_bins, "length_"))
)

df_alk[setdiff(tolower(names(out_new)), tolower(names(df_alk)))] <- NA
data_list_list[[module_name]] <- df_alk

# final df for data_list module
# rbind() rather than bind_rows(): every frame has already been padded
# to the standard column set above, so rbind() succeeds and keeps the
Expand Down
7 changes: 4 additions & 3 deletions R/conout_ss3.R
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ conout_ss3 <- function(
# ID dimensions and assign
df1 <- as.data.frame.table(extract, responseName = "value", stringsAsFactors = FALSE)
colnames(df1)[1:3] <- c("len_bins", "age", "slice_label")

# Dynamically extract all "Key: Value" pairs and pivot
df2 <- df1 |>
# Match every 'Word: Number' pair (supporting decimals and negative numbers)
Expand All @@ -1021,15 +1021,16 @@ conout_ss3 <- function(
# I believe this is the only ones
rename_map <- c(
season = "seas",
subseason = "sub_seas" ,
subseason = "sub_seas",
estimate = "value"
# gp = "growth_pattern"
)
df3 <- df2 |>
dplyr::rename(dplyr::any_of(rename_map)) |>
dplyr::mutate(
module_name = parm_sel,
label = "age_to_length_conversion")
label = "age_to_length_conversion"
)
df3[setdiff(tolower(names(out_new)), tolower(names(df3)))] <- NA
# Add to out list
out_list[[parm_sel]] <- df3
Expand Down
32 changes: 15 additions & 17 deletions R/convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' appended together.
#'
#' `names(files)` will be used as the name of the model in the output.
#'
#' @param file `r lifecycle::badge('deprecated')`
#'
#' @param file `r lifecycle::badge('deprecated')`
#' This argument was deprecated in favor of the `files` argument in order
#' to clarify that multiple files can be processed simultaneously.
#'
Expand All @@ -21,8 +21,8 @@
#' Options: "ss3", "bam", "fims", "rceattle"
#'
#' Should match length of `files` if >1 model.
#'
#' @param model `r lifecycle::badge('deprecated')`
#'
#' @param model `r lifecycle::badge('deprecated')`
#' This argument was deprecated in favor of the `models` argument in order
#' to clarify that multiple models can be processed simultaneously.
#'
Expand Down Expand Up @@ -99,26 +99,26 @@ convert_output <- function(
# check for used deprecated arguments
if (lifecycle::is_present(file)) {
lifecycle::deprecate_warn(
when = "0.16.0",
what = "convert_output(file)",
when = "0.16.0",
what = "convert_output(file)",
details = "Please use `files` instead of `file`"
)

files <- file
}
if (lifecycle::is_present(model)) {
lifecycle::deprecate_warn(
when = "0.16.0",
what = "convert_output(model)",
when = "0.16.0",
what = "convert_output(model)",
details = "Please use `models` instead of `model`"
)

models <- model
}
# Is user inputs a single model file and doesn't wrap it as a list object
# First check handles WHAM and Rceattle models, second check handles single model input,
# final check handles FIMS models
if(any(grepl("Data_File|data_list|info|TMBfilename", names(files))) || is.character(files) || is.data.frame(files)){
if (any(grepl("Data_File|data_list|info|TMBfilename", names(files))) || is.character(files) || is.data.frame(files)) {
cli::cli_alert("`files` is a single model. Converting to list object.")
files <- list(files)
names(files) <- "model_1"
Expand Down Expand Up @@ -234,8 +234,7 @@ convert_output <- function(
}
if (is.character(file)) {
if (is.null(models) || is.na(models[i])) {
model <- switch(
stringr::str_extract(file, "\\.([^.]+)$"),
model <- switch(stringr::str_extract(file, "\\.([^.]+)$"),
".sso" = {
cli::cli_alert_info("Processing Stock Synthesis output file...")
"ss3"
Expand All @@ -257,9 +256,8 @@ convert_output <- function(
} else {
model <- models[i]
}
} else if (is.null(models) || is.na(models[i])){
model <- switch(
class(file)[1],
} else if (is.null(models) || is.na(models[i])) {
model <- switch(class(file)[1],
"FIMSFit" = {
cli::cli_alert_info("Processing FIMS output file...")
"fims"
Expand All @@ -286,7 +284,7 @@ convert_output <- function(
},
cli::cli_abort("Unknown file type. Please indicate model.")
)
} else{
} else {
model <- models[i]
}

Expand Down
6 changes: 3 additions & 3 deletions R/extract_sis_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#'
#' @details See the "Automating data extraction for SIS" vignette for a
#' detailed overview of the workflow and requirements for this function.
#'
#'
#' Note that if you have created figures and tables using a subset of data, and your rda and
#' key_quantities.csv files represent that subsetted data, the data extracted with this
#' function will also be subsetted. To extract data from the full dataset,
#' key_quantities.csv files represent that subsetted data, the data extracted with this
#' function will also be subsetted. To extract data from the full dataset,
#' create new figures and tables and rerun this function.
#'
#' @export
Expand Down
25 changes: 12 additions & 13 deletions R/plot_age_to_length_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Creates a density plot of age-to-length conversion, either fixed input or
#' realized growth-derived output.
#'
#'
#' @inheritParams plot_spawning_biomass
#' @param ... Arguments called from \link[ggridges]{geom_density_ridges}
#' @examples
Expand Down Expand Up @@ -45,17 +45,17 @@ plot_age_to_length_conversion <- function(
dat = prepared_data,
facet = facet
)

plot_data <- processing[[1]]
group <- processing[[2]]
if (!is.null(processing[[3]])) facet <- processing[[3]]
if(group %in% c("age", "length_bins")){
if (group %in% c("age", "length_bins")) {
group <- NULL
} else {
facet <- c(facet, group)
group <- NULL
}
if(any(facet %in% c("age", "length_bins"))) {
if (any(facet %in% c("age", "length_bins"))) {
facet <- facet[-grep("age|length_bins", facet)]
}
# Override grouping variable when there is only NA's
Expand All @@ -73,20 +73,19 @@ plot_age_to_length_conversion <- function(
# add check in case facet is returned as character(0)
facet = if (length(facet) > 0) facet else NULL,
...
) +
theme_noaa()

) +
theme_noaa()

### Make RDA ----
if (make_rda) {

### Make RDA ----
if (make_rda) {
plot_data <- plot_data |>
dplyr::mutate(
age = as.numeric(age),
length_bins = as.numeric(length_bins)
)

#add any additional key quantities or processing needed before creating the RDA
# add any additional key quantities or processing needed before creating the RDA
age.length.group <- glue::glue_collapse(facet, sep = ", ", last = " and ")
length.units <- unit_label
age.length.age.min <- min(plot_data[["age"]], na.rm = TRUE)
Expand Down Expand Up @@ -219,12 +218,12 @@ plot_age_to_length_conversion <- function(
dat = plot_data,
dir = figures_dir
)
} #end make_rda
} # end make_rda

if (exists("selected_module")) {
rm(selected_module, envir = .GlobalEnv)
}

# Output final plot
plt
}
24 changes: 12 additions & 12 deletions R/utils_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -1379,13 +1379,13 @@ plot_density <- function(
plot <- ggplot2::ggplot()

# Add geom
plot <- plot +
plot <- plot +
ggridges::geom_density_ridges(
data = dat,
data = dat,
ggplot2::aes(
x = as.integer(.data[[x]]),
y = as.integer(.data[[y]]),
weight = .data[["estimate"]],
weight = .data[["estimate"]],
group = .data[[y]],
color = .data[["model"]],
fill = .data[["model"]]
Expand All @@ -1396,16 +1396,16 @@ plot_density <- function(
alpha = 0.8,
...
) +
ggplot2::coord_flip()
ggplot2::coord_flip()

# Add labels to axis and legend
final <- plot + ggplot2::labs(
x = xlab,
y = ylab,
color = "Model",
fill = "Model"
)
final <- plot + ggplot2::labs(
x = xlab,
y = ylab,
color = "Model",
fill = "Model"
)

# Remove legend if no group is selected
if (length(unique(dat$model)) == 1) {
final <- final + ggplot2::theme(legend.position = "none")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/fixtures/prepare_rceattle_output_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ stopifnot(
# "index_hat", "catch_hat", "age_hat", "log_index_hat", "sel_at_age",
# "M_at_age"
# )
#
#
# fixture <- structure(
# list(
# data_list = full$data_list[intersect(data_list_keep, names(full$data_list))],
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test_that("missing arguments trigger warnings or errors", {
),
"Output file not compatible."
)

dir.create(fs::path("fixtures", "ss3_models_converted", "Hake_2018"), recursive = TRUE)

expect_error(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-plot-age-to-length.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ test_that("plot_age_to_length_conversion generates plots without errors", {
)
)

expect_no_error(
expect_no_error(
plot_age_to_length_conversion(
stockplotr::ss3_simple |>
dplyr::filter(age < 10),
dplyr::filter(age < 10),
group = NULL,
facet = NULL,
interactive = FALSE,
Expand Down
11 changes: 7 additions & 4 deletions tests/testthat/test-plot_spawning_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ test_that("plot_spawning_biomass plots all models by default", {
)
dat <- dplyr::bind_rows(
dplyr::mutate(stockplotr::example_data, model = "model1"),
dplyr::mutate(stockplotr::example_data, model = "model2", estimate=estimate*1.5)
dplyr::mutate(stockplotr::example_data, model = "model2", estimate = estimate * 1.5)
)
plot <- plot_spawning_biomass(
dat, module = "TIME_SERIES", unit_label = "mt",
dat,
module = "TIME_SERIES", unit_label = "mt",
ref_line = c("msy" = 2000000), interactive = TRUE
)
expect_setequal(unique(plot$layers[[2]]$data$model), c("model1", "model2"))
expect_length(unique(ggplot2::ggplot_build(plot)$data[[2]]$group), 2)

plot <- plot_spawning_biomass(
dat, module = "TIME_SERIES", model = "model2", unit_label = "mt",
dat,
module = "TIME_SERIES", model = "model2", unit_label = "mt",
ref_line = c("msy" = 2000000), interactive = FALSE
)
expect_equal(unique(plot$layers[[2]]$data$model), "model2")
Expand All @@ -75,7 +77,8 @@ test_that("interactive model selection limits the plot", {
dplyr::mutate(stockplotr::example_data, model = "model2")
)
plot <- plot_spawning_biomass(
dat, module = "TIME_SERIES", model = NULL, interactive = TRUE,
dat,
module = "TIME_SERIES", model = NULL, interactive = TRUE,
unit_label = "mt", ref_line = c("msy" = 2000000)
)
expect_equal(unique(plot$layers[[2]]$data$model), "model2")
Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-plot_stock_recruitment.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ test_that("stock recruitment uses the selected models for both series", {
dplyr::mutate(stockplotr::example_data, model = "model2")
)
plot <- plot_stock_recruitment(
dat, model = "model2", interactive = FALSE,
dat,
model = "model2", interactive = FALSE,
spawning_biomass_label = "mt", recruitment_label = "mt"
)
expect_equal(unique(plot$layers[[1]]$data$model), "model2")
Expand Down
Loading