Continue TeX output

This commit is contained in:
2024-09-02 20:30:47 +02:00
parent 4545f76667
commit 4dbd76df55
6 changed files with 143 additions and 71 deletions

View File

@@ -7,8 +7,10 @@
#include "common/Colors.h"
#include "common/CLocale.h"
#include "common/Paths.h"
#include "common/Utils.h" // compute_std
#include "results/Result.h"
#include "BestResultsExcel.h"
#include "BestResultsTex.h"
#include "best/Statistics.h"
#include "BestResults.h"
@@ -210,56 +212,20 @@ namespace platform {
table["dateTable"] = ftime_to_string(maxDate);
return table;
}
double compute_std(std::vector<double> values, double mean)
{
// Compute standard devation of the values
double sum = 0.0;
for (const auto& value : values) {
sum += std::pow(value - mean, 2);
}
double variance = sum / values.size();
return std::sqrt(variance);
}
void BestResults::printTableResults(std::vector<std::string> models, json table, bool tex)
{
std::stringstream oss;
oss << Colors::GREEN() << "Best results for " << score << " as of " << table.at("dateTable").get<std::string>() << std::endl;
std::FILE* output_tex;
std::cout << oss.str();
std::cout << std::string(oss.str().size() - 8, '-') << std::endl;
std::cout << Colors::GREEN() << " # " << std::setw(maxDatasetName + 1) << std::left << std::string("Dataset");
auto bestResultsTex = BestResultsTex(models, table.at("dateTable").get<std::string>());
if (tex) {
auto file_name = Paths::tex_output();
output_tex = fopen(file_name.c_str(), "w");
if (output_tex == NULL) {
std::cerr << "Error opening file "<< file_name << std::endl;
exit(1);
}
fprintf(output_tex, "%% This file has been generated by the platform program\n");
fprintf(output_tex, "%% Date: %s\n", table.at("dateTable").get<std::string>().c_str());
fprintf(output_tex, "%%\n");
fprintf(output_tex, "%% Table of results\n");
fprintf(output_tex, "%%\n");
fprintf(output_tex, "\\begin{table}[htbp] \n");
fprintf(output_tex, "\\centering \n");
fprintf(output_tex, "\\tiny \n");
fprintf(output_tex, "\\renewcommand{\\arraystretch }{1.2} \n");
fprintf(output_tex, "\\renewcommand{\\tabcolsep }{0.07cm} \n");
fprintf(output_tex, "\\caption{Accuracy results(mean ± std) for all the algorithms and datasets} \n");
fprintf(output_tex, "\\label{tab:results_accuracy}\n");
fprintf(output_tex, "\\begin{tabular} {{r%s}}\n", std::string(models.size(), 'c').c_str());
fprintf(output_tex, "\\hline \n");
fprintf(output_tex, "Id");
bestResultsTex.results_header();
}
for (const auto& model : models) {
std::cout << std::setw(maxModelName) << std::left << model << " ";
if (tex) {
fprintf(output_tex, "& %s ", model.c_str());
}
}
if (tex) {
fprintf(output_tex, "\\\\ \n");
fprintf(output_tex, "\\hline \n");
}
std::cout << std::endl;
std::cout << "=== " << std::string(maxDatasetName, '=') << " ";
@@ -271,7 +237,9 @@ namespace platform {
std::map<std::string, std::vector<double>> totals;
int nDatasets = table.begin().value().size();
auto datasets = getDatasets(table.begin().value());
if (tex) {
bestResultsTex.results_body(datasets, table);
}
for (auto const& dataset_ : datasets) {
auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN();
std::cout << color << std::setw(3) << std::fixed << std::right << i++ << " ";
@@ -290,9 +258,6 @@ namespace platform {
maxValue = value;
}
}
if (tex) {
fprintf(output_tex, "%d ", i);
}
// Print the row with red colors on max values
for (const auto& model : models) {
std::string efectiveColor = color;
@@ -312,16 +277,8 @@ namespace platform {
totals[model].push_back(value);
std::cout << efectiveColor << std::setw(maxModelName) << std::setprecision(maxModelName - 2) << std::fixed << value << " ";
}
if (tex) {
auto std_value = table[model].at(dataset_).at(3).get<double>();
const char* bold = value == maxValue ? "\\bfseries" : "";
fprintf(output_tex, "& %s %0.4f±%0.3f", bold, value, std_value);
}
}
std::cout << std::endl;
if (tex) {
fprintf(output_tex, "\\\\\n");
}
}
std::cout << Colors::GREEN() << "=== " << std::string(maxDatasetName, '=') << " ";
for (const auto& model : models) {
@@ -339,23 +296,14 @@ namespace platform {
}
}
if (tex) {
fprintf(output_tex, "\\hline \n");
fprintf(output_tex, "Average ");
bestResultsTex.results_footer(totals, best_model);
}
for (const auto& model : models) {
std::string efectiveColor = model == best_model ? Colors::RED() : Colors::GREEN();
double value = std::reduce(totals[model].begin(), totals[model].end()) / nDatasets;
double std_value = compute_std(totals[model], value);
std::cout << efectiveColor << std::right << std::setw(maxModelName) << std::setprecision(maxModelName - 4) << std::fixed << value << " ";
if (tex) {
const char* bold = model == best_model ? "\\bfseries" : "";
fprintf(output_tex, "& %s %0.4f±%0.3f", bold, value, std_value);
}
}
if (tex) {
// Footer for TeX
fprintf(output_tex, "\\ \n\\hline \n\\end{tabular}\n\\end{table}\n");
fclose(output_tex);
}
std::cout << std::endl;
}
@@ -389,7 +337,7 @@ namespace platform {
ranksModels = stats.getRanks();
}
if (tex) {
messageOutputFile("TeX", Paths::tex_output());
messageOutputFile("TeX", Paths::tex() + Paths::tex_output());
}
if (excel) {
BestResultsExcel excel(score, datasets);