From 817cfe000fbe3eaa2f10468b9472277fe4ddac4b Mon Sep 17 00:00:00 2001 From: Ethan Plunkett Date: Fri, 27 Sep 2024 11:28:54 -0400 Subject: [PATCH] Moved build_collection_index() to BirdFlowPipeline --- DESCRIPTION | 2 +- NAMESPACE | 1 - NEWS.md | 7 + R/build_collection_index.R | 151 ------------------- man/build_collection_index.Rd | 29 ---- tests/testthat/test-build_collection_index.R | 29 ---- 6 files changed, 8 insertions(+), 211 deletions(-) delete mode 100644 R/build_collection_index.R delete mode 100644 man/build_collection_index.Rd delete mode 100644 tests/testthat/test-build_collection_index.R diff --git a/DESCRIPTION b/DESCRIPTION index 535e8903..e79a98d3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: BirdFlowR Title: Predict and Visualize Bird Movement -Version: 0.1.0.9066 +Version: 0.1.0.9068 Authors@R: c(person("Ethan", "Plunkett", email = "plunkett@umass.edu", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4405-2251")), diff --git a/NAMESPACE b/NAMESPACE index 42cf2da3..8a8e8b9a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,7 +22,6 @@ export(as_distr) export(as_transitions) export(birdflow_crs) export(birdflow_options) -export(build_collection_index) export(build_transitions) export(calc_flux) export(calc_movement_vectors) diff --git a/NEWS.md b/NEWS.md index 61ac9771..ce415c40 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# BirdFlowR 0.1.0.9068 +2024-09-24 + +Move `build_collection_index()` to **BirdFlowPipeline** + + + # BirdFlowR 0.1.0.9067 2024-07-24 diff --git a/R/build_collection_index.R b/R/build_collection_index.R deleted file mode 100644 index 1e6c2cd6..00000000 --- a/R/build_collection_index.R +++ /dev/null @@ -1,151 +0,0 @@ -#' Convert a directory of BirdFlow models into a collection -#' -#' A BirdFlow model collection consists of 1 or more BirdFlow objects each -#' saved to a single `.Rds` file, an `index.Rds` file, an `index.html` file and -#' a `index_md5.txt` file. This function converts a directory with model `.Rds` -#' files into a collection. It needs to be run locally to an R session so likely -#' the resulting directory will be synchronized with a server after it is -#' created. -#' -#' It is primarily intended for use by the BirdFlow team but is exported from -#' the package so anyone can use it. -#' -#' @param dir A local directory for containing BirdFlow models saved as `.Rds` -#' files. The base file name will be used as the model name and other metadata -#' will be extracted from the BirdFlow object. -#' -#' @param collection_url The final URL for the BirdFlow collection (where it -#' will be served). -#' -#' @export -#' @keywords internal -#' -build_collection_index <- function(dir, collection_url) { - - - index_path <- file.path(dir, "index.Rds") - index_md5_path <- file.path(dir, "index_md5.txt") - - model_extension <- "Rds" - cols <- c("model", "species_code", "scientific_name", "common_name", - "file", "release_date", "md5", "version", "size") - species_cols <- c("species_code", "scientific_name", "common_name") - - # List model files in directory - files <- list.files(dir, pattern = paste0("^.*\\.", model_extension, "$"), - ignore.case = TRUE) - files <- files[!tolower(files) %in% "index.rds"] - - if (length(files) == 0) - stop("No model files detected.") - - # Create an empty index table - index <- matrix(nrow = length(files), ncol = length(cols), - dimnames = list(NULL, cols)) |> - as.data.frame() - index$file <- files - index$model <- gsub("\\.Rds", "", files, ignore.case = TRUE) - - # Merge in data (if any) from old index - if (file.exists(index_path)) { - old_index <- readRDS(index_path) - old_index <- old_index[tolower(old_index$file) %in% tolower(files), , - drop = FALSE] - old_cols <- colnames(old_index) - if (setequal(old_cols, cols) && all(old_cols == cols) && - any(old_index$file %in% files)) { - if (all(files %in% old_index$file)) { - new_index <- old_index - } else { - new_index <- - rbind(old_index, - index[!index$files %in% old_index$files, , drop = FALSE]) - - } - stopifnot(setequal(tolower(new_index$file), tolower(files))) - index <- new_index - } - } - - index <- index[order(tolower(index$file)), , drop = FALSE] - - index$error <- FALSE # temporary col to track errors - - for (i in seq_len(nrow(index))) { - f <- file.path(dir, index$file[i]) - md5 <- as.character(tools::md5sum(f)) - - if (is.na(index$md5[i]) || index$md5[i] != md5) { # if new or changed model - bf_msg("Reading metadata for ", index$model[i], "\n") - index$md5[i] <- md5 - bf <- readRDS(f) - if (!inherits(bf, "BirdFlow")) { - index$error[i] <- TRUE - next - } - for (col in species_cols) { - index[[col]][i] <- bf$species[[col]] - } - index$release_date[i] <- as.character(lubridate::today()) - index$size[i] <- file.size(f) / (1000^2) - } # end new or changed - } # end loop through models - if (any(index$error)) { - warning("Some .", model_extension, - "files do not appear to be valid BirdFlow models", - 'and were skipped: "', - paste(index$files[index$error], collapse = '", "'), '"', sep = "") - index <- index[!index$error, , drop = FALSE] - } - index$error <- NULL # drop temporary column - - index$version <- "beta" - - - # Save index .Rds - saveRDS(index, index_path) - - # Download logo - logo_file <- file.path(dir, "logo.png") - if (!file.exists(logo_file)) { - - utils::download.file( - "https://birdflow-science.github.io/BirdFlowR/logo.png", - destfile = logo_file, method = "libcurl", mode = "wb", - quiet = !birdflow_options("verbose")) - } - # Save index.htm - model <- index$model[1] - - rmd_file <- file.path(dir, "index_template.Rmd") - - # Edit text in rmd to reference this specific collection url and the first - # model in it - # I couldn't figure out how to do inline text within a code block - the - # reverse of inline code in the document text so I'm editing the .Rmd - # here - text <- readLines(system.file("markdown_templates/collection_index.Rmd", - package = "BirdFlowR")) - text <- gsub("[collection_url]", collection_url, text, fixed = TRUE) - text <- gsub("[model]", model, text, fixed = TRUE) - writeLines(text, rmd_file) - - # Add information on whether the report exists to the index - # Index is already saved so it won't persist in the file - # but is used by the markdown document - index$report_exists <- file.exists( - file.path(dir, paste0(index$model, ".html"))) - - suppressMessages({ - rmarkdown::render( - input = rmd_file, - output_file = file.path(dir, "index.html"), - params = list(index = index, collection_url = collection_url), - quiet = !birdflow_options("verbose")) - }) - file.remove(rmd_file) - - # Write separate index.md5 file - index_md5 <- as.character(tools::md5sum(index_path)) - writeLines(index_md5, index_md5_path) -} diff --git a/man/build_collection_index.Rd b/man/build_collection_index.Rd deleted file mode 100644 index 8da70903..00000000 --- a/man/build_collection_index.Rd +++ /dev/null @@ -1,29 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/build_collection_index.R -\name{build_collection_index} -\alias{build_collection_index} -\title{Convert a directory of BirdFlow models into a collection} -\usage{ -build_collection_index(dir, collection_url) -} -\arguments{ -\item{dir}{A local directory for containing BirdFlow models saved as \code{.Rds} -files. The base file name will be used as the model name and other metadata -will be extracted from the BirdFlow object.} - -\item{collection_url}{The final URL for the BirdFlow collection (where it -will be served).} -} -\description{ -A BirdFlow model collection consists of 1 or more BirdFlow objects each -saved to a single \code{.Rds} file, an \code{index.Rds} file, an \code{index.html} file and -a \code{index_md5.txt} file. This function converts a directory with model \code{.Rds} -files into a collection. It needs to be run locally to an R session so likely -the resulting directory will be synchronized with a server after it is -created. -} -\details{ -It is primarily intended for use by the BirdFlow team but is exported from -the package so anyone can use it. -} -\keyword{internal} diff --git a/tests/testthat/test-build_collection_index.R b/tests/testthat/test-build_collection_index.R deleted file mode 100644 index ae4365ce..00000000 --- a/tests/testthat/test-build_collection_index.R +++ /dev/null @@ -1,29 +0,0 @@ -test_that("Build collection index works", { - bf <- BirdFlowModels::amewoo - bf1 <- truncate_birdflow(bf, start = 1, end = 3) - bf2 <- truncate_birdflow(bf, start = 10, end = 13) - - # Create test directory and promise to delete it - dir <- local_test_dir("collection_test") - - # Turn off messages (to not clutter up tests) - local_quiet() - - # Write test files - saveRDS(bf1, file.path(dir, "amewoo1-3.Rds")) - saveRDS(bf2, file.path(dir, "amewoo10-13.Rds")) - - # Create fake html files - writeLines(text = "test", file.path(dir, "amewoo1-3.html")) - writeLines(text = "test", file.path(dir, "amewoo10-13.html")) - - collection_url <- "https://fake-url.com" - - expect_no_error(build_collection_index(dir, collection_url)) - - expect_true(file.exists(file.path(dir, "index.html"))) - expect_true(file.exists(file.path(dir, "logo.png"))) - expect_true(file.exists(file.path(dir, "index.Rds"))) - expect_true(file.exists(file.path(dir, "index_md5.txt"))) - -})