tests/testthat/test-pops-multirun.R

context("test-pops-multirun")

test_that("Model stops if files don't exist or aren't the correct extension", {
  infected_file_list <-
    system.file("extdata", "simple2x2", "infected.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
  parameter_means <- c(0, 21, 1, 500, 0, 0, 0, 0)
  parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)
  pest_host_table <-
    system.file("extdata", "pest_host_table_singlehost_nomort.csv", package = "PoPS")
  competency_table <- system.file("extdata", "competency_table_singlehost.csv", package = "PoPS")

  expect_error(pops_multirun(infected_file_list = "",
                    host_file_list =  host_file_list,
                    total_populations_file =  host_file_list,
                    parameter_means = parameter_means,
                    parameter_cov_matrix = parameter_cov_matrix,
                    pest_host_table = pest_host_table,
                    competency_table = competency_table),
               file_exists_error)

  expect_error(pops_multirun(infected_file_list = infected_file_list,
                             host_file_list =  host_file_list,
                             total_populations_file =  host_file_list,
                             parameter_means = parameter_means,
                             parameter_cov_matrix = parameter_cov_matrix,
                             pest_host_table = pest_host_table,
                             competency_table = competency_table,
                             mask = ""),
               file_exists_error)
})

test_that("Multirun model outputs work", {
  skip_on_os("windows")
  infected_file_list <- system.file("extdata", "simple2x2", "infected.tif", package = "PoPS")
  host_file_list <- system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
  total_populations_file <-
    system.file("extdata", "simple2x2", "total_plants.tif", package = "PoPS")
  temperature_file <- ""
  temperature_coefficient_file <- ""
  precipitation_coefficient_file <- ""
  use_lethal_temperature <- FALSE
  temp <- FALSE
  precip <- FALSE
  season_month_start <- 5
  season_month_end <- 11
  time_step <- "month"
  start_date <- "2019-01-01"
  end_date <- "2019-12-31"
  lethal_temperature <- -35
  lethal_temperature_month <- 1
  random_seed <- 42
  treatments_file <- ""
  treatment_dates <- c("2019-11-01")
  treatment_method <- "ratio"
  management <- FALSE
  mortality_frequency <- "Year"
  mortality_frequency_n <- 1
  natural_kernel_type <- "cauchy"
  anthropogenic_kernel_type <- "cauchy"
  natural_dir <- "NONE"
  anthropogenic_dir <- "NONE"
  pesticide_duration <- c(0)
  pesticide_efficacy <- 1.0
  random_seed <- NULL
  output_frequency <- "year"
  movements_file <- ""
  use_movements <- FALSE
  number_of_iterations <- 2
  number_of_cores <- 2
  model_type <- "SI"
  latency_period <- 0
  parameter_means <- c(0, 21, 1, 500, 0, 0, 0, 0)
  parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)
  pest_host_table <-
    system.file("extdata", "pest_host_table_singlehost_nomort.csv", package = "PoPS")
  competency_table <- system.file("extdata", "competency_table_singlehost.csv", package = "PoPS")
  start_exposed <- FALSE
  generate_stochasticity <- TRUE
  establishment_stochasticity <- TRUE
  movement_stochasticity <- TRUE
  dispersal_stochasticity <- TRUE
  establishment_probability <- 0.5
  dispersal_percentage <- 0.99
  quarantine_areas_file <- ""
  use_quarantine <- FALSE
  output_frequency_n <- 1
  use_spreadrates <- TRUE
  use_overpopulation_movements <- FALSE
  overpopulation_percentage <- 0
  leaving_percentage <- 0
  leaving_scale_coefficient <- 1
  exposed_file_list <- ""
  mask <- NULL
  write_outputs <- "None"
  output_folder_path <- tempdir()
  network_filename <- ""
  use_survival_rates <- FALSE
  survival_rate_month <- 3
  survival_rate_day <- 15
  survival_rates_file <- ""
  network_movement <- "walk"
  use_initial_condition_uncertainty <- FALSE
  use_host_uncertainty <- FALSE
  weather_type <- "deterministic"
  temperature_coefficient_sd_file <- ""
  precipitation_coefficient_sd_file <- ""
  dispersers_to_soils_percentage <- 0
  quarantine_directions <- ""
  multiple_random_seeds <- FALSE
  file_random_seeds <- NULL
  use_soils <- FALSE
  soil_starting_pest_file <- ""
  start_with_soil_populations <- FALSE

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  # expect_equal(terra::as.matrix(data$susceptible_run[[1]], wide = TRUE),
  #              matrix(c(10, 6, 14, 15), nrow = 2, ncol = 2))
  expect_equal(terra::as.matrix(data$probability[[1]], wide = TRUE),
               matrix(c(100, 0, 0, 0), nrow = 2, ncol = 2))
  expect_equal(data$number_infecteds[[1]], 5)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 900)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)

  output_frequency <- "month"
  write_outputs <- "None"
  output_folder_path <- ""

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty = FALSE,
                        use_host_uncertainty = FALSE,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  # expect_equal(terra::as.matrix(data$susceptible_run[[1]], wide = TRUE),
  #              matrix(c(10, 6, 14, 15), nrow = 2, ncol = 2))
  expect_equal(terra::as.matrix(data$probability[[1]], wide = TRUE),
               matrix(c(100, 0, 0, 0), nrow = 2, ncol = 2))
  expect_equal(data$number_infecteds[[1]], 5)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 900)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)

  output_frequency <- "year"
  quarantine_areas_file <-
    system.file("extdata", "simple2x2", "infected.tif", package = "PoPS")
  use_quarantine <- TRUE

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  # expect_equal(terra::as.matrix(data$susceptible_run[[1]], wide = TRUE),
  #              matrix(c(10, 6, 14, 15), nrow = 2, ncol = 2))
  expect_equal(terra::as.matrix(data$probability[[1]], wide = TRUE),
               matrix(c(100, 0, 0, 0), nrow = 2, ncol = 2))
  expect_equal(data$number_infecteds[[1]], 5)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 900)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)

  output_frequency <- "month"

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  # expect_equal(terra::as.matrix(data$susceptible_run[[1]], wide = TRUE),
  #              matrix(c(10, 6, 14, 15), nrow = 2, ncol = 2))
  expect_equal(terra::as.matrix(data$probability[[1]], wide = TRUE),
               matrix(c(100, 0, 0, 0), nrow = 2, ncol = 2))
  expect_equal(data$number_infecteds[[1]], 5)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 900)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)
})

test_that("Multirun model outputs work with mask", {
  skip_on_os("windows")
  infected_file_list <-
    system.file("extdata", "simple20x20", "initial_infection.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host.tif", package = "PoPS")
  total_populations_file <-
    system.file("extdata", "simple20x20", "all_plants.tif", package = "PoPS")
  temperature_file <- ""
  temperature_coefficient_file <- ""
  precipitation_coefficient_file <- ""
  use_lethal_temperature <- FALSE
  temp <- FALSE
  precip <- FALSE
  season_month_start <- 5
  season_month_end <- 11
  time_step <- "month"
  start_date <- "2019-01-01"
  end_date <- "2019-12-31"
  lethal_temperature <- -35
  lethal_temperature_month <- 1
  random_seed <- NA
  treatments_file <- ""
  treatment_dates <- c("2019-11-01")
  treatment_method <- "ratio"
  management <- FALSE
  mortality_frequency <- "Year"
  mortality_frequency_n <- 1
  natural_kernel_type <- "cauchy"
  anthropogenic_kernel_type <- "cauchy"
  natural_dir <- "NONE"
  anthropogenic_dir <- "NONE"
  pesticide_duration <- c(0)
  pesticide_efficacy <- 1.0
  random_seed <- NULL
  output_frequency <- "year"
  movements_file <- ""
  use_movements <- FALSE
  number_of_iterations <- 2
  number_of_cores <- 2
  model_type <- "SI"
  latency_period <- 0
  parameter_means <- c(0, 21, 1, 500, 0, 0, 0, 0)
  parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)
  pest_host_table <-
    system.file("extdata", "pest_host_table_singlehost_nomort.csv", package = "PoPS")
  competency_table <- system.file("extdata", "competency_table_singlehost.csv", package = "PoPS")
  start_exposed <- FALSE
  generate_stochasticity <- TRUE
  establishment_stochasticity <- TRUE
  movement_stochasticity <- TRUE
  dispersal_stochasticity <- TRUE
  establishment_probability <- 0.5
  dispersal_percentage <- 0.99
  quarantine_areas_file <- ""
  use_quarantine <- FALSE
  output_frequency_n <- 1
  use_spreadrates <- TRUE
  mask <- system.file("extdata", "simple20x20", "mask.tif", package = "PoPS")
  use_survival_rates <- FALSE
  survival_rate_month <- 3
  survival_rate_day <- 15
  survival_rates_file <- ""
  network_movement <- "walk"
  use_overpopulation_movements <- FALSE
  overpopulation_percentage <- 0
  leaving_percentage <- 0
  leaving_scale_coefficient <- 1
  exposed_file_list <- ""
  write_outputs <- "None"
  output_folder_path <- tempdir()
  network_filename <- ""
  quarantine_directions <- ""
  multiple_random_seeds <- TRUE
  file_random_seeds <- NULL
  use_soils <- FALSE
  soil_starting_pest_file <- ""
  start_with_soil_populations <- FALSE
  use_initial_condition_uncertainty <- FALSE
  use_host_uncertainty <- FALSE
  weather_type <- "deterministic"
  temperature_coefficient_sd_file <- ""
  precipitation_coefficient_sd_file <- ""
  dispersers_to_soils_percentage <- 0
  quarantine_directions <- ""
  multiple_random_seeds <- FALSE
  file_random_seeds <- NULL
  use_soils <- FALSE
  soil_starting_pest_file <- ""
  start_with_soil_populations <- FALSE

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty = FALSE,
                        use_host_uncertainty = FALSE,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  expect_equal(data$number_infecteds[[1]], 1)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 10000)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)

  use_initial_condition_uncertainty <- TRUE
  use_host_uncertainty <- TRUE
  infected_file_list <-
    system.file("extdata", "simple20x20", "infected_wsd.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host_w_sd2.tif", package = "PoPS")

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)

  use_initial_condition_uncertainty <- TRUE
  use_host_uncertainty <- FALSE
  infected_file_list <-
    system.file("extdata", "simple20x20", "infected_wsd.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host_w_sd2.tif", package = "PoPS")

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)


  use_initial_condition_uncertainty <- FALSE
  use_host_uncertainty <- TRUE
  infected_file_list <-
    system.file("extdata", "simple20x20", "infected_wsd.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host_w_sd2.tif", package = "PoPS")

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)


  use_initial_condition_uncertainty <- FALSE
  use_host_uncertainty <- FALSE
  infected_file_list <-
    system.file("extdata", "simple20x20", "infected_wsd.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host_w_sd2.tif", package = "PoPS")

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty,
                        weather_type = "deterministic",
                        temperature_coefficient_sd_file = "",
                        precipitation_coefficient_sd_file = "",
                        dispersers_to_soils_percentage = 0,
                        quarantine_directions = "",
                        multiple_random_seeds = FALSE,
                        file_random_seeds = NULL,
                        use_soils = FALSE,
                        soil_starting_pest_file = "",
                        start_with_soil_populations = FALSE)

  expect_equal(length(data), 18)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)
})

test_that("Multirun model outputs work with writing all simulations and random seeds", {
  skip_on_os("windows")
  infected_file_list <-
    system.file("extdata", "simple20x20", "initial_infection.tif", package = "PoPS")
  host_file_list <-
    system.file("extdata", "simple20x20", "host.tif", package = "PoPS")
  total_populations_file <-
    system.file("extdata", "simple20x20", "all_plants.tif", package = "PoPS")
  temperature_file <- ""
  temperature_coefficient_file <- ""
  precipitation_coefficient_file <- ""
  use_lethal_temperature <- FALSE
  temp <- FALSE
  precip <- FALSE
  season_month_start <- 5
  season_month_end <- 11
  time_step <- "month"
  start_date <- "2019-01-01"
  end_date <- "2019-12-31"
  lethal_temperature <- -35
  lethal_temperature_month <- 1
  random_seed <- 42
  treatments_file <- ""
  treatment_dates <- c("2019-11-01")
  treatment_method <- "ratio"
  management <- FALSE
  mortality_frequency <- "Year"
  mortality_frequency_n <- 1
  natural_kernel_type <- "cauchy"
  anthropogenic_kernel_type <- "cauchy"
  natural_dir <- "NONE"
  anthropogenic_dir <- "NONE"
  pesticide_duration <- c(0)
  pesticide_efficacy <- 1.0
  random_seed <- NULL
  output_frequency <- "year"
  movements_file <- ""
  use_movements <- FALSE
  number_of_iterations <- 2
  number_of_cores <- 2
  model_type <- "SEI"
  latency_period <- 2
  parameter_means <- c(0, 21, 1, 500, 0, 0, 0, 0)
  parameter_cov_matrix <- matrix(0, nrow = 8, ncol = 8)
  pest_host_table <-
    system.file("extdata", "pest_host_table_singlehost_nomort.csv", package = "PoPS")
  competency_table <- system.file("extdata", "competency_table_singlehost.csv", package = "PoPS")
  start_exposed <- FALSE
  generate_stochasticity <- TRUE
  establishment_stochasticity <- TRUE
  movement_stochasticity <- TRUE
  dispersal_stochasticity <- TRUE
  establishment_probability <- 0.5
  dispersal_percentage <- 0.99
  quarantine_areas_file <- ""
  use_quarantine <- FALSE
  output_frequency_n <- 1
  use_spreadrates <- TRUE
  mask <- system.file("extdata", "simple20x20", "mask.tif", package = "PoPS")
  use_survival_rates <- FALSE
  survival_rate_month <- 3
  survival_rate_day <- 15
  survival_rates_file <- ""
  network_movement <- "walk"
  network_movement <- "walk"
  use_overpopulation_movements <- FALSE
  overpopulation_percentage <- 0
  leaving_percentage <- 0
  leaving_scale_coefficient <- 1
  exposed_file_list <- ""
  write_outputs <- "all_simulations"
  output_folder_path <- tempdir()
  network_filename <- ""
  use_initial_condition_uncertainty <- FALSE
  use_host_uncertainty <- FALSE
  weather_type <- "deterministic"
  temperature_coefficient_sd_file <- ""
  precipitation_coefficient_sd_file <- ""
  dispersers_to_soils_percentage <- 0
  quarantine_directions <- ""
  multiple_random_seeds <- TRUE
  file_random_seeds <- NULL
  use_soils <- FALSE
  soil_starting_pest_file <- ""
  start_with_soil_populations <- FALSE

  data <- pops_multirun(infected_file_list,
                        host_file_list,
                        total_populations_file,
                        parameter_means,
                        parameter_cov_matrix,
                        pest_host_table = pest_host_table,
                        competency_table = competency_table,
                        temp,
                        temperature_coefficient_file,
                        precip,
                        precipitation_coefficient_file,
                        model_type,
                        latency_period,
                        time_step,
                        season_month_start,
                        season_month_end,
                        start_date,
                        end_date,
                        use_survival_rates,
                        survival_rate_month,
                        survival_rate_day,
                        survival_rates_file,
                        use_lethal_temperature,
                        temperature_file,
                        lethal_temperature,
                        lethal_temperature_month,
                        mortality_frequency,
                        mortality_frequency_n,
                        management,
                        treatment_dates,
                        treatments_file,
                        treatment_method,
                        natural_kernel_type,
                        anthropogenic_kernel_type,
                        natural_dir,
                        anthropogenic_dir,
                        number_of_iterations,
                        number_of_cores,
                        pesticide_duration,
                        pesticide_efficacy,
                        random_seed,
                        output_frequency,
                        output_frequency_n,
                        movements_file,
                        use_movements,
                        start_exposed,
                        generate_stochasticity,
                        establishment_stochasticity,
                        movement_stochasticity,
                        dispersal_stochasticity,
                        establishment_probability,
                        dispersal_percentage,
                        quarantine_areas_file,
                        use_quarantine,
                        use_spreadrates,
                        use_overpopulation_movements,
                        overpopulation_percentage,
                        leaving_percentage,
                        leaving_scale_coefficient,
                        exposed_file_list,
                        mask,
                        write_outputs,
                        output_folder_path,
                        network_filename,
                        network_movement,
                        use_initial_condition_uncertainty,
                        use_host_uncertainty,
                        weather_type,
                        temperature_coefficient_sd_file,
                        precipitation_coefficient_sd_file,
                        dispersers_to_soils_percentage,
                        quarantine_directions,
                        multiple_random_seeds,
                        file_random_seeds,
                        use_soils,
                        soil_starting_pest_file,
                        start_with_soil_populations)

  expect_equal(length(data), 18)
  expect_equal(terra::as.matrix(data$median_run[[1]], wide = TRUE),
               terra::as.matrix(terra::rast(infected_file_list), wide = TRUE))
  expect_equal(data$number_infecteds[[1]], 1)
  expect_equal(data$number_infecteds[[2]], 0)
  expect_equal(data$infected_areas[[1]], 10000)
  expect_equal(data$infected_areas[[2]], 0)
  expect_equal(data$west_rate[[1]], 0)
  expect_equal(data$west_rate[[2]], 0)
  expect_equal(data$east_rate[[1]], 0)
  expect_equal(data$east_rate[[2]], 0)
  expect_equal(data$south_rate[[1]], 0)
  expect_equal(data$south_rate[[2]], 0)
  expect_equal(data$north_rate[[1]], 0)
  expect_equal(data$north_rate[[2]], 0)
})
ncsu-landscape-dynamics/rpops documentation built on May 1, 2024, 10:21 a.m.