From 72cda3784a08770f37882678a49f41af6f349d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Mon, 11 Mar 2024 17:02:58 +0100 Subject: [PATCH] Add bold max score per model in b_list results --- README.md | 5 ++-- src/list/ResultsDatasetExcel.cpp | 17 ++++++++--- src/list/b_list.cpp | 49 ++++++++++++++++++++------------ src/reports/ExcelFile.cpp | 21 ++++++++++++++ src/reports/ExcelFile.h | 4 +++ 5 files changed, 72 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index f0d234d..1c0ec04 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ # Platform - -[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) +![C++](https://img.shields.io/badge/c++-%2300599C.svg?style=flat&logo=c%2B%2B&logoColor=white) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)]() +![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/platform?gitea_url=https://gitea.rmontanana.es:3000&logo=gitea) Platform to run Bayesian Networks and Machine Learning Classifiers experiments. diff --git a/src/list/ResultsDatasetExcel.cpp b/src/list/ResultsDatasetExcel.cpp index 1775fbe..945e503 100644 --- a/src/list/ResultsDatasetExcel.cpp +++ b/src/list/ResultsDatasetExcel.cpp @@ -1,5 +1,4 @@ #include "ResultsDatasetExcel.h" -#include namespace platform { ResultsDatasetExcel::ResultsDatasetExcel() { @@ -25,11 +24,21 @@ namespace platform { writeString(row, col++, name, "bodyHeader"); } // Body + std::string bold = "_bold"; double maxResult = data["maxResult"].get(); for (const auto& item : data["results"]) { row++; col = 0; - std::string style = item["score"] == data["maxResult"] ? "_bold" : ""; + std::string style = ""; + auto score = item["score"].get(); + if (score == data["max_models"][item["model"].get()]) { + boldBlue(); + style = bold; + } + if (score == maxResult) { + boldRed(); + style = bold; + } writeInt(row, col++, row - 3, "ints" + style); writeString(row, col++, item["model"], "text" + style); writeString(row, col++, item["date"], "text" + style); @@ -39,8 +48,8 @@ namespace platform { } // Format columns worksheet_freeze_panes(worksheet, 3, 2); - auto modelSize = data["maxModel"].get(); - auto hyperSize = data["maxHyper"].get(); + auto modelSize = data["lengths"]["maxModel"].get(); + auto hyperSize = data["lengths"]["maxHyper"].get(); std::vector columns_sizes = { 5, modelSize + 3, 12, 9, 11, hyperSize + 10 }; for (int i = 0; i < columns_sizes.size(); ++i) { worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL); diff --git a/src/list/b_list.cpp b/src/list/b_list.cpp index 9fa50c6..f0f74d6 100644 --- a/src/list/b_list.cpp +++ b/src/list/b_list.cpp @@ -89,31 +89,21 @@ void list_results(argparse::ArgumentParser& program) std::cerr << Colors::RED() << "No results found for dataset " << dataset << " and model " << model << Colors::RESET() << std::endl; exit(1); } - // - // List data - // int maxModel = results.maxModelSize(); int maxHyper = results.maxHyperSize(); double maxResult = results.maxResultScore(); - std::cout << Colors::GREEN() << "Results of dataset " << dataset << " - for " << model << " model" << std::endl; - std::cout << "There are " << results.size() << " results" << std::endl; - std::cout << Colors::GREEN() << " # " << std::setw(maxModel + 1) << std::left << "Model" << "Date Time Score Hyperparameters" << std::endl; - std::cout << "=== " << std::string(maxModel, '=') << " ========== ======== =========== " << std::string(maxHyper, '=') << std::endl; - auto i = 0; + // Build data for the Report json data = json::object(); data["results"] = json::array(); + data["max_models"] = json::object(); // Max score per model for (const auto& result : results) { auto results = result.getData(); + if (!data["max_models"].contains(result.getModel())) { + data["max_models"][result.getModel()] = 0; + } for (const auto& item : results["results"]) { if (item["dataset"] == dataset) { - auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN(); - color = item["score"].get() == maxResult ? Colors::RED() : color; - std::cout << color << std::setw(3) << std::fixed << std::right << i++ << " "; - std::cout << std::setw(maxModel) << std::left << result.getModel() << " "; - std::cout << color << result.getDate() << " "; - std::cout << color << result.getTime() << " "; - std::cout << std::setw(11) << std::setprecision(9) << std::fixed << item["score"].get() << " "; - std::cout << item["hyperparameters"].dump() << std::endl; + // Store data for Excel report json res = json::object(); res["date"] = result.getDate(); @@ -122,16 +112,39 @@ void list_results(argparse::ArgumentParser& program) res["score"] = item["score"].get(); res["hyperparameters"] = item["hyperparameters"].dump(); data["results"].push_back(res); + if (item["score"].get() > data["max_models"][result.getModel()]) { + data["max_models"][result.getModel()] = item["score"].get(); + } break; } } } + // + // List the results + // + std::cout << Colors::GREEN() << "Results of dataset " << dataset << " - for " << model << " model" << std::endl; + std::cout << "There are " << results.size() << " results" << std::endl; + std::cout << Colors::GREEN() << " # " << std::setw(maxModel + 1) << std::left << "Model" << "Date Time Score Hyperparameters" << std::endl; + std::cout << "=== " << std::string(maxModel, '=') << " ========== ======== =========== " << std::string(maxHyper, '=') << std::endl; + auto i = 0; + for (const auto& item : data["results"]) { + auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN(); + auto score = item["score"].get(); + color = score == data["max_models"][item["model"].get()] ? Colors::YELLOW() : color; + color = score == maxResult ? Colors::RED() : color; + std::cout << color << std::setw(3) << std::fixed << std::right << i++ << " "; + std::cout << std::setw(maxModel) << std::left << item["model"].get() << " "; + std::cout << color << item["date"].get() << " "; + std::cout << color << item["time"].get() << " "; + std::cout << std::setw(11) << std::setprecision(9) << std::fixed << score << " "; + std::cout << item["hyperparameters"].get() << std::endl; + } if (excel) { data["dataset"] = dataset; data["score"] = score; data["model"] = model; - data["maxModel"] = maxModel; - data["maxHyper"] = maxHyper; + data["lengths"]["maxModel"] = maxModel; + data["lengths"]["maxHyper"] = maxHyper; data["maxResult"] = maxResult; auto report = platform::ResultsDatasetExcel(); report.report(data); diff --git a/src/reports/ExcelFile.cpp b/src/reports/ExcelFile.cpp index 41dcc55..3cafbcf 100644 --- a/src/reports/ExcelFile.cpp +++ b/src/reports/ExcelFile.cpp @@ -64,6 +64,27 @@ namespace platform { } return efectiveStyle; } + void ExcelFile::boldFontColor(const uint32_t color) + { + createFormats(); + for (const std::string& style : { "text", "ints", "result" }) { + for (const std::string& suffix : { "_odd", "_even" }) { + format_set_font_color(styles[style + "_bold" + suffix], lxw_color_t(color)); + } + } + } + void ExcelFile::boldGreen() + { + boldFontColor(0x00FF00); + } + void ExcelFile::boldRed() + { + boldFontColor(0xFF0000); + } + void ExcelFile::boldBlue() + { + boldFontColor(0x0000FF); + } void ExcelFile::writeString(int row, int col, const std::string& text, const std::string& style) { worksheet_write_string(worksheet, row, col, text.c_str(), efectiveStyle(style)); diff --git a/src/reports/ExcelFile.h b/src/reports/ExcelFile.h index e930856..d462821 100644 --- a/src/reports/ExcelFile.h +++ b/src/reports/ExcelFile.h @@ -26,6 +26,10 @@ namespace platform { void writeInt(int row, int col, const int number, const std::string& style = ""); void writeDouble(int row, int col, const double number, const std::string& style = ""); void createFormats(); + void boldFontColor(const uint32_t color); // the same color for bold styles + void boldRed(); //set red color for the bold styles + void boldBlue(); //set blue color for the bold styles + void boldGreen(); //set green color for the bold styles void createStyle(const std::string& name, lxw_format* style, bool odd); void addColor(lxw_format* style, bool odd); lxw_format* efectiveStyle(const std::string& name);