From 2a3fc9aa4569d39b9d296868400e038acc547ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Mon, 14 Aug 2023 17:03:06 +0200 Subject: [PATCH] Add colors and enhace input control --- src/Platform/Colors.h | 14 ++++++++++++++ src/Platform/Report.cc | 28 ++++++++++++++++++---------- src/Platform/Report.h | 1 + src/Platform/Results.cc | 30 +++++++++++++++++++++--------- 4 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 src/Platform/Colors.h diff --git a/src/Platform/Colors.h b/src/Platform/Colors.h new file mode 100644 index 0000000..7ab2e08 --- /dev/null +++ b/src/Platform/Colors.h @@ -0,0 +1,14 @@ +#ifndef COLORS_H +#define COLORS_H +class Colors { +public: + static std::string MAGENTA() { return "\033[1;35m"; } + static std::string BLUE() { return "\033[1;34m"; } + static std::string CYAN() { return "\033[1;36m"; } + static std::string GREEN() { return "\033[1;32m"; } + static std::string YELLOW() { return "\033[1;33m"; } + static std::string RED() { return "\033[1;31m"; } + static std::string WHITE() { return "\033[1;37m"; } + static std::string RESET() { return "\033[0m"; } +}; +#endif // COLORS_H \ No newline at end of file diff --git a/src/Platform/Report.cc b/src/Platform/Report.cc index 7bd7d69..a40a482 100644 --- a/src/Platform/Report.cc +++ b/src/Platform/Report.cc @@ -33,7 +33,7 @@ namespace platform { } void Report::header() { - cout << string(MAXL, '*') << endl; + cout << Colors::MAGENTA() << string(MAXL, '*') << endl; cout << headerLine("Report " + data["model"].get() + " ver. " + data["version"].get() + " with " + to_string(data["folds"].get()) + " Folds cross validation and " + to_string(data["seeds"].size()) + " random seeds. " + data["date"].get() + " " + data["time"].get()); cout << headerLine(data["title"].get()); cout << headerLine("Random seeds: " + fromVector("seeds") + " Stratified: " + (data["stratified"].get() ? "True" : "False")); @@ -44,24 +44,32 @@ namespace platform { } void Report::body() { - cout << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl; - cout << "============================== ====== ===== === ======= ======= ======= =============== ================= ===============" << endl; + cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl; + cout << "============================== ====== ===== === ======= ======= ======= =============== ================== ===============" << endl; json lastResult; totalScore = 0; + bool odd = true; for (const auto& r : data["results"]) { - cout << setw(30) << left << r["dataset"].get() << " "; + auto color = odd ? Colors::CYAN() : Colors::BLUE(); + cout << color << setw(30) << left << r["dataset"].get() << " "; cout << setw(6) << right << r["samples"].get() << " "; cout << setw(5) << right << r["features"].get() << " "; cout << setw(3) << right << r["classes"].get() << " "; cout << setw(7) << setprecision(2) << fixed << r["nodes"].get() << " "; cout << setw(7) << setprecision(2) << fixed << r["leaves"].get() << " "; cout << setw(7) << setprecision(2) << fixed << r["depth"].get() << " "; - cout << setw(8) << right << setprecision(6) << fixed << r["score_test"].get() << "±" << setw(6) << setprecision(4) << fixed << r["score_test_std"].get() << " "; - cout << setw(10) << right << setprecision(6) << fixed << r["test_time"].get() << "±" << setw(6) << setprecision(4) << fixed << r["test_time_std"].get() << " "; - cout << " " << r["hyperparameters"].get(); + cout << setw(8) << right << setprecision(6) << fixed << r["score"].get() << "±" << setw(6) << setprecision(4) << fixed << r["score_std"].get() << " "; + cout << setw(11) << right << setprecision(6) << fixed << r["time"].get() << "±" << setw(6) << setprecision(4) << fixed << r["time_std"].get() << " "; + try { + cout << r["hyperparameters"].get(); + } + catch (const exception& err) { + cout << r["hyperparameters"]; + } cout << endl; lastResult = r; - totalScore += r["score_test"].get(); + totalScore += r["score"].get(); + odd = !odd; } if (data["results"].size() == 1) { cout << string(MAXL, '*') << endl; @@ -74,12 +82,12 @@ namespace platform { } void Report::footer() { - cout << string(MAXL, '*') << endl; + cout << Colors::MAGENTA() << string(MAXL, '*') << endl; auto score = data["score_name"].get(); if (score == BestResult::scoreName()) { cout << headerLine(score + " compared to " + BestResult::title() + " .: " + to_string(totalScore / BestResult::score())); } - cout << string(MAXL, '*') << endl; + cout << string(MAXL, '*') << endl << Colors::RESET(); } } \ No newline at end of file diff --git a/src/Platform/Report.h b/src/Platform/Report.h index 302ac60..5934b2f 100644 --- a/src/Platform/Report.h +++ b/src/Platform/Report.h @@ -3,6 +3,7 @@ #include #include #include +#include "Colors.h" using json = nlohmann::json; const int MAXL = 121; diff --git a/src/Platform/Results.cc b/src/Platform/Results.cc index 48c7e9a..0bf4070 100644 --- a/src/Platform/Results.cc +++ b/src/Platform/Results.cc @@ -3,6 +3,7 @@ #include "Results.h" #include "Report.h" #include "BestResult.h" +#include "Colors.h" namespace platform { Result::Result(const string& path, const string& filename) : path(path) @@ -59,25 +60,35 @@ namespace platform { } void Results::show() const { - cout << "Results found: " << files.size() << endl; + cout << Colors::GREEN() << "Results found: " << files.size() << endl; cout << "-------------------" << endl; auto i = 0; cout << " # Date Model Score Name Score Duration Title" << endl; cout << "=== ========== ============ =========== =========== ========= =============================================================" << endl; + bool odd = true; for (const auto& result : files) { - cout << setw(3) << fixed << right << i++ << " "; + auto color = odd ? Colors::BLUE() : Colors::CYAN(); + cout << color << setw(3) << fixed << right << i++ << " "; cout << result.to_string() << endl; if (i == max && max != 0) { break; } + odd = !odd; } } int Results::getIndex(const string& intent) const { - cout << "Choose result to " << intent << ": "; - int index; - cin >> index; - if (index >= 0 && index < files.size()) { + string color; + if (intent == "delete") { + color = Colors::RED(); + } else { + color = Colors::YELLOW(); + } + cout << color << "Choose result to " << intent << " (cancel=-1): "; + string line; + getline(cin, line); + int index = stoi(line); + if (index >= -1 && index < static_cast(files.size())) { return index; } cout << "Invalid index" << endl; @@ -85,7 +96,7 @@ namespace platform { } void Results::report(const int index) const { - cout << "Reporting " << files.at(index).getFilename() << endl; + cout << Colors::YELLOW() << "Reporting " << files.at(index).getFilename() << endl; auto data = files.at(index).load(); Report report(data); report.show(); @@ -97,7 +108,7 @@ namespace platform { bool finished = false; string filename, line, options = "qldhsr"; while (!finished) { - cout << "Choose option (quit='q', list='l', delete='d', hide='h', sort='s', report='r'): "; + cout << Colors::RESET() << "Choose option (quit='q', list='l', delete='d', hide='h', sort='s', report='r'): "; getline(cin, line); if (line.size() == 0) continue; @@ -131,6 +142,7 @@ namespace platform { cout << "Deleting " << filename << endl; remove((path + "/" + filename).c_str()); files.erase(files.begin() + index); + cout << "File: " + filename + " deleted!" << endl; show(); break; case 'h': @@ -161,7 +173,7 @@ namespace platform { } void Results::sortList() { - cout << "Choose sorting field (date='d', score='s', duration='u', model='m'): "; + cout << Colors::YELLOW() << "Choose sorting field (date='d', score='s', duration='u', model='m'): "; string line; char option; getline(cin, line);