Skip to content

Commit

Permalink
specify timezones to avoid unit test errors
Browse files Browse the repository at this point in the history
If these are run in other locales the unit tests will fail due to timezones altering model outputs
  • Loading branch information
PaulMelloy committed Nov 15, 2024
1 parent 9290751 commit 44926d2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
4 changes: 2 additions & 2 deletions R/calc_DIV.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ calc_DIV <- function(date_time, Tm,RH, rain,dat){
"moist_ind") := list(temperature_index(temp),
moisture_index(rh,rain,70))]

DIV <- dat[, list(DIV = mean(fifelse(test = Tm_index == 0 | moist_ind == 0,
yes = 0, no = (Tm_index * moist_ind)))),
DIV <- dat[, list(DIV = round(mean(fifelse(test = Tm_index == 0 | moist_ind == 0,
yes = 0, no = Tm_index * moist_ind)),digits = 7)),
by = c("Year", "Month", "Day")]

return(DIV)
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
23 changes: 9 additions & 14 deletions tests/testthat/test-calc_DIV.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ test_that("calc_DIV works", {
RH = RH1,
rain = rain1
)
expect_equal(DIV1$DIV, c(0.8445079, 0.8196010 ,0.8513760), tolerance = 0.00001)
expect_equal(DIV1$DIV, c(0.7845169, 0.8551818), tolerance = 0.00001)
expect_type(DIV1,"list")
expect_equal(dim(DIV1), c(3,4))
expect_equal(dim(DIV1), c(2,4))
expect_equal(colnames(DIV1), c("Year","Month","Day","DIV"))

in_dat <- data.frame(
Expand All @@ -21,23 +21,18 @@ test_that("calc_DIV works", {
expect_error(calc_DIV(dat = in_dat),
regexp = "'dat' data.frame must have colnames 'times','temp','rh','rain'")

# accept a data.frame as input
in_dat <- data.frame(
times = date_t,
temp = Tm1,
rh = RH1,
rain = rain1
)
#calc_DIV(dat = in_dat)
dat_out <- calc_DIV(dat = in_dat)

})

# define DIV calculation
expect_equal(dat_out$DIV, c(0.7845169, 0.8551818), tolerance = 0.00001)
expect_type(dat_out,"list")
expect_equal(dim(dat_out), c(2,4))
expect_equal(colnames(dat_out), c("Year","Month","Day","DIV"))

# dat <- data.table(Tm = rep(0:49, times = 50),
# Rh = rep (51:100, each = 50))
#
# dat[, DIV := list(temperature_index(Tm)*
# moisture_index(Rh))]
# library(ggplot2)
# ggplot(dat, aes(x = Tm, y = Rh, z = DIV))+
# geom_contour_filled()
})
27 changes: 14 additions & 13 deletions tests/testthat/test-calc_epidemic_onset.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# load the weather data to be formatted
# import BOM data file
set.seed(321)
brisvegas <-
system.file("extdata", "bris_weather_obs.csv", package = "epiphytoolR")
bris <- data.table::fread(brisvegas)
Expand Down Expand Up @@ -74,14 +75,14 @@ test_that("epidemic onset produces expected outcome", {
rh)]

# susceptible cultivar
sus_out <- calc_epidemic_onset(c_closure = as.POSIXct("2023-06-01"),
sus_out <- calc_epidemic_onset(c_closure = as.POSIXct("2023-06-01", tz = "UTC"),
weather = bris_formated,
cultivar_sus = 3)
expect_type(sus_out,"double")
expect_equal(sus_out, as.POSIXct("2023-06-07",tz = "UTC"))

# resistant cultivar
res_out <- calc_epidemic_onset(c_closure = as.POSIXct("2023-06-01"),
res_out <- calc_epidemic_onset(c_closure = as.POSIXct("2023-06-01", tz = "UTC"),
weather = bris_formated,
cultivar_sus = 5)
expect_type(res_out,"double")
Expand All @@ -99,7 +100,7 @@ test_that("different start dates provide different epidemic dates",{
# set NA wind direction values to 20 degrees. Wind is not important for this model
w_dat[,WR200 := runif(.N,min = 0,359)]
# remove all data after September as it contains missing data
w_dat <- w_dat[Datum < as.POSIXct("2022-10-01")]
w_dat <- w_dat[Datum < as.POSIXct("2022-10-01",tz = "UTC")]
# set NA wind speed values to zero
w_dat[is.na(WG200),WG200 := 0]

Expand All @@ -125,21 +126,21 @@ test_that("different start dates provide different epidemic dates",{
}
out <- calc_epidemic_onset(start = as.POSIXct("2022-04-25",tz = "UTC"),
end = as.POSIXct("2022-09-30",tz = "UTC"),
c_closure = as.POSIXct("2022-05-01")+(i*3*86400), # 86400 is the number of seconds in a day
c_closure = as.POSIXct("2022-05-01",tz = "UTC")+(i*3*86400), # 86400 is the number of seconds in a day
weather = w_dat,
cultivar_sus = 3)

out2 <- c(out2,as.character(out))

}
# cat(out2,sep = "\", \"")
expect_equal(out2, c("2022-05-20", "2022-05-20", "2022-05-21", "2022-05-24",
"2022-05-24", "2022-05-29", "2022-06-05", "2022-06-08",
"2022-06-09", "2022-06-11", "2022-06-12", "2022-06-13",
"2022-06-20", "2022-06-24", "2022-06-26", "2022-06-27",
"2022-06-30", "2022-07-01", "2022-07-04", "2022-07-08",
"2022-07-11", "2022-07-13", "2022-07-17", "2022-07-22",
"2022-07-25", "2022-07-26", "2022-07-29", "2022-08-01",
"2022-08-01", "2022-08-04"))

expect_equal(out2, c("2022-05-20", "2022-05-21", "2022-05-22", "2022-05-24",
"2022-05-25", "2022-05-31", "2022-06-06", "2022-06-08",
"2022-06-09", "2022-06-11", "2022-06-12", "2022-06-17",
"2022-06-22", "2022-06-25", "2022-06-26", "2022-06-27",
"2022-06-30", "2022-07-01", "2022-07-07", "2022-07-09",
"2022-07-11", "2022-07-13", "2022-07-20", "2022-07-23",
"2022-07-26", "2022-07-26", "2022-07-29", "2022-08-01",
"2022-08-03", "2022-08-05"))
})

0 comments on commit 44926d2

Please sign in to comment.