From 2a5544294c59f657a935d42722e9bff8271a1901 Mon Sep 17 00:00:00 2001 From: Sam Abbott Date: Sat, 5 Oct 2024 13:13:16 +0100 Subject: [PATCH] Issue 70: Restructure stan code (#123) * move the location of the stan files * add a news item' * update docs path * update publish path * update upload path --- .Rbuildignore | 3 +- .github/workflows/render-stan-docs.yaml | 6 +- .gitignore | 1 + NEWS.md | 1 + R/pcd-stan-tools.R | 2 +- R/pcd_cmdstan_model.R | 2 +- inst/stan/{ => functions}/Doxyfile | 0 inst/stan/{ => functions}/expgrowth.stan | 0 .../primary_censored_dist.stan | 0 .../primary_censored_dist_analytical_cdf.stan | 0 inst/{ => stan}/pcens_model.stan | 0 tests/testthat/test-pcd-stan-tools.R | 2 +- tests/testthat/test-pcd_as_cmdstan_data.R | 68 ++++++++++--------- 13 files changed, 47 insertions(+), 38 deletions(-) rename inst/stan/{ => functions}/Doxyfile (100%) rename inst/stan/{ => functions}/expgrowth.stan (100%) rename inst/stan/{ => functions}/primary_censored_dist.stan (100%) rename inst/stan/{ => functions}/primary_censored_dist_analytical_cdf.stan (100%) rename inst/{ => stan}/pcens_model.stan (100%) diff --git a/.Rbuildignore b/.Rbuildignore index 40321ac..565b2dd 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -25,4 +25,5 @@ ^vignettes/using-stan-tools\.Rmd$ ^vignettes/fitting-dists-with-stan\.Rmd$ ^inst/pcens_model$ -^touchstone$ \ No newline at end of file +^inst/stan/pcens_model$ +^touchstone$ diff --git a/.github/workflows/render-stan-docs.yaml b/.github/workflows/render-stan-docs.yaml index e2e05e5..7d37d92 100644 --- a/.github/workflows/render-stan-docs.yaml +++ b/.github/workflows/render-stan-docs.yaml @@ -28,14 +28,14 @@ jobs: # Path to Doxyfile doxyfile-path: "Doxyfile" # Working directory - working-directory: "./inst/stan/" + working-directory: "./inst/stan/functions/" - name: Archive documentation if: github.ref != 'refs/heads/main' uses: actions/upload-artifact@v4 with: name: stan-docs - path: ./inst/stan/html + path: ./inst/stan/functions/html if-no-files-found: error - name: Deploy @@ -45,5 +45,5 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} # Default Doxyfile build documentation to html directory. # Change the directory if changes in Doxyfile - publish_dir: ./inst/stan/html + publish_dir: ./inst/stan/functions/html destination_dir: stan diff --git a/.gitignore b/.gitignore index fa2ccbc..047b747 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ cache *.pdf *.html inst/pcens_model +inst/stan/pcens_model diff --git a/NEWS.md b/NEWS.md index 0c93319..8f3d84d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,7 @@ This is the development version of `primarycensoreddist` and is not yet ready fo * Removed the need to assign functions to the global environment for `fitdistdoublecens()` by using `withr`. * Added a `check_truncation()` function to check if the truncation time is larger than the maximum observed delay. This is used in `fitdistdoublecens()` and `pcd_as_stan_data()` to ensure that the truncation time is appropriate to maximise computational efficiency. * `pcd_as_cmdstan_data()` has been renamed to `pcd_as_stan_data()` to better reflect that it is used for `Stan` models in general rather than just the `CmdStan` models. +* The stan code has been refactored into a folder of functions within the current `stan` folder and the `stan` model has been moved into the `stan` folder. All paths to the stan code have been updated to reflect this. ## Documentation diff --git a/R/pcd-stan-tools.R b/R/pcd-stan-tools.R index 996f10c..97b39c2 100644 --- a/R/pcd-stan-tools.R +++ b/R/pcd-stan-tools.R @@ -6,7 +6,7 @@ #' #' @export pcd_stan_path <- function() { - system.file("stan", package = "primarycensoreddist") + system.file("stan", "functions", package = "primarycensoreddist") } #' Count the number of unmatched braces in a line diff --git a/R/pcd_cmdstan_model.R b/R/pcd_cmdstan_model.R index 05906b1..524f93f 100644 --- a/R/pcd_cmdstan_model.R +++ b/R/pcd_cmdstan_model.R @@ -37,7 +37,7 @@ pcd_cmdstan_model <- function( } pcd_stan_model <- system.file( - "pcens_model.stan", + "stan", "pcens_model.stan", package = "primarycensoreddist" ) diff --git a/inst/stan/Doxyfile b/inst/stan/functions/Doxyfile similarity index 100% rename from inst/stan/Doxyfile rename to inst/stan/functions/Doxyfile diff --git a/inst/stan/expgrowth.stan b/inst/stan/functions/expgrowth.stan similarity index 100% rename from inst/stan/expgrowth.stan rename to inst/stan/functions/expgrowth.stan diff --git a/inst/stan/primary_censored_dist.stan b/inst/stan/functions/primary_censored_dist.stan similarity index 100% rename from inst/stan/primary_censored_dist.stan rename to inst/stan/functions/primary_censored_dist.stan diff --git a/inst/stan/primary_censored_dist_analytical_cdf.stan b/inst/stan/functions/primary_censored_dist_analytical_cdf.stan similarity index 100% rename from inst/stan/primary_censored_dist_analytical_cdf.stan rename to inst/stan/functions/primary_censored_dist_analytical_cdf.stan diff --git a/inst/pcens_model.stan b/inst/stan/pcens_model.stan similarity index 100% rename from inst/pcens_model.stan rename to inst/stan/pcens_model.stan diff --git a/tests/testthat/test-pcd-stan-tools.R b/tests/testthat/test-pcd-stan-tools.R index 825e801..ded5a2d 100644 --- a/tests/testthat/test-pcd-stan-tools.R +++ b/tests/testthat/test-pcd-stan-tools.R @@ -1,7 +1,7 @@ test_that("pcd_stan_path returns correct path", { path <- pcd_stan_path() expect_true(file.exists(path)) - expect_true(grepl("stan$", path)) # nolint + expect_true(grepl(file.path("stan", "functions$"), path)) expect_true(dir.exists(path)) }) diff --git a/tests/testthat/test-pcd_as_cmdstan_data.R b/tests/testthat/test-pcd_as_cmdstan_data.R index a086177..c52f603 100644 --- a/tests/testthat/test-pcd_as_cmdstan_data.R +++ b/tests/testthat/test-pcd_as_cmdstan_data.R @@ -14,14 +14,16 @@ test_that("pcd_as_stan_data correctly formats data", { priors <- list(location = c(1, 1), scale = c(1, 1)) primary_priors <- list(location = numeric(0), scale = numeric(0)) - result <- pcd_as_stan_data( - data, - dist_id = dist_id, - primary_dist_id = primary_dist_id, - param_bounds = param_bounds, - primary_param_bounds = primary_param_bounds, - priors = priors, - primary_priors = primary_priors + expect_message( + result <- pcd_as_stan_data( # nolint + data, + dist_id = dist_id, + primary_dist_id = primary_dist_id, + param_bounds = param_bounds, + primary_param_bounds = primary_param_bounds, + priors = priors, + primary_priors = primary_priors + ) ) expect_type(result, "list") @@ -81,16 +83,18 @@ test_that("pcd_as_stan_data handles optional parameters correctly", { relative_obs_time = c(10, 10, 10) ) - result <- pcd_as_stan_data( - data, - dist_id = 1, - primary_dist_id = 1, - param_bounds = list(lower = c(0, 0), upper = c(10, 10)), - primary_param_bounds = list(lower = numeric(0), upper = numeric(0)), - priors = list(location = c(1, 1), scale = c(1, 1)), - primary_priors = list(location = numeric(0), scale = numeric(0)), - compute_log_lik = TRUE, - use_reduce_sum = TRUE + expect_message( + result <- pcd_as_stan_data( # nolint + data, + dist_id = 1, + primary_dist_id = 1, + param_bounds = list(lower = c(0, 0), upper = c(10, 10)), + primary_param_bounds = list(lower = numeric(0), upper = numeric(0)), + priors = list(location = c(1, 1), scale = c(1, 1)), + primary_priors = list(location = numeric(0), scale = numeric(0)), + compute_log_lik = TRUE, + use_reduce_sum = TRUE + ) ) expect_identical(result$compute_log_lik, 1L) @@ -106,19 +110,21 @@ test_that("pcd_as_stan_data handles custom column names correctly", { obs_time = c(10, 10, 10) ) - result <- pcd_as_stan_data( - data, - delay = "obs_delay", - delay_upper = "obs_delay_upper", - n = "count", - pwindow = "primary_window", - relative_obs_time = "obs_time", - dist_id = 1, - primary_dist_id = 1, - param_bounds = list(lower = c(0, 0), upper = c(10, 10)), - primary_param_bounds = list(lower = numeric(0), upper = numeric(0)), - priors = list(location = c(1, 1), scale = c(1, 1)), - primary_priors = list(location = numeric(0), scale = numeric(0)) + expect_message( + result <- pcd_as_stan_data( # nolint + data, + delay = "obs_delay", + delay_upper = "obs_delay_upper", + n = "count", + pwindow = "primary_window", + relative_obs_time = "obs_time", + dist_id = 1, + primary_dist_id = 1, + param_bounds = list(lower = c(0, 0), upper = c(10, 10)), + primary_param_bounds = list(lower = numeric(0), upper = numeric(0)), + priors = list(location = c(1, 1), scale = c(1, 1)), + primary_priors = list(location = numeric(0), scale = numeric(0)) + ) ) expect_identical(result$d, data$obs_delay)