Skip to content
Merged
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: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
^LICENSE\.md$
^data-raw$
^data_pipeline$
^localtmp$
^flow_debug.log
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Imports:
Suggests:
testthat (>= 3.0.0),
callthat,
httr
httr,
withr
Config/testthat/edition: 3
Remotes:
birdflow-science/BirdFlowR,
Expand Down
23 changes: 2 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
MIT License

Copyright (c) 2025 UMass Amherst Center for Data Science & AI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
YEAR: 2025
COPYRIGHT HOLDER: UMassCDS
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(flow)
export(get_s3_config)
export(load_models)
export(range_rescale)
export(save_json_palette)
export(set_s3_config)
export(symbolize_raster_data)
importFrom(stats,predict)
3 changes: 3 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@
#' \item{"jitter_radius"}{Jitter radius in meters}
#' }
"counties_json"

# Declare these as global variables so package check doesn't flag them
utils::globalVariables(c("species", "flow_colors"))
9 changes: 9 additions & 0 deletions R/flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ save_local_path <- "config/save_local.flag"
#' `legend`
#' `type`
#' @export
#' @importFrom stats predict
flow <- function(loc, week, taxa, n, direction = "forward", save_local = FALSE) {
s3_cfg <- get_s3_config()
s3_enabled <- !is.na(s3_cfg$bucket) && nzchar(s3_cfg$bucket)
Expand Down Expand Up @@ -195,6 +196,7 @@ flow <- function(loc, week, taxa, n, direction = "forward", save_local = FALSE)
combined <- NULL
any_valid <- FALSE


for (i in seq_along(target_species)) {
sp <- target_species[i]
bf <- models[[sp]]
Expand All @@ -215,13 +217,20 @@ flow <- function(loc, week, taxa, n, direction = "forward", save_local = FALSE)
start_proportion <- sum(initial_population_distr[location_i]) / 1
abundance <- pred * species$population[species$species == sp] / prod(terra::res(bf) / 1000) * start_proportion
this_raster <- BirdFlowR::rasterize_distr(abundance, bf = bf, format = "terra")

# Make NA zero (for adding)
this_raster[is.na(this_raster)] <- 0

if (is.null(combined)) {
combined <- this_raster
} else {
combined <- combined + this_raster
}
}

# Make zeros transparent
combined[combined == 0] <- NA

if (!any_valid) {
log_progress("No valid starting location. Returning error.")
return(format_error("Invalid starting location", "outside mask"))
Expand Down
14 changes: 14 additions & 0 deletions man/get_s3_config.Rd

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

34 changes: 34 additions & 0 deletions man/set_s3_config.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
load_models()
set_s3_config()


# Set the cache directory to a new tempdir for the duration of the tests.
# and delete it after tests complete.
# If running individual tests manually don't run this code.
original_local_temp <- get_s3_config()$local_temp
local_temp <- withr::local_tempdir("cache", .local_envir = teardown_env())
s3_config$local_temp <- local_temp
withr::defer(
s3_config$local_temp <- original_local_temp, envir = teardown_env()
)
26 changes: 26 additions & 0 deletions tests/testthat/test-flow.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,29 @@ test_that("result contents are valid (inflow)", {
expect_true(row$type == "inflow")
}
})



test_that("total is not constrained by individual taxa NAs", {
params <- standard_flow_input()

# Make a total projection
params$taxa <- "total"
expect_no_error(total_result <- do.call(flow, params))
total <- terra::rast(total_result$geotiff)

# Same start but for american black duck
params$taxa <- "ambduc"
expect_no_error(ambduc_result <- do.call(flow, params))
ambduc <- terra::rast(ambduc_result$geotiff)

# There should be more NA's in the American black duck result than in the total
ambduc_nas <- terra::values(ambduc) |> is.na() |> sum()
total_nas <- terra::values(total) |> is.na() |> sum()
expect_true(ambduc_nas > total_nas)

# The total should not be NA anywhere that ambduc is not NA
n_wrong <- (is.na(total) & !is.na(ambduc)) |> terra::values() |> sum()
expect_equal(n_wrong, 0)

})