From c4f9187e2a811a2f5e2d590e4ab041a3e758e13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Fri, 22 Sep 2023 01:03:55 +0200 Subject: [PATCH] Complete best build and report --- src/Platform/BestResults.cc | 46 +++++++++++++++++++++++++++++++++---- src/Platform/BestResults.h | 6 +++-- src/Platform/Result.cc | 1 + src/Platform/best.cc | 14 +++++++---- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/Platform/BestResults.cc b/src/Platform/BestResults.cc index b0e75f7..95b38bb 100644 --- a/src/Platform/BestResults.cc +++ b/src/Platform/BestResults.cc @@ -7,11 +7,12 @@ namespace platform { - void BestResults::build() + string BestResults::build() { auto files = loadFiles(); if (files.size() == 0) { - throw runtime_error("No result files were found!"); + cerr << Colors::MAGENTA() << "No result files were found!" << Colors::RESET() << endl; + exit(1); } json bests; for (const auto& file : files) { @@ -20,7 +21,7 @@ namespace platform { for (auto const& item : data.at("results")) { bool update = false; if (bests.contains(item.at("dataset").get())) { - if (item.at("score").get() > bests["dataset"].at(0).get()) { + if (item.at("score").get() > bests[item.at("dataset").get()].at(0).get()) { update = true; } } else { @@ -31,14 +32,15 @@ namespace platform { } } } - string bestFileName = path + "/" + bestResultFile(); + string bestFileName = path + bestResultFile(); if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) { fclose(fileTest); - cout << Colors::MAGENTA() << "File " << bestFileName << " already exists and it shall be overwritten." << Colors::RESET(); + cout << Colors::MAGENTA() << "File " << bestFileName << " already exists and it shall be overwritten." << Colors::RESET() << endl; } ofstream file(bestFileName); file << bests; file.close(); + return bestFileName; } string BestResults::bestResultFile() @@ -60,9 +62,43 @@ namespace platform { } return files; } + json BestResults::loadFile(const string& fileName) + { + ifstream resultData(fileName); + if (resultData.is_open()) { + json data = json::parse(resultData); + return data; + } + throw invalid_argument("Unable to open result file. [" + fileName + "]"); + } void BestResults::report() { + string bestFileName = path + bestResultFile(); + if (FILE* fileTest = fopen(bestFileName.c_str(), "r")) { + fclose(fileTest); + } else { + cerr << Colors::MAGENTA() << "File " << bestFileName << " doesn't exist." << Colors::RESET() << endl; + exit(1); + } + auto data = loadFile(bestFileName); + cout << Colors::GREEN() << "Best results for " << model << " and " << score << endl; + cout << "------------------------------------------" << endl; + cout << Colors::GREEN() << " # Dataset Score File Hyperparameters" << endl; + cout << "=== ========================= =========== ================================================================== ================================================= " << endl; + auto i = 0; + bool odd = true; + for (auto const& item : data.items()) { + auto color = odd ? Colors::BLUE() : Colors::CYAN(); + cout << color << setw(3) << fixed << right << i++ << " "; + cout << setw(25) << left << item.key() << " "; + cout << setw(11) << setprecision(9) << fixed << item.value().at(0).get() << " "; + cout << setw(66) << item.value().at(2).get() << " "; + cout << item.value().at(1) << " "; + cout << endl; + odd = !odd; + } + } } \ No newline at end of file diff --git a/src/Platform/BestResults.h b/src/Platform/BestResults.h index 05c04f7..91bc45a 100644 --- a/src/Platform/BestResults.h +++ b/src/Platform/BestResults.h @@ -1,17 +1,19 @@ #ifndef BESTRESULTS_H #define BESTRESULTS_H #include +#include using namespace std; - +using json = nlohmann::json; namespace platform { class BestResults { public: explicit BestResults(const string& path, const string& score, const string& model) : path(path), score(score), model(model) {} - void build(); + string build(); void report(); private: vector loadFiles(); string bestResultFile(); + json loadFile(const string& fileName); string path; string score; string model; diff --git a/src/Platform/Result.cc b/src/Platform/Result.cc index a438238..156ffa7 100644 --- a/src/Platform/Result.cc +++ b/src/Platform/Result.cc @@ -1,5 +1,6 @@ #include #include +#include #include "Result.h" #include "Colors.h" #include "BestScore.h" diff --git a/src/Platform/best.cc b/src/Platform/best.cc index c4bd9fc..e76ccdd 100644 --- a/src/Platform/best.cc +++ b/src/Platform/best.cc @@ -2,14 +2,15 @@ #include #include "Paths.h" #include "BestResults.h" +#include "Colors.h" using namespace std; argparse::ArgumentParser manageArguments(int argc, char** argv) { argparse::ArgumentParser program("best"); - program.add_argument("-m", "--model").default_value("any").help("Filter results of the selected model)"); - program.add_argument("-s", "--score").default_value("any").help("Filter results of the score name supplied"); + program.add_argument("-m", "--model").default_value("").help("Filter results of the selected model)"); + program.add_argument("-s", "--score").default_value("").help("Filter results of the score name supplied"); program.add_argument("--build").help("build best score results file").default_value(false).implicit_value(true); program.add_argument("--report").help("report of best score results file").default_value(false).implicit_value(true); try { @@ -18,6 +19,9 @@ argparse::ArgumentParser manageArguments(int argc, char** argv) auto score = program.get("score"); auto build = program.get("build"); auto report = program.get("report"); + if (model == "" || score == "") { + throw runtime_error("Model and score name must be supplied"); + } } catch (const exception& err) { cerr << err.what() << endl; @@ -35,12 +39,14 @@ int main(int argc, char** argv) auto build = program.get("build"); auto report = program.get("report"); if (!report && !build) { - cout << "Either build, report or both, have to be selected to do anything!" << endl; + cerr << "Either build, report or both, have to be selected to do anything!" << endl; + cerr << program; exit(1); } auto results = platform::BestResults(platform::Paths::results(), model, score); if (build) { - results.build(); + string fileName = results.build(); + cout << Colors::GREEN() << fileName << " created!" << Colors::RESET() << endl; } if (report) { results.report();