diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index c38f862..d9b4b1f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -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' diff --git a/.github/workflows/build-and-push-image.yaml b/.github/workflows/build-and-push-image.yaml deleted file mode 100644 index 0d19292..0000000 --- a/.github/workflows/build-and-push-image.yaml +++ /dev/null @@ -1,48 +0,0 @@ -name: Create and publish a Docker image - -on: - push: - branches: ['release', 'main'] - -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} - -jobs: - build-and-push-image: - runs-on: ubuntu-latest - permissions: - contents: read - packages: write - - steps: - - - name: Cancel previous builds if present - uses: n1hility/cancel-previous-runs@v3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Log in to the Container registry - uses: docker/login-action@v3 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - - - name: Build and push Docker image - uses: docker/build-push-action@v6 - with: - context: . - file: ./.devcontainer/Dockerfile - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/check-cmdstan.yaml b/.github/workflows/check-cmdstan.yaml index 1cd4caf..c9da90e 100644 --- a/.github/workflows/check-cmdstan.yaml +++ b/.github/workflows/check-cmdstan.yaml @@ -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 diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index ff2b58b..1ff45d0 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -4,6 +4,10 @@ on: push: branches: [main] workflow_dispatch: + pull_request: + branches: + - main + name: pkgdown diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 56e5f5c..9a4586c 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -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: diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 8914368..38e4573 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -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, @@ -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) } diff --git a/tests/testthat/test-stan-expgrowth.R b/tests/testthat/test-stan-expgrowth.R new file mode 100644 index 0000000..5a0777a --- /dev/null +++ b/tests/testthat/test-stan-expgrowth.R @@ -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) +})