From 140c9220343c841f54cf9c2c487c4cd6202dc0be Mon Sep 17 00:00:00 2001 From: Bai Li - NOAA <59936250+Bai-Li-NOAA@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:37:10 +0000 Subject: [PATCH] return a list of modules when calling intialize_fims() --- R/initialize_modules.R | 20 ++++++++++++++++++- .../testthat/helper-integration-tests-setup.R | 4 ++-- tests/testthat/test-initialize_modules.R | 2 +- ...ntegration-fims-estimation-with-wrappers.R | 4 ++-- vignettes/fims-demo.Rmd | 8 ++++---- vignettes/logging_system.Rmd | 2 +- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/R/initialize_modules.R b/R/initialize_modules.R index fc817c02..3ad5e3f2 100644 --- a/R/initialize_modules.R +++ b/R/initialize_modules.R @@ -762,8 +762,26 @@ initialize_fims <- function(parameters, data) { CreateTMBModel() # Create parameter list from Rcpp modules parameter_list <- list(p = get_fixed()) + module_list <- list( + fleet = fleet, + fleet_selectivity = fleet_selectivity, + fleet_index = fleet_index, + fleet_index_distribution = fleet_index_distribution, + fleet_age_comp = fleet_age_comp, + fleet_agecomp_distribution = fleet_agecomp_distribution, + fleet_length_comp = fleet_length_comp, + fleet_lengthcomp_distribution = fleet_lengthcomp_distribution, + recruitment = recruitment, + recruitment_distribution = recruitment_distribution, + growth = growth, + maturity = maturity, + population = population + ) - return(parameter_list) + output <- list( + parameter_list = parameter_list, + module_list = module_list + ) } #' Set parameter vector values based on module input diff --git a/tests/testthat/helper-integration-tests-setup.R b/tests/testthat/helper-integration-tests-setup.R index f1133486..9be04078 100644 --- a/tests/testthat/helper-integration-tests-setup.R +++ b/tests/testthat/helper-integration-tests-setup.R @@ -558,13 +558,13 @@ setup_and_run_FIMS_with_wrappers <- function(iter_id, modified_parameters = modified_parameters ) - parameter_list <- initialize_fims( + output <- initialize_fims( parameters = parameters, data = data ) input <- list() - input$parameters <- parameter_list + input$parameters <- output$parameter_list input$version <- "Model Comparison Project example" fit <- fit_fims(input, optimize = estimation_mode) diff --git a/tests/testthat/test-initialize_modules.R b/tests/testthat/test-initialize_modules.R index 8aaf5dbe..6fe19fa5 100644 --- a/tests/testthat/test-initialize_modules.R +++ b/tests/testthat/test-initialize_modules.R @@ -44,7 +44,7 @@ test_that("initialize_fims works", { result <- initialize_fims(parameters = default_parameters, data = data) expect_type(result, "list") - expect_named(result, "p") + expect_named(result, c("parameter_list", "module_list")) clear() }) diff --git a/tests/testthat/test-integration-fims-estimation-with-wrappers.R b/tests/testthat/test-integration-fims-estimation-with-wrappers.R index 4eebf043..58e00337 100644 --- a/tests/testthat/test-integration-fims-estimation-with-wrappers.R +++ b/tests/testthat/test-integration-fims-estimation-with-wrappers.R @@ -302,12 +302,12 @@ test_that("estimation test of fims using high-level wrappers", { # Clear any previous FIMS settings clear() - parameter_list <- initialize_fims( + output <- initialize_fims( parameters = parameters, data = data ) input <- list() - input$parameters <- parameter_list + input$parameters <- output$parameter_list input$version <- "Model Comparison Project example" fit <- fit_fims(input, optimize = TRUE) diff --git a/vignettes/fims-demo.Rmd b/vignettes/fims-demo.Rmd index 30355958..c5a736bd 100644 --- a/vignettes/fims-demo.Rmd +++ b/vignettes/fims-demo.Rmd @@ -161,13 +161,13 @@ With data and parameters in place, we can now initialize modules using `initiali # Run the model without optimization to help ensure a viable model test_fit <- parameters |> initialize_fims(data = fims_frame) |> - (\(parameter_list) list(parameters = parameter_list, version = "FIMS run without optimization"))() |> + (\(output) list(parameters = output$parameter_list, version = "FIMS run without optimization"))() |> fit_fims(optimize = FALSE) # Run the model with optimization fit <- parameters |> initialize_fims(data = fims_frame) |> - (\(parameter_list) list(parameters = parameter_list, version = "FIMS run with optimization"))() |> + (\(output) list(parameters = output$parameter_list, version = "FIMS run with optimization"))() |> fit_fims(optimize = TRUE) # Clear memory post-run @@ -194,14 +194,14 @@ parameters_low_slope <- parameters |> high_slope_fit <- parameters_high_slope |> initialize_fims(data = fims_frame) |> - (\(parameter_list) list(parameters = parameter_list, version = "High slope run"))() |> + (\(output) list(parameters = output$parameter_list, version = "High slope run"))() |> fit_fims(optimize = TRUE) clear() low_slope_fit <- parameters_low_slope |> initialize_fims(data = fims_frame) |> - (\(parameter_list) list(parameters = parameter_list, version = "Low slope run"))() |> + (\(output) list(parameters = output$parameter_list, version = "low slope run"))() |> fit_fims(optimize = TRUE) clear() diff --git a/vignettes/logging_system.Rmd b/vignettes/logging_system.Rmd index 7e79c805..678109bd 100644 --- a/vignettes/logging_system.Rmd +++ b/vignettes/logging_system.Rmd @@ -123,7 +123,7 @@ Assuming a model has already been defined, below is an example of using the logg ```{r example_code, eval = TRUE} fit <- default_parameters |> initialize_fims(data = fims_frame) |> - (\(parameter_list) list(parameters = parameter_list, version = "FIMS run with optimization"))() |> + (\(output) list(parameters = output$parameter_list, version = "FIMS run with optimization"))() |> fit_fims(optimize = TRUE) # get the log as a string in JSON format and parse into a list