Refactor Report class into ReportBase & ReportCons

This commit is contained in:
Ricardo Montañana Gómez 2023-08-21 17:16:29 +02:00
parent 0f66ac73d0
commit 8066701c3c
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
11 changed files with 122 additions and 73 deletions

View File

@ -55,6 +55,7 @@ endif (ENABLE_CLANG_TIDY)
add_git_submodule("lib/mdlp")
add_git_submodule("lib/argparse")
add_git_submodule("lib/json")
add_git_submodule("lib/openXLSX")
# Subdirectories
# --------------

1
lib/openXLSX Submodule

@ -0,0 +1 @@
Subproject commit b80da42d1454f361c29117095ebe1989437db390

View File

@ -4,9 +4,9 @@ include_directories(${BayesNet_SOURCE_DIR}/lib/Files)
include_directories(${BayesNet_SOURCE_DIR}/lib/mdlp)
include_directories(${BayesNet_SOURCE_DIR}/lib/argparse/include)
include_directories(${BayesNet_SOURCE_DIR}/lib/json/include)
add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.cc Models.cc Report.cc)
add_executable(manage manage.cc Results.cc Report.cc)
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 ReportConsole.cc ReportBase.cc)
add_executable(list list.cc platformUtils Datasets.cc)
target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}")
target_link_libraries(manage "${TORCH_LIBRARIES}")
target_link_libraries(manage "${TORCH_LIBRARIES}" OpenXLSX::OpenXLSX)
target_link_libraries(list ArffFiles mdlp "${TORCH_LIBRARIES}")

View File

@ -1,7 +1,7 @@
#include "Experiment.h"
#include "Datasets.h"
#include "Models.h"
#include "Report.h"
#include "ReportConsole.h"
namespace platform {
using json = nlohmann::json;
@ -91,7 +91,7 @@ namespace platform {
void Experiment::report()
{
json data = build_json();
Report report(data);
ReportConsole report(data);
report.show();
}

View File

@ -1,26 +0,0 @@
#ifndef REPORT_H
#define REPORT_H
#include <string>
#include <iostream>
#include <nlohmann/json.hpp>
#include "Colors.h"
using json = nlohmann::json;
const int MAXL = 128;
namespace platform {
using namespace std;
class Report {
public:
explicit Report(json data_) { data = data_; };
virtual ~Report() = default;
void show();
private:
void header();
void body();
void footer();
string fromVector(const string& key);
json data;
double totalScore; // Total score of all results in a report
};
};
#endif

View File

@ -0,0 +1,38 @@
#include <sstream>
#include <locale>
#include "ReportBase.h"
#include "BestResult.h"
namespace platform {
string ReportBase::fromVector(const string& key)
{
stringstream oss;
string sep = "";
oss << "[";
for (auto& item : data[key]) {
oss << sep << item.get<double>();
sep = ", ";
}
oss << "]";
return oss.str();
}
string ReportBase::fVector(const string& title, const json& data, const int width, const int precision)
{
stringstream oss;
string sep = "";
oss << title << "[";
for (const auto& item : data) {
oss << sep << fixed << setw(width) << setprecision(precision) << item.get<double>();
sep = ", ";
}
oss << "]";
return oss.str();
}
void ReportBase::show()
{
header();
body();
footer();
}
}

25
src/Platform/ReportBase.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef REPORTBASE_H
#define REPORTBASE_H
#include <string>
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace platform {
using namespace std;
class ReportBase {
public:
explicit ReportBase(json data_) { data = data_; };
virtual ~ReportBase() = default;
void show();
protected:
json data;
double totalScore; // Total score of all results in a report
string fromVector(const string& key);
string fVector(const string& title, const json& data, const int width, const int precision);
virtual void header() = 0;
virtual void body() = 0;
virtual void footer() = 0;
};
};
#endif

View File

@ -1,52 +1,23 @@
#include <sstream>
#include <locale>
#include "Report.h"
#include "ReportConsole.h"
#include "BestResult.h"
namespace platform {
struct separated : numpunct<char> {
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
string do_grouping() const { return "\03"; }
};
string headerLine(const string& text)
{
int n = MAXL - text.length() - 3;
n = n < 0 ? 0 : n;
return "* " + text + string(n, ' ') + "*\n";
}
string Report::fromVector(const string& key)
{
stringstream oss;
string sep = "";
oss << "[";
for (auto& item : data[key]) {
oss << sep << item.get<double>();
sep = ", ";
}
oss << "]";
return oss.str();
}
string fVector(const string& title, const json& data, const int width, const int precision)
{
stringstream oss;
string sep = "";
oss << title << "[";
for (const auto& item : data) {
oss << sep << fixed << setw(width) << setprecision(precision) << item.get<double>();
sep = ", ";
}
oss << "]";
return oss.str();
}
void Report::show()
{
header();
body();
footer();
}
struct separated : numpunct<char> {
char do_decimal_point() const { return ','; }
char do_thousands_sep() const { return '.'; }
string do_grouping() const { return "\03"; }
};
void Report::header()
void ReportConsole::header()
{
locale mylocale(cout.getloc(), new separated);
locale::global(mylocale);
@ -62,7 +33,7 @@ namespace platform {
cout << string(MAXL, '*') << endl;
cout << endl;
}
void Report::body()
void ReportConsole::body()
{
cout << Colors::GREEN() << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl;
cout << "============================== ====== ===== === ========= ========= ========= =============== ================== ===============" << endl;
@ -100,7 +71,7 @@ namespace platform {
cout << string(MAXL, '*') << endl;
}
}
void Report::footer()
void ReportConsole::footer()
{
cout << Colors::MAGENTA() << string(MAXL, '*') << endl;
auto score = data["score_name"].get<string>();
@ -110,6 +81,5 @@ namespace platform {
cout << headerLine(oss.str());
}
cout << string(MAXL, '*') << endl << Colors::RESET();
}
}

View File

@ -0,0 +1,24 @@
#ifndef REPORTCONSOLE_H
#define REPORTCONSOLE_H
#include <string>
#include <iostream>
#include <nlohmann/json.hpp>
#include "ReportBase.h"
#include "Colors.h"
using json = nlohmann::json;
const int MAXL = 128;
namespace platform {
using namespace std;
class ReportConsole : public ReportBase{
public:
explicit ReportConsole(json data_) : ReportBase(data_) {};
virtual ~ReportConsole() = default;
private:
void header() override;
void body() override;
void footer() override;
};
};
#endif

View File

@ -0,0 +1,16 @@
#ifndef REPORTEXCEL_H
#define REPORTEXCEL_H
#include "ReportBase.h"
namespace platform {
using namespace std;
class ReportExcel : public ReportBase{
public:
explicit ReportExcel(json data_) : ReportBase(data_) {};
virtual ~ReportExcel() = default;
private:
void header() override;
void body() override;
void footer() override;
};
};
#endif // !REPORTEXCEL_H

View File

@ -1,7 +1,7 @@
#include <filesystem>
#include "platformUtils.h"
#include "Results.h"
#include "Report.h"
#include "ReportConsole.h"
#include "BestResult.h"
#include "Colors.h"
namespace platform {
@ -98,7 +98,7 @@ namespace platform {
{
cout << Colors::YELLOW() << "Reporting " << files.at(index).getFilename() << endl;
auto data = files.at(index).load();
Report report(data);
ReportConsole report(data);
report.show();
}
void Results::menu()