Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
macartan committed Apr 18, 2024
1 parent 0687bc6 commit c08ff17
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 9 additions & 0 deletions R/grab.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#' \item \code{"posterior_distribution"} a data frame of the parameter posterior distribution,
#' \item \code{"posterior_event_probabilities"} a sample of data (event) probabilities from the posterior,
#' \item \code{"stan_objects"} stan_objects is a list of Stan outputs that can include the stanfit object, the data that was used, and distributions over causal types and event probabilities.
#' \item \code{"data"} the data that was provided to update the model,
#' \item \code{"stan_fit"} the stanfit object generated by Stan,
#' \item \code{"stan_summary"} a summary of the stanfit object generated by Stan,
#' \item \code{"type_prior"} a matrix of type probabilities using priors,
Expand Down Expand Up @@ -68,6 +69,7 @@
#' grab(model, object = "posterior_distribution")
#' grab(model, object = "posterior_event_probabilities")
#' grab(model, object = "stan_objects")
#' grab(model, object = "data")
#' grab(model, object = "stan_fit")
#' grab(model, object = "stan_summary")
#' grab(model, object = "type_prior")
Expand Down Expand Up @@ -122,6 +124,13 @@ grab <- function(model, object = NULL, ...) {
} else {
model$stan_objects
},
data =
if (is.null(model$stan_objects)) {
stop("Model does not contain stan_objects; update model")
} else {
if (is.null(model$stan_objects$data)) print("No data provided")
model$stan_objects$data
},
stan_fit =
if (is.null(model$stan_objects$stanfit)) {
stop("Model does not contain stanfit; update model and set keep_fit = TRUE")
Expand Down
2 changes: 2 additions & 0 deletions man/grab.Rd

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

9 changes: 6 additions & 3 deletions tests/testthat/test_grab.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ testthat::test_that(
"posterior_distribution",
"posterior_event_probabilities",
"stan_objects",
"data",
"stan_fit",
"stan_summary",
"type_prior",
Expand Down Expand Up @@ -56,6 +57,7 @@ testthat::test_that(
"parameters_posterior",
"posterior_event_probabilities",
"stan_objects",
"data.frame",
"stanfit",
"stan_summary",
"type_prior",
Expand All @@ -65,7 +67,7 @@ testthat::test_that(

model <- make_model("X->Y") |>
set_prior_distribution() |>
update_model(keep_event_probabilities = TRUE, keep_fit = TRUE)
update_model(data.frame(X=1), keep_event_probabilities = TRUE, keep_fit = TRUE)

for(j in 1:length(args)){
print(paste(j, args[j]))
Expand All @@ -90,6 +92,9 @@ testthat::test_that(
expect_error(model |> grab("stan_fit"))
expect_error(model |> grab("stan_summary"))

expect_true(make_model("X->Y") |>
update_model() |> grab("data") |> is.null())

# Print methods
out <- capture.output(print(grab(model, object = "nodes")))
expect_true(any(grepl("Nodes:", out)))
Expand All @@ -102,8 +107,6 @@ testthat::test_that(
expect_true(any(grepl("first 10 rows:", out)))
}



)


Expand Down

0 comments on commit c08ff17

Please sign in to comment.