Skip to content

Commit

Permalink
Issue 70: Restructure stan code (#123)
Browse files Browse the repository at this point in the history
* move the location of the stan files

* add a news item'

* update docs path

* update publish path

* update upload path
  • Loading branch information
seabbs authored Oct 5, 2024
1 parent 14f259a commit 2a55442
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
^vignettes/using-stan-tools\.Rmd$
^vignettes/fitting-dists-with-stan\.Rmd$
^inst/pcens_model$
^touchstone$
^inst/stan/pcens_model$
^touchstone$
6 changes: 3 additions & 3 deletions .github/workflows/render-stan-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ cache
*.pdf
*.html
inst/pcens_model
inst/stan/pcens_model
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion R/pcd-stan-tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion R/pcd_cmdstan_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pcd_cmdstan_model <- function(
}

pcd_stan_model <- system.file(
"pcens_model.stan",
"stan", "pcens_model.stan",
package = "primarycensoreddist"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/testthat/test-pcd-stan-tools.R
Original file line number Diff line number Diff line change
@@ -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))
})

Expand Down
68 changes: 37 additions & 31 deletions tests/testthat/test-pcd_as_cmdstan_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 2a55442

Please sign in to comment.