From 8f329afcc061cd51dc023cef9a12431e3056d982 Mon Sep 17 00:00:00 2001 From: Anika Chowdhury Date: Sat, 29 Jan 2022 15:20:59 -0500 Subject: [PATCH 1/2] Cleaning up instances of .str().length() for logger.info() --- src/stan/callbacks/stream_logger.hpp | 3 ++- src/stan/model/gradient.hpp | 3 +-- src/stan/services/optimize/bfgs.hpp | 6 ++---- src/stan/services/optimize/lbfgs.hpp | 6 ++---- src/stan/services/optimize/newton.hpp | 6 ++---- src/stan/services/util/initialize.hpp | 21 +++++++------------ src/stan/variational/advi.hpp | 3 +-- .../variational/families/normal_fullrank.hpp | 3 +-- .../variational/families/normal_meanfield.hpp | 3 +-- 9 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/stan/callbacks/stream_logger.hpp b/src/stan/callbacks/stream_logger.hpp index 677add7c707..9ea189ab9ec 100644 --- a/src/stan/callbacks/stream_logger.hpp +++ b/src/stan/callbacks/stream_logger.hpp @@ -46,7 +46,8 @@ class stream_logger final : public logger { void info(const std::string& message) { info_ << message << std::endl; } void info(const std::stringstream& message) { - info_ << message.str() << std::endl; + if(message.str().length() > 0) + info_ << message.str() << std::endl; } void warn(const std::string& message) { warn_ << message << std::endl; } diff --git a/src/stan/model/gradient.hpp b/src/stan/model/gradient.hpp index 12a3f318a3e..79eb49456b8 100644 --- a/src/stan/model/gradient.hpp +++ b/src/stan/model/gradient.hpp @@ -30,8 +30,7 @@ void gradient(const M& model, const Eigen::Matrix& x, logger.info(ss); throw; } - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); } } // namespace model diff --git a/src/stan/services/optimize/bfgs.hpp b/src/stan/services/optimize/bfgs.hpp index d2396217057..31069619c3e 100644 --- a/src/stan/services/optimize/bfgs.hpp +++ b/src/stan/services/optimize/bfgs.hpp @@ -90,8 +90,7 @@ int bfgs(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream msg; model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg); - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); values.insert(values.begin(), lp); parameter_writer(values); @@ -158,8 +157,7 @@ int bfgs(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream msg; model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg); - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); values.insert(values.begin(), lp); parameter_writer(values); } diff --git a/src/stan/services/optimize/lbfgs.hpp b/src/stan/services/optimize/lbfgs.hpp index 1c06a99440d..afb0fa2c505 100644 --- a/src/stan/services/optimize/lbfgs.hpp +++ b/src/stan/services/optimize/lbfgs.hpp @@ -93,8 +93,7 @@ int lbfgs(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream msg; model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg); - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); values.insert(values.begin(), lp); parameter_writer(values); @@ -160,8 +159,7 @@ int lbfgs(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream msg; model.write_array(rng, cont_vector, disc_vector, values, true, true, &msg); - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); values.insert(values.begin(), lp); parameter_writer(values); diff --git a/src/stan/services/optimize/newton.hpp b/src/stan/services/optimize/newton.hpp index 82c3f8c9a83..596f0328bd4 100644 --- a/src/stan/services/optimize/newton.hpp +++ b/src/stan/services/optimize/newton.hpp @@ -88,8 +88,7 @@ int newton(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream ss; model.write_array(rng, cont_vector, disc_vector, values, true, true, &ss); - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); values.insert(values.begin(), lp); parameter_writer(values); } @@ -111,8 +110,7 @@ int newton(Model& model, const stan::io::var_context& init, std::vector values; std::stringstream ss; model.write_array(rng, cont_vector, disc_vector, values, true, true, &ss); - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); values.insert(values.begin(), lp); parameter_writer(values); } diff --git a/src/stan/services/util/initialize.hpp b/src/stan/services/util/initialize.hpp index 696013531c9..d117ca99010 100644 --- a/src/stan/services/util/initialize.hpp +++ b/src/stan/services/util/initialize.hpp @@ -102,8 +102,7 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, model.transform_inits(context, disc_vector, unconstrained, &msg); } } catch (std::domain_error& e) { - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); logger.info("Rejecting initial value:"); logger.info( " Error evaluating the log probability" @@ -111,8 +110,7 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, logger.info(e.what()); continue; } catch (std::exception& e) { - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); logger.info( "Unrecoverable error evaluating the log probability" " at the initial value."); @@ -128,11 +126,9 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, // the parameters. log_prob = model.template log_prob(unconstrained, disc_vector, &msg); - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); } catch (std::domain_error& e) { - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); logger.info("Rejecting initial value:"); logger.info( " Error evaluating the log probability" @@ -140,8 +136,7 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, logger.info(e.what()); continue; } catch (std::exception& e) { - if (msg.str().length() > 0) - logger.info(msg); + logger.info(msg); logger.info( "Unrecoverable error evaluating the log probability" " at the initial value."); @@ -167,8 +162,7 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, log_prob = stan::model::log_prob_grad( model, unconstrained, disc_vector, gradient, &log_prob_msg); } catch (const std::exception& e) { - if (log_prob_msg.str().length() > 0) - logger.info(log_prob_msg); + logger.info(log_prob_msg); logger.info(e.what()); throw; } @@ -177,8 +171,7 @@ std::vector initialize(Model& model, const InitContext& init, RNG& rng, = std::chrono::duration_cast(end - start) .count() / 1000000.0; - if (log_prob_msg.str().length() > 0) - logger.info(log_prob_msg); + logger.info(log_prob_msg); bool gradient_ok = std::isfinite(stan::math::sum(gradient)); diff --git a/src/stan/variational/advi.hpp b/src/stan/variational/advi.hpp index 4dca19d0335..dd114e4539c 100644 --- a/src/stan/variational/advi.hpp +++ b/src/stan/variational/advi.hpp @@ -103,8 +103,7 @@ class advi { try { std::stringstream ss; double log_prob = model_.template log_prob(zeta, &ss); - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); stan::math::check_finite(function, "log_prob", log_prob); elbo += log_prob; ++i; diff --git a/src/stan/variational/families/normal_fullrank.hpp b/src/stan/variational/families/normal_fullrank.hpp index 4f421a183be..8973c777faf 100644 --- a/src/stan/variational/families/normal_fullrank.hpp +++ b/src/stan/variational/families/normal_fullrank.hpp @@ -428,8 +428,7 @@ class normal_fullrank : public base_family { try { std::stringstream ss; stan::model::gradient(m, zeta, tmp_lp, tmp_mu_grad, &ss); - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); stan::math::check_finite(function, "Gradient of mu", tmp_mu_grad); mu_grad += tmp_mu_grad; diff --git a/src/stan/variational/families/normal_meanfield.hpp b/src/stan/variational/families/normal_meanfield.hpp index afb47c0b13b..f1900d300af 100644 --- a/src/stan/variational/families/normal_meanfield.hpp +++ b/src/stan/variational/families/normal_meanfield.hpp @@ -363,8 +363,7 @@ class normal_meanfield : public base_family { try { std::stringstream ss; stan::model::gradient(m, zeta, tmp_lp, tmp_mu_grad, &ss); - if (ss.str().length() > 0) - logger.info(ss); + logger.info(ss); stan::math::check_finite(function, "Gradient of mu", tmp_mu_grad); mu_grad += tmp_mu_grad; omega_grad.array() += tmp_mu_grad.array().cwiseProduct(eta.array()); From cb2d7e3f86550d05644ac8369b7ab83f0a75c684 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Sat, 29 Jan 2022 20:31:53 +0000 Subject: [PATCH 2/2] [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final) --- src/stan/callbacks/stream_logger.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stan/callbacks/stream_logger.hpp b/src/stan/callbacks/stream_logger.hpp index 9ea189ab9ec..548f46ccbea 100644 --- a/src/stan/callbacks/stream_logger.hpp +++ b/src/stan/callbacks/stream_logger.hpp @@ -46,7 +46,7 @@ class stream_logger final : public logger { void info(const std::string& message) { info_ << message << std::endl; } void info(const std::stringstream& message) { - if(message.str().length() > 0) + if (message.str().length() > 0) info_ << message.str() << std::endl; }