Skip to content

Commit

Permalink
overall ci and unit tests for expgrowth
Browse files Browse the repository at this point in the history
  • Loading branch information
seabbs committed Aug 29, 2024
1 parent 481512e commit 36eba28
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 53 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
needs: check

- name: Install cmdstan
if: matrix.config.os == 'ubuntu-latest'
uses: epinowcast/actions/install-cmdstan@v1
with:
cmdstan-version: 'latest'
Expand Down
48 changes: 0 additions & 48 deletions .github/workflows/build-and-push-image.yaml

This file was deleted.

9 changes: 7 additions & 2 deletions .github/workflows/check-cmdstan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ jobs:

- name: Compile model and check syntax
run: |
model <- epinowcast::enw_model()
model <- primarycensoreddist::pcd_load_stan_functions(
wrap_in_block = TRUE,
write_to_file = TRUE,
output_file = file.path(tempdir(), "pcd_stan_functions.stan")
)
model <- cmdstanr::cmdstan_model(model)
# If the model is not syntactically correct above will fail
# however it may be correct enougth to compile but still contain
# soft depreciated syntax and so we check the syntax again below
# and test the output.
message <- capture.output(
model$check_syntax(pedantic = FALSE),
model$check_syntax(pedantic = TRUE),
type = "message"
)
# We can't use TRUE here as pendatic check return lots of false
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
push:
branches: [main]
workflow_dispatch:
pull_request:
branches:
- main


name: pkgdown

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master, develop]
branches: [main]
pull_request:
branches: [main, master, develop]
branches: [main]
workflow_dispatch:
merge_group:

Expand Down
5 changes: 4 additions & 1 deletion tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
options(datatable.print.class = FALSE)
options(datatable.print.keys = FALSE)

if (on_ci() && Sys.info()["sysname"] == "Linux" && not_on_cran()) {
if (
!on_ci() || (on_ci() && Sys.info()["sysname"] == "Linux" && not_on_cran())
) {
library(cmdstanr)
stan_functions <- pcd_load_stan_functions(
wrap_in_block = TRUE,
Expand All @@ -12,4 +14,5 @@ if (on_ci() && Sys.info()["sysname"] == "Linux" && not_on_cran()) {
model <- suppressMessages(suppressWarnings(cmdstanr::cmdstan_model(
file.path(tempdir(), "pcd_stan_functions.stan")
)))
model$expose_functions(global = TRUE)
}
67 changes: 67 additions & 0 deletions tests/testthat/test-stan-expgrowth.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
skip_on_cran()
skip_on_os("windows")
skip_on_os("mac")

test_that("Stan expgrowth_pdf matches R dexpgrowth", {
x <- seq(0, 1, by = 0.1)
min <- 0
max <- 1
r <- 0.5

stan_pdf <- sapply(x, expgrowth_pdf, min, max, r)
r_pdf <- dexpgrowth(x, min, max, r)

expect_equal(stan_pdf, r_pdf, tolerance = 1e-4)
})

test_that("Stan expgrowth_lpdf matches R dexpgrowth with log = TRUE", {
x <- seq(0, 1, by = 0.1)
min <- 0
max <- 1
r <- 0.5

stan_lpdf <- sapply(x, expgrowth_lpdf, min, max, r)
r_lpdf <- dexpgrowth(x, min, max, r, log = TRUE)

expect_equal(stan_lpdf, r_lpdf, tolerance = 1e-4)
})

test_that("Stan expgrowth_cdf matches R pexpgrowth", {
x <- seq(0, 1, by = 0.1)
min <- 0
max <- 1
r <- 0.5

stan_cdf <- sapply(x, expgrowth_cdf, min, max, r)
r_cdf <- pexpgrowth(x, min, max, r)

expect_equal(stan_cdf, r_cdf, tolerance = 1e-4)
})

test_that("Stan expgrowth_lcdf matches R pexpgrowth with log.p = TRUE", {
x <- seq(0, 1, by = 0.1)
min <- 0
max <- 1
r <- 0.5

stan_lcdf <- sapply(x, expgrowth_lcdf, min, max, r)
r_lcdf <- pexpgrowth(x, min, max, r, log.p = TRUE)

expect_equal(stan_lcdf, r_lcdf, tolerance = 1e-6)
})

test_that("Stan expgrowth_rng matches distribution of R rexpgrowth", {
n <- 10000
min <- 0
max <- 1
r <- 0.5

set.seed(123)
stan_samples <- replicate(n, expgrowth_rng(min, max, r))
set.seed(123)
r_samples <- rexpgrowth(n, min, max, r)

expect_equal(mean(stan_samples), mean(r_samples), tolerance = 1e-2)
expect_equal(sd(stan_samples), sd(r_samples), tolerance = 1e-2)
expect_equal(quantile(stan_samples), quantile(r_samples), tolerance = 1e-2)
})

0 comments on commit 36eba28

Please sign in to comment.