Begin b_list excel
This commit is contained in:
175
src/reports/ExcelFile.cc
Normal file
175
src/reports/ExcelFile.cc
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "ExcelFile.h"
|
||||
|
||||
namespace platform {
|
||||
ExcelFile::ExcelFile()
|
||||
{
|
||||
setDefault();
|
||||
createFormats();
|
||||
}
|
||||
ExcelFile::ExcelFile(lxw_workbook* workbook) : workbook(workbook)
|
||||
{
|
||||
setDefault();
|
||||
createFormats();
|
||||
}
|
||||
ExcelFile::ExcelFile(lxw_workbook* workbook, lxw_worksheet* worksheet) : workbook(workbook), worksheet(worksheet)
|
||||
{
|
||||
setDefault();
|
||||
createFormats();
|
||||
}
|
||||
void ExcelFile::setDefault()
|
||||
{
|
||||
normalSize = 14; //font size for report body
|
||||
row = 0;
|
||||
colorTitle = 0xB1A0C7;
|
||||
colorOdd = 0xDCE6F1;
|
||||
colorEven = 0xFDE9D9;
|
||||
}
|
||||
|
||||
lxw_workbook* ExcelFile::getWorkbook()
|
||||
{
|
||||
return workbook;
|
||||
}
|
||||
std::string ExcelFile::getFileName()
|
||||
{
|
||||
return Paths::excel() + file_name;
|
||||
}
|
||||
void ExcelFile::setProperties(std::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 std::string& style)
|
||||
{
|
||||
lxw_format* efectiveStyle = NULL;
|
||||
if (style != "") {
|
||||
std::string suffix = row % 2 ? "_odd" : "_even";
|
||||
try {
|
||||
efectiveStyle = styles.at(style + suffix);
|
||||
}
|
||||
catch (const std::out_of_range& oor) {
|
||||
try {
|
||||
efectiveStyle = styles.at(style);
|
||||
}
|
||||
catch (const std::out_of_range& oor) {
|
||||
throw std::invalid_argument("Style " + style + " not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
return efectiveStyle;
|
||||
}
|
||||
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));
|
||||
}
|
||||
void ExcelFile::writeInt(int row, int col, const int number, const std::string& style)
|
||||
{
|
||||
worksheet_write_number(worksheet, row, col, number, efectiveStyle(style));
|
||||
}
|
||||
void ExcelFile::writeDouble(int row, int col, const double number, const std::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 std::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);
|
||||
format_set_align(style, LXW_ALIGN_VERTICAL_CENTER);
|
||||
} 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 (std::string name : styleNames) {
|
||||
lxw_format* 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;
|
||||
}
|
||||
}
|
46
src/reports/ExcelFile.h
Normal file
46
src/reports/ExcelFile.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef EXCELFILE_H
|
||||
#define EXCELFILE_H
|
||||
#include <locale>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include "Paths.h"
|
||||
#include "xlsxwriter.h"
|
||||
|
||||
namespace platform {
|
||||
struct separated : std::numpunct<char> {
|
||||
char do_decimal_point() const { return ','; }
|
||||
|
||||
char do_thousands_sep() const { return '.'; }
|
||||
|
||||
std::string do_grouping() const { return "\03"; }
|
||||
};
|
||||
class ExcelFile {
|
||||
public:
|
||||
ExcelFile();
|
||||
ExcelFile(lxw_workbook* workbook);
|
||||
ExcelFile(lxw_workbook* workbook, lxw_worksheet* worksheet);
|
||||
lxw_workbook* getWorkbook();
|
||||
std::string getFileName();
|
||||
protected:
|
||||
void setProperties(std::string title);
|
||||
void writeString(int row, int col, const std::string& text, const std::string& style = "");
|
||||
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 createStyle(const std::string& name, lxw_format* style, bool odd);
|
||||
void addColor(lxw_format* style, bool odd);
|
||||
lxw_format* efectiveStyle(const std::string& name);
|
||||
lxw_workbook* workbook;
|
||||
lxw_worksheet* worksheet;
|
||||
std::map<std::string, lxw_format*> styles;
|
||||
int row;
|
||||
int normalSize; //font size for report body
|
||||
uint32_t colorTitle;
|
||||
uint32_t colorOdd;
|
||||
uint32_t colorEven;
|
||||
std::string file_name;
|
||||
private:
|
||||
void setDefault();
|
||||
};
|
||||
}
|
||||
#endif // !EXCELFILE_H
|
113
src/reports/ReportBase.cc
Normal file
113
src/reports/ReportBase.cc
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include "Datasets.h"
|
||||
#include "ReportBase.h"
|
||||
#include "DotEnv.h"
|
||||
|
||||
namespace platform {
|
||||
ReportBase::ReportBase(json data_, bool compare) : data(data_), compare(compare), margin(0.1)
|
||||
{
|
||||
std::stringstream oss;
|
||||
oss << "Better than ZeroR + " << std::setprecision(1) << fixed << margin * 100 << "%";
|
||||
meaning = {
|
||||
{Symbols::equal_best, "Equal to best"},
|
||||
{Symbols::better_best, "Better than best"},
|
||||
{Symbols::cross, "Less than or equal to ZeroR"},
|
||||
{Symbols::upward_arrow, oss.str()}
|
||||
};
|
||||
}
|
||||
std::string ReportBase::fromVector(const std::string& key)
|
||||
{
|
||||
std::stringstream oss;
|
||||
std::string sep = "";
|
||||
oss << "[";
|
||||
for (auto& item : data[key]) {
|
||||
oss << sep << item.get<double>();
|
||||
sep = ", ";
|
||||
}
|
||||
oss << "]";
|
||||
return oss.str();
|
||||
}
|
||||
std::string ReportBase::fVector(const std::string& title, const json& data, const int width, const int precision)
|
||||
{
|
||||
std::stringstream oss;
|
||||
std::string sep = "";
|
||||
oss << title << "[";
|
||||
for (const auto& item : data) {
|
||||
oss << sep << fixed << setw(width) << std::setprecision(precision) << item.get<double>();
|
||||
sep = ", ";
|
||||
}
|
||||
oss << "]";
|
||||
return oss.str();
|
||||
}
|
||||
void ReportBase::show()
|
||||
{
|
||||
header();
|
||||
body();
|
||||
}
|
||||
std::string ReportBase::compareResult(const std::string& dataset, double result)
|
||||
{
|
||||
std::string status = " ";
|
||||
if (compare) {
|
||||
double best = bestResult(dataset, data["model"].get<std::string>());
|
||||
if (result == best) {
|
||||
status = Symbols::equal_best;
|
||||
} else if (result > best) {
|
||||
status = Symbols::better_best;
|
||||
}
|
||||
} else {
|
||||
if (data["score_name"].get<std::string>() == "accuracy") {
|
||||
auto dt = Datasets(false, Paths::datasets());
|
||||
dt.loadDataset(dataset);
|
||||
auto numClasses = dt.getNClasses(dataset);
|
||||
if (numClasses == 2) {
|
||||
std::vector<int> distribution = dt.getClassesCounts(dataset);
|
||||
double nSamples = dt.getNSamples(dataset);
|
||||
std::vector<int>::iterator maxValue = max_element(distribution.begin(), distribution.end());
|
||||
double mark = *maxValue / nSamples * (1 + margin);
|
||||
if (mark > 1) {
|
||||
mark = 0.9995;
|
||||
}
|
||||
status = result < mark ? Symbols::cross : result > mark ? Symbols::upward_arrow : "=";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (status != " ") {
|
||||
auto item = summary.find(status);
|
||||
if (item != summary.end()) {
|
||||
summary[status]++;
|
||||
} else {
|
||||
summary[status] = 1;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
double ReportBase::bestResult(const std::string& dataset, const std::string& model)
|
||||
{
|
||||
double value = 0.0;
|
||||
if (bestResults.size() == 0) {
|
||||
// try to load the best results
|
||||
std::string score = data["score_name"];
|
||||
replace(score.begin(), score.end(), '_', '-');
|
||||
std::string fileName = "best_results_" + score + "_" + model + ".json";
|
||||
ifstream resultData(Paths::results() + "/" + fileName);
|
||||
if (resultData.is_open()) {
|
||||
bestResults = json::parse(resultData);
|
||||
} else {
|
||||
existBestFile = false;
|
||||
}
|
||||
}
|
||||
try {
|
||||
value = bestResults.at(dataset).at(0);
|
||||
}
|
||||
catch (exception) {
|
||||
value = 1.0;
|
||||
|
||||
}
|
||||
return value;
|
||||
}
|
||||
bool ReportBase::getExistBestFile()
|
||||
{
|
||||
return existBestFile;
|
||||
}
|
||||
}
|
36
src/reports/ReportBase.h
Normal file
36
src/reports/ReportBase.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef REPORTBASE_H
|
||||
#define REPORTBASE_H
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "Paths.h"
|
||||
#include "Symbols.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
using json = nlohmann::json;
|
||||
namespace platform {
|
||||
|
||||
class ReportBase {
|
||||
public:
|
||||
explicit ReportBase(json data_, bool compare);
|
||||
virtual ~ReportBase() = default;
|
||||
void show();
|
||||
protected:
|
||||
json data;
|
||||
std::string fromVector(const std::string& key);
|
||||
std::string fVector(const std::string& title, const json& data, const int width, const int precision);
|
||||
bool getExistBestFile();
|
||||
virtual void header() = 0;
|
||||
virtual void body() = 0;
|
||||
virtual void showSummary() = 0;
|
||||
std::string compareResult(const std::string& dataset, double result);
|
||||
std::map<std::string, int> summary;
|
||||
double margin;
|
||||
std::map<std::string, std::string> meaning;
|
||||
bool compare;
|
||||
private:
|
||||
double bestResult(const std::string& dataset, const std::string& model);
|
||||
json bestResults;
|
||||
bool existBestFile = true;
|
||||
};
|
||||
};
|
||||
#endif
|
128
src/reports/ReportConsole.cc
Normal file
128
src/reports/ReportConsole.cc
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include "ReportConsole.h"
|
||||
#include "BestScore.h"
|
||||
#include "CLocale.h"
|
||||
|
||||
namespace platform {
|
||||
std::string ReportConsole::headerLine(const std::string& text, int utf = 0)
|
||||
{
|
||||
int n = MAXL - text.length() - 3;
|
||||
n = n < 0 ? 0 : n;
|
||||
return "* " + text + std::string(n + utf, ' ') + "*\n";
|
||||
}
|
||||
|
||||
void ReportConsole::header()
|
||||
{
|
||||
std::stringstream oss;
|
||||
std::cout << Colors::MAGENTA() << std::string(MAXL, '*') << std::endl;
|
||||
std::cout << headerLine(
|
||||
"Report " + data["model"].get<std::string>() + " ver. " + data["version"].get<std::string>()
|
||||
+ " with " + std::to_string(data["folds"].get<int>()) + " Folds cross validation and " + std::to_string(data["seeds"].size())
|
||||
+ " random seeds. " + data["date"].get<std::string>() + " " + data["time"].get<std::string>()
|
||||
);
|
||||
std::cout << headerLine(data["title"].get<std::string>());
|
||||
std::cout << headerLine(
|
||||
"Random seeds: " + fromVector("seeds") + " Discretized: " + (data["discretized"].get<bool>() ? "True" : "False")
|
||||
+ " Stratified: " + (data["stratified"].get<bool>() ? "True" : "False")
|
||||
);
|
||||
oss << "Execution took " << std::setprecision(2) << std::fixed << data["duration"].get<float>()
|
||||
<< " seconds, " << data["duration"].get<float>() / 3600 << " hours, on " << data["platform"].get<std::string>();
|
||||
std::cout << headerLine(oss.str());
|
||||
std::cout << headerLine("Score is " + data["score_name"].get<std::string>());
|
||||
std::cout << std::string(MAXL, '*') << std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
void ReportConsole::body()
|
||||
{
|
||||
auto tmp = ConfigLocale();
|
||||
int maxHyper = 15;
|
||||
int maxDataset = 7;
|
||||
for (const auto& r : data["results"]) {
|
||||
maxHyper = std::max(maxHyper, (int)r["hyperparameters"].dump().size());
|
||||
maxDataset = std::max(maxDataset, (int)r["dataset"].get<std::string>().size());
|
||||
|
||||
}
|
||||
std::cout << Colors::GREEN() << " # " << std::setw(maxDataset) << std::left << "Dataset" << " Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << std::endl;
|
||||
std::cout << "=== " << std::string(maxDataset, '=') << " ====== ===== === ========= ========= ========= =============== =================== " << std::string(maxHyper, '=') << std::endl;
|
||||
json lastResult;
|
||||
double totalScore = 0.0;
|
||||
bool odd = true;
|
||||
int index = 0;
|
||||
for (const auto& r : data["results"]) {
|
||||
if (selectedIndex != -1 && index != selectedIndex) {
|
||||
index++;
|
||||
continue;
|
||||
}
|
||||
auto color = odd ? Colors::CYAN() : Colors::BLUE();
|
||||
std::cout << color;
|
||||
std::string separator{ " " };
|
||||
if (r.find("notes") != r.end()) {
|
||||
separator = r["notes"].size() > 0 ? Colors::YELLOW() + Symbols::notebook + color : " ";
|
||||
}
|
||||
std::cout << std::setw(3) << std::right << index++ << separator;
|
||||
std::cout << std::setw(maxDataset) << std::left << r["dataset"].get<std::string>() << " ";
|
||||
std::cout << std::setw(6) << std::right << r["samples"].get<int>() << " ";
|
||||
std::cout << std::setw(5) << std::right << r["features"].get<int>() << " ";
|
||||
std::cout << std::setw(3) << std::right << r["classes"].get<int>() << " ";
|
||||
std::cout << std::setw(9) << std::setprecision(2) << std::fixed << r["nodes"].get<float>() << " ";
|
||||
std::cout << std::setw(9) << std::setprecision(2) << std::fixed << r["leaves"].get<float>() << " ";
|
||||
std::cout << std::setw(9) << std::setprecision(2) << std::fixed << r["depth"].get<float>() << " ";
|
||||
std::cout << std::setw(8) << std::right << std::setprecision(6) << std::fixed << r["score"].get<double>() << "±" << std::setw(6) << std::setprecision(4) << std::fixed << r["score_std"].get<double>();
|
||||
const std::string status = compareResult(r["dataset"].get<std::string>(), r["score"].get<double>());
|
||||
std::cout << status;
|
||||
std::cout << std::setw(12) << std::right << std::setprecision(6) << std::fixed << r["time"].get<double>() << "±" << std::setw(6) << std::setprecision(4) << std::fixed << r["time_std"].get<double>() << " ";
|
||||
std::cout << r["hyperparameters"].dump();
|
||||
std::cout << std::endl;
|
||||
std::cout << std::flush;
|
||||
lastResult = r;
|
||||
totalScore += r["score"].get<double>();
|
||||
odd = !odd;
|
||||
}
|
||||
if (data["results"].size() == 1 || selectedIndex != -1) {
|
||||
std::cout << std::string(MAXL, '*') << std::endl;
|
||||
if (lastResult.find("notes") != lastResult.end()) {
|
||||
if (lastResult["notes"].size() > 0) {
|
||||
std::cout << headerLine("Notes: ");
|
||||
for (const auto& note : lastResult["notes"]) {
|
||||
std::cout << headerLine(note.get<std::string>());
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << headerLine(fVector("Train scores: ", lastResult["scores_train"], 14, 12));
|
||||
std::cout << headerLine(fVector("Test scores: ", lastResult["scores_test"], 14, 12));
|
||||
std::cout << headerLine(fVector("Train times: ", lastResult["times_train"], 10, 3));
|
||||
std::cout << headerLine(fVector("Test times: ", lastResult["times_test"], 10, 3));
|
||||
} else {
|
||||
footer(totalScore);
|
||||
}
|
||||
std::cout << std::string(MAXL, '*') << Colors::RESET() << std::endl;
|
||||
}
|
||||
void ReportConsole::showSummary()
|
||||
{
|
||||
for (const auto& item : summary) {
|
||||
std::stringstream oss;
|
||||
oss << std::setw(3) << std::left << item.first;
|
||||
oss << std::setw(3) << std::right << item.second << " ";
|
||||
oss << std::left << meaning.at(item.first);
|
||||
std::cout << headerLine(oss.str(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void ReportConsole::footer(double totalScore)
|
||||
{
|
||||
std::cout << Colors::MAGENTA() << std::string(MAXL, '*') << std::endl;
|
||||
showSummary();
|
||||
auto score = data["score_name"].get<std::string>();
|
||||
auto best = BestScore::getScore(score);
|
||||
if (best.first != "") {
|
||||
std::stringstream oss;
|
||||
oss << score << " compared to " << best.first << " .: " << totalScore / best.second;
|
||||
std::cout << headerLine(oss.str());
|
||||
}
|
||||
if (!getExistBestFile() && compare) {
|
||||
std::cout << headerLine("*** Best Results File not found. Couldn't compare any result!");
|
||||
}
|
||||
}
|
||||
}
|
22
src/reports/ReportConsole.h
Normal file
22
src/reports/ReportConsole.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef REPORTCONSOLE_H
|
||||
#define REPORTCONSOLE_H
|
||||
#include <string>
|
||||
#include "ReportBase.h"
|
||||
#include "Colors.h"
|
||||
|
||||
namespace platform {
|
||||
const int MAXL = 133;
|
||||
class ReportConsole : public ReportBase {
|
||||
public:
|
||||
explicit ReportConsole(json data_, bool compare = false, int index = -1) : ReportBase(data_, compare), selectedIndex(index) {};
|
||||
virtual ~ReportConsole() = default;
|
||||
private:
|
||||
int selectedIndex;
|
||||
std::string headerLine(const std::string& text, int utf);
|
||||
void header() override;
|
||||
void body() override;
|
||||
void footer(double totalScore);
|
||||
void showSummary() override;
|
||||
};
|
||||
};
|
||||
#endif
|
234
src/reports/ReportExcel.cc
Normal file
234
src/reports/ReportExcel.cc
Normal file
@@ -0,0 +1,234 @@
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include "ReportExcel.h"
|
||||
#include "BestScore.h"
|
||||
|
||||
|
||||
namespace platform {
|
||||
|
||||
ReportExcel::ReportExcel(json data_, bool compare, lxw_workbook* workbook, lxw_worksheet* worksheet) : ReportBase(data_, compare), ExcelFile(workbook, worksheet)
|
||||
{
|
||||
createFile();
|
||||
}
|
||||
|
||||
void ReportExcel::formatColumns()
|
||||
{
|
||||
worksheet_freeze_panes(worksheet, 6, 1);
|
||||
std::vector<int> columns_sizes = { 22, 10, 9, 7, 12, 12, 12, 12, 12, 3, 15, 12, 23 };
|
||||
for (int i = 0; i < columns_sizes.size(); ++i) {
|
||||
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()
|
||||
{
|
||||
if (workbook == NULL) {
|
||||
workbook = workbook_new((Paths::excel() + Paths::excelResults()).c_str());
|
||||
}
|
||||
if (worksheet == NULL) {
|
||||
createWorksheet();
|
||||
}
|
||||
setProperties(data["title"].get<std::string>());
|
||||
formatColumns();
|
||||
}
|
||||
|
||||
void ReportExcel::closeFile()
|
||||
{
|
||||
workbook_close(workbook);
|
||||
}
|
||||
|
||||
void ReportExcel::header()
|
||||
{
|
||||
std::locale mylocale(std::cout.getloc(), new separated);
|
||||
std::locale::global(mylocale);
|
||||
std::cout.imbue(mylocale);
|
||||
std::stringstream oss;
|
||||
std::string message = data["model"].get<std::string>() + " ver. " + data["version"].get<std::string>() + " " +
|
||||
data["language"].get<std::string>() + " ver. " + data["language_version"].get<std::string>() +
|
||||
" with " + std::to_string(data["folds"].get<int>()) + " Folds cross validation and " + std::to_string(data["seeds"].size()) +
|
||||
" random seeds. " + data["date"].get<std::string>() + " " + data["time"].get<std::string>();
|
||||
worksheet_merge_range(worksheet, 0, 0, 0, 12, message.c_str(), styles["headerFirst"]);
|
||||
worksheet_merge_range(worksheet, 1, 0, 1, 12, data["title"].get<std::string>().c_str(), styles["headerRest"]);
|
||||
worksheet_merge_range(worksheet, 2, 0, 3, 0, ("Score is " + data["score_name"].get<std::string>()).c_str(), styles["headerRest"]);
|
||||
worksheet_merge_range(worksheet, 2, 1, 3, 3, "Execution time", styles["headerRest"]);
|
||||
oss << std::setprecision(2) << std::fixed << data["duration"].get<float>() << " s";
|
||||
worksheet_merge_range(worksheet, 2, 4, 2, 5, oss.str().c_str(), styles["headerRest"]);
|
||||
oss.str("");
|
||||
oss.clear();
|
||||
oss << std::setprecision(2) << std::fixed << data["duration"].get<float>() / 3600 << " h";
|
||||
worksheet_merge_range(worksheet, 3, 4, 3, 5, oss.str().c_str(), styles["headerRest"]);
|
||||
worksheet_merge_range(worksheet, 2, 6, 3, 7, "Platform", styles["headerRest"]);
|
||||
worksheet_merge_range(worksheet, 2, 8, 3, 9, data["platform"].get<std::string>().c_str(), styles["headerRest"]);
|
||||
worksheet_merge_range(worksheet, 2, 10, 2, 12, ("Random seeds: " + fromVector("seeds")).c_str(), styles["headerSmall"]);
|
||||
oss.str("");
|
||||
oss.clear();
|
||||
oss << "Stratified: " << (data["stratified"].get<bool>() ? "True" : "False");
|
||||
worksheet_merge_range(worksheet, 3, 10, 3, 11, oss.str().c_str(), styles["headerSmall"]);
|
||||
oss.str("");
|
||||
oss.clear();
|
||||
oss << "Discretized: " << (data["discretized"].get<bool>() ? "True" : "False");
|
||||
worksheet_write_string(worksheet, 3, 12, oss.str().c_str(), styles["headerSmall"]);
|
||||
}
|
||||
void ReportExcel::header_notes(int row)
|
||||
{
|
||||
writeString(row, 0, "Dataset", "bodyHeader");
|
||||
worksheet_merge_range(worksheet, row, 1, row, 6, "Note", styles["bodyHeader_even"]);
|
||||
}
|
||||
void ReportExcel::append_notes(const json& r, int row)
|
||||
{
|
||||
static bool even_note = true;
|
||||
std::string suffix;
|
||||
if (even_note) {
|
||||
even_note = false;
|
||||
suffix = "_even";
|
||||
} else {
|
||||
even_note = true;
|
||||
suffix = "_odd";
|
||||
}
|
||||
lxw_format* style = NULL;
|
||||
style = styles.at("text" + suffix);
|
||||
auto initial_row = row;
|
||||
for (const auto& note : r["notes"]) {
|
||||
worksheet_merge_range(worksheet, row, 1, row, 6, note.get<std::string>().c_str(), style);
|
||||
row++;
|
||||
}
|
||||
if (row - 1 == initial_row) {
|
||||
writeString(initial_row, 0, r["dataset"].get<std::string>(), "text");
|
||||
} else {
|
||||
worksheet_merge_range(worksheet, initial_row, 0, row - 1, 0, r["dataset"].get<std::string>().c_str(), style);
|
||||
}
|
||||
}
|
||||
|
||||
void ReportExcel::body()
|
||||
{
|
||||
auto head = std::vector<std::string>(
|
||||
{ "Dataset", "Samples", "Features", "Classes", "Nodes", "Edges", "States", "Score", "Score Std.", "St.", "Time",
|
||||
"Time Std.", "Hyperparameters" });
|
||||
int col = 0;
|
||||
for (const auto& item : head) {
|
||||
writeString(5, col++, item, "bodyHeader");
|
||||
}
|
||||
row = 6;
|
||||
col = 0;
|
||||
int hypSize = 22;
|
||||
json lastResult;
|
||||
double totalScore = 0.0;
|
||||
std::string hyperparameters;
|
||||
bool only_one_result = data["results"].size() == 1;
|
||||
bool first_note = true;
|
||||
int notes_row = 15 + data["results"].size();
|
||||
for (const auto& r : data["results"]) {
|
||||
writeString(row, col, r["dataset"].get<std::string>(), "text");
|
||||
writeInt(row, col + 1, r["samples"].get<int>(), "ints");
|
||||
writeInt(row, col + 2, r["features"].get<int>(), "ints");
|
||||
writeInt(row, col + 3, r["classes"].get<int>(), "ints");
|
||||
writeDouble(row, col + 4, r["nodes"].get<float>(), "floats");
|
||||
writeDouble(row, col + 5, r["leaves"].get<float>(), "floats");
|
||||
writeDouble(row, col + 6, r["depth"].get<double>(), "floats");
|
||||
writeDouble(row, col + 7, r["score"].get<double>(), "result");
|
||||
writeDouble(row, col + 8, r["score_std"].get<double>(), "result");
|
||||
const std::string status = compareResult(r["dataset"].get<std::string>(), r["score"].get<double>());
|
||||
writeString(row, col + 9, status, "textCentered");
|
||||
writeDouble(row, col + 10, r["time"].get<double>(), "time");
|
||||
writeDouble(row, col + 11, r["time_std"].get<double>(), "time");
|
||||
hyperparameters = r["hyperparameters"].dump();
|
||||
if (hyperparameters.size() > hypSize) {
|
||||
hypSize = hyperparameters.size();
|
||||
}
|
||||
writeString(row, col + 12, hyperparameters, "text");
|
||||
lastResult = r;
|
||||
totalScore += r["score"].get<double>();
|
||||
row++;
|
||||
if (!only_one_result) {
|
||||
// take care of the possible notes
|
||||
if (r.find("notes") != r.end()) {
|
||||
if (r["notes"].size() > 0) {
|
||||
if (first_note) {
|
||||
first_note = false;
|
||||
header_notes(notes_row++);
|
||||
}
|
||||
append_notes(r, notes_row);
|
||||
notes_row += r["notes"].size();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Set the right column width of hyperparameters with the maximum length
|
||||
worksheet_set_column(worksheet, 12, 12, hypSize + 5, NULL);
|
||||
// Show totals if only one dataset is present in the result
|
||||
if (only_one_result) {
|
||||
row++;
|
||||
if (lastResult.find("notes") != lastResult.end()) {
|
||||
if (lastResult["notes"].size() > 0) {
|
||||
writeString(row++, 1, "Notes: ", "bodyHeader");
|
||||
for (const auto& note : lastResult["notes"]) {
|
||||
worksheet_merge_range(worksheet, row, 2, row, 5, note.get<std::string>().c_str(), efectiveStyle("text"));
|
||||
row++;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const std::string& group : { "scores_train", "scores_test", "times_train", "times_test" }) {
|
||||
row++;
|
||||
col = 1;
|
||||
writeString(row, col, group, "text");
|
||||
for (double item : lastResult[group]) {
|
||||
std::string style = group.find("scores") != std::string::npos ? "result" : "time";
|
||||
writeDouble(row, ++col, item, style);
|
||||
}
|
||||
}
|
||||
// Set with of columns to show those totals completely
|
||||
worksheet_set_column(worksheet, 1, 1, 12, NULL);
|
||||
for (int i = 2; i < 7; ++i) {
|
||||
// doesn't work with from col to col, so...
|
||||
worksheet_set_column(worksheet, i, i, 15, NULL);
|
||||
}
|
||||
} else {
|
||||
footer(totalScore, row);
|
||||
}
|
||||
}
|
||||
|
||||
void ReportExcel::showSummary()
|
||||
{
|
||||
for (const auto& item : summary) {
|
||||
worksheet_write_string(worksheet, row + 2, 1, item.first.c_str(), styles["summaryStyle"]);
|
||||
worksheet_write_number(worksheet, row + 2, 2, item.second, styles["summaryStyle"]);
|
||||
worksheet_merge_range(worksheet, row + 2, 3, row + 2, 5, meaning.at(item.first).c_str(), styles["summaryStyle"]);
|
||||
row += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ReportExcel::footer(double totalScore, int row)
|
||||
{
|
||||
showSummary();
|
||||
row += 4 + summary.size();
|
||||
auto score = data["score_name"].get<std::string>();
|
||||
auto best = BestScore::getScore(score);
|
||||
if (best.first != "") {
|
||||
worksheet_merge_range(worksheet, row, 1, row, 5, (score + " compared to " + best.first + " .:").c_str(), efectiveStyle("text"));
|
||||
writeDouble(row, 6, totalScore / best.second, "result");
|
||||
}
|
||||
if (!getExistBestFile() && compare) {
|
||||
worksheet_write_string(worksheet, row + 1, 0, "*** Best Results File not found. Couldn't compare any result!", styles["summaryStyle"]);
|
||||
}
|
||||
}
|
||||
}
|
25
src/reports/ReportExcel.h
Normal file
25
src/reports/ReportExcel.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef REPORTEXCEL_H
|
||||
#define REPORTEXCEL_H
|
||||
#include<map>
|
||||
#include "xlsxwriter.h"
|
||||
#include "ReportBase.h"
|
||||
#include "ExcelFile.h"
|
||||
#include "Colors.h"
|
||||
namespace platform {
|
||||
class ReportExcel : public ReportBase, public ExcelFile {
|
||||
public:
|
||||
explicit ReportExcel(json data_, bool compare, lxw_workbook* workbook, lxw_worksheet* worksheet = NULL);
|
||||
private:
|
||||
void formatColumns();
|
||||
void createFile();
|
||||
void createWorksheet();
|
||||
void closeFile();
|
||||
void header() override;
|
||||
void body() override;
|
||||
void showSummary() override;
|
||||
void footer(double totalScore, int row);
|
||||
void append_notes(const json& r, int row);
|
||||
void header_notes(int row);
|
||||
};
|
||||
};
|
||||
#endif // !REPORTEXCEL_H
|
Reference in New Issue
Block a user