Skip to content
Snippets Groups Projects
Commit 51e51c28 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

New FitPrintService prints more meaningful info during iterations

parent 61b76530
No related branches found
No related tags found
No related merge requests found
...@@ -14,33 +14,97 @@ ...@@ -14,33 +14,97 @@
#include "FitPrintService.h" #include "FitPrintService.h"
#include "FitObjective.h" #include "FitObjective.h"
#include "StringUtils.h"
#include <iostream> #include <iostream>
#include <iomanip> #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) void FitPrintService::print(const FitObjective& objective)
{ {
std::ostringstream ostr;
if (objective.iterationCount() == 0) { if (objective.iterationCount() == 0) {
m_run_time.start(); m_run_time.start();
m_last_call_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()) 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(); m_run_time.stop();
std::cout << "Total time spend: " << std::fixed << std::setprecision(2)
<< m_run_time.runTime() << " sec." <<std::endl; result << "This was the last iteration." << std::endl;
std::cout << std::endl; result << "Total time spend: " << std::fixed << std::setprecision(2)
<< m_run_time.runTime() << " sec." << "\n\n";
return result.str();
} }
...@@ -30,7 +30,10 @@ public: ...@@ -30,7 +30,10 @@ public:
void print(const FitObjective& objective); void print(const FitObjective& objective);
private: 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_run_time;
TimeInterval m_last_call_time; TimeInterval m_last_call_time;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment