From 51e51c28f1a952bc37db604ece408f0e93a5dd6c Mon Sep 17 00:00:00 2001 From: Gennady Pospelov <g.pospelov@fz-juelich.de> Date: Mon, 16 Jul 2018 13:40:31 +0200 Subject: [PATCH] New FitPrintService prints more meaningful info during iterations --- Core/Fitting/FitPrintService.cpp | 80 ++++++++++++++++++++++++++++---- Core/Fitting/FitPrintService.h | 5 +- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/Core/Fitting/FitPrintService.cpp b/Core/Fitting/FitPrintService.cpp index 046c144d3c4..120f6ab5057 100644 --- a/Core/Fitting/FitPrintService.cpp +++ b/Core/Fitting/FitPrintService.cpp @@ -14,33 +14,97 @@ #include "FitPrintService.h" #include "FitObjective.h" +#include "StringUtils.h" #include <iostream> #include <iomanip> +#include <sstream> -FitPrintService::FitPrintService() + +namespace { + +size_t length_of_longest_name(const Fit::Parameters& params) { + size_t result(0); + for (const auto& par : params) { + if (par.name().size() + result) + result = par.name().size(); + } + return result; +} } +FitPrintService::FitPrintService() = default; + void FitPrintService::print(const FitObjective& objective) { + std::ostringstream ostr; + if (objective.iterationCount() == 0) { m_run_time.start(); m_last_call_time.start(); } - std::cout << "FitPrintService::print() " << objective.iterationCount() << std::endl; + ostr << iterationHeaderString(objective); + ostr << wallTimeString(); + ostr << parameterString(objective); if (objective.isCompleted()) - printFitResults(); + ostr << fitResultString(); + + std::cout << ostr.str() << "\n"; +} + +std::string FitPrintService::iterationHeaderString(const FitObjective& objective) +{ + std::ostringstream result; + + result << "FitPrintObserver::update() -> Info." + << " NCall:" << objective.iterationCount() + << " Chi2:" << std::scientific << std::setprecision(8) + << objective.iterationInfo().chi2() << "\n"; + + return result.str(); +} +std::string FitPrintService::wallTimeString() +{ + std::ostringstream result; + + m_last_call_time.stop(); + result << "Wall time since last call:" << std::fixed << std::setprecision(2) + << m_last_call_time.runTime() << "\n"; + m_last_call_time.start(); + + return result.str(); } -void FitPrintService::printFitResults() +std::string FitPrintService::parameterString(const FitObjective& objective) { - std::cout << "This was the last iteration." << std::endl; + std::ostringstream result; + + const auto params = objective.iterationInfo().parameters(); + const auto length = length_of_longest_name(params); + + for (const auto& par : params) { + result << StringUtils::padRight(par.name(), length) + << std::scientific << std::setprecision(6) + << " " << par.startValue() << " " << par.limits().toString() + << " " << par.value() << "\n"; + } + + return result.str(); +} + +std::string FitPrintService::fitResultString() +{ + std::ostringstream result; + m_run_time.stop(); - std::cout << "Total time spend: " << std::fixed << std::setprecision(2) - << m_run_time.runTime() << " sec." <<std::endl; - std::cout << std::endl; + + result << "This was the last iteration." << std::endl; + result << "Total time spend: " << std::fixed << std::setprecision(2) + << m_run_time.runTime() << " sec." << "\n\n"; + + return result.str(); } diff --git a/Core/Fitting/FitPrintService.h b/Core/Fitting/FitPrintService.h index 3cda7b1648e..ef811f3b5d4 100644 --- a/Core/Fitting/FitPrintService.h +++ b/Core/Fitting/FitPrintService.h @@ -30,7 +30,10 @@ public: void print(const FitObjective& objective); private: - void printFitResults(); + std::string iterationHeaderString(const FitObjective& objective); + std::string wallTimeString(); + std::string parameterString(const FitObjective& objective); + std::string fitResultString(); TimeInterval m_run_time; TimeInterval m_last_call_time; -- GitLab