Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley committed Feb 3, 2024
1 parent f32cf14 commit 3088c86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
31 changes: 20 additions & 11 deletions R/gsdd-cf.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
#' beginning of winter.
#' It is the accumulated thermal units (in C)
#' during the growing season based on the mean daily water temperature values.
#'
#' The GSDD is calculated across the longest consecutive sequence of non-missing
#' values which must be at least 180 elements in length otherwise a
#' missing value is returned.
#' If the time series includes missing values it is recommended that they are
#' replaced by estimates of the actual values using linear
#' interpolation (`[interpolate_numeric_vector()]`) or other predictive methods.
#' If the user removes the missing values then the returned GSDD value
#' will be less than the actual GSDD.
#'
#' Truncation occurs when the start and/or end
#' of the time series is part way through a growing season.
#'
#' Missing values (`NA`s) at the start and
#' end of the time series are trimmed prior to calculating GSDD.
#' If the time series still contains one or missing values or
#' is less than 90 values in length then a missing value is returned.
#' In this situation it is recommended that the user replace
#' the missing value(s) by interpolation or other methods.
#' If the user chooses to ignore truncation then the returned value
#' will be less than the actual GSDD.
#'
#' By default the growing season is based on the interpretation of
#' Coleman and Fausch (2007) who stated that
Expand Down Expand Up @@ -99,10 +104,14 @@ gsdd_cf <- function(x,
pick,
c("biggest", "smallest", "longest", "shortest", "first", "last", "all"))
chk_flag(quiet)

x <- trim_na(x)

if(length(x) < 90 || anyNA(x)) {

if(length(x) < 180) {
return(NA_real_)
}
if(anyNA(x)) {
x <- trim_na(x)
}
if(length(x) < 180 || anyNA(x)) {
return(NA_real_)
}
# create rolling mean vector from x and window width
Expand Down
35 changes: 19 additions & 16 deletions man/gsdd_cf.Rd

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

12 changes: 6 additions & 6 deletions tests/testthat/test-gsdd-cf.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,17 @@ test_that("Gets with two weeks and 3 day window and smaller", {
})

test_that("Gets one week with end day after of 0", {
x <- c(rep(0, 90), rep(5.1, 7), rep(1, 0))
x <- c(rep(0, 180), rep(5.1, 7), rep(1, 0))
expect_equal(gsdd_cf(x, ignore_truncation = "end", quiet = TRUE), 5.1 * 7)
})

test_that("Gets one week with end day after of 1", {
x <- c(rep(0, 90), rep(5.1, 7), rep(1, 1))
x <- c(rep(0, 180), rep(5.1, 7), rep(1, 1))
expect_equal(gsdd_cf(x, ignore_truncation = "end", quiet = TRUE), 5.1 * 7 + 1)
})

test_that("Gets with two weeks and 3 day window and smaller", {
x <- c(rep(0, 90), rep(5.1, 7))
x <- c(rep(0, 180), rep(5.1, 7))
expect_equal(gsdd_cf(x, ignore_truncation = "end", quiet = TRUE), 5.1 * 7)
})

Expand Down Expand Up @@ -324,9 +324,9 @@ test_that("Left truncated triangle", {
expect_equal(gsdd_cf(x, ignore_truncation = "start", quiet = TRUE), sum(x[0:25]))
})

test_that("NA if less than 90 values after trimming trailing NAs", {
x <- c(rep(1,74), rep(NA,100))
test_that("NA if less than 180 values after trimming trailing NAs", {
x <- c(rep(1,179), rep(NA,100))
expect_identical(gsdd_cf(x),NA_real_)
x <- c(rep(1,90), rep(NA,100))
x <- c(rep(1,180), rep(NA,100))
expect_identical(gsdd_cf(x),0)
})

0 comments on commit 3088c86

Please sign in to comment.