Skip to content

Commit

Permalink
Merge pull request #45 from nmfs-ost/test-convert-output
Browse files Browse the repository at this point in the history
Add SS3 integration tests for testing convert_output()
  • Loading branch information
Bai-Li-NOAA authored Sep 26, 2024
2 parents f782e2d + 0f38d38 commit 7efb129
Show file tree
Hide file tree
Showing 21 changed files with 226,978 additions and 1 deletion.
7 changes: 6 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ Imports:
Suggests:
knitr,
rmarkdown,
parallel,
snowfall,
r4ss,
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Remotes:
nmfs-ost/satf
nmfs-ost/satf,
r4ss/r4ss
Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
60 changes: 60 additions & 0 deletions tests/testthat/fixtures/prepare_ss3_output_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Download SS3 models and place them in the specified directory
ss3_models <- r4ss::download_models(
dir = test_path("fixtures", "ss3_models")
)

# Get the SS3 executable for running the models
ss3_exe <- r4ss::get_ss3_exe(
dir = test_path("fixtures", "ss3_models")
)

# Function to run a specific SS3 model
run_ss3_models <- function(model_id, model_dir, exe_dir){
# Define the working directory for the model
working_dir <- model_dir[model_id]

# Copy the SS3 executable to the model's working directory
file.copy(Sys.which(file.path(exe_dir, "ss3")), working_dir)

# Run SS3 model
r4ss::run(dir = working_dir)
}

# List all the model directories to be processed
all_models <- list.dirs(
file.path(test_path("fixtures", "ss3_models"), "models"),
full.names = TRUE,
recursive = FALSE
)

# Detect the number of available CPU cores, leaving one free
core_num <- parallel::detectCores() - 1

# Initialize snowfall for parallel processing, using the detected core count
snowfall::sfInit(parallel = TRUE, cpus = core_num)

# Run the SS3 models in parallel using the snowfall library
snowfall::sfLapply(
1:length(all_models),
run_ss3_models,
all_models,
test_path("fixtures", "ss3_models")
)

# Stop the snowfall parallel session
snowfall::sfStop()

# Remove the SS3 files except Report.sso from the working directory after the run is complete
all_files <- list.files(
test_path("fixtures", "ss3_models", "models"),
full.names = TRUE,
recursive = TRUE
)
report_files <- file.path(all_models, "Report.sso")

report_id <- match(report_files, all_files)
files_to_delete <- all_files[-report_id]
file.remove(files_to_delete)

# Clean up by removing the SS3 executable from the main directory after running all models
file.remove(Sys.which(test_path("fixtures", "ss3_models", "ss3")))
Loading

0 comments on commit 7efb129

Please sign in to comment.