Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a function download_models to download ss3 test models #721

Merged
merged 5 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export(SSunavailableSpawningOutput)
export(TSCplot)
export(add_legend)
export(copy_SS_inputs)
export(download_models)
export(getADMBHessian)
export(get_SIS_info)
export(is.wholenumber)
Expand Down
50 changes: 50 additions & 0 deletions R/download_models.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#' Download SS3 test models
#'
#' Function to download the models folder within the
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' nmfs-stock-synthesis/test-models repsitory
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' @param dir The folder where the models subfolder should be written to.
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' @param branch The name of the github branch to download.
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' @template overwrite
#' @return Invisibly return a value revealing whether the files were copied
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' (TRUE) or not (FALSE). This function is used for its side effects of
#' downling SS3 test models.
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
#' @examples
#' download_models(dir = getwd())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why the examples are indented. Also, I am guessing we should wrap this in \dontrun{} because all examples are checked during a build and I think this one will take too long.

#' model_name <- list.files("models") # see the model names
#' # remove files
#' unlink(file.path("models"), recursive = TRUE)
#' @author Kathryn Doering
#' @references [nmfs-stock-synthesis/test-models repository](https://github.com/nmfs-stock-synthesis/test-models#test-models)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure that links can be split between lines and they still work. See r-lib/roxygen2#628

#' @export
download_models <- function(dir = file.path("inst", "extdata"),
branch = "main", overwrite = FALSE) {
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
# checks
if (!dir.exists(dir)) {
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
stop("dir ", dir, " does not exist")
}
zip_file_path <- file.path(dir, "test-models.zip")
result <- tryCatch(utils::download.file(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to change 26-33 to

  result <- tryCatch(
    utils::download.file(
      url = paste0(
        "https://github.com/nmfs-stock-synthesis/test-models/archive/",
        branch,
        ".zip"
        ),
      destfile = zip_file_path
    ),
    warning = function(e) {
      stop(
        "The test-models zip file could not be downloaded.",
        " Does the branch (", branch, ") exist?",
        call. = FALSE
      )
    }
  )

where the indents make it more clear what tryCatch is doing and the behavior of the warning is linked to the stop().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I tried this out, and when using warning= instead of error= it creates a situation where the zip file connection is left open and so isn't properly deleted (because it is being accessed by a different program). This then means the tests can't be fully cleaned up. I found changing to error= prevents this unintended consequence from happening; Do you think that is an acceptable compromise?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will push my changes shortly so you can see them!

url = paste0("https://github.com/nmfs-stock-synthesis/test-models/archive/",
branch, ".zip"),
destfile = zip_file_path),
error = function(e) e)
if("simpleError" %in% class(result)) {
stop("The test models could not be downloaded. Does the branch exist?")
}
list_files <- utils::unzip(list = TRUE, zipfile = zip_file_path)
save_files <- list_files[grep("/models/", list_files$Name, fixed = TRUE), ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the forward slashes around models always be forward regardless of the operating system?

utils::unzip(zipfile = zip_file_path, files = save_files[["Name"]],
exdir = dir)
if (dir.exists(file.path(dir, "models")) & overwrite == FALSE) {
warning("The model directory ", file.path(dir, "models"), " already exists ",
"\nand overwrite is FALSE, so no new files will be written." )
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
}
dir.create(file.path(dir, "models"), showWarnings = FALSE)
copy_status <- file.copy(from = file.path(dir, paste0("test-models-", branch), "models"),
to = file.path(dir), recursive = TRUE, overwrite = overwrite)
# clean up
unlink(zip_file_path)
unlink(file.path(dir, paste0("test-models-", branch)),
recursive = TRUE)
invisible(copy_status)
}
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
46 changes: 46 additions & 0 deletions man/download_models.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions tests/testthat/test-download_models.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

# setup
temp_dir <- tempdir()
save_models_dir <- file.path(temp_dir, "save_mods")
# create directories and use on exit statements to ensure cleanup at the end.
dir.create(save_models_dir)
on.exit(unlink(save_models_dir, recursive = TRUE), add = TRUE)
if (!dir.exists("inst")) {
dir.create("inst")
dir.create("inst/extdata")
on.exit(unlink("inst", recursive = TRUE), add = TRUE)
}
if (!dir.exists("inst/extdata")) {
dir.create("inst/extdata")
on.exit(unlink("inst/extdata", recursive = TRUE), add = TRUE)
}

test_that("download_models with defaults work", {
download_models()
expect_true(file.exists("inst/extdata/models"))
expect_true(file.exists("inst/extdata/models/Simple"))
expect_true(file.exists("inst/extdata/models/Simple/ss.par"))
})

test_that("download_models with different dir works", {
download_models(dir = save_models_dir)
expect_true(file.exists(file.path(save_models_dir, "models")))
expect_true(file.exists(file.path(save_models_dir, "models", "Simple")))
expect_true(file.exists(file.path(save_models_dir, "models", "Simple", "ss.par")))
})

# test_that("download models works with different branch", {
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
# temp_dir_branch <- file.path(save_models_dir, "diff_branch")
# dir.create(temp_dir_branch)
# download_models(dir = temp_dir_branch, branch = "super_test")
# expect_true(file.exists(file.path(temp_dir_branch, "models")))
# expect_true(file.exists(file.path(temp_dir_branch, "models", "Simple")))
# expect_true(file.exists(file.path(temp_dir_branch, "models", "Simple", "ss.par")))
# })

test_that("download_models() fails when the branch doesn't exist", {
temp_dir_branch_dne <- file.path(save_models_dir, "diff_branch_dne")
dir.create(temp_dir_branch_dne)
expect_error(
download_models(dir = temp_dir_branch_dne,
branch = "not_existing_branch"),
"The test models could not be downloaded")
k-doering-NOAA marked this conversation as resolved.
Show resolved Hide resolved
})

test_that("download_models() fails when the dir doesn't exist", {
temp_dir_missing_dir <- file.path(save_models_dir, "missing")
# don't create it
expect_error(download_models(dir = temp_dir_missing_dir), "does not exist")

})