From f4fb5c6c275f62a21fb3c8f5e6c061be2078071e Mon Sep 17 00:00:00 2001 From: Brian Ward Date: Tue, 27 Oct 2026 15:38:28 -0400 Subject: [PATCH] Add write_transformed_data_json to model_base --- src/stan/model/model_base.hpp | 9 +++++++++ src/test/unit/io/random_var_context_test.cpp | 3 +++ src/test/unit/model/model_base_crtp_test.cpp | 2 ++ src/test/unit/model/model_base_test.cpp | 4 +++- src/test/unit/services/util/initialize_test.cpp | 2 ++ src/test/unit/services/util/mcmc_writer_test.cpp | 2 ++ 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/stan/model/model_base.hpp b/src/stan/model/model_base.hpp index ad92415330d..79b0b7ba802 100644 --- a/src/stan/model/model_base.hpp +++ b/src/stan/model/model_base.hpp @@ -179,6 +179,15 @@ class model_base : public prob_grad { bool include_tparams = true, bool include_gqs = true) const = 0; + /** + * Writes a JSON string containing the transformed data computed by the model + * at construction. + * The format of the string is the same as the expected input format for the + * `stan::json` input methods, i.e. complex numbers are rendered as a 2-list, + * tuples as objects with integer keys counting from 1 + */ + virtual void write_transformed_data_json(std::ostream* output) const = 0; + /** * Return the log density for the specified unconstrained * parameters, without Jacobian and with normalizing constants for diff --git a/src/test/unit/io/random_var_context_test.cpp b/src/test/unit/io/random_var_context_test.cpp index 64ea370f17a..e6c8b5c7b85 100644 --- a/src/test/unit/io/random_var_context_test.cpp +++ b/src/test/unit/io/random_var_context_test.cpp @@ -71,6 +71,9 @@ class mock_throwing_model_in_write_array : public stan::model::prob_grad { param_names__.push_back(param_name.str()); } } + + void write_transformed_data_json(std::ostream* out) const { *out << "{}"; } + template void write_array(RNG& base_rng__, std::vector& params_r__, std::vector& params_i__, std::vector& vars__, diff --git a/src/test/unit/model/model_base_crtp_test.cpp b/src/test/unit/model/model_base_crtp_test.cpp index 6db95fa208c..4a33497a826 100644 --- a/src/test/unit/model/model_base_crtp_test.cpp +++ b/src/test/unit/model/model_base_crtp_test.cpp @@ -34,6 +34,8 @@ struct mock_model : public stan::model::model_base_crtp { bool include_tparams, bool include_gqs) const override {} + void write_transformed_data_json(std::ostream* out) const { *out << "{}"; } + template T log_prob(Eigen::Matrix& params_r, std::ostream* msgs) const { if constexpr (std::is_same::value) { diff --git a/src/test/unit/model/model_base_test.cpp b/src/test/unit/model/model_base_test.cpp index c4a81f71323..4a2d1f7eef7 100644 --- a/src/test/unit/model/model_base_test.cpp +++ b/src/test/unit/model/model_base_test.cpp @@ -23,7 +23,9 @@ struct mock_model : public stan::model::model_base { bool include_gqs) const override {} void get_dims(std::vector >& dimss, bool include_tparams, bool include_gqs) const override {} - + void write_transformed_data_json(std::ostream* out) const override { + *out << "{}"; + } void constrained_param_names(std::vector& param_names, bool include_tparams, bool include_gqs) const override {} diff --git a/src/test/unit/services/util/initialize_test.cpp b/src/test/unit/services/util/initialize_test.cpp index f39a2433bb3..861d6ef824c 100644 --- a/src/test/unit/services/util/initialize_test.cpp +++ b/src/test/unit/services/util/initialize_test.cpp @@ -234,6 +234,8 @@ class mock_throwing_model : public stan::model::prob_grad { param_names__.push_back(param_name.str()); } } + void write_transformed_data_json(std::ostream* out) const { *out << "{}"; } + template void write_array(RNG& base_rng__, std::vector& params_r__, std::vector& params_i__, std::vector& vars__, diff --git a/src/test/unit/services/util/mcmc_writer_test.cpp b/src/test/unit/services/util/mcmc_writer_test.cpp index c9840a68806..04538f97f9d 100644 --- a/src/test/unit/services/util/mcmc_writer_test.cpp +++ b/src/test/unit/services/util/mcmc_writer_test.cpp @@ -129,6 +129,8 @@ class throwing_model : public stan::model::model_base_crtp { } // unconstrained_param_names() + void write_transformed_data_json(std::ostream* out) const { *out << "{}"; } + // Begin method overload boilerplate template void write_array(RNG& base_rng,