Skip to content

Commit

Permalink
lint and add documentation for random_seeds to pops_model function
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisJones687 committed Sep 28, 2023
1 parent 3744444 commit a142acc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 50 deletions.
6 changes: 3 additions & 3 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,6 @@ output_from_raster_mean_and_sd <- function(x) {

# Combine two standard deviation spatRasters
combined_sd <- function(v1, v2, m1, m2, n1, n2) {
(((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 -1)) +
(((n1 * n2) * (m1 - m2)^2) / ((n1 +n2) * (n1 + n2 -1)))
}
(((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 1)) +
(((n1 * n2) * (m1 - m2)^2) / ((n1 + n2) * (n1 + n2 - 1)))
}
5 changes: 4 additions & 1 deletion R/pops_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@
#' overwinter population that emerges
#' @param weather_size the number of matrices in a list or layers in a raster object
#' @param weather_type string indicating how the weather data is passed in either
#' as a mean and standard deviation to represent uncertainty ("probablisticc") or as a time
#' as a mean and standard deviation to represent uncertainty ("probabilistic") or as a time
#' series ("deterministic")
#' @param dispersers_to_soils_percentage range from 0 to 1 representing the percentage
#' of dispersers that fall to the soil and survive.
#' @param soil_reservoirs list of matrices with soil pests created from soil_pest_file.
#' @param random_seeds vector of random seeds in the order of "disperser_generation",
#' "natural_dispersal", "anthropogenic_dispersal", "establishment", "weather", "movement",
#' "overpopulation", "survival_rate", "soil"
#'
#' @return list of vector matrices of infected and susceptible hosts per
#' simulated year and associated statistics (e.g. spread rate)
Expand Down
6 changes: 5 additions & 1 deletion man/pops_model.Rd

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

97 changes: 52 additions & 45 deletions tests/testthat/test-pops.r
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,6 @@ test_that("Input raster resolutions, extents, and crs all match", {

})



test_that("Infected results return initial infected if reproductive rate is set to 0", {
infected_file <- system.file("extdata", "simple2x2", "infected.tif", package = "PoPS")
host_file <- system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
Expand Down Expand Up @@ -953,7 +951,7 @@ test_that(
parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)

reduced_inf <- matrix(0, ncol = 2, nrow = 2)
reduced_inf[1,1] <- 3
reduced_inf[1, 1] <- 3

expect_equal(pops(infected_file = infected_file,
host_file = host_file,
Expand Down Expand Up @@ -1005,10 +1003,12 @@ test_that("Infected and Susceptible results return all 0's if treatments file is
coefficient_file <-
system.file("extdata", "simple2x2", "temperature_coefficient.tif", package = "PoPS")
temperature_file <-
system.file("extdata", "simple2x2", "critical_temp_all_below_threshold.tif", package = "PoPS")
system.file("extdata", "simple2x2", "critical_temp_all_below_threshold.tif",
package = "PoPS")
start_date <- "2008-01-01"
end_date <- "2010-12-31"
treatments_file <- system.file("extdata", "simple2x2", "treatments.tif", package = "PoPS")
treatments_file <- system.file("extdata", "simple2x2", "treatments.tif",
package = "PoPS")
parameter_means <- c(0, 21, 1, 500, 0, 0, 0, 0)
parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)

Expand Down Expand Up @@ -1691,7 +1691,8 @@ test_that("Infected results are greater with same parameters for daily spread vs
coefficient_file <-
system.file("extdata", "simple2x2", "temperature_coefficient.tif", package = "PoPS")
temperature_file <-
system.file("extdata", "simple2x2", "critical_temp_all_below_threshold.tif", package = "PoPS")
system.file("extdata", "simple2x2", "critical_temp_all_below_threshold.tif",
package = "PoPS")
start_date <- "2008-01-01"
end_date <- "2010-12-31"
parameter_means <- c(0.1, 21, 1, 500, 0, 0, 0, 0)
Expand Down Expand Up @@ -2765,7 +2766,8 @@ test_that("uncertainty propogation works as expected", {

infected_file <- system.file("extdata", "simple20x20", "infected_wsd.tif", package = "PoPS")
host_file <- system.file("extdata", "simple20x20", "all_plants.tif", package = "PoPS")
total_populations_file <- system.file("extdata", "simple20x20", "all_plants.tif", package = "PoPS")
total_populations_file <- system.file("extdata", "simple20x20", "all_plants.tif",
package = "PoPS")
start_date <- "2008-01-01"
end_date <- "2008-03-31"
parameter_means <- c(2, 21, 1, 500, 0, 0, 100, 1000)
Expand Down Expand Up @@ -2881,53 +2883,58 @@ test_that("multiple_random seeds works and returns expected results", {

test_that("Using soils returns expected results", {
infected_file <-
system.file("extdata", "simple20x20", "initial_infection.tif", package = "PoPS")
host_file <- system.file("extdata", "simple20x20", "host.tif", package = "PoPS")
system.file("extdata", "simple2x2", "infected.tif", package = "PoPS")
host_file <- system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
total_populations_file <-
system.file("extdata", "simple20x20", "all_plants.tif", package = "PoPS")
system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
start_date <- "2008-01-01"
end_date <- "2008-03-31"
end_date <- "2008-12-31"
parameter_means <- c(5, 21, 1, 500, 0, 0, 100, 1000)
parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)
use_soils <- TRUE
dispersers_to_soils_percentage <- 0.05
coefficient_file <-
system.file("extdata", "simple2x2", "temperature_coefficient.tif", package = "PoPS")

#
# data <-
# pops(infected_file = infected_file,
# host_file = host_file,
# total_populations_file = total_populations_file,
# parameter_means = parameter_means,
# parameter_cov_matrix = parameter_cov_matrix,
# start_date = start_date,
# end_date = end_date,
# anthropogenic_kernel_type = anthropogenic_kernel_type,
# multiple_random_seeds = multiple_random_seeds,
# file_random_seeds = file_random_seeds)
#
# test_mat <- terra::as.matrix(terra::rast(infected_file), wide = TRUE)
# expect_gte(data$infected[[1]][[1]], test_mat[[1]])
# expect_gte(data$infected[[1]][[2]], test_mat[[2]])
# expect_gte(data$infected[[1]][[3]], test_mat[[3]])
# expect_gte(data$infected[[1]][[4]], test_mat[[4]])
#
# file_random_seeds <- system.file("extdata", "simple2x2", "randoms.csv", package = "PoPS")
#
# data <-
# pops(infected_file = infected_file,
# host_file = host_file,
# total_populations_file = total_populations_file,
# parameter_means = parameter_means,
# parameter_cov_matrix = parameter_cov_matrix,
# start_date = start_date,
# end_date = end_date,
# anthropogenic_kernel_type = anthropogenic_kernel_type,
# multiple_random_seeds = multiple_random_seeds,
# file_random_seeds = file_random_seeds)

data <-
pops(infected_file = infected_file,
host_file = host_file,
total_populations_file = total_populations_file,
parameter_means = parameter_means,
parameter_cov_matrix = parameter_cov_matrix,
start_date = start_date,
end_date = end_date,
temp = TRUE,
temperature_coefficient_file = coefficient_file,
use_soils = use_soils,
dispersers_to_soils_percentage = dispersers_to_soils_percentage)

test_mat <- terra::as.matrix(terra::rast(infected_file), wide = TRUE)
expect_gte(data$infected[[1]][[1]], test_mat[[1]])
expect_gte(data$infected[[1]][[2]], test_mat[[2]])
expect_gte(data$infected[[1]][[3]], test_mat[[3]])
expect_gte(data$infected[[1]][[4]], test_mat[[4]])
expect_equal(length(data$soil_reservoirs[1]), 20)

data <-
pops(infected_file = infected_file,
host_file = host_file,
total_populations_file = total_populations_file,
parameter_means = parameter_means,
parameter_cov_matrix = parameter_cov_matrix,
start_date = start_date,
end_date = end_date,
temp = TRUE,
temperature_coefficient_file = coefficient_file,
use_soils = use_soils,
dispersers_to_soils_percentage = dispersers_to_soils_percentage,
soil_starting_pest_file = infected_file,
start_with_soil_populations = TRUE)
#
# test_mat <- terra::as.matrix(terra::rast(infected_file), wide = TRUE)
# expect_gte(data$infected[[1]][[1]], test_mat[[1]])
# expect_gte(data$infected[[1]][[2]], test_mat[[2]])
# expect_gte(data$infected[[1]][[3]], test_mat[[3]])
# expect_gte(data$infected[[1]][[4]], test_mat[[4]])
})


0 comments on commit a142acc

Please sign in to comment.