refactor max length compute in bestResults

This commit is contained in:
2024-02-01 11:53:11 +01:00
parent d7174e930b
commit c9dc378f98

View File

@@ -52,10 +52,6 @@ namespace platform {
} }
} }
std::string bestFileName = path + bestResultFile(); std::string bestFileName = path + bestResultFile();
if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) {
fclose(fileTest);
std::cout << Colors::MAGENTA() << "File " << bestFileName << " already exists and it shall be overwritten." << Colors::RESET() << std::endl;
}
std::ofstream file(bestFileName); std::ofstream file(bestFileName);
file << bests; file << bests;
file.close(); file.close();
@@ -119,6 +115,8 @@ namespace platform {
models.insert(fileModel); models.insert(fileModel);
} }
result = std::vector<std::string>(models.begin(), models.end()); result = std::vector<std::string>(models.begin(), models.end());
maxModelName = (*max_element(result.begin(), result.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
maxModelName = std::max(12, maxModelName);
return result; return result;
} }
std::vector<std::string> BestResults::getDatasets(json table) std::vector<std::string> BestResults::getDatasets(json table)
@@ -127,16 +125,20 @@ namespace platform {
for (const auto& dataset : table.items()) { for (const auto& dataset : table.items()) {
datasets.push_back(dataset.key()); datasets.push_back(dataset.key());
} }
maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
maxDatasetName = std::max(7, maxDatasetName);
return datasets; return datasets;
} }
void BestResults::buildAll() void BestResults::buildAll()
{ {
auto models = getModels(); auto models = getModels();
std::cout << "Building best results for model: ";
for (const auto& model : models) { for (const auto& model : models) {
std::cout << "Building best results for model: " << model << std::endl;
this->model = model; this->model = model;
std::cout << model << ", ";
build(); build();
} }
std::cout << "end." << std::endl << std::endl;;
model = "any"; model = "any";
} }
void BestResults::listFile() void BestResults::listFile()
@@ -152,7 +154,6 @@ namespace platform {
auto date = ftime_to_string(std::filesystem::last_write_time(bestFileName)); auto date = ftime_to_string(std::filesystem::last_write_time(bestFileName));
auto data = loadFile(bestFileName); auto data = loadFile(bestFileName);
auto datasets = getDatasets(data); auto datasets = getDatasets(data);
int maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
int maxFileName = 0; int maxFileName = 0;
int maxHyper = 15; int maxHyper = 15;
for (auto const& item : data.items()) { for (auto const& item : data.items()) {
@@ -181,7 +182,8 @@ namespace platform {
odd = !odd; odd = !odd;
} }
std::cout << Colors::GREEN() << "=== " << std::string(maxDatasetName, '=') << " ===========" << std::endl; std::cout << Colors::GREEN() << "=== " << std::string(maxDatasetName, '=') << " ===========" << std::endl;
std::cout << std::setw(5 + maxDatasetName) << "Total.................. " << std::setw(11) << std::setprecision(8) << std::fixed << total << std::endl; std::cout << Colors::GREEN() << " Total" << std::string(maxDatasetName - 5, '.') << " " << std::setw(11) << std::setprecision(8) << std::fixed << total << std::endl;
} }
json BestResults::buildTableResults(std::vector<std::string> models) json BestResults::buildTableResults(std::vector<std::string> models)
{ {
@@ -276,16 +278,16 @@ namespace platform {
std::cout << std::string(maxModelName, '=') << " "; std::cout << std::string(maxModelName, '=') << " ";
} }
std::cout << std::endl; std::cout << std::endl;
std::cout << Colors::GREEN() << std::setw(5 + maxDatasetName) << " Totals..................."; std::cout << Colors::GREEN() << " Totals" << std::string(maxDatasetName - 6, '.') << " ";
double max = 0.0; double max_value = 0.0;
for (const auto& total : totals) { for (const auto& total : totals) {
if (total.second > max) { if (total.second > max_value) {
max = total.second; max_value = total.second;
} }
} }
for (const auto& model : models) { for (const auto& model : models) {
std::string efectiveColor = Colors::GREEN(); std::string efectiveColor = Colors::GREEN();
if (totals[model] == max) { if (totals[model] == max_value) {
efectiveColor = Colors::RED(); efectiveColor = Colors::RED();
} }
std::cout << efectiveColor << std::right << std::setw(maxModelName) << std::setprecision(maxModelName - 4) << std::fixed << totals[model] << " "; std::cout << efectiveColor << std::right << std::setw(maxModelName) << std::setprecision(maxModelName - 4) << std::fixed << totals[model] << " ";
@@ -311,10 +313,6 @@ namespace platform {
// Build the table of results // Build the table of results
json table = buildTableResults(models); json table = buildTableResults(models);
std::vector<std::string> datasets = getDatasets(table.begin().value()); std::vector<std::string> datasets = getDatasets(table.begin().value());
maxModelName = (*max_element(models.begin(), models.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
maxModelName = std::max(12, maxModelName);
maxDatasetName = (*max_element(datasets.begin(), datasets.end(), [](const std::string& a, const std::string& b) { return a.size() < b.size(); })).size();
maxDatasetName = std::max(25, maxDatasetName);
// Print the table of results // Print the table of results
printTableResults(models, table); printTableResults(models, table);
// Compute the Friedman test // Compute the Friedman test