From 011684bd606c1ff0a3ed26e0500543e45d219734 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Nov 2021 09:30:53 -0500 Subject: [PATCH 1/9] bool is_empty() for not writing to files. --- src/stan/callbacks/stream_writer.hpp | 34 ++++++++---- src/stan/callbacks/tee_writer.hpp | 5 ++ src/stan/callbacks/unique_stream_writer.hpp | 55 +++++++++++-------- src/stan/callbacks/writer.hpp | 7 +++ src/stan/services/util/mcmc_writer.hpp | 22 +++++--- src/stan/variational/advi.hpp | 17 +++--- .../unit/callbacks/stream_writer_test.cpp | 13 ++++- src/test/unit/callbacks/tee_writer_test.cpp | 29 ++++++++-- .../callbacks/unique_stream_writer_test.cpp | 17 +++++- 9 files changed, 143 insertions(+), 56 deletions(-) diff --git a/src/stan/callbacks/stream_writer.hpp b/src/stan/callbacks/stream_writer.hpp index 6519531c0ac..1da036c97e7 100644 --- a/src/stan/callbacks/stream_writer.hpp +++ b/src/stan/callbacks/stream_writer.hpp @@ -24,8 +24,8 @@ class stream_writer : public writer { * each comment line. Default is "". */ explicit stream_writer(std::ostream& output, - const std::string& comment_prefix = "") - : output_(output), comment_prefix_(comment_prefix) {} + const std::string& comment_prefix = "", bool is_empty = false) + : output_(output), comment_prefix_(comment_prefix), empty_(is_empty) {} /** * Virtual destructor @@ -57,7 +57,7 @@ class stream_writer : public writer { /** * Writes the comment_prefix to the stream followed by a newline. */ - void operator()() { output_ << comment_prefix_ << std::endl; } + void operator()() { if (!empty_) {output_ << comment_prefix_ << std::endl;} } /** * Writes the comment_prefix then the message followed by a newline. @@ -65,9 +65,16 @@ class stream_writer : public writer { * @param[in] message A string */ void operator()(const std::string& message) { - output_ << comment_prefix_ << message << std::endl; + if (!empty_) { + output_ << comment_prefix_ << message << std::endl; + } } + inline bool is_empty() const { + return empty_; + } + + private: /** * Output stream @@ -79,6 +86,7 @@ class stream_writer : public writer { */ std::string comment_prefix_; + bool empty_; /** * Writes a set of values in csv format followed by a newline. * @@ -89,16 +97,18 @@ class stream_writer : public writer { */ template void write_vector(const std::vector& v) { - if (v.empty()) - return; + if (!empty_) { + if (v.empty()) + return; - typename std::vector::const_iterator last = v.end(); - --last; + typename std::vector::const_iterator last = v.end(); + --last; - for (typename std::vector::const_iterator it = v.begin(); it != last; - ++it) - output_ << *it << ","; - output_ << v.back() << std::endl; + for (typename std::vector::const_iterator it = v.begin(); it != last; + ++it) + output_ << *it << ","; + output_ << v.back() << std::endl; + } } }; diff --git a/src/stan/callbacks/tee_writer.hpp b/src/stan/callbacks/tee_writer.hpp index c491832e9ec..58d5da44492 100644 --- a/src/stan/callbacks/tee_writer.hpp +++ b/src/stan/callbacks/tee_writer.hpp @@ -49,6 +49,10 @@ class tee_writer final : public writer { writer2_(message); } + inline bool is_empty() const { + return writer1_.is_empty() && writer2_.is_empty(); + } + private: /** * The first writer @@ -58,6 +62,7 @@ class tee_writer final : public writer { * The second writer */ writer& writer2_; + }; } // namespace callbacks diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index 60e1c4e3843..2f22a417853 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -27,14 +27,14 @@ class unique_stream_writer final : public writer { * Default is "". */ explicit unique_stream_writer(std::unique_ptr&& output, - const std::string& comment_prefix = "") - : output_(std::move(output)), comment_prefix_(comment_prefix) {} + const std::string& comment_prefix = "", bool is_empty = false) + : output_(std::move(output)), comment_prefix_(comment_prefix), empty_(is_empty) {} unique_stream_writer(); unique_stream_writer(unique_stream_writer& other) = delete; unique_stream_writer(unique_stream_writer&& other) : output_(std::move(other.output_)), - comment_prefix_(std::move(other.comment_prefix_)) {} + comment_prefix_(std::move(other.comment_prefix_)), empty_(other.empty_) {} /** * Virtual destructor */ @@ -70,10 +70,12 @@ class unique_stream_writer final : public writer { * Writes the comment_prefix to the stream followed by a newline. */ void operator()() { - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - streamer << comment_prefix_ << std::endl; - *output_ << streamer.str(); + if (!empty_) { + std::stringstream streamer; + streamer.precision(output_.get()->precision()); + streamer << comment_prefix_ << std::endl; + *output_ << streamer.str(); + } } /** @@ -82,10 +84,16 @@ class unique_stream_writer final : public writer { * @param[in] message A string */ void operator()(const std::string& message) { - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - streamer << comment_prefix_ << message << std::endl; - *output_ << streamer.str(); + if (!empty_) { + std::stringstream streamer; + streamer.precision(output_.get()->precision()); + streamer << comment_prefix_ << message << std::endl; + *output_ << streamer.str(); + } + } + + inline bool is_empty() const { + return empty_; } private: @@ -99,6 +107,7 @@ class unique_stream_writer final : public writer { */ std::string comment_prefix_; + bool empty_{false}; /** * Writes a set of values in csv format followed by a newline. * @@ -109,18 +118,20 @@ class unique_stream_writer final : public writer { */ template void write_vector(const std::vector& v) { - if (v.empty()) - return; - using const_iter = typename std::vector::const_iterator; - const_iter last = v.end(); - --last; - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - for (const_iter it = v.begin(); it != last; ++it) { - streamer << *it << ","; + if (!empty_) { + if (v.empty()) + return; + using const_iter = typename std::vector::const_iterator; + const_iter last = v.end(); + --last; + std::stringstream streamer; + streamer.precision(output_.get()->precision()); + for (const_iter it = v.begin(); it != last; ++it) { + streamer << *it << ","; + } + streamer << v.back() << std::endl; + *output_ << streamer.str(); } - streamer << v.back() << std::endl; - *output_ << streamer.str(); } }; diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index a12108b256f..38b20612971 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -45,6 +45,13 @@ class writer { * @param[in] message A string */ virtual void operator()(const std::string& message) {} + + /** + * Check if the writer is writing to an empty stream + */ + virtual bool is_empty() const { + return true; + } }; } // namespace callbacks diff --git a/src/stan/services/util/mcmc_writer.hpp b/src/stan/services/util/mcmc_writer.hpp index 22b0b4fbb94..fd712ab0103 100644 --- a/src/stan/services/util/mcmc_writer.hpp +++ b/src/stan/services/util/mcmc_writer.hpp @@ -149,17 +149,19 @@ class mcmc_writer { template void write_diagnostic_names(stan::mcmc::sample sample, stan::mcmc::base_mcmc& sampler, Model& model) { - std::vector names; + if (!diagnostic_writer_.is_empty()) { + std::vector names; - sample.get_sample_param_names(names); - sampler.get_sampler_param_names(names); + sample.get_sample_param_names(names); + sampler.get_sampler_param_names(names); - std::vector model_names; - model.unconstrained_param_names(model_names, false, false); + std::vector model_names; + model.unconstrained_param_names(model_names, false, false); - sampler.get_sampler_diagnostic_names(model_names, names); + sampler.get_sampler_diagnostic_names(model_names, names); - diagnostic_writer_(names); + diagnostic_writer_(names); + } } /** @@ -170,6 +172,7 @@ class mcmc_writer { */ void write_diagnostic_params(stan::mcmc::sample& sample, stan::mcmc::base_mcmc& sampler) { + if (!diagnostic_writer_.is_empty()) { std::vector values; sample.get_sample_params(values); @@ -177,6 +180,7 @@ class mcmc_writer { sampler.get_sampler_diagnostics(values); diagnostic_writer_(values); + } } /** @@ -247,7 +251,9 @@ class mcmc_writer { */ void write_timing(double warmDeltaT, double sampleDeltaT) { write_timing(warmDeltaT, sampleDeltaT, sample_writer_); - write_timing(warmDeltaT, sampleDeltaT, diagnostic_writer_); + if (!diagnostic_writer_.is_empty()) { + write_timing(warmDeltaT, sampleDeltaT, diagnostic_writer_); + } log_timing(warmDeltaT, sampleDeltaT); } }; diff --git a/src/stan/variational/advi.hpp b/src/stan/variational/advi.hpp index 4dca19d0335..e89ab70ac11 100644 --- a/src/stan/variational/advi.hpp +++ b/src/stan/variational/advi.hpp @@ -392,12 +392,14 @@ class advi { = std::chrono::duration_cast(end - start) .count() / 1000.0; - std::vector print_vector; - print_vector.clear(); - print_vector.push_back(iter_counter); - print_vector.push_back(delta_t); - print_vector.push_back(elbo); - diagnostic_writer(print_vector); + if (!diagnostic_writer.is_empty()) { + std::vector print_vector; + print_vector.clear(); + print_vector.push_back(iter_counter); + print_vector.push_back(delta_t); + print_vector.push_back(elbo); + diagnostic_writer(print_vector); + } if (delta_elbo_ave < tol_rel_obj) { ss << " MEAN ELBO CONVERGED"; @@ -459,8 +461,9 @@ class advi { double tol_rel_obj, int max_iterations, callbacks::logger& logger, callbacks::writer& parameter_writer, callbacks::writer& diagnostic_writer) const { + if (!diagnostic_writer.is_empty()) { diagnostic_writer("iter,time_in_seconds,ELBO"); - + } // Initialize variational approximation Q variational = Q(cont_params_); diff --git a/src/test/unit/callbacks/stream_writer_test.cpp b/src/test/unit/callbacks/stream_writer_test.cpp index e0bc01bb9be..effba2466c9 100644 --- a/src/test/unit/callbacks/stream_writer_test.cpp +++ b/src/test/unit/callbacks/stream_writer_test.cpp @@ -5,7 +5,7 @@ class StanInterfaceCallbacksStreamWriter : public ::testing::Test { public: StanInterfaceCallbacksStreamWriter() - : ss(), writer(ss), writer_prefix(ss, "# ") {} + : ss(), writer(ss), writer_prefix(ss, "# "), empty_writer(ss, "# ", true) {} void SetUp() { ss.str(std::string()); @@ -16,6 +16,7 @@ class StanInterfaceCallbacksStreamWriter : public ::testing::Test { std::stringstream ss; stan::callbacks::stream_writer writer; stan::callbacks::stream_writer writer_prefix; + stan::callbacks::stream_writer empty_writer; }; TEST_F(StanInterfaceCallbacksStreamWriter, double_vector) { @@ -28,6 +29,16 @@ TEST_F(StanInterfaceCallbacksStreamWriter, double_vector) { EXPECT_EQ("0,1,2,3,4\n", ss.str()); } +TEST_F(StanInterfaceCallbacksStreamWriter, empty_vector) { + const int N = 5; + std::vector x; + for (int n = 0; n < N; ++n) + x.push_back(n); + + EXPECT_NO_THROW(empty_writer(x)); + EXPECT_EQ("", ss.str()); +} + TEST_F(StanInterfaceCallbacksStreamWriter, string_vector) { const int N = 5; std::vector x; diff --git a/src/test/unit/callbacks/tee_writer_test.cpp b/src/test/unit/callbacks/tee_writer_test.cpp index 0dc4dbd4e2f..5a1d88eb8f8 100644 --- a/src/test/unit/callbacks/tee_writer_test.cpp +++ b/src/test/unit/callbacks/tee_writer_test.cpp @@ -5,26 +5,37 @@ namespace test { class mock_writer : public stan::callbacks::writer { public: int N; + bool empty_; mock_writer() : N(0) {} + mock_writer(bool is_empty) : N(0), empty_(is_empty) {} - void operator()(const std::vector& names) { ++N; } + void operator()(const std::vector& names) { if (!empty_) {++N;} } - void operator()(const std::vector& state) { ++N; } + void operator()(const std::vector& state) { if (!empty_) {++N;} } - void operator()() { ++N; } + void operator()() { if (!empty_) {++N;} } - void operator()(const std::string& message) { ++N; } + void operator()(const std::string& message) { + if (!empty_) {++N;} + } + + inline bool is_empty() { + return false; + } }; } // namespace test class StanCallbacksTeeWriter : public ::testing::Test { public: StanCallbacksTeeWriter() - : writer1(), writer2(), tee_writer(writer1, writer2) {} + : writer1(), writer2(), tee_writer(writer1, writer2), + empty_writer1(true), empty_writer2(true), empty_tee_writer(empty_writer1, empty_writer2) {} test::mock_writer writer1, writer2; stan::callbacks::tee_writer tee_writer; + test::mock_writer empty_writer1, empty_writer2; + stan::callbacks::tee_writer empty_tee_writer; }; TEST_F(StanCallbacksTeeWriter, names) { @@ -35,6 +46,14 @@ TEST_F(StanCallbacksTeeWriter, names) { EXPECT_EQ(1, writer2.N); } +TEST_F(StanCallbacksTeeWriter, empty_names) { + std::vector names; + + empty_tee_writer(names); + EXPECT_EQ(0, empty_writer1.N); + EXPECT_EQ(0, empty_writer2.N); +} + TEST_F(StanCallbacksTeeWriter, state) { std::vector state; diff --git a/src/test/unit/callbacks/unique_stream_writer_test.cpp b/src/test/unit/callbacks/unique_stream_writer_test.cpp index 43d783f48c4..8314aee86d3 100644 --- a/src/test/unit/callbacks/unique_stream_writer_test.cpp +++ b/src/test/unit/callbacks/unique_stream_writer_test.cpp @@ -5,15 +5,19 @@ class StanInterfaceCallbacksStreamWriter : public ::testing::Test { public: StanInterfaceCallbacksStreamWriter() - : writer(std::make_unique(std::stringstream{})) {} + : writer(std::make_unique(std::stringstream{})), + empty_writer(std::make_unique(std::stringstream{}), "#", true) {} void SetUp() { static_cast(writer.get_stream()).str(std::string()); static_cast(writer.get_stream()).clear(); + static_cast(empty_writer.get_stream()).str(std::string()); + static_cast(empty_writer.get_stream()).clear(); } void TearDown() {} stan::callbacks::unique_stream_writer writer; + stan::callbacks::unique_stream_writer empty_writer; }; TEST_F(StanInterfaceCallbacksStreamWriter, double_vector) { @@ -27,6 +31,17 @@ TEST_F(StanInterfaceCallbacksStreamWriter, double_vector) { static_cast(writer.get_stream()).str()); } +TEST_F(StanInterfaceCallbacksStreamWriter, empty_test) { + const int N = 5; + std::vector x; + for (int n = 0; n < N; ++n) + x.push_back(n); + + EXPECT_NO_THROW(empty_writer(x)); + EXPECT_EQ("", + static_cast(empty_writer.get_stream()).str()); +} + TEST_F(StanInterfaceCallbacksStreamWriter, double_vector_precision2) { const int N = 5; std::vector x{1.23456789, 2.3456789, 3.45678910, 4.567890123}; From b1c86307d07162b299b7f65ad58f7d41e236d955 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Nov 2021 09:53:04 -0500 Subject: [PATCH 2/9] add docs for empty member and member function --- src/stan/callbacks/stream_writer.hpp | 10 ++++++++-- src/stan/callbacks/tee_writer.hpp | 5 ++++- src/stan/callbacks/unique_stream_writer.hpp | 10 ++++++++-- src/stan/callbacks/writer.hpp | 2 +- src/test/unit/callbacks/tee_writer_test.cpp | 2 +- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/stan/callbacks/stream_writer.hpp b/src/stan/callbacks/stream_writer.hpp index 1da036c97e7..c569723fb8d 100644 --- a/src/stan/callbacks/stream_writer.hpp +++ b/src/stan/callbacks/stream_writer.hpp @@ -70,7 +70,10 @@ class stream_writer : public writer { } } - inline bool is_empty() const { + /** + * Check if the writer is writing to an empty stream + */ + inline bool is_empty() const noexcept final { return empty_; } @@ -86,7 +89,10 @@ class stream_writer : public writer { */ std::string comment_prefix_; - bool empty_; + /** + * Used as check for whether output stream needs to be written to. + */ + bool empty_{false}; /** * Writes a set of values in csv format followed by a newline. * diff --git a/src/stan/callbacks/tee_writer.hpp b/src/stan/callbacks/tee_writer.hpp index 58d5da44492..c8620cc29e6 100644 --- a/src/stan/callbacks/tee_writer.hpp +++ b/src/stan/callbacks/tee_writer.hpp @@ -49,7 +49,10 @@ class tee_writer final : public writer { writer2_(message); } - inline bool is_empty() const { + /** + * Check if both writers are writing to an empty stream + */ + inline bool is_empty() const noexcept { return writer1_.is_empty() && writer2_.is_empty(); } diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index 2f22a417853..895a3465f1f 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -54,7 +54,7 @@ class unique_stream_writer final : public writer { /** * Get the underlying stream */ - auto& get_stream() { return *output_; } + inline auto& get_stream() noexcept { return *output_; } /** * Writes a set of values in csv format followed by a newline. @@ -92,7 +92,10 @@ class unique_stream_writer final : public writer { } } - inline bool is_empty() const { + /** + * Check if the writer is writing to an empty stream + */ + inline bool is_empty() const noexcept { return empty_; } @@ -107,6 +110,9 @@ class unique_stream_writer final : public writer { */ std::string comment_prefix_; + /** + * Used as check for whether output stream needs to be written to. + */ bool empty_{false}; /** * Writes a set of values in csv format followed by a newline. diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 38b20612971..36433f10402 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -49,7 +49,7 @@ class writer { /** * Check if the writer is writing to an empty stream */ - virtual bool is_empty() const { + virtual bool is_empty() const noexcept { return true; } }; diff --git a/src/test/unit/callbacks/tee_writer_test.cpp b/src/test/unit/callbacks/tee_writer_test.cpp index 5a1d88eb8f8..9440ac0f36f 100644 --- a/src/test/unit/callbacks/tee_writer_test.cpp +++ b/src/test/unit/callbacks/tee_writer_test.cpp @@ -20,7 +20,7 @@ class mock_writer : public stan::callbacks::writer { if (!empty_) {++N;} } - inline bool is_empty() { + inline bool is_empty() const noexcept { return false; } }; From 2ca39a46c6aed2281373bf946410865aaa1f57e8 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 9 Nov 2021 14:57:25 +0000 Subject: [PATCH 3/9] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- src/stan/callbacks/stream_writer.hpp | 14 ++++---- src/stan/callbacks/tee_writer.hpp | 1 - src/stan/callbacks/unique_stream_writer.hpp | 16 +++++---- src/stan/callbacks/writer.hpp | 4 +-- src/stan/services/util/mcmc_writer.hpp | 14 ++++---- src/stan/variational/advi.hpp | 4 +-- .../unit/callbacks/stream_writer_test.cpp | 5 ++- src/test/unit/callbacks/tee_writer_test.cpp | 34 ++++++++++++++----- .../callbacks/unique_stream_writer_test.cpp | 6 ++-- 9 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/stan/callbacks/stream_writer.hpp b/src/stan/callbacks/stream_writer.hpp index c569723fb8d..35a06a3e64b 100644 --- a/src/stan/callbacks/stream_writer.hpp +++ b/src/stan/callbacks/stream_writer.hpp @@ -24,7 +24,8 @@ class stream_writer : public writer { * each comment line. Default is "". */ explicit stream_writer(std::ostream& output, - const std::string& comment_prefix = "", bool is_empty = false) + const std::string& comment_prefix = "", + bool is_empty = false) : output_(output), comment_prefix_(comment_prefix), empty_(is_empty) {} /** @@ -57,7 +58,11 @@ class stream_writer : public writer { /** * Writes the comment_prefix to the stream followed by a newline. */ - void operator()() { if (!empty_) {output_ << comment_prefix_ << std::endl;} } + void operator()() { + if (!empty_) { + output_ << comment_prefix_ << std::endl; + } + } /** * Writes the comment_prefix then the message followed by a newline. @@ -73,10 +78,7 @@ class stream_writer : public writer { /** * Check if the writer is writing to an empty stream */ - inline bool is_empty() const noexcept final { - return empty_; - } - + inline bool is_empty() const noexcept final { return empty_; } private: /** diff --git a/src/stan/callbacks/tee_writer.hpp b/src/stan/callbacks/tee_writer.hpp index c8620cc29e6..d0acd77baf4 100644 --- a/src/stan/callbacks/tee_writer.hpp +++ b/src/stan/callbacks/tee_writer.hpp @@ -65,7 +65,6 @@ class tee_writer final : public writer { * The second writer */ writer& writer2_; - }; } // namespace callbacks diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index 895a3465f1f..44fe778bbba 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -27,14 +27,18 @@ class unique_stream_writer final : public writer { * Default is "". */ explicit unique_stream_writer(std::unique_ptr&& output, - const std::string& comment_prefix = "", bool is_empty = false) - : output_(std::move(output)), comment_prefix_(comment_prefix), empty_(is_empty) {} + const std::string& comment_prefix = "", + bool is_empty = false) + : output_(std::move(output)), + comment_prefix_(comment_prefix), + empty_(is_empty) {} unique_stream_writer(); unique_stream_writer(unique_stream_writer& other) = delete; unique_stream_writer(unique_stream_writer&& other) : output_(std::move(other.output_)), - comment_prefix_(std::move(other.comment_prefix_)), empty_(other.empty_) {} + comment_prefix_(std::move(other.comment_prefix_)), + empty_(other.empty_) {} /** * Virtual destructor */ @@ -75,7 +79,7 @@ class unique_stream_writer final : public writer { streamer.precision(output_.get()->precision()); streamer << comment_prefix_ << std::endl; *output_ << streamer.str(); - } + } } /** @@ -95,9 +99,7 @@ class unique_stream_writer final : public writer { /** * Check if the writer is writing to an empty stream */ - inline bool is_empty() const noexcept { - return empty_; - } + inline bool is_empty() const noexcept { return empty_; } private: /** diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 36433f10402..41a8a2327fc 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -49,9 +49,7 @@ class writer { /** * Check if the writer is writing to an empty stream */ - virtual bool is_empty() const noexcept { - return true; - } + virtual bool is_empty() const noexcept { return true; } }; } // namespace callbacks diff --git a/src/stan/services/util/mcmc_writer.hpp b/src/stan/services/util/mcmc_writer.hpp index fd712ab0103..77e7e9d2b4c 100644 --- a/src/stan/services/util/mcmc_writer.hpp +++ b/src/stan/services/util/mcmc_writer.hpp @@ -172,15 +172,15 @@ class mcmc_writer { */ void write_diagnostic_params(stan::mcmc::sample& sample, stan::mcmc::base_mcmc& sampler) { - if (!diagnostic_writer_.is_empty()) { - std::vector values; + if (!diagnostic_writer_.is_empty()) { + std::vector values; - sample.get_sample_params(values); - sampler.get_sampler_params(values); - sampler.get_sampler_diagnostics(values); + sample.get_sample_params(values); + sampler.get_sampler_params(values); + sampler.get_sampler_diagnostics(values); - diagnostic_writer_(values); - } + diagnostic_writer_(values); + } } /** diff --git a/src/stan/variational/advi.hpp b/src/stan/variational/advi.hpp index e89ab70ac11..35fdc19be67 100644 --- a/src/stan/variational/advi.hpp +++ b/src/stan/variational/advi.hpp @@ -462,8 +462,8 @@ class advi { callbacks::writer& parameter_writer, callbacks::writer& diagnostic_writer) const { if (!diagnostic_writer.is_empty()) { - diagnostic_writer("iter,time_in_seconds,ELBO"); - } + diagnostic_writer("iter,time_in_seconds,ELBO"); + } // Initialize variational approximation Q variational = Q(cont_params_); diff --git a/src/test/unit/callbacks/stream_writer_test.cpp b/src/test/unit/callbacks/stream_writer_test.cpp index effba2466c9..18f7839362f 100644 --- a/src/test/unit/callbacks/stream_writer_test.cpp +++ b/src/test/unit/callbacks/stream_writer_test.cpp @@ -5,7 +5,10 @@ class StanInterfaceCallbacksStreamWriter : public ::testing::Test { public: StanInterfaceCallbacksStreamWriter() - : ss(), writer(ss), writer_prefix(ss, "# "), empty_writer(ss, "# ", true) {} + : ss(), + writer(ss), + writer_prefix(ss, "# "), + empty_writer(ss, "# ", true) {} void SetUp() { ss.str(std::string()); diff --git a/src/test/unit/callbacks/tee_writer_test.cpp b/src/test/unit/callbacks/tee_writer_test.cpp index 9440ac0f36f..276f2c618ad 100644 --- a/src/test/unit/callbacks/tee_writer_test.cpp +++ b/src/test/unit/callbacks/tee_writer_test.cpp @@ -10,27 +10,43 @@ class mock_writer : public stan::callbacks::writer { mock_writer() : N(0) {} mock_writer(bool is_empty) : N(0), empty_(is_empty) {} - void operator()(const std::vector& names) { if (!empty_) {++N;} } + void operator()(const std::vector& names) { + if (!empty_) { + ++N; + } + } - void operator()(const std::vector& state) { if (!empty_) {++N;} } + void operator()(const std::vector& state) { + if (!empty_) { + ++N; + } + } - void operator()() { if (!empty_) {++N;} } + void operator()() { + if (!empty_) { + ++N; + } + } void operator()(const std::string& message) { - if (!empty_) {++N;} + if (!empty_) { + ++N; + } } - inline bool is_empty() const noexcept { - return false; - } + inline bool is_empty() const noexcept { return false; } }; } // namespace test class StanCallbacksTeeWriter : public ::testing::Test { public: StanCallbacksTeeWriter() - : writer1(), writer2(), tee_writer(writer1, writer2), - empty_writer1(true), empty_writer2(true), empty_tee_writer(empty_writer1, empty_writer2) {} + : writer1(), + writer2(), + tee_writer(writer1, writer2), + empty_writer1(true), + empty_writer2(true), + empty_tee_writer(empty_writer1, empty_writer2) {} test::mock_writer writer1, writer2; stan::callbacks::tee_writer tee_writer; diff --git a/src/test/unit/callbacks/unique_stream_writer_test.cpp b/src/test/unit/callbacks/unique_stream_writer_test.cpp index 8314aee86d3..7505e63e3e6 100644 --- a/src/test/unit/callbacks/unique_stream_writer_test.cpp +++ b/src/test/unit/callbacks/unique_stream_writer_test.cpp @@ -6,12 +6,14 @@ class StanInterfaceCallbacksStreamWriter : public ::testing::Test { public: StanInterfaceCallbacksStreamWriter() : writer(std::make_unique(std::stringstream{})), - empty_writer(std::make_unique(std::stringstream{}), "#", true) {} + empty_writer(std::make_unique(std::stringstream{}), + "#", true) {} void SetUp() { static_cast(writer.get_stream()).str(std::string()); static_cast(writer.get_stream()).clear(); - static_cast(empty_writer.get_stream()).str(std::string()); + static_cast(empty_writer.get_stream()) + .str(std::string()); static_cast(empty_writer.get_stream()).clear(); } void TearDown() {} From 074ec153da7bb9cfd5083983e8e33397932d40aa Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Nov 2021 12:56:41 -0500 Subject: [PATCH 4/9] fix instrumented_writer --- src/stan/callbacks/writer.hpp | 2 +- src/test/unit/callbacks/tee_writer_test.cpp | 4 +++- src/test/unit/services/experimental/advi/meanfield_test.cpp | 2 +- src/test/unit/services/instrumented_callbacks.hpp | 4 ++++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 41a8a2327fc..cd2454e1019 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -49,7 +49,7 @@ class writer { /** * Check if the writer is writing to an empty stream */ - virtual bool is_empty() const noexcept { return true; } + virtual bool is_empty() const noexcept =0; }; } // namespace callbacks diff --git a/src/test/unit/callbacks/tee_writer_test.cpp b/src/test/unit/callbacks/tee_writer_test.cpp index 276f2c618ad..50a4e9f4d00 100644 --- a/src/test/unit/callbacks/tee_writer_test.cpp +++ b/src/test/unit/callbacks/tee_writer_test.cpp @@ -34,7 +34,9 @@ class mock_writer : public stan::callbacks::writer { } } - inline bool is_empty() const noexcept { return false; } + inline bool is_empty() const noexcept { + return false; + } }; } // namespace test diff --git a/src/test/unit/services/experimental/advi/meanfield_test.cpp b/src/test/unit/services/experimental/advi/meanfield_test.cpp index 118b497922f..cf820561d32 100644 --- a/src/test/unit/services/experimental/advi/meanfield_test.cpp +++ b/src/test/unit/services/experimental/advi/meanfield_test.cpp @@ -1,8 +1,8 @@ #include -#include #include #include #include +#include class ServicesExperimentalAdvi : public testing::Test { public: diff --git a/src/test/unit/services/instrumented_callbacks.hpp b/src/test/unit/services/instrumented_callbacks.hpp index 797959ef0ee..3e04157ee89 100644 --- a/src/test/unit/services/instrumented_callbacks.hpp +++ b/src/test/unit/services/instrumented_callbacks.hpp @@ -41,6 +41,10 @@ class instrumented_writer : public stan::callbacks::writer { public: instrumented_writer() {} + inline bool is_empty() const noexcept { + return false; + } + void operator()(const std::string& key, double value) { counter_["string_double"]++; string_double.push_back(std::make_pair(key, value)); From d480dd179ffa14bfcb2e00f64cbcb204b6ee1365 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Nov 2021 13:07:46 -0500 Subject: [PATCH 5/9] make is_empty() non-pure --- src/stan/callbacks/writer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index cd2454e1019..41a8a2327fc 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -49,7 +49,7 @@ class writer { /** * Check if the writer is writing to an empty stream */ - virtual bool is_empty() const noexcept =0; + virtual bool is_empty() const noexcept { return true; } }; } // namespace callbacks From 9c9aa47b2bd07a5c08280d88789f71295ef1fdd6 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 9 Nov 2021 13:08:03 -0500 Subject: [PATCH 6/9] update --- src/test/unit/callbacks/tee_writer_test.cpp | 4 +--- src/test/unit/services/instrumented_callbacks.hpp | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/test/unit/callbacks/tee_writer_test.cpp b/src/test/unit/callbacks/tee_writer_test.cpp index 50a4e9f4d00..276f2c618ad 100644 --- a/src/test/unit/callbacks/tee_writer_test.cpp +++ b/src/test/unit/callbacks/tee_writer_test.cpp @@ -34,9 +34,7 @@ class mock_writer : public stan::callbacks::writer { } } - inline bool is_empty() const noexcept { - return false; - } + inline bool is_empty() const noexcept { return false; } }; } // namespace test diff --git a/src/test/unit/services/instrumented_callbacks.hpp b/src/test/unit/services/instrumented_callbacks.hpp index 3e04157ee89..61d7ced0e10 100644 --- a/src/test/unit/services/instrumented_callbacks.hpp +++ b/src/test/unit/services/instrumented_callbacks.hpp @@ -41,9 +41,7 @@ class instrumented_writer : public stan::callbacks::writer { public: instrumented_writer() {} - inline bool is_empty() const noexcept { - return false; - } + inline bool is_empty() const noexcept { return false; } void operator()(const std::string& key, double value) { counter_["string_double"]++; From fe56a8d30228adf7b9fc6e7d2576d0ffae6a0a77 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Thu, 11 Nov 2021 11:38:11 -0500 Subject: [PATCH 7/9] update is_empty() to false --- src/stan/callbacks/writer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/writer.hpp b/src/stan/callbacks/writer.hpp index 41a8a2327fc..4e0df8878f2 100644 --- a/src/stan/callbacks/writer.hpp +++ b/src/stan/callbacks/writer.hpp @@ -49,7 +49,7 @@ class writer { /** * Check if the writer is writing to an empty stream */ - virtual bool is_empty() const noexcept { return true; } + virtual bool is_empty() const noexcept { return false; } }; } // namespace callbacks From 192fc78f8fd5a9a7e71178d5dd7cd2af4203c293 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 19 Nov 2021 13:36:42 -0500 Subject: [PATCH 8/9] remove the stringstream from unique_stream_writer --- src/stan/callbacks/unique_stream_writer.hpp | 25 +++++++-------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index 44fe778bbba..05d1a9700cc 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -75,10 +75,7 @@ class unique_stream_writer final : public writer { */ void operator()() { if (!empty_) { - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - streamer << comment_prefix_ << std::endl; - *output_ << streamer.str(); + *output_ << comment_prefix_ << std::endl; } } @@ -89,10 +86,7 @@ class unique_stream_writer final : public writer { */ void operator()(const std::string& message) { if (!empty_) { - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - streamer << comment_prefix_ << message << std::endl; - *output_ << streamer.str(); + *output_ << comment_prefix_ << message << std::endl; } } @@ -127,18 +121,15 @@ class unique_stream_writer final : public writer { template void write_vector(const std::vector& v) { if (!empty_) { - if (v.empty()) + if (v.empty()) { return; - using const_iter = typename std::vector::const_iterator; - const_iter last = v.end(); + } + auto last = v.end(); --last; - std::stringstream streamer; - streamer.precision(output_.get()->precision()); - for (const_iter it = v.begin(); it != last; ++it) { - streamer << *it << ","; + for (auto it = v.begin(); it != last; ++it) { + *output_ << *it << ","; } - streamer << v.back() << std::endl; - *output_ << streamer.str(); + *output_ << v.back() << std::endl; } } }; From 6716d83017721414e838dfe646a6048224623503 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Fri, 19 Nov 2021 18:42:33 +0000 Subject: [PATCH 9/9] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- src/stan/callbacks/unique_stream_writer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/unique_stream_writer.hpp b/src/stan/callbacks/unique_stream_writer.hpp index 05d1a9700cc..79fee41fcdc 100644 --- a/src/stan/callbacks/unique_stream_writer.hpp +++ b/src/stan/callbacks/unique_stream_writer.hpp @@ -127,7 +127,7 @@ class unique_stream_writer final : public writer { auto last = v.end(); --last; for (auto it = v.begin(); it != last; ++it) { - *output_ << *it << ","; + *output_ << *it << ","; } *output_ << v.back() << std::endl; }