diff --git a/src/Platform/BestResultsExcel.cc b/src/Platform/BestResultsExcel.cc index b565db3..34291c9 100644 --- a/src/Platform/BestResultsExcel.cc +++ b/src/Platform/BestResultsExcel.cc @@ -1,9 +1,23 @@ #include #include "BestResultsExcel.h" #include "Paths.h" +#include +#include #include "Statistics.h" +#include "ReportExcel.h" namespace platform { + json loadResultData(const string& fileName) + { + json data; + ifstream resultData(fileName); + if (resultData.is_open()) { + data = json::parse(resultData); + } else { + throw invalid_argument("Unable to open result file. [" + fileName + "]"); + } + return data; + } string getColumnName(int colNum) { string columnName = ""; @@ -47,13 +61,8 @@ namespace platform { cerr << "File " << fileName << " doesn't exist." << endl; exit(1); } - json data; - ifstream resultData(fileName); - if (resultData.is_open()) { - data = json::parse(resultData); - } else { - throw invalid_argument("Unable to open result file. [" + fileName + "]"); - } + json data = loadResultData(fileName); + string title = "Best results for " + model; worksheet_merge_range(worksheet, 0, 0, 0, 4, title.c_str(), styles["headerFirst"]); // Body header @@ -67,12 +76,29 @@ namespace platform { auto i = 0; string hyperparameters; int hypSize = 0; + map files; // map of files imported and their tabs for (auto const& item : data.items()) { row++; writeInt(row, 0, i++, "ints"); writeString(row, 1, item.key().c_str(), "text"); writeDouble(row, 2, item.value().at(0).get(), "result"); - writeString(row, 3, item.value().at(2).get(), "text"); + auto fileName = item.value().at(2).get(); + string hyperlink = ""; + try { + hyperlink = files.at(fileName); + } + catch (const out_of_range& oor) { + auto tabName = "table_" + to_string(i); + auto worksheetNew = workbook_add_worksheet(workbook, tabName.c_str()); + json data = loadResultData(Paths::results() + fileName); + auto report = ReportExcel(data, false, workbook, worksheetNew); + report.show(); + hyperlink = "#table_" + to_string(i); + files[fileName] = hyperlink; + } + hyperlink += "!H" + to_string(i + 6); + string fileNameText = "=HYPERLINK(\"" + hyperlink + "\",\"" + fileName + "\")"; + worksheet_write_formula(worksheet, row, 3, fileNameText.c_str(), efectiveStyle("text")); hyperparameters = item.value().at(1).dump(); if (hyperparameters.size() > hypSize) { hypSize = hyperparameters.size(); @@ -85,7 +111,6 @@ namespace platform { stringstream oss; auto colName = getColumnName(2); oss << "=sum(" << colName << "5:" << colName << row << ")"; - cout << "[" << oss.str() << "]" << endl; worksheet_write_formula(worksheet, row, 2, oss.str().c_str(), styles["bodyHeader_odd"]); // Set format worksheet_freeze_panes(worksheet, 4, 2); diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index 5c8c317..0588ff2 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -9,9 +9,9 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/libxlsxwriter/include) add_executable(b_main b_main.cc Folding.cc Experiment.cc Datasets.cc Dataset.cc Models.cc ReportConsole.cc ReportBase.cc) add_executable(b_manage b_manage.cc Results.cc ManageResults.cc CommandParser.cc Result.cc ReportConsole.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc) add_executable(b_list b_list.cc Datasets.cc Dataset.cc) -add_executable(b_best b_best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc ExcelFile.cc) +add_executable(b_best b_best.cc BestResults.cc Result.cc Statistics.cc BestResultsExcel.cc ReportExcel.cc ReportBase.cc Datasets.cc Dataset.cc ExcelFile.cc) target_link_libraries(b_main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}") target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp) -target_link_libraries(b_best Boost::boost "${XLSXWRITER_LIB}") +target_link_libraries(b_best Boost::boost "${XLSXWRITER_LIB}" "${TORCH_LIBRARIES}" ArffFiles mdlp) target_link_libraries(b_list ArffFiles mdlp "${TORCH_LIBRARIES}") \ No newline at end of file diff --git a/src/Platform/ExcelFile.cc b/src/Platform/ExcelFile.cc index 04970ab..4449df8 100644 --- a/src/Platform/ExcelFile.cc +++ b/src/Platform/ExcelFile.cc @@ -9,6 +9,10 @@ namespace platform { { setDefault(); } + ExcelFile::ExcelFile(lxw_workbook* workbook, lxw_worksheet* worksheet) : workbook(workbook), worksheet(worksheet) + { + setDefault(); + } void ExcelFile::setDefault() { normalSize = 14; //font size for report body diff --git a/src/Platform/ExcelFile.h b/src/Platform/ExcelFile.h index a81acc3..931261e 100644 --- a/src/Platform/ExcelFile.h +++ b/src/Platform/ExcelFile.h @@ -18,6 +18,7 @@ namespace platform { public: ExcelFile(); ExcelFile(lxw_workbook* workbook); + ExcelFile(lxw_workbook* workbook, lxw_worksheet* worksheet); lxw_workbook* getWorkbook(); protected: void setProperties(string title); diff --git a/src/Platform/ManageResults.cc b/src/Platform/ManageResults.cc index d44e960..5bca520 100644 --- a/src/Platform/ManageResults.cc +++ b/src/Platform/ManageResults.cc @@ -93,6 +93,7 @@ namespace platform { reporter.show(); openExcel = true; workbook = reporter.getWorkbook(); + cout << "Adding sheet to " << Paths::excel() + Paths::excelResults() << endl; } else { ReportConsole reporter(data, compare); reporter.show(); diff --git a/src/Platform/Paths.h b/src/Platform/Paths.h index c27dd3d..c642cae 100644 --- a/src/Platform/Paths.h +++ b/src/Platform/Paths.h @@ -14,6 +14,7 @@ namespace platform { auto env = platform::DotEnv(); return env.get("source_data"); } + static std::string excelResults() { return "some_results.xlsx"; } }; } #endif \ No newline at end of file diff --git a/src/Platform/ReportExcel.cc b/src/Platform/ReportExcel.cc index d091b10..2411358 100644 --- a/src/Platform/ReportExcel.cc +++ b/src/Platform/ReportExcel.cc @@ -6,7 +6,7 @@ namespace platform { - ReportExcel::ReportExcel(json data_, bool compare, lxw_workbook* workbook) : ReportBase(data_, compare), ExcelFile(workbook) + ReportExcel::ReportExcel(json data_, bool compare, lxw_workbook* workbook, lxw_worksheet* worksheet) : ReportBase(data_, compare), ExcelFile(workbook, worksheet) { createFile(); } @@ -19,12 +19,8 @@ namespace platform { worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL); } } - - void ReportExcel::createFile() + void ReportExcel::createWorksheet() { - if (workbook == NULL) { - workbook = workbook_new((Paths::excel() + fileName).c_str()); - } const string name = data["model"].get(); string suffix = ""; string efectiveName; @@ -42,7 +38,16 @@ namespace platform { throw invalid_argument("Couldn't create sheet " + efectiveName); } } - cout << "Adding sheet " << efectiveName << " to " << Paths::excel() + fileName << endl; + } + + void ReportExcel::createFile() + { + if (workbook == NULL) { + workbook = workbook_new((Paths::excel() + Paths::excelResults()).c_str()); + } + if (worksheet == NULL) { + createWorksheet(); + } setProperties(data["title"].get()); createFormats(); formatColumns(); diff --git a/src/Platform/ReportExcel.h b/src/Platform/ReportExcel.h index 0fb9a22..3d614b0 100644 --- a/src/Platform/ReportExcel.h +++ b/src/Platform/ReportExcel.h @@ -9,11 +9,11 @@ namespace platform { using namespace std; class ReportExcel : public ReportBase, public ExcelFile { public: - explicit ReportExcel(json data_, bool compare, lxw_workbook* workbook); + explicit ReportExcel(json data_, bool compare, lxw_workbook* workbook, lxw_worksheet* worksheet = NULL); private: - const string fileName = "some_results.xlsx"; void formatColumns(); void createFile(); + void createWorksheet(); void closeFile(); void header() override; void body() override;