From 782321df767b0b6980ce082450cb81907c193ef4 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 14:12:53 -0400 Subject: [PATCH 01/38] replace boost::lexical_cast --- src/stan/callbacks/writer.hpp | 1 - src/stan/io/dump.hpp | 15 +++++++-------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 2ee3a7c1727..62b6604eeb4 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -2,7 +2,6 @@ #define STAN_CALLBACKS_WRITER_HPP #include -#include #include #include diff --git a/src/stan/io/dump.hpp b/src/stan/io/dump.hpp index 2e81772f992..4170c7c50fa 100644 --- a/src/stan/io/dump.hpp +++ b/src/stan/io/dump.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -242,8 +241,8 @@ class dump_reader { scan_optional_long(); size_t d = 0; try { - d = boost::lexical_cast(buf_); - } catch (const boost::bad_lexical_cast& exc) { + d = std::stoull(buf_); + } catch (const std::exception& exc) { std::string msg = "value " + buf_ + " beyond array dimension range"; throw std::invalid_argument(msg); } @@ -269,8 +268,8 @@ class dump_reader { int get_int() { int n = 0; try { - n = boost::lexical_cast(buf_); - } catch (const boost::bad_lexical_cast& exc) { + n = std::stol(buf_); + } catch (const std::exception& exc) { std::string msg = "value " + buf_ + " beyond int range"; throw std::invalid_argument(msg); } @@ -280,17 +279,17 @@ class dump_reader { double scan_double() { double x = 0; try { - x = boost::lexical_cast(buf_); + x = std::stod(buf_); if (x == 0) validate_zero_buf(buf_); - } catch (const boost::bad_lexical_cast& exc) { + } catch (const std::exception& exc) { std::string msg = "value " + buf_ + " beyond numeric range"; throw std::invalid_argument(msg); } return x; } - // scan number stores number or throws bad lexical cast exception + // scan number stores number or throws exception void scan_number(bool negate_val) { // must take longest first! if (scan_chars("Inf")) { From bc7db02e5a243bc1971af9e743603c7f606d726c Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 15:19:13 -0400 Subject: [PATCH 02/38] get rid of boost::lexical_cast, add optional newline to json_writer end_record --- src/stan/callbacks/json_writer.hpp | 5 ++++- src/stan/io/validate_zero_buf.hpp | 4 ++-- src/stan/mcmc/hmc/hamiltonians/ps_point.hpp | 1 - src/stan/variational/advi.hpp | 1 - src/test/unit/callbacks/json_writer_test.cpp | 6 ++++++ src/test/unit/io/validate_zero_buf_test.cpp | 4 ++-- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 7f8d254e673..37c8ce61574 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -284,10 +284,13 @@ class json_writer { /** * Writes "}", final token of a JSON record. */ - void end_record() { + void end_record(bool add_newline=false) { if (output_ == nullptr) return; *output_ << "}"; + if (add_newline) + *output_ << std::endl; + record_depth_--; if (record_depth_ > 0) { record_needs_comma_ = true; diff --git a/src/stan/io/validate_zero_buf.hpp b/src/stan/io/validate_zero_buf.hpp index 03f3938a978..76632d534c7 100644 --- a/src/stan/io/validate_zero_buf.hpp +++ b/src/stan/io/validate_zero_buf.hpp @@ -14,7 +14,7 @@ namespace io { * operator[](size_t). * * @tparam B Character buffer type - * @throw boost::bad_lexical_cast if the buffer + * @throw std::bad_cast if the buffer * contains non-zero characters before an exponentiation symbol. */ template @@ -23,7 +23,7 @@ void validate_zero_buf(const B& buf) { if (buf[i] == 'e' || buf[i] == 'E') return; if (buf[i] >= '1' && buf[i] <= '9') - boost::conversion::detail::throw_bad_cast(); + throw std::bad_cast(); } } diff --git a/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp b/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp index 54060d29ece..b66cbc6c12e 100644 --- a/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp +++ b/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp @@ -3,7 +3,6 @@ #include #include -#include #include #include diff --git a/src/stan/variational/advi.hpp b/src/stan/variational/advi.hpp index 4dca19d0335..87391382f03 100644 --- a/src/stan/variational/advi.hpp +++ b/src/stan/variational/advi.hpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 2d2ec831d2c..2615b128ce3 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -28,6 +28,12 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end) { EXPECT_EQ("{}", ss.str()); } +TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end_newline) { + writer.begin_record(); + writer.end_record(true); + EXPECT_EQ("{}\n", ss.str()); +} + TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { std::string key("key"); const int N = 5; diff --git a/src/test/unit/io/validate_zero_buf_test.cpp b/src/test/unit/io/validate_zero_buf_test.cpp index 1ab60f0473e..46eea094e39 100644 --- a/src/test/unit/io/validate_zero_buf_test.cpp +++ b/src/test/unit/io/validate_zero_buf_test.cpp @@ -15,8 +15,8 @@ TEST(ioValidateZeroBuf, tester) { EXPECT_NO_THROW(validate_zero_buf(s)); s = "1.0"; - EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast); + EXPECT_THROW(validate_zero_buf(s), std::bad_cast); s = "1e1"; - EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast); + EXPECT_THROW(validate_zero_buf(s), std::bad_cast); } From cf4d0f255d0da5904c16a1d88941a128c535c52c Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 15:53:44 -0400 Subject: [PATCH 03/38] add optional newline to json_writer begin_record --- src/stan/callbacks/json_writer.hpp | 16 +++++++++++----- src/test/unit/callbacks/json_writer_test.cpp | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 37c8ce61574..e85176859f6 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -260,11 +260,13 @@ class json_writer { /** * Writes "{", initial token of a JSON record. */ - void begin_record() { + void begin_record(bool newline_after=false) { if (output_ == nullptr) return; write_record_comma_if_needed(); *output_ << "{"; + if (newline_after) + *output_ << std::endl; record_needs_comma_ = false; record_depth_++; } @@ -272,23 +274,27 @@ class json_writer { /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. + * @param[in] newline_before if true, add newline before open token. */ - void begin_record(const std::string& key) { + void begin_record(const std::string& key, bool newline_before=false) { if (output_ == nullptr) return; write_record_comma_if_needed(); - *output_ << "\"" << key << "\": {"; + *output_ << "\"" << key << "\":"; + newline_before ? *output_ << std::endl : *output_ << " "; + *output_ << "{"; record_needs_comma_ = false; record_depth_++; } /** * Writes "}", final token of a JSON record. + * @param[in] newline_after if true, add newline at end. */ - void end_record(bool add_newline=false) { + void end_record(bool newline_after=false) { if (output_ == nullptr) return; *output_ << "}"; - if (add_newline) + if (newline_after) *output_ << std::endl; record_depth_--; diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 2615b128ce3..034e24b6287 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -33,6 +33,11 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end_newline) { writer.end_record(true); EXPECT_EQ("{}\n", ss.str()); } +TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_newline_end_newline) { + writer.begin_record(true); + writer.end_record(true); + EXPECT_EQ("{\n}\n", ss.str()); +} TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { std::string key("key"); @@ -67,6 +72,17 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member) { EXPECT_EQ("{\"key\" : \"value\"}", ss.str()); } +TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { + std::string key("key"); + std::string value("value"); + writer.begin_record(true); + writer.begin_record("child", true); + writer.write(key, value); + writer.end_record(); + writer.end_record(true); + EXPECT_EQ("{\n\"child\":\n{\"key\" : \"value\"}}\n", ss.str()); +} + TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { std::string key("key"); std::string value("value"); From dc3e4e71814f036a5d9752d97066b970a6ca1e65 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 16:24:48 -0400 Subject: [PATCH 04/38] checkpointing, passes unit tests --- src/stan/callbacks/json_writer.hpp | 17 +++++++++-------- src/stan/callbacks/structured_writer.hpp | 9 ++++++--- src/stan/services/pathfinder/single.hpp | 16 ++++++++-------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index e85176859f6..d6cecfd2e6a 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -259,13 +259,14 @@ class json_writer { /** * Writes "{", initial token of a JSON record. + * @param[in] newline (optional) if true, add newline after open token. */ - void begin_record(bool newline_after=false) { + void begin_record(bool newline=false) { if (output_ == nullptr) return; write_record_comma_if_needed(); *output_ << "{"; - if (newline_after) + if (newline) *output_ << std::endl; record_needs_comma_ = false; record_depth_++; @@ -274,27 +275,27 @@ class json_writer { /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. - * @param[in] newline_before if true, add newline before open token. + * @param[in] newline (optional) if true, add newline between key, open token. */ - void begin_record(const std::string& key, bool newline_before=false) { + void begin_record(const std::string& key, bool newline=false) { if (output_ == nullptr) return; write_record_comma_if_needed(); *output_ << "\"" << key << "\":"; - newline_before ? *output_ << std::endl : *output_ << " "; + newline ? *output_ << std::endl : *output_ << " "; *output_ << "{"; record_needs_comma_ = false; record_depth_++; } /** * Writes "}", final token of a JSON record. - * @param[in] newline_after if true, add newline at end. + * @param[in] newline (optional) if true, add newline after close token. */ - void end_record(bool newline_after=false) { + void end_record(bool newline=false) { if (output_ == nullptr) return; *output_ << "}"; - if (newline_after) + if (newline) *output_ << std::endl; record_depth_--; diff --git a/src/stan/callbacks/structured_writer.hpp b/src/stan/callbacks/structured_writer.hpp index 3b03f8a80a1..3c6afa549c2 100644 --- a/src/stan/callbacks/structured_writer.hpp +++ b/src/stan/callbacks/structured_writer.hpp @@ -21,17 +21,20 @@ class structured_writer { virtual ~structured_writer() {} /** * Writes "{", initial token of a JSON record. + * @param[in] newline (optional) if true, add newline after open token. */ - virtual void begin_record() {} + virtual void begin_record(bool newline=false) {} /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. + * @param[in] newline (optional) if true, add newline between key, open token. */ - virtual void begin_record(const std::string&) {} + virtual void begin_record(const std::string&, bool newline=false) {} /** * Writes "}", final token of a JSON record. + * @param[in] newline (optional) if true, add newline after close token. */ - virtual void end_record() {} + virtual void end_record(bool newline=false) {} /** * Writes "[", initial token of a JSON list. */ diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 9e2b5705974..ff1c07cfe2f 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -636,12 +636,12 @@ inline auto pathfinder_lbfgs_single( Eigen::VectorXd prev_grads(num_parameters); stan::model::log_prob_grad(model, prev_params, prev_grads); if (unlikely(save_iterations)) { - diagnostic_writer.begin_record(); - diagnostic_writer.begin_record("0"); + diagnostic_writer.begin_record(true); + diagnostic_writer.begin_record("0", true); diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); - diagnostic_writer.end_record(); + diagnostic_writer.end_record(true); } auto constrain_fun = [&model](auto&& rng, auto&& unconstrained_draws, auto&& constrained_draws) { @@ -698,7 +698,7 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { - diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); + diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num()), true); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); @@ -714,7 +714,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("lbfgs_success", false); diagnostic_writer.write("pathfinder_success", false); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); - diagnostic_writer.end_record(); + diagnostic_writer.end_record(true); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -770,7 +770,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("alpha", pathfinder_res.second.alpha); diagnostic_writer.write("full", pathfinder_res.second.use_full); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); - diagnostic_writer.end_record(); + diagnostic_writer.end_record(true); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -790,7 +790,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("history_size", history_size); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.write("pathfinder_error", std::string(e.what())); - diagnostic_writer.end_record(); + diagnostic_writer.end_record(true); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -800,7 +800,7 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { - diagnostic_writer.end_record(); + diagnostic_writer.end_record(true); } if (unlikely(ret <= 0)) { std::string prefix_err_msg From 0c50ee1d2e147cd4a0afb8f0704870b5b7281149 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 17:19:15 -0400 Subject: [PATCH 05/38] checkpointing, passes unit tests --- src/stan/callbacks/json_writer.hpp | 18 ++++++++---------- src/test/unit/callbacks/json_writer_test.cpp | 6 +++--- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index d6cecfd2e6a..aea7dbfb4b2 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -43,10 +43,12 @@ class json_writer { /** * Writes a comma separator for the record's parent object if needed. */ - void write_record_comma_if_needed() { + void write_record_comma_if_needed(bool newline=false) { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ","; } + if (newline) + *output_ << std::endl; } /** @@ -259,15 +261,13 @@ class json_writer { /** * Writes "{", initial token of a JSON record. - * @param[in] newline (optional) if true, add newline after open token. + * @param[in] newline (optional) if true, add newline before open token. */ void begin_record(bool newline=false) { if (output_ == nullptr) return; - write_record_comma_if_needed(); + write_record_comma_if_needed(newline); *output_ << "{"; - if (newline) - *output_ << std::endl; record_needs_comma_ = false; record_depth_++; } @@ -275,15 +275,13 @@ class json_writer { /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. - * @param[in] newline (optional) if true, add newline between key, open token. + * @param[in] newline (optional) if true, add newline before open token. */ void begin_record(const std::string& key, bool newline=false) { if (output_ == nullptr) return; - write_record_comma_if_needed(); - *output_ << "\"" << key << "\":"; - newline ? *output_ << std::endl : *output_ << " "; - *output_ << "{"; + write_record_comma_if_needed(newline); + *output_ << "\"" << key << "\":{"; record_needs_comma_ = false; record_depth_++; } diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 034e24b6287..c8a6bd2c66a 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -36,7 +36,7 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end_newline) { TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_newline_end_newline) { writer.begin_record(true); writer.end_record(true); - EXPECT_EQ("{\n}\n", ss.str()); + EXPECT_EQ("\n{}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { @@ -75,12 +75,12 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member) { TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { std::string key("key"); std::string value("value"); - writer.begin_record(true); + writer.begin_record(); writer.begin_record("child", true); writer.write(key, value); writer.end_record(); writer.end_record(true); - EXPECT_EQ("{\n\"child\":\n{\"key\" : \"value\"}}\n", ss.str()); + EXPECT_EQ("{\n\"child\":{\"key\" : \"value\"}}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { From 326bc78c609885b1e333f5f70a89e379b66187a5 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 17:59:28 -0400 Subject: [PATCH 06/38] newlines tweaks --- src/stan/callbacks/json_writer.hpp | 14 ++++++++------ src/stan/services/pathfinder/single.hpp | 10 +++++----- src/test/unit/callbacks/json_writer_test.cpp | 11 +++++++---- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index aea7dbfb4b2..aa683efc200 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -281,7 +281,7 @@ class json_writer { if (output_ == nullptr) return; write_record_comma_if_needed(newline); - *output_ << "\"" << key << "\":{"; + *output_ << "\"" << key << "\" : {"; record_needs_comma_ = false; record_depth_++; } @@ -292,15 +292,17 @@ class json_writer { void end_record(bool newline=false) { if (output_ == nullptr) return; - *output_ << "}"; - if (newline) - *output_ << std::endl; - record_depth_--; if (record_depth_ > 0) { record_needs_comma_ = true; - } + } else { + if (newline) + *output_ << std::endl; + } record_element_needs_comma_ = false; + *output_ << "}"; + if (newline) + *output_ << std::endl; } /** diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index ff1c07cfe2f..ea039a99d6b 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -636,12 +636,12 @@ inline auto pathfinder_lbfgs_single( Eigen::VectorXd prev_grads(num_parameters); stan::model::log_prob_grad(model, prev_params, prev_grads); if (unlikely(save_iterations)) { - diagnostic_writer.begin_record(true); + diagnostic_writer.begin_record(); diagnostic_writer.begin_record("0", true); diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); - diagnostic_writer.end_record(true); + diagnostic_writer.end_record(); } auto constrain_fun = [&model](auto&& rng, auto&& unconstrained_draws, auto&& constrained_draws) { @@ -714,7 +714,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("lbfgs_success", false); diagnostic_writer.write("pathfinder_success", false); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); - diagnostic_writer.end_record(true); + diagnostic_writer.end_record(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -770,7 +770,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("alpha", pathfinder_res.second.alpha); diagnostic_writer.write("full", pathfinder_res.second.use_full); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); - diagnostic_writer.end_record(true); + diagnostic_writer.end_record(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -790,7 +790,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("history_size", history_size); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.write("pathfinder_error", std::string(e.what())); - diagnostic_writer.end_record(true); + diagnostic_writer.end_record(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index c8a6bd2c66a..b845433a9fe 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -31,12 +31,15 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end) { TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end_newline) { writer.begin_record(); writer.end_record(true); - EXPECT_EQ("{}\n", ss.str()); + EXPECT_EQ("{\n}\n", ss.str()); } -TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_newline_end_newline) { + +TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_newline_nested) { + writer.begin_record(true); writer.begin_record(true); writer.end_record(true); - EXPECT_EQ("\n{}\n", ss.str()); + writer.end_record(true); + EXPECT_EQ("\n{\n{}\n\n}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { @@ -80,7 +83,7 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { writer.write(key, value); writer.end_record(); writer.end_record(true); - EXPECT_EQ("{\n\"child\":{\"key\" : \"value\"}}\n", ss.str()); + EXPECT_EQ("{\n\"child\" : {\"key\" : \"value\"}\n}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { From 791933f862691bb6c18d676b1d45e2aa5bed6f26 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Fri, 30 Jun 2023 18:17:42 -0400 Subject: [PATCH 07/38] lint fix --- src/stan/callbacks/json_writer.hpp | 10 +++++----- src/stan/callbacks/structured_writer.hpp | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index aa683efc200..c96ec4270f3 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -43,7 +43,7 @@ class json_writer { /** * Writes a comma separator for the record's parent object if needed. */ - void write_record_comma_if_needed(bool newline=false) { + void write_record_comma_if_needed(bool newline = false) { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ","; } @@ -263,7 +263,7 @@ class json_writer { * Writes "{", initial token of a JSON record. * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(bool newline=false) { + void begin_record(bool newline = false) { if (output_ == nullptr) return; write_record_comma_if_needed(newline); @@ -277,7 +277,7 @@ class json_writer { * @param[in] key The name of the record. * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(const std::string& key, bool newline=false) { + void begin_record(const std::string& key, bool newline = false) { if (output_ == nullptr) return; write_record_comma_if_needed(newline); @@ -289,7 +289,7 @@ class json_writer { * Writes "}", final token of a JSON record. * @param[in] newline (optional) if true, add newline after close token. */ - void end_record(bool newline=false) { + void end_record(bool newline = false) { if (output_ == nullptr) return; record_depth_--; @@ -298,7 +298,7 @@ class json_writer { } else { if (newline) *output_ << std::endl; - } + } record_element_needs_comma_ = false; *output_ << "}"; if (newline) diff --git a/src/stan/callbacks/structured_writer.hpp b/src/stan/callbacks/structured_writer.hpp index 3c6afa549c2..85306a28197 100644 --- a/src/stan/callbacks/structured_writer.hpp +++ b/src/stan/callbacks/structured_writer.hpp @@ -23,18 +23,18 @@ class structured_writer { * Writes "{", initial token of a JSON record. * @param[in] newline (optional) if true, add newline after open token. */ - virtual void begin_record(bool newline=false) {} + virtual void begin_record(bool newline = false) {} /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. * @param[in] newline (optional) if true, add newline between key, open token. */ - virtual void begin_record(const std::string&, bool newline=false) {} + virtual void begin_record(const std::string&, bool newline = false) {} /** * Writes "}", final token of a JSON record. * @param[in] newline (optional) if true, add newline after close token. */ - virtual void end_record(bool newline=false) {} + virtual void end_record(bool newline = false) {} /** * Writes "[", initial token of a JSON list. */ From e6cc04971bef8a188cbd41fcf464799d3cae051a Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 30 Jun 2023 18:23:51 -0400 Subject: [PATCH 08/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/callbacks/json_writer.hpp | 10 +++++----- src/stan/callbacks/structured_writer.hpp | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index aa683efc200..c96ec4270f3 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -43,7 +43,7 @@ class json_writer { /** * Writes a comma separator for the record's parent object if needed. */ - void write_record_comma_if_needed(bool newline=false) { + void write_record_comma_if_needed(bool newline = false) { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ","; } @@ -263,7 +263,7 @@ class json_writer { * Writes "{", initial token of a JSON record. * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(bool newline=false) { + void begin_record(bool newline = false) { if (output_ == nullptr) return; write_record_comma_if_needed(newline); @@ -277,7 +277,7 @@ class json_writer { * @param[in] key The name of the record. * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(const std::string& key, bool newline=false) { + void begin_record(const std::string& key, bool newline = false) { if (output_ == nullptr) return; write_record_comma_if_needed(newline); @@ -289,7 +289,7 @@ class json_writer { * Writes "}", final token of a JSON record. * @param[in] newline (optional) if true, add newline after close token. */ - void end_record(bool newline=false) { + void end_record(bool newline = false) { if (output_ == nullptr) return; record_depth_--; @@ -298,7 +298,7 @@ class json_writer { } else { if (newline) *output_ << std::endl; - } + } record_element_needs_comma_ = false; *output_ << "}"; if (newline) diff --git a/src/stan/callbacks/structured_writer.hpp b/src/stan/callbacks/structured_writer.hpp index 3c6afa549c2..85306a28197 100644 --- a/src/stan/callbacks/structured_writer.hpp +++ b/src/stan/callbacks/structured_writer.hpp @@ -23,18 +23,18 @@ class structured_writer { * Writes "{", initial token of a JSON record. * @param[in] newline (optional) if true, add newline after open token. */ - virtual void begin_record(bool newline=false) {} + virtual void begin_record(bool newline = false) {} /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. * @param[in] newline (optional) if true, add newline between key, open token. */ - virtual void begin_record(const std::string&, bool newline=false) {} + virtual void begin_record(const std::string&, bool newline = false) {} /** * Writes "}", final token of a JSON record. * @param[in] newline (optional) if true, add newline after close token. */ - virtual void end_record(bool newline=false) {} + virtual void end_record(bool newline = false) {} /** * Writes "[", initial token of a JSON list. */ From 697cdd3f218fdb2f9d111e1d95604187b79e99a2 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 13:29:39 -0400 Subject: [PATCH 09/38] redo callbacks --- src/stan/callbacks/json_writer.hpp | 32 ++++++++++---------- src/stan/callbacks/structured_writer.hpp | 22 +++++++++++--- src/test/unit/callbacks/json_writer_test.cpp | 27 ++++++++--------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index c96ec4270f3..39040ad256c 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -43,12 +43,10 @@ class json_writer { /** * Writes a comma separator for the record's parent object if needed. */ - void write_record_comma_if_needed(bool newline = false) { + void write_record_comma_if_needed() { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ","; } - if (newline) - *output_ << std::endl; } /** @@ -261,12 +259,11 @@ class json_writer { /** * Writes "{", initial token of a JSON record. - * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(bool newline = false) { + void begin_record() { if (output_ == nullptr) return; - write_record_comma_if_needed(newline); + write_record_comma_if_needed(); *output_ << "{"; record_needs_comma_ = false; record_depth_++; @@ -275,34 +272,27 @@ class json_writer { /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. - * @param[in] newline (optional) if true, add newline before open token. */ - void begin_record(const std::string& key, bool newline = false) { + void begin_record(const std::string& key) { if (output_ == nullptr) return; - write_record_comma_if_needed(newline); + write_record_comma_if_needed(); *output_ << "\"" << key << "\" : {"; record_needs_comma_ = false; record_depth_++; } /** * Writes "}", final token of a JSON record. - * @param[in] newline (optional) if true, add newline after close token. */ - void end_record(bool newline = false) { + void end_record() { if (output_ == nullptr) return; record_depth_--; if (record_depth_ > 0) { record_needs_comma_ = true; - } else { - if (newline) - *output_ << std::endl; } record_element_needs_comma_ = false; *output_ << "}"; - if (newline) - *output_ << std::endl; } /** @@ -472,6 +462,16 @@ class json_writer { } *output_ << " ]"; } + + /** + * Add newline to output. + */ + void newline() { + if (output_ == nullptr) + return; + *output_ << std::endl; + } + }; } // namespace callbacks diff --git a/src/stan/callbacks/structured_writer.hpp b/src/stan/callbacks/structured_writer.hpp index 85306a28197..66262c83db0 100644 --- a/src/stan/callbacks/structured_writer.hpp +++ b/src/stan/callbacks/structured_writer.hpp @@ -19,26 +19,28 @@ class structured_writer { * Virtual destructor. */ virtual ~structured_writer() {} + /** * Writes "{", initial token of a JSON record. - * @param[in] newline (optional) if true, add newline after open token. */ - virtual void begin_record(bool newline = false) {} + virtual void begin_record() {} + /** * Writes "\"key\" : {", initial token of a named JSON record. * @param[in] key The name of the record. - * @param[in] newline (optional) if true, add newline between key, open token. */ virtual void begin_record(const std::string&, bool newline = false) {} + /** * Writes "}", final token of a JSON record. - * @param[in] newline (optional) if true, add newline after close token. */ - virtual void end_record(bool newline = false) {} + virtual void end_record() {} + /** * Writes "[", initial token of a JSON list. */ virtual void begin_list() {} + /** * Writes "]", final token of a JSON list. */ @@ -50,16 +52,19 @@ class structured_writer { * @param key Name of the value pair */ virtual void write(const std::string& key) {} + /** * Write a key-value pair where the value is a string. * @param key Name of the value pair * @param value string to write. */ virtual void write(const std::string& key, const std::string& value) {} + /** * No-op */ virtual void write() {} + /** * Write a key-value pair where the value is a bool. * @param key Name of the value pair @@ -73,18 +78,21 @@ class structured_writer { * @param value int to write. */ virtual void write(const std::string& key, int value) {} + /** * Write a key-value pair where the value is an `std::size_t`. * @param key Name of the value pair * @param value `std::size_t` to write. */ virtual void write(const std::string& key, std::size_t value) {} + /** * Write a key-value pair where the value is a double. * @param key Name of the value pair * @param value double to write. */ virtual void write(const std::string& key, double value) {} + /** * Write a key-value pair where the value is a complex value. * @param key Name of the value pair @@ -99,6 +107,7 @@ class structured_writer { */ virtual void write(const std::string& key, const std::vector values) { } + /** * Write a key-value pair where the value is a vector of strings to be made a * list. @@ -106,18 +115,21 @@ class structured_writer { * @param values vector of strings to write. */ void write(const std::string& key, const std::vector& values) {} + /** * Write a key-value pair where the value is an Eigen Matrix. * @param key Name of the value pair * @param mat Eigen Matrix to write. */ void write(const std::string& key, const Eigen::MatrixXd& mat) {} + /** * Write a key-value pair where the value is an Eigen Vector. * @param key Name of the value pair * @param vec Eigen Vector to write. */ void write(const std::string& key, const Eigen::VectorXd& vec) {} + /** * Write a key-value pair where the value is a Eigen RowVector. * @param key Name of the value pair diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index b845433a9fe..26a975a3898 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -22,26 +22,20 @@ class StanInterfaceCallbacksJsonWriter : public ::testing::Test { stan::callbacks::json_writer writer; }; -TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end) { +TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record) { writer.begin_record(); writer.end_record(); EXPECT_EQ("{}", ss.str()); } -TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_end_newline) { +TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_newline) { writer.begin_record(); - writer.end_record(true); + writer.newline(); + writer.end_record(); + writer.newline(); EXPECT_EQ("{\n}\n", ss.str()); } -TEST_F(StanInterfaceCallbacksJsonWriter, begin_record_newline_nested) { - writer.begin_record(true); - writer.begin_record(true); - writer.end_record(true); - writer.end_record(true); - EXPECT_EQ("\n{\n{}\n\n}\n", ss.str()); -} - TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { std::string key("key"); const int N = 5; @@ -79,11 +73,16 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { std::string key("key"); std::string value("value"); writer.begin_record(); - writer.begin_record("child", true); + writer.newline(); + writer.begin_record("child"); + writer.newline(); writer.write(key, value); + writer.newline(); + writer.end_record(); + writer.newline(); writer.end_record(); - writer.end_record(true); - EXPECT_EQ("{\n\"child\" : {\"key\" : \"value\"}\n}\n", ss.str()); + writer.newline(); + EXPECT_EQ("{\n\"child\" : {\n\"key\" : \"value\"\n}\n}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { From 38ca1a44a09f7e32a99e155c09f348a3113288fd Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 13:34:02 -0400 Subject: [PATCH 10/38] redo callbacks --- src/stan/services/pathfinder/single.hpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index ea039a99d6b..ff534667a74 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -637,7 +637,8 @@ inline auto pathfinder_lbfgs_single( stan::model::log_prob_grad(model, prev_params, prev_grads); if (unlikely(save_iterations)) { diagnostic_writer.begin_record(); - diagnostic_writer.begin_record("0", true); + diagnostic_writer.newline() + diagnostic_writer.begin_record("0") diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); @@ -698,7 +699,8 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { - diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num()), true); + diagnostic_writer.newline(); + diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); @@ -800,7 +802,9 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { - diagnostic_writer.end_record(true); + diagnostic_writer.newline(); // add final close record on its own line + diagnostic_writer.end_record(); + diagnostic_writer.newline(); } if (unlikely(ret <= 0)) { std::string prefix_err_msg From fd1451d6dc869aacd5870d6c84c97be4093e8b5d Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 13:42:48 -0400 Subject: [PATCH 11/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/callbacks/json_writer.hpp | 1 - src/stan/services/pathfinder/single.hpp | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 39040ad256c..0624e2a62ff 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -471,7 +471,6 @@ class json_writer { return; *output_ << std::endl; } - }; } // namespace callbacks diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index ff534667a74..eb809d2246f 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -638,8 +638,8 @@ inline auto pathfinder_lbfgs_single( if (unlikely(save_iterations)) { diagnostic_writer.begin_record(); diagnostic_writer.newline() - diagnostic_writer.begin_record("0") - diagnostic_writer.write("iter", static_cast(0)); + diagnostic_writer.begin_record("0") + diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); diagnostic_writer.end_record(); From 4455ef1d600f24756997b96123eef510b4ff3dc3 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 15:27:35 -0400 Subject: [PATCH 12/38] fix --- src/stan/services/pathfinder/single.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index ff534667a74..1188edf488f 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -637,7 +637,7 @@ inline auto pathfinder_lbfgs_single( stan::model::log_prob_grad(model, prev_params, prev_grads); if (unlikely(save_iterations)) { diagnostic_writer.begin_record(); - diagnostic_writer.newline() + diagnostic_writer.newline(); diagnostic_writer.begin_record("0") diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); From 7a976a5e65b39351c9ed04f92ef256c6556bf653 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 15:30:21 -0400 Subject: [PATCH 13/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/services/pathfinder/single.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 1188edf488f..ed2a2c36c0c 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -639,7 +639,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.begin_record(); diagnostic_writer.newline(); diagnostic_writer.begin_record("0") - diagnostic_writer.write("iter", static_cast(0)); + diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); diagnostic_writer.end_record(); From 9cf71ee3879c989aa68b0390f5510a7292a9be2f Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 16:25:44 -0400 Subject: [PATCH 14/38] fix typo --- src/stan/services/pathfinder/single.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index ed2a2c36c0c..42cd24160ed 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -638,8 +638,8 @@ inline auto pathfinder_lbfgs_single( if (unlikely(save_iterations)) { diagnostic_writer.begin_record(); diagnostic_writer.newline(); - diagnostic_writer.begin_record("0") - diagnostic_writer.write("iter", static_cast(0)); + diagnostic_writer.begin_record("0"); + diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); diagnostic_writer.end_record(); From 1c26789c6f56f12ec4540eac9ebc78caf55ea8e8 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 17:22:59 -0400 Subject: [PATCH 15/38] use json_writer in pathfinder unit tests --- src/test/unit/services/pathfinder/eight_schools_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/unit/services/pathfinder/eight_schools_test.cpp b/src/test/unit/services/pathfinder/eight_schools_test.cpp index 78cac755a76..44af266fbc6 100644 --- a/src/test/unit/services/pathfinder/eight_schools_test.cpp +++ b/src/test/unit/services/pathfinder/eight_schools_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include @@ -82,7 +82,7 @@ TEST_F(ServicesPathfinderEightSchools, multi) { std::ofstream empty_ostream(nullptr); stan::test::test_logger logger(empty_ostream); std::vector single_path_parameter_writer(num_paths); - std::vector single_path_diagnostic_writer( + std::vector> single_path_diagnostic_writer( num_paths); std::vector> single_path_inits; for (int i = 0; i < num_paths; ++i) { From e1c4ac4913fe07666b7a33ded4cb76a23e7fbc06 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 17:23:46 -0400 Subject: [PATCH 16/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/test/unit/services/pathfinder/eight_schools_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/unit/services/pathfinder/eight_schools_test.cpp b/src/test/unit/services/pathfinder/eight_schools_test.cpp index 44af266fbc6..c9192994fe2 100644 --- a/src/test/unit/services/pathfinder/eight_schools_test.cpp +++ b/src/test/unit/services/pathfinder/eight_schools_test.cpp @@ -82,8 +82,8 @@ TEST_F(ServicesPathfinderEightSchools, multi) { std::ofstream empty_ostream(nullptr); stan::test::test_logger logger(empty_ostream); std::vector single_path_parameter_writer(num_paths); - std::vector> single_path_diagnostic_writer( - num_paths); + std::vector> + single_path_diagnostic_writer(num_paths); std::vector> single_path_inits; for (int i = 0; i < num_paths; ++i) { single_path_inits.emplace_back( From 00b4d07eda92fdb102f71f349735971dae7ac1d2 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 18:03:02 -0400 Subject: [PATCH 17/38] output tweak --- src/stan/services/pathfinder/single.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 42cd24160ed..e875807148d 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -699,7 +699,6 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { - diagnostic_writer.newline(); diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); @@ -802,7 +801,7 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { - diagnostic_writer.newline(); // add final close record on its own line + diagnostic_writer.newline(); diagnostic_writer.end_record(); diagnostic_writer.newline(); } From 9616add98057a0b3a53d5ab0d78d970e7fc65dbd Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 18:31:17 -0400 Subject: [PATCH 18/38] newline tweaks --- src/stan/callbacks/json_writer.hpp | 1 + src/stan/services/pathfinder/single.hpp | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 0624e2a62ff..b4b8db98abb 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -469,6 +469,7 @@ class json_writer { void newline() { if (output_ == nullptr) return; + write_sep(); *output_ << std::endl; } }; diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index e875807148d..d7e62c0e540 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -643,6 +643,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); diagnostic_writer.end_record(); + diagnostic_writer.newline(); } auto constrain_fun = [&model](auto&& rng, auto&& unconstrained_draws, auto&& constrained_draws) { @@ -716,6 +717,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("pathfinder_success", false); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.end_record(); + diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -772,6 +774,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("full", pathfinder_res.second.use_full); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.end_record(); + diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -792,6 +795,7 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.write("pathfinder_error", std::string(e.what())); diagnostic_writer.end_record(); + diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -801,7 +805,6 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { - diagnostic_writer.newline(); diagnostic_writer.end_record(); diagnostic_writer.newline(); } From f0858fc20b5272d130e8327e37b44a37652db8c1 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 18:44:42 -0400 Subject: [PATCH 19/38] newline tweaks --- src/stan/services/pathfinder/single.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index d7e62c0e540..032e3ac0b44 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -643,7 +643,6 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("unconstrained_parameters", prev_params); diagnostic_writer.write("grads", prev_grads); diagnostic_writer.end_record(); - diagnostic_writer.newline(); } auto constrain_fun = [&model](auto&& rng, auto&& unconstrained_draws, auto&& constrained_draws) { @@ -700,6 +699,7 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { + diagnostic_writer.newline(); diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); @@ -717,7 +717,6 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("pathfinder_success", false); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.end_record(); - diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -774,7 +773,6 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("full", pathfinder_res.second.use_full); diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.end_record(); - diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -795,7 +793,6 @@ inline auto pathfinder_lbfgs_single( diagnostic_writer.write("lbfgs_note", lbfgs_ss.str()); diagnostic_writer.write("pathfinder_error", std::string(e.what())); diagnostic_writer.end_record(); - diagnostic_writer.newline(); } if (lbfgs_ss.str().length() > 0) { logger.info(lbfgs_ss); @@ -805,6 +802,7 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { + diagnostic_writer.newline(); diagnostic_writer.end_record(); diagnostic_writer.newline(); } From ca9bfbae7b0aa7f332ee57b6f1344f33cd4144fb Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 18:56:26 -0400 Subject: [PATCH 20/38] newline tweaks --- src/stan/callbacks/json_writer.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index b4b8db98abb..e3bd516ecf8 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -46,6 +46,7 @@ class json_writer { void write_record_comma_if_needed() { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ","; + record_needs_comma_ = false; } } @@ -265,7 +266,6 @@ class json_writer { return; write_record_comma_if_needed(); *output_ << "{"; - record_needs_comma_ = false; record_depth_++; } @@ -278,7 +278,6 @@ class json_writer { return; write_record_comma_if_needed(); *output_ << "\"" << key << "\" : {"; - record_needs_comma_ = false; record_depth_++; } /** @@ -464,12 +463,12 @@ class json_writer { } /** - * Add newline to output. + * Add newline between records. */ void newline() { if (output_ == nullptr) return; - write_sep(); + write_record_comma_if_needed(); *output_ << std::endl; } }; From 2934566cb01a10961ffef12869ddc3d606220c22 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 19:20:55 -0400 Subject: [PATCH 21/38] newline tweaks --- src/stan/callbacks/json_writer.hpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index e3bd516ecf8..09aa1b3ada6 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -40,6 +40,16 @@ class json_writer { // Whether or not the record's parent object needs a comma separator bool record_needs_comma_; + /** + * Writes a comma separator for record's parent object. + */ +g void write_record_comma() { + if (record_needs_comma_) { + *output_ << ","; + record_needs_comma_ = false; + } + } + /** * Writes a comma separator for the record's parent object if needed. */ @@ -50,6 +60,7 @@ class json_writer { } } + /** * Determines whether a record's internal object requires a comma separator */ @@ -463,12 +474,12 @@ class json_writer { } /** - * Add newline between records. + * Add newline */ void newline() { if (output_ == nullptr) return; - write_record_comma_if_needed(); + write_record_comma(); *output_ << std::endl; } }; From 2c57777838a555c258126c32470786b92d7df374 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 19:21:41 -0400 Subject: [PATCH 22/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/callbacks/json_writer.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 09aa1b3ada6..adc4f6bde3c 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -43,7 +43,7 @@ class json_writer { /** * Writes a comma separator for record's parent object. */ -g void write_record_comma() { + g void write_record_comma() { if (record_needs_comma_) { *output_ << ","; record_needs_comma_ = false; @@ -60,7 +60,6 @@ g void write_record_comma() { } } - /** * Determines whether a record's internal object requires a comma separator */ From ab0b457c275d9ba57e925a9e78a78d24d7d2d075 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 19:36:33 -0400 Subject: [PATCH 23/38] newline tweaks --- src/stan/callbacks/json_writer.hpp | 21 ++++++++------------- src/stan/services/pathfinder/single.hpp | 2 +- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 09aa1b3ada6..dadcd23efe1 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -40,16 +40,6 @@ class json_writer { // Whether or not the record's parent object needs a comma separator bool record_needs_comma_; - /** - * Writes a comma separator for record's parent object. - */ -g void write_record_comma() { - if (record_needs_comma_) { - *output_ << ","; - record_needs_comma_ = false; - } - } - /** * Writes a comma separator for the record's parent object if needed. */ @@ -474,12 +464,17 @@ g void write_record_comma() { } /** - * Add newline + * Emit newline between top-level objects. + * Add comma separator as needed. + * @param sep Name of the value pair */ - void newline() { + void newline(bool add_comma=false) { if (output_ == nullptr) return; - write_record_comma(); + if (sep) { + *output_ << ","; + record_needs_comma_ = false; + } *output_ << std::endl; } }; diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 032e3ac0b44..2c452a9d3b5 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -699,7 +699,7 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { - diagnostic_writer.newline(); + diagnostic_writer.newline(true); diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); From 7a4e434d6acebb7e1b9b4cf72a7e06c52da2d1ef Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 20:21:23 -0400 Subject: [PATCH 24/38] newline tweaks --- src/stan/callbacks/json_writer.hpp | 2 +- src/test/unit/callbacks/json_writer_test.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index dadcd23efe1..017fb1a0bd8 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -471,7 +471,7 @@ class json_writer { void newline(bool add_comma=false) { if (output_ == nullptr) return; - if (sep) { + if (add_comma) { *output_ << ","; record_needs_comma_ = false; } diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 26a975a3898..6a086ccd062 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -79,10 +79,10 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { writer.write(key, value); writer.newline(); writer.end_record(); - writer.newline(); + writer.newline(true); writer.end_record(); writer.newline(); - EXPECT_EQ("{\n\"child\" : {\n\"key\" : \"value\"\n}\n}\n", ss.str()); + EXPECT_EQ("{\n\"child\" : {\n\"key\" : \"value\"\n},\n}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { From 19e619622eccfc3896cbcd84244497d76f3f08e4 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 20:23:46 -0400 Subject: [PATCH 25/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/callbacks/json_writer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index f4be41c5820..64110c44e81 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -467,7 +467,7 @@ class json_writer { * Add comma separator as needed. * @param sep Name of the value pair */ - void newline(bool add_comma=false) { + void newline(bool add_comma = false) { if (output_ == nullptr) return; if (add_comma) { From a9bb20c7998443e29aedd4e5b49ed662950e3bc9 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sun, 2 Jul 2023 20:33:17 -0400 Subject: [PATCH 26/38] fix doc comments for fn newline --- src/stan/callbacks/json_writer.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index f4be41c5820..75fedbd7490 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -463,9 +463,9 @@ class json_writer { } /** - * Emit newline between top-level objects. - * Add comma separator as needed. - * @param sep Name of the value pair + * Emit newline between records, optionally adding comma before newline. + * + * @param add_comma - if true, output comma, reset flag. */ void newline(bool add_comma=false) { if (output_ == nullptr) From 0342612d65505bc22f4a210eab198e06c750ee9f Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sun, 2 Jul 2023 20:34:14 -0400 Subject: [PATCH 27/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/stan/callbacks/json_writer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 1c6bba289d1..9ac59c65714 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -464,7 +464,7 @@ class json_writer { /** * Emit newline between records, optionally adding comma before newline. - * + * * @param add_comma - if true, output comma, reset flag. */ void newline(bool add_comma = false) { From 283cb9cc2fc38b94a28fccbc9d318df98cbca084 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Tue, 4 Jul 2023 10:29:45 -0400 Subject: [PATCH 28/38] simplify --- src/stan/callbacks/json_writer.hpp | 10 ++-------- src/stan/services/pathfinder/single.hpp | 3 --- src/test/unit/callbacks/json_writer_test.cpp | 16 ---------------- 3 files changed, 2 insertions(+), 27 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 1c6bba289d1..3dd3321f5e1 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -463,17 +463,11 @@ class json_writer { } /** - * Emit newline between records, optionally adding comma before newline. - * - * @param add_comma - if true, output comma, reset flag. + * Emit newline. */ - void newline(bool add_comma = false) { + void newline() { if (output_ == nullptr) return; - if (add_comma) { - *output_ << ","; - record_needs_comma_ = false; - } *output_ << std::endl; } }; diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 2c452a9d3b5..779e93bf5ae 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -637,7 +637,6 @@ inline auto pathfinder_lbfgs_single( stan::model::log_prob_grad(model, prev_params, prev_grads); if (unlikely(save_iterations)) { diagnostic_writer.begin_record(); - diagnostic_writer.newline(); diagnostic_writer.begin_record("0"); diagnostic_writer.write("iter", static_cast(0)); diagnostic_writer.write("unconstrained_parameters", prev_params); @@ -699,7 +698,6 @@ inline auto pathfinder_lbfgs_single( static_cast(max_history_size)); if (unlikely(save_iterations)) { - diagnostic_writer.newline(true); diagnostic_writer.begin_record(std::to_string(lbfgs.iter_num())); diagnostic_writer.write("iter", lbfgs.iter_num()); diagnostic_writer.write("unconstrained_parameters", prev_params); @@ -802,7 +800,6 @@ inline auto pathfinder_lbfgs_single( } } if (unlikely(save_iterations)) { - diagnostic_writer.newline(); diagnostic_writer.end_record(); diagnostic_writer.newline(); } diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 6a086ccd062..bccc785e47a 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -69,22 +69,6 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member) { EXPECT_EQ("{\"key\" : \"value\"}", ss.str()); } -TEST_F(StanInterfaceCallbacksJsonWriter, single_member_newlines) { - std::string key("key"); - std::string value("value"); - writer.begin_record(); - writer.newline(); - writer.begin_record("child"); - writer.newline(); - writer.write(key, value); - writer.newline(); - writer.end_record(); - writer.newline(true); - writer.end_record(); - writer.newline(); - EXPECT_EQ("{\n\"child\" : {\n\"key\" : \"value\"\n},\n}\n", ss.str()); -} - TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { std::string key("key"); std::string value("value"); From e8b757c24d1d95dbbf5f3117bc6ef29e73b236c9 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Thu, 6 Jul 2023 15:25:33 -0400 Subject: [PATCH 29/38] add newline between records --- src/stan/callbacks/json_writer.hpp | 15 ++++-------- src/stan/services/pathfinder/single.hpp | 1 - src/test/unit/callbacks/json_writer_test.cpp | 25 +++++++++++--------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 3dd3321f5e1..556b0b10452 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -45,7 +45,7 @@ class json_writer { */ void write_record_comma_if_needed() { if (record_depth_ > 0 && record_needs_comma_) { - *output_ << ","; + *output_ << ",\n"; record_needs_comma_ = false; } } @@ -286,12 +286,14 @@ class json_writer { void end_record() { if (output_ == nullptr) return; + *output_ << "}"; record_depth_--; if (record_depth_ > 0) { record_needs_comma_ = true; + } else { + *output_ << "\n"; } record_element_needs_comma_ = false; - *output_ << "}"; } /** @@ -461,15 +463,6 @@ class json_writer { } *output_ << " ]"; } - - /** - * Emit newline. - */ - void newline() { - if (output_ == nullptr) - return; - *output_ << std::endl; - } }; } // namespace callbacks diff --git a/src/stan/services/pathfinder/single.hpp b/src/stan/services/pathfinder/single.hpp index 779e93bf5ae..9e2b5705974 100644 --- a/src/stan/services/pathfinder/single.hpp +++ b/src/stan/services/pathfinder/single.hpp @@ -801,7 +801,6 @@ inline auto pathfinder_lbfgs_single( } if (unlikely(save_iterations)) { diagnostic_writer.end_record(); - diagnostic_writer.newline(); } if (unlikely(ret <= 0)) { std::string prefix_err_msg diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index bccc785e47a..2ae46241952 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -25,15 +25,21 @@ class StanInterfaceCallbacksJsonWriter : public ::testing::Test { TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record) { writer.begin_record(); writer.end_record(); - EXPECT_EQ("{}", ss.str()); + EXPECT_EQ("{}\n", ss.str()); } -TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_newline) { +TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { + std::string key("key"); + std::string value("value"); writer.begin_record(); - writer.newline(); + writer.begin_record(key); + writer.write(key, value); + writer.end_record(); + writer.begin_record(key); + writer.write(key, value); + writer.end_record(); writer.end_record(); - writer.newline(); - EXPECT_EQ("{\n}\n", ss.str()); + EXPECT_EQ("{\"key\" : {\"key\" : \"value\"},\n\"key\" : {\"key\" : \"value\"}}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { @@ -66,7 +72,7 @@ TEST_F(StanInterfaceCallbacksJsonWriter, single_member) { writer.begin_record(); writer.write(key, value); writer.end_record(); - EXPECT_EQ("{\"key\" : \"value\"}", ss.str()); + EXPECT_EQ("{\"key\" : \"value\"}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { @@ -77,16 +83,13 @@ TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { writer.write(key, value); EXPECT_EQ( - "{\"key\" : \"value\"" - ", \"key\" : \"value\"", + "{\"key\" : \"value\", \"key\" : \"value\"", ss.str()); writer.write(key, value); writer.end_record(); EXPECT_EQ( - "{\"key\" : \"value\"" - ", \"key\" : \"value\"" - ", \"key\" : \"value\"}", + "{\"key\" : \"value\", \"key\" : \"value\", \"key\" : \"value\"}\n", ss.str()); } From a635d11744fa96bd5b0010e6fd28c6ec75ac57f8 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 6 Jul 2023 15:26:31 -0400 Subject: [PATCH 30/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/test/unit/callbacks/json_writer_test.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 2ae46241952..e39b99f540a 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -39,7 +39,9 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { writer.write(key, value); writer.end_record(); writer.end_record(); - EXPECT_EQ("{\"key\" : {\"key\" : \"value\"},\n\"key\" : {\"key\" : \"value\"}}\n", ss.str()); + EXPECT_EQ( + "{\"key\" : {\"key\" : \"value\"},\n\"key\" : {\"key\" : \"value\"}}\n", + ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { @@ -82,15 +84,12 @@ TEST_F(StanInterfaceCallbacksJsonWriter, more_members) { writer.write(key, value); writer.write(key, value); - EXPECT_EQ( - "{\"key\" : \"value\", \"key\" : \"value\"", - ss.str()); + EXPECT_EQ("{\"key\" : \"value\", \"key\" : \"value\"", ss.str()); writer.write(key, value); writer.end_record(); - EXPECT_EQ( - "{\"key\" : \"value\", \"key\" : \"value\", \"key\" : \"value\"}\n", - ss.str()); + EXPECT_EQ("{\"key\" : \"value\", \"key\" : \"value\", \"key\" : \"value\"}\n", + ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector_precision2) { From 3f1cdb32de06d32e02848f312387c09fd6103dcc Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Thu, 6 Jul 2023 15:50:40 -0400 Subject: [PATCH 31/38] more unit tests, more fixes --- src/stan/callbacks/json_writer.hpp | 7 +++++-- src/test/unit/callbacks/json_writer_test.cpp | 9 ++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/stan/callbacks/json_writer.hpp b/src/stan/callbacks/json_writer.hpp index 556b0b10452..54f54bf3850 100644 --- a/src/stan/callbacks/json_writer.hpp +++ b/src/stan/callbacks/json_writer.hpp @@ -38,7 +38,7 @@ class json_writer { // separator) int record_depth_ = 0; // Whether or not the record's parent object needs a comma separator - bool record_needs_comma_; + bool record_needs_comma_ = false; /** * Writes a comma separator for the record's parent object if needed. @@ -47,6 +47,8 @@ class json_writer { if (record_depth_ > 0 && record_needs_comma_) { *output_ << ",\n"; record_needs_comma_ = false; + } else { + write_sep(); } } @@ -267,6 +269,7 @@ class json_writer { write_record_comma_if_needed(); *output_ << "{"; record_depth_++; + record_element_needs_comma_ = false; } /** @@ -279,6 +282,7 @@ class json_writer { write_record_comma_if_needed(); *output_ << "\"" << key << "\" : {"; record_depth_++; + record_element_needs_comma_ = false; } /** * Writes "}", final token of a JSON record. @@ -293,7 +297,6 @@ class json_writer { } else { *output_ << "\n"; } - record_element_needs_comma_ = false; } /** diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 2ae46241952..eecca17780c 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -32,14 +32,17 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { std::string key("key"); std::string value("value"); writer.begin_record(); - writer.begin_record(key); + writer.begin_record("1"); writer.write(key, value); writer.end_record(); - writer.begin_record(key); + writer.begin_record("2"); writer.write(key, value); + writer.begin_record("2.1"); + writer.write(key, value); + writer.end_record(); writer.end_record(); writer.end_record(); - EXPECT_EQ("{\"key\" : {\"key\" : \"value\"},\n\"key\" : {\"key\" : \"value\"}}\n", ss.str()); + EXPECT_EQ("{\"1\" : {\"key\" : \"value\"},\n\"2\" : {\"key\" : \"value\", \"2.1\" : {\"key\" : \"value\"}}}\n", ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { From 1d734abe0ac80a31f4c7c6e4d705b7865b28cc29 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 6 Jul 2023 15:53:41 -0400 Subject: [PATCH 32/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/test/unit/callbacks/json_writer_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index f31b58abcee..efc9126bee0 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -44,7 +44,8 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { writer.end_record(); EXPECT_EQ( "{\"1\" : {\"key\" : \"value\"},\n\"2\" : {\"key\" : \"value\"," - " \"2.1\" : {\"key\" : \"value\"}}}\n", ss.str()); + " \"2.1\" : {\"key\" : \"value\"}}}\n", + ss.str()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { From fc363225157a7a9cd3f0fab6c526dd633fc582fc Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Thu, 6 Jul 2023 16:16:04 -0400 Subject: [PATCH 33/38] validate json diagnostics --- src/test/unit/services/pathfinder/normal_glm_test.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/unit/services/pathfinder/normal_glm_test.cpp b/src/test/unit/services/pathfinder/normal_glm_test.cpp index 1e9018b4b80..5668bde695a 100644 --- a/src/test/unit/services/pathfinder/normal_glm_test.cpp +++ b/src/test/unit/services/pathfinder/normal_glm_test.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include // Locally tests can use threads but for jenkins we should just use 1 thread @@ -131,6 +132,9 @@ TEST_F(ServicesPathfinderGLM, single) { for (int i = 2; i < all_mean_vals.cols(); ++i) { EXPECT_NEAR(0, all_sd_vals(2, i), .1); } + rapidjson::Document document; + ASSERT_FALSE(document.Parse<0>(diagnostic_ss.str().c_str()).HasParseError()); + } TEST_F(ServicesPathfinderGLM, multi) { From 957770ae9a6a6440a2ea0b1723aa8d2e6d43478d Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Thu, 6 Jul 2023 16:17:26 -0400 Subject: [PATCH 34/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/test/unit/services/pathfinder/normal_glm_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/unit/services/pathfinder/normal_glm_test.cpp b/src/test/unit/services/pathfinder/normal_glm_test.cpp index 5668bde695a..937e6357725 100644 --- a/src/test/unit/services/pathfinder/normal_glm_test.cpp +++ b/src/test/unit/services/pathfinder/normal_glm_test.cpp @@ -134,7 +134,6 @@ TEST_F(ServicesPathfinderGLM, single) { } rapidjson::Document document; ASSERT_FALSE(document.Parse<0>(diagnostic_ss.str().c_str()).HasParseError()); - } TEST_F(ServicesPathfinderGLM, multi) { From 02759779b6728e3befab0af771412bcf897bba01 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sat, 8 Jul 2023 15:56:26 -0400 Subject: [PATCH 35/38] backout lexical_cast changes --- src/stan/callbacks/writer.hpp | 1 + src/stan/io/dump.hpp | 15 ++++++++------- src/stan/io/validate_zero_buf.hpp | 4 ++-- src/stan/mcmc/hmc/hamiltonians/ps_point.hpp | 1 + src/stan/variational/advi.hpp | 1 + src/test/unit/io/validate_zero_buf_test.cpp | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 62b6604eeb4..2ee3a7c1727 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -2,6 +2,7 @@ #define STAN_CALLBACKS_WRITER_HPP #include +#include #include #include diff --git a/src/stan/io/dump.hpp b/src/stan/io/dump.hpp index 4170c7c50fa..2e81772f992 100644 --- a/src/stan/io/dump.hpp +++ b/src/stan/io/dump.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -241,8 +242,8 @@ class dump_reader { scan_optional_long(); size_t d = 0; try { - d = std::stoull(buf_); - } catch (const std::exception& exc) { + d = boost::lexical_cast(buf_); + } catch (const boost::bad_lexical_cast& exc) { std::string msg = "value " + buf_ + " beyond array dimension range"; throw std::invalid_argument(msg); } @@ -268,8 +269,8 @@ class dump_reader { int get_int() { int n = 0; try { - n = std::stol(buf_); - } catch (const std::exception& exc) { + n = boost::lexical_cast(buf_); + } catch (const boost::bad_lexical_cast& exc) { std::string msg = "value " + buf_ + " beyond int range"; throw std::invalid_argument(msg); } @@ -279,17 +280,17 @@ class dump_reader { double scan_double() { double x = 0; try { - x = std::stod(buf_); + x = boost::lexical_cast(buf_); if (x == 0) validate_zero_buf(buf_); - } catch (const std::exception& exc) { + } catch (const boost::bad_lexical_cast& exc) { std::string msg = "value " + buf_ + " beyond numeric range"; throw std::invalid_argument(msg); } return x; } - // scan number stores number or throws exception + // scan number stores number or throws bad lexical cast exception void scan_number(bool negate_val) { // must take longest first! if (scan_chars("Inf")) { diff --git a/src/stan/io/validate_zero_buf.hpp b/src/stan/io/validate_zero_buf.hpp index 76632d534c7..03f3938a978 100644 --- a/src/stan/io/validate_zero_buf.hpp +++ b/src/stan/io/validate_zero_buf.hpp @@ -14,7 +14,7 @@ namespace io { * operator[](size_t). * * @tparam B Character buffer type - * @throw std::bad_cast if the buffer + * @throw boost::bad_lexical_cast if the buffer * contains non-zero characters before an exponentiation symbol. */ template @@ -23,7 +23,7 @@ void validate_zero_buf(const B& buf) { if (buf[i] == 'e' || buf[i] == 'E') return; if (buf[i] >= '1' && buf[i] <= '9') - throw std::bad_cast(); + boost::conversion::detail::throw_bad_cast(); } } diff --git a/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp b/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp index b66cbc6c12e..54060d29ece 100644 --- a/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp +++ b/src/stan/mcmc/hmc/hamiltonians/ps_point.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include diff --git a/src/stan/variational/advi.hpp b/src/stan/variational/advi.hpp index 87391382f03..4dca19d0335 100644 --- a/src/stan/variational/advi.hpp +++ b/src/stan/variational/advi.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include diff --git a/src/test/unit/io/validate_zero_buf_test.cpp b/src/test/unit/io/validate_zero_buf_test.cpp index 46eea094e39..1ab60f0473e 100644 --- a/src/test/unit/io/validate_zero_buf_test.cpp +++ b/src/test/unit/io/validate_zero_buf_test.cpp @@ -15,8 +15,8 @@ TEST(ioValidateZeroBuf, tester) { EXPECT_NO_THROW(validate_zero_buf(s)); s = "1.0"; - EXPECT_THROW(validate_zero_buf(s), std::bad_cast); + EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast); s = "1e1"; - EXPECT_THROW(validate_zero_buf(s), std::bad_cast); + EXPECT_THROW(validate_zero_buf(s), boost::bad_lexical_cast); } From 419ac458d5495e150a8d996e32de9d5fb4c170b2 Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sat, 8 Jul 2023 16:11:43 -0400 Subject: [PATCH 36/38] changes per code review, more unit tests --- src/test/unit/callbacks/json_writer_test.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index efc9126bee0..58ea8e8857d 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -34,17 +34,24 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { writer.begin_record(); writer.begin_record("1"); writer.write(key, value); - writer.end_record(); + writer.end_record(); // 1 writer.begin_record("2"); writer.write(key, value); writer.begin_record("2.1"); writer.write(key, value); - writer.end_record(); - writer.end_record(); + writer.write(key, value); + writer.end_record(); // 2.1 + writer.begin_record("2.2"); + writer.write(key, value); + writer.write(key, value); + writer.end_record(); // 2.2 + writer.end_record(); // 2 writer.end_record(); EXPECT_EQ( - "{\"1\" : {\"key\" : \"value\"},\n\"2\" : {\"key\" : \"value\"," - " \"2.1\" : {\"key\" : \"value\"}}}\n", + "{\"1\" : {\"key\" : \"value\"},\n" + "\"2\" : {\"key\" : \"value\"," + " \"2.1\" : {\"key\" : \"value\", \"key\" : \"value\"},\n" + "\"2.2\" : {\"key\" : \"value\", \"key\" : \"value\"}}}\n", ss.str()); } From c5bc3ebbcc6ffb2eaf755f5d4ae8f374a4e9c227 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 8 Jul 2023 16:12:31 -0400 Subject: [PATCH 37/38] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- src/test/unit/callbacks/json_writer_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 58ea8e8857d..22036da3525 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -34,18 +34,18 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { writer.begin_record(); writer.begin_record("1"); writer.write(key, value); - writer.end_record(); // 1 + writer.end_record(); // 1 writer.begin_record("2"); writer.write(key, value); writer.begin_record("2.1"); writer.write(key, value); writer.write(key, value); - writer.end_record(); // 2.1 + writer.end_record(); // 2.1 writer.begin_record("2.2"); writer.write(key, value); writer.write(key, value); - writer.end_record(); // 2.2 - writer.end_record(); // 2 + writer.end_record(); // 2.2 + writer.end_record(); // 2 writer.end_record(); EXPECT_EQ( "{\"1\" : {\"key\" : \"value\"},\n" From 11b5a779ff6727dabfd605a6d3f5e7db5e5d26bd Mon Sep 17 00:00:00 2001 From: Mitzi Morris Date: Sat, 8 Jul 2023 16:18:11 -0400 Subject: [PATCH 38/38] more tests --- src/test/unit/callbacks/json_writer_test.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/unit/callbacks/json_writer_test.cpp b/src/test/unit/callbacks/json_writer_test.cpp index 22036da3525..71893db8585 100644 --- a/src/test/unit/callbacks/json_writer_test.cpp +++ b/src/test/unit/callbacks/json_writer_test.cpp @@ -1,6 +1,7 @@ -#include -#include #include +#include +#include +#include struct deleter_noop { template @@ -53,6 +54,8 @@ TEST_F(StanInterfaceCallbacksJsonWriter, begin_end_record_nested) { " \"2.1\" : {\"key\" : \"value\", \"key\" : \"value\"},\n" "\"2.2\" : {\"key\" : \"value\", \"key\" : \"value\"}}}\n", ss.str()); + rapidjson::Document document; + ASSERT_FALSE(document.Parse<0>(ss.str().c_str()).HasParseError()); } TEST_F(StanInterfaceCallbacksJsonWriter, write_double_vector) { @@ -146,7 +149,7 @@ TEST_F(StanInterfaceCallbacksJsonWriter, write_string_vector) { const int N = 5; std::vector x; for (int n = 0; n < N; ++n) - x.push_back(boost::lexical_cast(n)); + x.push_back(std::to_string(n)); writer.write("key", x); EXPECT_EQ("\"key\" : [ 0, 1, 2, 3, 4 ]", ss.str());