Compare commits

...

2 Commits

5 changed files with 39 additions and 35 deletions

View File

@@ -22,6 +22,27 @@ namespace platform {
colorOdd = 0xDCE6F1; colorOdd = 0xDCE6F1;
colorEven = 0xFDE9D9; colorEven = 0xFDE9D9;
} }
lxw_worksheet* ExcelFile::createWorksheet(const std::string& name)
{
lxw_worksheet* sheet;
std::string suffix = "";
std::string efectiveName;
int num = 1;
// Create a sheet with the name of the model
while (true) {
efectiveName = name + suffix;
if (workbook_get_worksheet_by_name(workbook, efectiveName.c_str())) {
suffix = std::to_string(++num);
} else {
sheet = workbook_add_worksheet(workbook, efectiveName.c_str());
break;
}
if (num > 100) {
throw std::invalid_argument("Couldn't create sheet " + efectiveName);
}
}
return sheet;
}
lxw_workbook* ExcelFile::getWorkbook() lxw_workbook* ExcelFile::getWorkbook()
{ {
@@ -75,7 +96,7 @@ namespace platform {
} }
void ExcelFile::boldGreen() void ExcelFile::boldGreen()
{ {
boldFontColor(0x00FF00); boldFontColor(0x009900);
} }
void ExcelFile::boldRed() void ExcelFile::boldRed()
{ {

View File

@@ -24,6 +24,7 @@ namespace platform {
void boldBlue(); //set blue color for the bold styles void boldBlue(); //set blue color for the bold styles
void boldGreen(); //set green 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 createStyle(const std::string& name, lxw_format* style, bool odd);
lxw_worksheet* createWorksheet(const std::string& name);
void addColor(lxw_format* style, bool odd); void addColor(lxw_format* style, bool odd);
lxw_format* efectiveStyle(const std::string& name); lxw_format* efectiveStyle(const std::string& name);
lxw_workbook* workbook; lxw_workbook* workbook;

View File

@@ -17,26 +17,7 @@ namespace platform {
worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL); worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL);
} }
} }
void ReportExcel::createWorksheet()
{
const std::string name = data["model"].get<std::string>();
std::string suffix = "";
std::string efectiveName;
int num = 1;
// Create a sheet with the name of the model
while (true) {
efectiveName = name + suffix;
if (workbook_get_worksheet_by_name(workbook, efectiveName.c_str())) {
suffix = std::to_string(++num);
} else {
worksheet = workbook_add_worksheet(workbook, efectiveName.c_str());
break;
}
if (num > 100) {
throw std::invalid_argument("Couldn't create sheet " + efectiveName);
}
}
}
void ReportExcel::createFile() void ReportExcel::createFile()
{ {
@@ -44,7 +25,8 @@ namespace platform {
workbook = workbook_new((Paths::excel() + Paths::excelResults()).c_str()); workbook = workbook_new((Paths::excel() + Paths::excelResults()).c_str());
} }
if (worksheet == NULL) { if (worksheet == NULL) {
createWorksheet(); const std::string name = data["model"].get<std::string>();
worksheet = createWorksheet(name);
} }
setProperties(data["title"].get<std::string>()); setProperties(data["title"].get<std::string>());
formatColumns(); formatColumns();
@@ -209,14 +191,14 @@ namespace platform {
} }
void ReportExcel::create_classification_report(const json& result) void ReportExcel::create_classification_report(const json& result)
{ {
auto matrix_sheet = workbook_add_worksheet(workbook, "classif_report");
auto matrix_sheet = createWorksheet("clf_report");
lxw_worksheet* tmp = worksheet; lxw_worksheet* tmp = worksheet;
worksheet = matrix_sheet; worksheet = matrix_sheet;
if (matrix_sheet == NULL) { if (matrix_sheet == NULL) {
throw std::invalid_argument("Couldn't create sheet classif_report"); throw std::invalid_argument("Couldn't create sheet classif_report");
} }
worksheet_merge_range(matrix_sheet, 0, 0, 0, 5, "Classification Report", efectiveStyle("bodyHeader")); row = 1;
int row = 2;
int col = 0; int col = 0;
if (result.find("confusion_matrices_train") != result.end()) { if (result.find("confusion_matrices_train") != result.end()) {
// Train classification report // Train classification report
@@ -229,11 +211,14 @@ namespace platform {
auto item = result["confusion_matrices_train"][i]; auto item = result["confusion_matrices_train"][i];
auto score_item = Scores(item); auto score_item = Scores(item);
auto title = "Train Fold " + std::to_string(i); auto title = "Train Fold " + std::to_string(i);
std::tie(new_row, new_col) = write_classification_report(score_item.classification_report_json(title), 2, new_col); std::tie(new_row, new_col) = write_classification_report(score_item.classification_report_json(title), 1, new_col);
new_col++; new_col++;
} }
col = new_col;
worksheet_merge_range(matrix_sheet, 0, 0, 0, col - 1, "Train Classification Report", efectiveStyle("headerRest"));
} }
// Test classification report // Test classification report
worksheet_merge_range(matrix_sheet, row, 0, row, col - 1, "Test Classification Report", efectiveStyle("headerRest"));
auto score = Scores::create_aggregate(result, "confusion_matrices"); auto score = Scores::create_aggregate(result, "confusion_matrices");
auto test = score.classification_report_json("Test"); auto test = score.classification_report_json("Test");
int init_row = ++row; int init_row = ++row;
@@ -256,9 +241,10 @@ namespace platform {
} }
std::pair<int, int> ReportExcel::write_classification_report(const json& result, int init_row, int init_col) std::pair<int, int> ReportExcel::write_classification_report(const json& result, int init_row, int init_col)
{ {
int row = init_row; row = init_row;
auto text = result["title"].get<std::string>(); auto text = result["title"].get<std::string>();
worksheet_merge_range(worksheet, row++, init_col, row, init_col + 5, text.c_str(), efectiveStyle("bodyHeader")); worksheet_merge_range(worksheet, row, init_col, row + 1, init_col + 5, text.c_str(), efectiveStyle("bodyHeader"));
row += 2;
int col = init_col + 2; int col = init_col + 2;
// Headers // Headers
bool first_item = true; bool first_item = true;
@@ -288,8 +274,6 @@ namespace platform {
} }
row++; row++;
} }
worksheet_merge_range(worksheet, row, init_col, row, init_col + 5, "", efectiveStyle("text"));
row++;
// Accuracy and average f1-score // Accuracy and average f1-score
for (const auto& item : { "accuracy", "averages", "weighted" }) { for (const auto& item : { "accuracy", "averages", "weighted" }) {
col = init_col + 2; col = init_col + 2;
@@ -307,11 +291,10 @@ namespace platform {
row++; row++;
} }
// Confusion matrix // Confusion matrix
worksheet_merge_range(worksheet, row, init_col, row, init_col + 5, "", efectiveStyle("bodyHeader"));
row++;
auto n_items = result["confusion_matrix"].size(); auto n_items = result["confusion_matrix"].size();
worksheet_merge_range(worksheet, row, init_col, row, init_col + n_items + 1, "Confusion Matrix", efectiveStyle("bodyHeader")); worksheet_merge_range(worksheet, row, init_col, row, init_col + n_items + 1, "Confusion Matrix", efectiveStyle("bodyHeader"));
row++; row++;
boldGreen();
for (int i = 0; i < n_items; ++i) { for (int i = 0; i < n_items; ++i) {
col = init_col + 2; col = init_col + 2;
auto label = result["body"][i][0].get<std::string>(); auto label = result["body"][i][0].get<std::string>();
@@ -326,7 +309,7 @@ namespace platform {
} }
row++; row++;
} }
int maxcol = std::max(5, int(init_col + n_items + 1)); int maxcol = std::max(init_col + 5, int(init_col + n_items + 1));
return { row, maxcol }; return { row, maxcol };
} }
void ReportExcel::showSummary() void ReportExcel::showSummary()

View File

@@ -15,7 +15,6 @@ namespace platform {
private: private:
void formatColumns(); void formatColumns();
void createFile(); void createFile();
void createWorksheet();
void header() override; void header() override;
void body() override; void body() override;
void showSummary() override; void showSummary() override;