Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up instances of .str().length() for logger.info() #3098

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/stan/callbacks/stream_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
3 changes: 1 addition & 2 deletions src/stan/model/gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ void gradient(const M& model, const Eigen::Matrix<double, Eigen::Dynamic, 1>& x,
logger.info(ss);
throw;
}
if (ss.str().length() > 0)
logger.info(ss);
logger.info(ss);
}

} // namespace model
Expand Down
6 changes: 2 additions & 4 deletions src/stan/services/optimize/bfgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ int bfgs(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
Expand Down Expand Up @@ -158,8 +157,7 @@ int bfgs(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/stan/services/optimize/lbfgs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ int lbfgs(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
Expand Down Expand Up @@ -160,8 +159,7 @@ int lbfgs(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
Expand Down
6 changes: 2 additions & 4 deletions src/stan/services/optimize/newton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ int newton(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
}
Expand All @@ -111,8 +110,7 @@ int newton(Model& model, const stan::io::var_context& init,
std::vector<double> 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);
}
Expand Down
21 changes: 7 additions & 14 deletions src/stan/services/util/initialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,15 @@ std::vector<double> 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"
" at the initial value.");
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.");
Expand All @@ -128,20 +126,17 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
// the parameters.
log_prob = model.template log_prob<false, Jacobian>(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"
" at the initial value.");
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.");
Expand All @@ -167,8 +162,7 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
log_prob = stan::model::log_prob_grad<true, Jacobian>(
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;
}
Expand All @@ -177,8 +171,7 @@ std::vector<double> initialize(Model& model, const InitContext& init, RNG& rng,
= std::chrono::duration_cast<std::chrono::microseconds>(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));

Expand Down
3 changes: 1 addition & 2 deletions src/stan/variational/advi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class advi {
try {
std::stringstream ss;
double log_prob = model_.template log_prob<false, true>(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;
Expand Down
3 changes: 1 addition & 2 deletions src/stan/variational/families/normal_fullrank.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/stan/variational/families/normal_meanfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down