From 85202260f3ebf428f2e2e2eb3aaec16113bf07a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Thu, 28 Sep 2023 13:07:11 +0200 Subject: [PATCH] Separate specific Excel methods to ExcelFile --- src/Platform/BestResultsExcel.cc | 35 ++++++- src/Platform/BestResultsExcel.h | 12 ++- src/Platform/CMakeLists.txt | 8 +- src/Platform/Datasets.cc | 4 +- src/Platform/ExcelFile.cc | 156 +++++++++++++++++++++++++++++++ src/Platform/ExcelFile.h | 35 +++++++ src/Platform/ReportExcel.cc | 148 +---------------------------- src/Platform/ReportExcel.h | 23 +---- 8 files changed, 247 insertions(+), 174 deletions(-) create mode 100644 src/Platform/ExcelFile.cc create mode 100644 src/Platform/ExcelFile.h diff --git a/src/Platform/BestResultsExcel.cc b/src/Platform/BestResultsExcel.cc index 55e0267..c7401ec 100644 --- a/src/Platform/BestResultsExcel.cc +++ b/src/Platform/BestResultsExcel.cc @@ -1,8 +1,41 @@ #include "BestResultsExcel.h" +#include "Paths.h" namespace platform { - void BestResultsExcel::build() + BestResultsExcel::BestResultsExcel(vector models, vector datasets, json table, bool friedman) : models(models), datasets(datasets), table(table), friedman(friedman) + { + workbook = workbook_new((Paths::excel() + fileName).c_str()); + worksheet = workbook_add_worksheet(workbook, "Best Results"); + setProperties("Best Results"); + createFormats(); + formatColumns(); + } + + BestResultsExcel::~BestResultsExcel() + { + workbook_close(workbook); + } + void BestResultsExcel::formatColumns() { } + void BestResultsExcel::build() + { + header(); + body(); + footer(); + } + void BestResultsExcel::header() + { + + } + void BestResultsExcel::body() + { + + } + void BestResultsExcel::footer() + { + + } + } \ No newline at end of file diff --git a/src/Platform/BestResultsExcel.h b/src/Platform/BestResultsExcel.h index 0645a4d..9813d61 100644 --- a/src/Platform/BestResultsExcel.h +++ b/src/Platform/BestResultsExcel.h @@ -1,5 +1,6 @@ #ifndef BESTRESULTS_EXCEL_H #define BESTRESULTS_EXCEL_H +#include "ExcelFile.h" #include #include @@ -7,15 +8,22 @@ using namespace std; using json = nlohmann::json; namespace platform { - class BestResultsExcel { + class BestResultsExcel : ExcelFile { public: - BestResultsExcel(vector models, vector datasets, json table, bool friedman) : models(models), datasets(datasets), table(table), friedman(friedman) {} + BestResultsExcel(vector models, vector datasets, json table, bool friedman); + ~BestResultsExcel(); void build(); private: + void header(); + void body(); + void footer(); + void formatColumns(); + const string fileName = "BestResults.xlsx"; vector models; vector datasets; json table; bool friedman; + }; } #endif //BESTRESULTS_EXCEL_H \ No newline at end of file diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index 49ac357..f3c4de6 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -6,15 +6,15 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include) include_directories(${BayesNet_SOURCE_DIR}/lib/json/include) include_directories(${BayesNet_SOURCE_DIR}/lib/libxlsxwriter/include) add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.cc Models.cc ReportConsole.cc ReportBase.cc) -add_executable(manage manage.cc Results.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc platformUtils.cc) +add_executable(manage manage.cc Results.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc platformUtils.cc ExcelFile.cc) add_executable(list list.cc platformUtils Datasets.cc) -add_executable(best best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc) +add_executable(best best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc ExcelFile.cc) target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}") if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Linux") target_link_libraries(manage "${TORCH_LIBRARIES}" libxlsxwriter.so ArffFiles mdlp stdc++fs) - target_link_libraries(best Boost::boost stdc++fs) + target_link_libraries(best Boost::boost "${XLSXWRITER_LIB}" stdc++fs) else() target_link_libraries(manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp) - target_link_libraries(best Boost::boost) + target_link_libraries(best Boost::boost "${XLSXWRITER_LIB}") endif() target_link_libraries(list ArffFiles mdlp "${TORCH_LIBRARIES}") \ No newline at end of file diff --git a/src/Platform/Datasets.cc b/src/Platform/Datasets.cc index 1e240a0..acf78e5 100644 --- a/src/Platform/Datasets.cc +++ b/src/Platform/Datasets.cc @@ -5,7 +5,7 @@ namespace platform { void Datasets::load() { - ifstream catalog(path + "/all.txt"); + ifstream catalog(path + "all.txt"); if (catalog.is_open()) { string line; while (getline(catalog, line)) { @@ -16,7 +16,7 @@ namespace platform { } catalog.close(); } else { - throw invalid_argument("Unable to open catalog file. [" + path + "/all.txt" + "]"); + throw invalid_argument("Unable to open catalog file. [" + path + "all.txt" + "]"); } } vector Datasets::getNames() diff --git a/src/Platform/ExcelFile.cc b/src/Platform/ExcelFile.cc new file mode 100644 index 0000000..c8c999b --- /dev/null +++ b/src/Platform/ExcelFile.cc @@ -0,0 +1,156 @@ +#include "ExcelFile.h" + +namespace platform { + ExcelFile::ExcelFile() + { + setDefault(); + } + ExcelFile::ExcelFile(lxw_workbook* workbook) : workbook(workbook) + { + setDefault(); + } + void ExcelFile::setDefault() + { + normalSize = 14; //font size for report body + row = 0; + colorTitle = 0xB1A0C7; + colorOdd = 0xDCE6F1; + colorEven = 0xFDE9D9; + } + + lxw_workbook* ExcelFile::getWorkbook() + { + return workbook; + } + void ExcelFile::setProperties(string title) + { + char line[title.size() + 1]; + strcpy(line, title.c_str()); + lxw_doc_properties properties = { + .title = line, + .subject = (char*)"Machine learning results", + .author = (char*)"Ricardo Montañana Gómez", + .manager = (char*)"Dr. J. A. Gámez, Dr. J. M. Puerta", + .company = (char*)"UCLM", + .comments = (char*)"Created with libxlsxwriter and c++", + }; + workbook_set_properties(workbook, &properties); + } + lxw_format* ExcelFile::efectiveStyle(const string& style) + { + lxw_format* efectiveStyle; + if (style == "") { + efectiveStyle = NULL; + } else { + string suffix = row % 2 ? "_odd" : "_even"; + efectiveStyle = styles.at(style + suffix); + } + return efectiveStyle; + } + void ExcelFile::writeString(int row, int col, const string& text, const string& style) + { + worksheet_write_string(worksheet, row, col, text.c_str(), efectiveStyle(style)); + } + void ExcelFile::writeInt(int row, int col, const int number, const string& style) + { + worksheet_write_number(worksheet, row, col, number, efectiveStyle(style)); + } + void ExcelFile::writeDouble(int row, int col, const double number, const string& style) + { + worksheet_write_number(worksheet, row, col, number, efectiveStyle(style)); + } + void ExcelFile::addColor(lxw_format* style, bool odd) + { + uint32_t efectiveColor = odd ? colorEven : colorOdd; + format_set_bg_color(style, lxw_color_t(efectiveColor)); + } + void ExcelFile::createStyle(const string& name, lxw_format* style, bool odd) + { + addColor(style, odd); + if (name == "textCentered") { + format_set_align(style, LXW_ALIGN_CENTER); + format_set_font_size(style, normalSize); + format_set_border(style, LXW_BORDER_THIN); + } else if (name == "text") { + format_set_font_size(style, normalSize); + format_set_border(style, LXW_BORDER_THIN); + } else if (name == "bodyHeader") { + format_set_bold(style); + format_set_font_size(style, normalSize); + format_set_align(style, LXW_ALIGN_CENTER); + format_set_align(style, LXW_ALIGN_VERTICAL_CENTER); + format_set_border(style, LXW_BORDER_THIN); + format_set_bg_color(style, lxw_color_t(colorTitle)); + } else if (name == "result") { + format_set_font_size(style, normalSize); + format_set_border(style, LXW_BORDER_THIN); + format_set_num_format(style, "0.0000000"); + } else if (name == "time") { + format_set_font_size(style, normalSize); + format_set_border(style, LXW_BORDER_THIN); + format_set_num_format(style, "#,##0.000000"); + } else if (name == "ints") { + format_set_font_size(style, normalSize); + format_set_num_format(style, "###,##0"); + format_set_border(style, LXW_BORDER_THIN); + } else if (name == "floats") { + format_set_border(style, LXW_BORDER_THIN); + format_set_font_size(style, normalSize); + format_set_num_format(style, "#,##0.00"); + } + } + + void ExcelFile::createFormats() + { + auto styleNames = { "text", "textCentered", "bodyHeader", "result", "time", "ints", "floats" }; + lxw_format* style; + for (string name : styleNames) { + lxw_format* style = workbook_add_format(workbook); + style = workbook_add_format(workbook); + createStyle(name, style, true); + styles[name + "_odd"] = style; + style = workbook_add_format(workbook); + createStyle(name, style, false); + styles[name + "_even"] = style; + } + + // Header 1st line + lxw_format* headerFirst = workbook_add_format(workbook); + format_set_bold(headerFirst); + format_set_font_size(headerFirst, 18); + format_set_align(headerFirst, LXW_ALIGN_CENTER); + format_set_align(headerFirst, LXW_ALIGN_VERTICAL_CENTER); + format_set_border(headerFirst, LXW_BORDER_THIN); + format_set_bg_color(headerFirst, lxw_color_t(colorTitle)); + + // Header rest + lxw_format* headerRest = workbook_add_format(workbook); + format_set_bold(headerRest); + format_set_align(headerRest, LXW_ALIGN_CENTER); + format_set_font_size(headerRest, 16); + format_set_align(headerRest, LXW_ALIGN_VERTICAL_CENTER); + format_set_border(headerRest, LXW_BORDER_THIN); + format_set_bg_color(headerRest, lxw_color_t(colorOdd)); + + // Header small + lxw_format* headerSmall = workbook_add_format(workbook); + format_set_bold(headerSmall); + format_set_align(headerSmall, LXW_ALIGN_LEFT); + format_set_font_size(headerSmall, 12); + format_set_border(headerSmall, LXW_BORDER_THIN); + format_set_align(headerSmall, LXW_ALIGN_VERTICAL_CENTER); + format_set_bg_color(headerSmall, lxw_color_t(colorOdd)); + + // Summary style + lxw_format* summaryStyle = workbook_add_format(workbook); + format_set_bold(summaryStyle); + format_set_font_size(summaryStyle, 16); + format_set_border(summaryStyle, LXW_BORDER_THIN); + format_set_align(summaryStyle, LXW_ALIGN_VERTICAL_CENTER); + + styles["headerFirst"] = headerFirst; + styles["headerRest"] = headerRest; + styles["headerSmall"] = headerSmall; + styles["summaryStyle"] = summaryStyle; + } +} \ No newline at end of file diff --git a/src/Platform/ExcelFile.h b/src/Platform/ExcelFile.h new file mode 100644 index 0000000..4bfef10 --- /dev/null +++ b/src/Platform/ExcelFile.h @@ -0,0 +1,35 @@ +#ifndef EXCELFILE_H +#define EXCELFILE_H +#include +#include +#include "xlsxwriter.h" + +using namespace std; +namespace platform { + class ExcelFile { + public: + ExcelFile(); + ExcelFile(lxw_workbook* workbook); + lxw_workbook* getWorkbook(); + protected: + void setProperties(string title); + void writeString(int row, int col, const string& text, const string& style = ""); + void writeInt(int row, int col, const int number, const string& style = ""); + void writeDouble(int row, int col, const double number, const string& style = ""); + void createFormats(); + void createStyle(const string& name, lxw_format* style, bool odd); + void addColor(lxw_format* style, bool odd); + lxw_format* efectiveStyle(const string& name); + lxw_workbook* workbook; + lxw_worksheet* worksheet; + map styles; + int row; + int normalSize; //font size for report body + uint32_t colorTitle; + uint32_t colorOdd; + uint32_t colorEven; + private: + void setDefault(); + }; +} +#endif // !EXCELFILE_H \ No newline at end of file diff --git a/src/Platform/ReportExcel.cc b/src/Platform/ReportExcel.cc index 9398185..24fe5e4 100644 --- a/src/Platform/ReportExcel.cc +++ b/src/Platform/ReportExcel.cc @@ -13,45 +13,11 @@ namespace platform { string do_grouping() const { return "\03"; } }; - ReportExcel::ReportExcel(json data_, bool compare, lxw_workbook* workbook) : ReportBase(data_, compare), row(0), workbook(workbook) + ReportExcel::ReportExcel(json data_, bool compare, lxw_workbook* workbook) : ReportBase(data_, compare), ExcelFile(workbook) { - normalSize = 14; //font size for report body - colorTitle = 0xB1A0C7; - colorOdd = 0xDCE6F1; - colorEven = 0xFDE9D9; createFile(); } - lxw_workbook* ReportExcel::getWorkbook() - { - return workbook; - } - - lxw_format* ReportExcel::efectiveStyle(const string& style) - { - lxw_format* efectiveStyle; - if (style == "") { - efectiveStyle = NULL; - } else { - string suffix = row % 2 ? "_odd" : "_even"; - efectiveStyle = styles.at(style + suffix); - } - return efectiveStyle; - } - - void ReportExcel::writeString(int row, int col, const string& text, const string& style) - { - worksheet_write_string(worksheet, row, col, text.c_str(), efectiveStyle(style)); - } - void ReportExcel::writeInt(int row, int col, const int number, const string& style) - { - worksheet_write_number(worksheet, row, col, number, efectiveStyle(style)); - } - void ReportExcel::writeDouble(int row, int col, const double number, const string& style) - { - worksheet_write_number(worksheet, row, col, number, efectiveStyle(style)); - } - void ReportExcel::formatColumns() { worksheet_freeze_panes(worksheet, 6, 1); @@ -61,116 +27,6 @@ namespace platform { } } - void ReportExcel::addColor(lxw_format* style, bool odd) - { - uint32_t efectiveColor = odd ? colorEven : colorOdd; - format_set_bg_color(style, lxw_color_t(efectiveColor)); - } - void ReportExcel::createStyle(const string& name, lxw_format* style, bool odd) - { - addColor(style, odd); - if (name == "textCentered") { - format_set_align(style, LXW_ALIGN_CENTER); - format_set_font_size(style, normalSize); - format_set_border(style, LXW_BORDER_THIN); - } else if (name == "text") { - format_set_font_size(style, normalSize); - format_set_border(style, LXW_BORDER_THIN); - } else if (name == "bodyHeader") { - format_set_bold(style); - format_set_font_size(style, normalSize); - format_set_align(style, LXW_ALIGN_CENTER); - format_set_align(style, LXW_ALIGN_VERTICAL_CENTER); - format_set_border(style, LXW_BORDER_THIN); - format_set_bg_color(style, lxw_color_t(colorTitle)); - } else if (name == "result") { - format_set_font_size(style, normalSize); - format_set_border(style, LXW_BORDER_THIN); - format_set_num_format(style, "0.0000000"); - } else if (name == "time") { - format_set_font_size(style, normalSize); - format_set_border(style, LXW_BORDER_THIN); - format_set_num_format(style, "#,##0.000000"); - } else if (name == "ints") { - format_set_font_size(style, normalSize); - format_set_num_format(style, "###,##0"); - format_set_border(style, LXW_BORDER_THIN); - } else if (name == "floats") { - format_set_border(style, LXW_BORDER_THIN); - format_set_font_size(style, normalSize); - format_set_num_format(style, "#,##0.00"); - } - } - - void ReportExcel::createFormats() - { - auto styleNames = { "text", "textCentered", "bodyHeader", "result", "time", "ints", "floats" }; - lxw_format* style; - for (string name : styleNames) { - lxw_format* style = workbook_add_format(workbook); - style = workbook_add_format(workbook); - createStyle(name, style, true); - styles[name + "_odd"] = style; - style = workbook_add_format(workbook); - createStyle(name, style, false); - styles[name + "_even"] = style; - } - - // Header 1st line - lxw_format* headerFirst = workbook_add_format(workbook); - format_set_bold(headerFirst); - format_set_font_size(headerFirst, 18); - format_set_align(headerFirst, LXW_ALIGN_CENTER); - format_set_align(headerFirst, LXW_ALIGN_VERTICAL_CENTER); - format_set_border(headerFirst, LXW_BORDER_THIN); - format_set_bg_color(headerFirst, lxw_color_t(colorTitle)); - - // Header rest - lxw_format* headerRest = workbook_add_format(workbook); - format_set_bold(headerRest); - format_set_align(headerRest, LXW_ALIGN_CENTER); - format_set_font_size(headerRest, 16); - format_set_align(headerRest, LXW_ALIGN_VERTICAL_CENTER); - format_set_border(headerRest, LXW_BORDER_THIN); - format_set_bg_color(headerRest, lxw_color_t(colorOdd)); - - // Header small - lxw_format* headerSmall = workbook_add_format(workbook); - format_set_bold(headerSmall); - format_set_align(headerSmall, LXW_ALIGN_LEFT); - format_set_font_size(headerSmall, 12); - format_set_border(headerSmall, LXW_BORDER_THIN); - format_set_align(headerSmall, LXW_ALIGN_VERTICAL_CENTER); - format_set_bg_color(headerSmall, lxw_color_t(colorOdd)); - - // Summary style - lxw_format* summaryStyle = workbook_add_format(workbook); - format_set_bold(summaryStyle); - format_set_font_size(summaryStyle, 16); - format_set_border(summaryStyle, LXW_BORDER_THIN); - format_set_align(summaryStyle, LXW_ALIGN_VERTICAL_CENTER); - - styles["headerFirst"] = headerFirst; - styles["headerRest"] = headerRest; - styles["headerSmall"] = headerSmall; - styles["summaryStyle"] = summaryStyle; - } - - void ReportExcel::setProperties() - { - char line[data["title"].get().size() + 1]; - strcpy(line, data["title"].get().c_str()); - lxw_doc_properties properties = { - .title = line, - .subject = (char*)"Machine learning results", - .author = (char*)"Ricardo Montañana Gómez", - .manager = (char*)"Dr. J. A. Gámez, Dr. J. M. Puerta", - .company = (char*)"UCLM", - .comments = (char*)"Created with libxlsxwriter and c++", - }; - workbook_set_properties(workbook, &properties); - } - void ReportExcel::createFile() { if (workbook == NULL) { @@ -194,7 +50,7 @@ namespace platform { } } cout << "Adding sheet " << efectiveName << " to " << Paths::excel() + fileName << endl; - setProperties(); + setProperties(data["title"].get()); createFormats(); formatColumns(); } diff --git a/src/Platform/ReportExcel.h b/src/Platform/ReportExcel.h index 75c8a02..cc2d57a 100644 --- a/src/Platform/ReportExcel.h +++ b/src/Platform/ReportExcel.h @@ -3,40 +3,25 @@ #include #include "xlsxwriter.h" #include "ReportBase.h" +#include "ExcelFile.h" #include "Colors.h" namespace platform { using namespace std; const int MAXLL = 128; - class ReportExcel : public ReportBase { + class ReportExcel : public ReportBase, public ExcelFile { public: explicit ReportExcel(json data_, bool compare, lxw_workbook* workbook); - lxw_workbook* getWorkbook(); private: - void writeString(int row, int col, const string& text, const string& style = ""); - void writeInt(int row, int col, const int number, const string& style = ""); - void writeDouble(int row, int col, const double number, const string& style = ""); + const string fileName = "some_results.xlsx"; void formatColumns(); - void createFormats(); - void setProperties(); void createFile(); void closeFile(); - lxw_workbook* workbook; - lxw_worksheet* worksheet; - map styles; - int row; - int normalSize; //font size for report body - uint32_t colorTitle; - uint32_t colorOdd; - uint32_t colorEven; - const string fileName = "some_results.xlsx"; void header() override; void body() override; void showSummary() override; void footer(double totalScore, int row); - void createStyle(const string& name, lxw_format* style, bool odd); - void addColor(lxw_format* style, bool odd); - lxw_format* efectiveStyle(const string& name); + }; }; #endif // !REPORTEXCEL_H \ No newline at end of file