Skip to content

Commit

Permalink
Merge branch 'initial_infectionFeature'
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Melloy committed Aug 4, 2021
2 parents 8d4ed7e + 15592e8 commit 98b7a86
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 20,042 deletions.
3 changes: 2 additions & 1 deletion R/format_weather.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ format_weather <- function(x,
r = NULL,
lonlat_file = NULL) {
# CRAN Note avoidance
times <- ..station <- ..time_zone <- NULL #nocov
times <- NULL #nocov

# Check x class
if (!is.data.frame(x)) {
Expand Down Expand Up @@ -322,6 +322,7 @@ format_weather <- function(x,
old = POSIXct_time,
new = "times",
skip_absent = TRUE)
x[,times := as.POSIXct(times)]
x[, YYYY := lubridate::year(x[, times])]
x[, MM := lubridate::month(x[, times])]
x[, DD := lubridate::day(x[, times])]
Expand Down
8 changes: 4 additions & 4 deletions R/one_day.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#' landing on a susceptible growing point
#' @param spores_per_gp_per_wet_hour Number of spores produced per sporulating growing point each wet hour.
#' Also known as the 'spore_rate'. Value is dependent on the susceptibility of the host genotype.
#' @param infection_start Date at which the infection starts in the paddock.
#' Must be after sowing date
#'
#' @return a `list` detailing daily data for the day `i_date` generated by the model including:
#' a `paddock` an 'x' 'y' data.table, iteration day (i_day), cumulative daily weather data, such as:
Expand All @@ -30,8 +28,7 @@ one_day <- function(i_date,
gp_rr,
max_gp,
spore_interception_parameter,
spores_per_gp_per_wet_hour,
infection_start) {
spores_per_gp_per_wet_hour) {

times <- temp <- wet_hours <- rain <- new_gp <- sporulating_gp <-
cdd_at_infection <- noninfected_gp <- NULL
Expand Down Expand Up @@ -66,6 +63,8 @@ if(any(is.na(daily_vals[["paddock"]][,sporulating_gp]))){
stop("NA values in daily_vals[['paddock']][,sporulating_gp] ")
}

# Don't spread spores if there are no infected coordinates
if(nrow(daily_vals[["infected_coords"]]) > 0){
# Spread spores and infect plants
# Update growing points for paddock coordinates
if(i_wet_hours > 2){
Expand Down Expand Up @@ -106,6 +105,7 @@ if(any(is.na(daily_vals[["paddock"]][,sporulating_gp]))){
# update infected coordinates
daily_vals[["infected_coords"]] <- daily_vals[["paddock"]][sporulating_gp > 0, c("x","y")]

}

# Grow Plants
# this code represents mathematica function `growth`; `updateRefUninfectiveGrowingPoints`
Expand Down
73 changes: 52 additions & 21 deletions R/trace_asco.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
#' per square meter. Defaults to \code{15000}.
#' @param max_new_gp Maximum number of new chickpea growing points (meristems)
#' which develop per day, per square meter. Defaults to \code{350}.
#' @param primary_infection_foci it refers to the inoculated quadrat
#' located at the centre of the paddock from where disease spreads
#' Defaults to \code{"centre"}
#' @param primary_infection_foci refers to the inoculated coordinates where the
#' eppidemic starts. Accepted inputs are: \code{"centre"} (Default), \code{random}
#' a randomly selected coordinate in the paddock, a two column data.table of
#' coordinates with colnames c("x","y"), a three column data.table where the third
#' @param primary_infection_intensity The intensity of the starting epidemic as
#' described by the number of number of sporulating growing points.
#' @param latent_period_cdd latent period in cumulative degree days (sum of
Expand Down Expand Up @@ -97,7 +98,7 @@ trace_asco <- function(weather,
spores_per_gp_per_wet_hour = 0.22){


x <- y <- sp_gp <- NULL
x <- y <- load <- NULL

# check date inputs for validity -----------------------------------------
.vali_date <- function(x) {
Expand All @@ -124,12 +125,12 @@ trace_asco <- function(weather,
}


if (primary_infection_intensity > seeding_rate) {
stop(
"primary_infection_intensity exceeds the number of starting growing points - 'seeding_rate': ",
seeding_rate
)
}
# if (primary_infection_intensity > seeding_rate) {
# stop(
# "primary_infection_intensity exceeds the number of starting growing points - 'seeding_rate': ",
# seeding_rate
# )
# }

# convert times to POSIXct -----------------------------------------------
initial_infection <-
Expand Down Expand Up @@ -205,7 +206,11 @@ trace_asco <- function(weather,
infected_rows <- which_paddock_row(paddock = paddock,
query = primary_infection_foci)
if(ncol(primary_infection_foci) == 2){
primary_infection_foci[,sp_gp := primary_infection_intensity]
primary_infection_foci[,load := primary_infection_intensity]
}else{
if(all(colnames(primary_infection_foci) %in% c("x", "y"))){
stop("colnames for 'primary_infection_foci' not 'x', 'y' & 'load'.")
}
}

# define paddock variables at time 1
Expand All @@ -223,11 +228,6 @@ trace_asco <- function(weather,
0
)]

paddock[infected_rows, "noninfected_gp" :=
seeding_rate - primary_infection_foci[,3]]
paddock[infected_rows, "sporulating_gp" :=
primary_infection_foci[,3]]

# calculate additional parameters
spore_interception_parameter <-
0.00006 * (max_gp_lim/max_new_gp)
Expand All @@ -243,6 +243,7 @@ trace_asco <- function(weather,
#
# refUninfectiveGPs <- minGrowingPoints <- seeding_rate

# Create a clean daily values list with no infection in paddocks
daily_vals_list <- list(
list(
paddock = paddock, # data.table each row is a 1 x 1m coordinate
Expand All @@ -254,7 +255,8 @@ trace_asco <- function(weather,
cr = 0, # cumulative rainfall
gp_standard = seeding_rate, # standard number of growing points for 1m^2 if not inhibited by infection (refUninfectiveGrowingPoints)
new_gp = seeding_rate, # new number of growing points for current iteration (refNewGrowingPoints)
infected_coords = primary_infection_foci, # data.table
infected_coords = data.table(x = numeric(),
y = numeric()), # data.table
newly_infected = data.table(x = numeric(),
y = numeric(),
spores_per_packet = numeric(),
Expand Down Expand Up @@ -285,13 +287,42 @@ trace_asco <- function(weather,
gp_rr = gp_rr,
max_gp = max_gp,
spore_interception_parameter = spore_interception_parameter,
spores_per_gp_per_wet_hour = spores_per_gp_per_wet_hour,
infection_start = initial_infection
spores_per_gp_per_wet_hour = spores_per_gp_per_wet_hour
)

# temporary line of code to test building of daily_vals in loop
#daily_vals_list <- day_out
# When the time of initial infection occurs, infect the paddock coordinates
if(initial_infection == time_increments[i]){

# if primary_infection_intensity exceeds the number of growing points send
# warning
if (primary_infection_intensity > daily_vals_list[[i]][["gp_standard"]]) {
warning(
"primary_infection_intensity exceeds the number of growing points at time of infection 'growing_points': ",
daily_vals_list[[i]][["gp_standard"]],
"\nThis may cause an over estimation of disease spread"
)
}


# update the remaining increments with the primary infected coordinates
daily_vals_list[i:length(daily_vals_list)] <-
lapply(daily_vals_list[i:length(daily_vals_list)], function(dl){

# Infecting paddock
pad1 <- data.table::copy(dl[["paddock"]])
pad1[infected_rows,
c("noninfected_gp",
"sporulating_gp") :=
.(noninfected_gp - primary_infection_foci[, load],
primary_infection_foci[, load])]
dl[["paddock"]] <- pad1

# Edit infected_coordinates data.table
dl[["infected_coords"]] <- primary_infection_foci[,c("x","y")]
return(dl)
})

}
}

daily_vals_list[[length(daily_vals_list)]][["i_date"]] <-
Expand Down
17 changes: 12 additions & 5 deletions man/trace_asco.Rd

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

Loading

0 comments on commit 98b7a86

Please sign in to comment.