From 7adae194a99284b3578861b89a79eb248544c2e0 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 12 Sep 2023 09:20:32 -0400 Subject: [PATCH] add soils file and use soils to all r functions. --- DESCRIPTION | 4 +--- R/calibrate.R | 7 +++++-- R/configuration.R | 22 ++++++++++++++++++++++ R/pops.r | 8 +++++++- R/pops_multirun.R | 8 ++++++-- R/validate.R | 6 +++++- man/calibrate.Rd | 10 +++++++++- man/pops.Rd | 7 ++++++- man/pops_multirun.Rd | 10 +++++++++- man/validate.Rd | 10 +++++++++- 10 files changed, 79 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 83883516..eac5327a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -17,7 +17,7 @@ Authors@R: family = "Petrasova", role = c("aut"), comment = c(ORCID = "0000-0002-5120-5538"))) -Depends: R (>= 3.3.0) +Depends: R (>= 4.0.0) Imports: raster (>= 3.5-15), terra (>= 1.5-17), @@ -26,7 +26,6 @@ Imports: foreach, parallel, doParallel, - iterators, landscapemetrics, lubridate, sp, @@ -41,7 +40,6 @@ URL: http://www.github.com/ncsu-landscape-dynamics/rpops Encoding: UTF-8 LazyData: true SystemRequirements: - C++11, GNU make Suggests: testthat, diff --git a/R/calibrate.R b/R/calibrate.R index 9f8b1261..4687e171 100644 --- a/R/calibrate.R +++ b/R/calibrate.R @@ -197,7 +197,9 @@ calibrate <- function(infected_years_file, dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL) { + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "") { # add all data to config list config <- c() @@ -289,7 +291,8 @@ calibrate <- function(infected_years_file, config$dispersers_to_soils_percentage <- dispersers_to_soils_percentage config$multiple_random_seeds <- multiple_random_seeds config$random_seeds <-random_seeds - + config$use_soils <- use_soils + config$soil_starting_pest_file <- soil_starting_pest_file # call configuration function to perform data checks and transform data into # format used in pops c++ diff --git a/R/configuration.R b/R/configuration.R index ee5b8ce6..cc90e544 100644 --- a/R/configuration.R +++ b/R/configuration.R @@ -302,6 +302,28 @@ configuration <- function(config) { return(config) } + # check that soils raster has the same crs, resolutin, and extent. + if (config$use_soils) { + if (config$function_name %in% c("casestudy_creation", "model_api")) { + soils_check <- + secondary_raster_checks(config$soil_starting_pest_file, infected, config$use_s3, config$bucket) + } else { + soils_check <- secondary_raster_checks(config$soil_starting_pest_file, infected) + } + if (soils_check$checks_passed) { + soil_pests <- soils_check$raster + config$soil_pests <- terra::as.matrix(soil_pests, wide = TRUE) + } else { + config$failure <- soils_check$failed_check + if (config$failure == file_exists_error) { + config$failure <- detailed_file_exists_error(config$soil_starting_pest_file) + } + return(config) + } + } else { + config$soil_pests <- zero_matrix + } + # check that survival_rates raster has the same crs, resolution, and extent if (config$use_survival_rates == TRUE) { if (config$function_name %in% c("casestudy_creation", "model_api")) { diff --git a/R/pops.r b/R/pops.r index a4e03a40..b91c7b9f 100644 --- a/R/pops.r +++ b/R/pops.r @@ -193,6 +193,8 @@ #' of dispersers that fall to the soil and survive. #' @param soil_starting_pest_file path to the raster file with the starting #' amount of pest or pathogen. +#' @param use_soils boolean to indicate if pests establish in the soil and spread out from there. +#' Typically used for soil borne pathogens. #' @param multiple_random_seeds boolean to indicate if the model should use multiple random seeds #' (allows for performing uncertainty partitioning) or a single random seed (backwards #' compatibility option). Default is FALSE. @@ -293,7 +295,9 @@ pops <- function(infected_file, dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL) { + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "") { config <- c() config$random_seed <- random_seed @@ -378,6 +382,8 @@ pops <- function(infected_file, config$dispersers_to_soils_percentage <- dispersers_to_soils_percentage config$multiple_random_seeds <- multiple_random_seeds config$random_seeds <-random_seeds + config$use_soils <- use_soils + config$soil_starting_pest_file <- soil_starting_pest_file config <- configuration(config) diff --git a/R/pops_multirun.R b/R/pops_multirun.R index 61d1eb3e..3d43c7fb 100644 --- a/R/pops_multirun.R +++ b/R/pops_multirun.R @@ -106,7 +106,9 @@ pops_multirun <- function(infected_file, dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL) { + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "") { config <- c() config$random_seed <- random_seed config$infected_file <- infected_file @@ -188,6 +190,8 @@ pops_multirun <- function(infected_file, config$dispersers_to_soils_percentage <- dispersers_to_soils_percentage config$multiple_random_seeds <- multiple_random_seeds config$random_seeds <-random_seeds + config$use_soils <- use_soils + config$soil_starting_pest_file <- soil_starting_pest_file config <- configuration(config) @@ -196,8 +200,8 @@ pops_multirun <- function(infected_file, } config$crs <- terra::crs(config$host) - # i <- NULL + i <- NULL cl <- parallel::makeCluster(config$core_count) doParallel::registerDoParallel(cl) diff --git a/R/validate.R b/R/validate.R index 8c0bdcc0..fe6ad97a 100644 --- a/R/validate.R +++ b/R/validate.R @@ -122,7 +122,9 @@ validate <- function(infected_years_file, dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL) { + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "") { config <- c() config$infected_years_file <- infected_years_file config$infected_file <- infected_file @@ -207,6 +209,8 @@ validate <- function(infected_years_file, config$dispersers_to_soils_percentage <- dispersers_to_soils_percentage config$multiple_random_seeds <- multiple_random_seeds config$random_seeds <-random_seeds + config$use_soils <- use_soils + config$soil_starting_pest_file <- soil_starting_pest_file config <- configuration(config) diff --git a/man/calibrate.Rd b/man/calibrate.Rd index b195443c..dfff8dae 100644 --- a/man/calibrate.Rd +++ b/man/calibrate.Rd @@ -88,7 +88,9 @@ calibrate( dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "" ) } \arguments{ @@ -443,6 +445,12 @@ compatibility option). Default is FALSE.} \item{random_seeds}{A file path to the file with the .csv file containing random_seeds table. Use a file if you are trying to recreate an exact analysis otherwise we suggest leaving the default. Default is Null which draws the seed numbers for each.} + +\item{use_soils}{boolean to indicate if pests establish in the soil and spread out from there. +Typically used for soil borne pathogens.} + +\item{soil_starting_pest_file}{path to the raster file with the starting +amount of pest or pathogen.} } \value{ a dataframe of the variables saved and their success metrics for diff --git a/man/pops.Rd b/man/pops.Rd index 18766fd5..0de17f45 100644 --- a/man/pops.Rd +++ b/man/pops.Rd @@ -75,7 +75,9 @@ pops( dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "" ) } \arguments{ @@ -349,6 +351,9 @@ compatibility option). Default is FALSE.} Use a file if you are trying to recreate an exact analysis otherwise we suggest leaving the default. Default is Null which draws the seed numbers for each.} +\item{use_soils}{boolean to indicate if pests establish in the soil and spread out from there. +Typically used for soil borne pathogens.} + \item{soil_starting_pest_file}{path to the raster file with the starting amount of pest or pathogen.} } diff --git a/man/pops_multirun.Rd b/man/pops_multirun.Rd index 39ffb760..6c9817a3 100644 --- a/man/pops_multirun.Rd +++ b/man/pops_multirun.Rd @@ -79,7 +79,9 @@ pops_multirun( dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "" ) } \arguments{ @@ -364,6 +366,12 @@ compatibility option). Default is FALSE.} \item{random_seeds}{A file path to the file with the .csv file containing random_seeds table. Use a file if you are trying to recreate an exact analysis otherwise we suggest leaving the default. Default is Null which draws the seed numbers for each.} + +\item{use_soils}{boolean to indicate if pests establish in the soil and spread out from there. +Typically used for soil borne pathogens.} + +\item{soil_starting_pest_file}{path to the raster file with the starting +amount of pest or pathogen.} } \value{ list of infected and susceptible per year diff --git a/man/validate.Rd b/man/validate.Rd index 567eec61..125d7894 100644 --- a/man/validate.Rd +++ b/man/validate.Rd @@ -83,7 +83,9 @@ validate( dispersers_to_soils_percentage = 0, quarantine_directions = "", multiple_random_seeds = FALSE, - random_seeds = NULL + random_seeds = NULL, + use_soils = FALSE, + soil_starting_pest_file = "" ) } \arguments{ @@ -371,6 +373,12 @@ compatibility option). Default is FALSE.} \item{random_seeds}{A file path to the file with the .csv file containing random_seeds table. Use a file if you are trying to recreate an exact analysis otherwise we suggest leaving the default. Default is Null which draws the seed numbers for each.} + +\item{use_soils}{boolean to indicate if pests establish in the soil and spread out from there. +Typically used for soil borne pathogens.} + +\item{soil_starting_pest_file}{path to the raster file with the starting +amount of pest or pathogen.} } \value{ a data frame of statistical measures of model performance.