From badc4a0f8233d828ea4ac56ecf66dc5e56c88398 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 31 Jul 2023 00:43:40 +0300 Subject: [PATCH 1/2] Fix init_model_methods for models with no data --- R/utils.R | 3 ++- inst/include/model_methods.cpp | 6 ++++++ tests/testthat/test-model-methods.R | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 7e732c0a5..bf65b513d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -759,7 +759,8 @@ expose_model_methods <- function(env, verbose = FALSE, hessian = FALSE) { } initialize_model_pointer <- function(env, data, seed = 0) { - ptr_and_rng <- env$model_ptr(data, seed) + datafile_path <- ifelse(is.null(data), "", data) + ptr_and_rng <- env$model_ptr(datafile_path, seed) env$model_ptr_ <- ptr_and_rng$model_ptr env$model_rng_ <- ptr_and_rng$base_rng env$num_upars_ <- env$get_num_upars(env$model_ptr_) diff --git a/inst/include/model_methods.cpp b/inst/include/model_methods.cpp index 65c673d22..196d7db68 100644 --- a/inst/include/model_methods.cpp +++ b/inst/include/model_methods.cpp @@ -6,9 +6,15 @@ #include #else #include +#include #endif std::shared_ptr var_context(std::string file_path) { + if (file_path == "") { + stan::io::empty_var_context empty_context; + return std::make_shared(empty_context); + } + std::fstream stream(file_path.c_str(), std::fstream::in); #ifdef CMDSTAN_JSON using json_data_t = cmdstan::json::json_data; diff --git a/tests/testthat/test-model-methods.R b/tests/testthat/test-model-methods.R index 31a99ada2..c997087da 100644 --- a/tests/testthat/test-model-methods.R +++ b/tests/testthat/test-model-methods.R @@ -268,3 +268,10 @@ test_that("unconstrain_draws returns correct values", { unconstrained_draws <- fit$unconstrain_draws(draws = fit$draws())[[1]] expect_equal(as.numeric(x_draws), exp(as.numeric(unconstrained_draws))) }) + +test_that("Model methods can be initialised for models with no data", { + stan_file <- write_stan_file("parameters { real x; } model { x ~ std_normal(); }") + mod <- cmdstan_model(stan_file, compile_model_methods = TRUE, force_recompile = TRUE) + expect_no_error(fit <- mod$sample()) + expect_equal(fit$log_prob(5), -12.5) +}) From 99bfe0fa8feb51d32e74baa6562b20df43a041ad Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 31 Jul 2023 02:36:42 +0300 Subject: [PATCH 2/2] Missed WSL test skip --- tests/testthat/test-model-methods.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-model-methods.R b/tests/testthat/test-model-methods.R index c997087da..8352a04f3 100644 --- a/tests/testthat/test-model-methods.R +++ b/tests/testthat/test-model-methods.R @@ -270,6 +270,8 @@ test_that("unconstrain_draws returns correct values", { }) test_that("Model methods can be initialised for models with no data", { + skip_if(os_is_wsl()) + stan_file <- write_stan_file("parameters { real x; } model { x ~ std_normal(); }") mod <- cmdstan_model(stan_file, compile_model_methods = TRUE, force_recompile = TRUE) expect_no_error(fit <- mod$sample())