Convert DatasetsConsole & ResultsDatasetConsole to string output
This commit is contained in:
@@ -40,8 +40,8 @@ target_link_libraries(b_grid ${MPI_CXX_LIBRARIES} "${PyClassifiers}" "${BayesNet
|
|||||||
add_executable(b_list commands/b_list.cpp ${list_sources}
|
add_executable(b_list commands/b_list.cpp ${list_sources}
|
||||||
common/Datasets.cpp common/Dataset.cpp
|
common/Datasets.cpp common/Dataset.cpp
|
||||||
main/Models.cpp
|
main/Models.cpp
|
||||||
reports/ReportExcel.cpp reports/ExcelFile.cpp reports/ReportBase.cpp reports/DatasetsExcel.cpp
|
reports/ReportExcel.cpp reports/ExcelFile.cpp reports/ReportBase.cpp reports/DatasetsExcel.cpp reports/DatasetsConsole.cpp
|
||||||
results/Result.cpp results/ResultsDatasetExcel.cpp results/ResultsDataset.cpp
|
results/Result.cpp results/ResultsDatasetExcel.cpp results/ResultsDataset.cpp results/ResultsDatasetConsole.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(b_list "${PyClassifiers}" "${BayesNet}" ArffFiles mdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
target_link_libraries(b_list "${PyClassifiers}" "${BayesNet}" ArffFiles mdlp ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} Boost::python Boost::numpy "${XLSXWRITER_LIB}")
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ list(TRANSFORM manage_sources PREPEND manage/)
|
|||||||
add_executable(
|
add_executable(
|
||||||
b_manage commands/b_manage.cpp ${manage_sources}
|
b_manage commands/b_manage.cpp ${manage_sources}
|
||||||
common/Datasets.cpp common/Dataset.cpp
|
common/Datasets.cpp common/Dataset.cpp
|
||||||
reports/ReportConsole.cpp reports/ReportExcel.cpp reports/ReportExcelCompared.cpp reports/ReportBase.cpp reports/ExcelFile.cpp
|
reports/ReportConsole.cpp reports/ReportExcel.cpp reports/ReportExcelCompared.cpp reports/ReportBase.cpp reports/ExcelFile.cpp reports/DatasetsConsole.cpp
|
||||||
results/Result.cpp
|
results/Result.cpp results/ResultsDatasetConsole.cpp results/ResultsDataset.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp)
|
target_link_libraries(b_manage "${TORCH_LIBRARIES}" "${XLSXWRITER_LIB}" ArffFiles mdlp)
|
||||||
|
@@ -9,10 +9,10 @@
|
|||||||
#include "common/Colors.h"
|
#include "common/Colors.h"
|
||||||
#include "common/Datasets.h"
|
#include "common/Datasets.h"
|
||||||
#include "reports/DatasetsExcel.h"
|
#include "reports/DatasetsExcel.h"
|
||||||
#include "reports/DatasetsConsole.hpp"
|
#include "reports/DatasetsConsole.h"
|
||||||
#include "results/ResultsDataset.h"
|
#include "results/ResultsDataset.h"
|
||||||
#include "results/ResultsDatasetExcel.h"
|
#include "results/ResultsDatasetExcel.h"
|
||||||
#include "results/ResultsDatasetConsole.hpp"
|
#include "results/ResultsDatasetConsole.h"
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@
|
|||||||
#include "common/Paths.h"
|
#include "common/Paths.h"
|
||||||
#include "CommandParser.h"
|
#include "CommandParser.h"
|
||||||
#include "ManageResults.h"
|
#include "ManageResults.h"
|
||||||
// #include "reports/DatasetsConsole.hpp"
|
//#include "reports/DatasetsConsole.h"
|
||||||
#include "reports/ReportConsole.h"
|
#include "reports/ReportConsole.h"
|
||||||
#include "reports/ReportExcel.h"
|
#include "reports/ReportExcel.h"
|
||||||
#include "reports/ReportExcelCompared.h"
|
#include "reports/ReportExcelCompared.h"
|
||||||
|
56
src/reports/DatasetsConsole.cpp
Normal file
56
src/reports/DatasetsConsole.cpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include "common/Colors.h"
|
||||||
|
#include "common/Datasets.h"
|
||||||
|
#include "common/Paths.h"
|
||||||
|
#include "DatasetsConsole.h"
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
const int DatasetsConsole::BALANCE_LENGTH = 75;
|
||||||
|
std::string DatasetsConsole::outputBalance(const std::string& balance)
|
||||||
|
{
|
||||||
|
auto temp = std::string(balance);
|
||||||
|
while (temp.size() > DatasetsConsole::BALANCE_LENGTH - 1) {
|
||||||
|
auto part = temp.substr(0, DatasetsConsole::BALANCE_LENGTH);
|
||||||
|
output << part << std::endl;
|
||||||
|
output << setw(52) << " ";
|
||||||
|
temp = temp.substr(DatasetsConsole::BALANCE_LENGTH);
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
void DatasetsConsole::list_datasets()
|
||||||
|
{
|
||||||
|
auto datasets = platform::Datasets(false, platform::Paths::datasets());
|
||||||
|
locale mylocale(std::cout.getloc(), new separated_datasets);
|
||||||
|
locale::global(mylocale);
|
||||||
|
output.imbue(mylocale);
|
||||||
|
output << Colors::GREEN() << " # Dataset Sampl. Feat. Cls Balance" << std::endl;
|
||||||
|
std::string balanceBars = std::string(DatasetsConsole::BALANCE_LENGTH, '=');
|
||||||
|
output << "=== ============================== ====== ===== === " << balanceBars << std::endl;
|
||||||
|
int num = 0;
|
||||||
|
for (const auto& dataset : datasets.getNames()) {
|
||||||
|
auto color = num % 2 ? Colors::CYAN() : Colors::BLUE();
|
||||||
|
output << color << setw(3) << right << num++ << " ";
|
||||||
|
output << setw(30) << left << dataset << " ";
|
||||||
|
datasets.loadDataset(dataset);
|
||||||
|
auto nSamples = datasets.getNSamples(dataset);
|
||||||
|
output << setw(6) << right << nSamples << " ";
|
||||||
|
output << setw(5) << right << datasets.getFeatures(dataset).size() << " ";
|
||||||
|
output << setw(3) << right << datasets.getNClasses(dataset) << " ";
|
||||||
|
std::stringstream oss;
|
||||||
|
std::string sep = "";
|
||||||
|
for (auto number : datasets.getClassesCounts(dataset)) {
|
||||||
|
oss << sep << std::setprecision(2) << fixed << (float)number / nSamples * 100.0 << "% (" << number << ")";
|
||||||
|
sep = " / ";
|
||||||
|
}
|
||||||
|
auto balance = outputBalance(oss.str());
|
||||||
|
output << balance << std::endl;
|
||||||
|
// Store data for Excel report
|
||||||
|
data[dataset] = json::object();
|
||||||
|
data[dataset]["samples"] = nSamples;
|
||||||
|
data[dataset]["features"] = datasets.getFeatures(dataset).size();
|
||||||
|
data[dataset]["classes"] = datasets.getNClasses(dataset);
|
||||||
|
data[dataset]["balance"] = oss.str();
|
||||||
|
}
|
||||||
|
numLines = num + 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
32
src/reports/DatasetsConsole.h
Normal file
32
src/reports/DatasetsConsole.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <locale>
|
||||||
|
#include <sstream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
struct separated_datasets : numpunct<char> {
|
||||||
|
char do_decimal_point() const { return ','; }
|
||||||
|
char do_thousands_sep() const { return '.'; }
|
||||||
|
std::string do_grouping() const { return "\03"; }
|
||||||
|
};
|
||||||
|
|
||||||
|
class DatasetsConsole {
|
||||||
|
public:
|
||||||
|
static const int BALANCE_LENGTH;
|
||||||
|
DatasetsConsole() = default;
|
||||||
|
~DatasetsConsole() = default;
|
||||||
|
std::string getOutput() const { return output.str(); }
|
||||||
|
int getNumLines() const { return numLines; }
|
||||||
|
json& getData() { return data; }
|
||||||
|
std::string outputBalance(const std::string& balance);
|
||||||
|
void list_datasets();
|
||||||
|
private:
|
||||||
|
std::stringstream output;
|
||||||
|
json data;
|
||||||
|
int numLines = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@@ -1,78 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <locale>
|
|
||||||
#include <map>
|
|
||||||
#include <sstream>
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include "common/Colors.h"
|
|
||||||
#include "common/Datasets.h"
|
|
||||||
|
|
||||||
namespace platform {
|
|
||||||
const int BALANCE_LENGTH = 75;
|
|
||||||
struct separated_datasets : numpunct<char> {
|
|
||||||
char do_decimal_point() const { return ','; }
|
|
||||||
char do_thousands_sep() const { return '.'; }
|
|
||||||
std::string do_grouping() const { return "\03"; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class DatasetsConsole {
|
|
||||||
public:
|
|
||||||
DatasetsConsole() = default;
|
|
||||||
~DatasetsConsole() = default;
|
|
||||||
std::string getOutput() const { return output.str(); }
|
|
||||||
int getNumLines() const { return numLines; }
|
|
||||||
json& getData() { return data; }
|
|
||||||
std::string outputBalance(const std::string& balance)
|
|
||||||
{
|
|
||||||
auto temp = std::string(balance);
|
|
||||||
while (temp.size() > BALANCE_LENGTH - 1) {
|
|
||||||
auto part = temp.substr(0, BALANCE_LENGTH);
|
|
||||||
output << part << std::endl;
|
|
||||||
output << setw(52) << " ";
|
|
||||||
temp = temp.substr(BALANCE_LENGTH);
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
void list_datasets()
|
|
||||||
{
|
|
||||||
auto datasets = platform::Datasets(false, platform::Paths::datasets());
|
|
||||||
locale mylocale(std::cout.getloc(), new separated_datasets);
|
|
||||||
locale::global(mylocale);
|
|
||||||
output.imbue(mylocale);
|
|
||||||
output << Colors::GREEN() << " # Dataset Sampl. Feat. Cls Balance" << std::endl;
|
|
||||||
std::string balanceBars = std::string(BALANCE_LENGTH, '=');
|
|
||||||
output << "=== ============================== ====== ===== === " << balanceBars << std::endl;
|
|
||||||
int num = 0;
|
|
||||||
for (const auto& dataset : datasets.getNames()) {
|
|
||||||
auto color = num % 2 ? Colors::CYAN() : Colors::BLUE();
|
|
||||||
output << color << setw(3) << right << num++ << " ";
|
|
||||||
output << setw(30) << left << dataset << " ";
|
|
||||||
datasets.loadDataset(dataset);
|
|
||||||
auto nSamples = datasets.getNSamples(dataset);
|
|
||||||
output << setw(6) << right << nSamples << " ";
|
|
||||||
output << setw(5) << right << datasets.getFeatures(dataset).size() << " ";
|
|
||||||
output << setw(3) << right << datasets.getNClasses(dataset) << " ";
|
|
||||||
std::stringstream oss;
|
|
||||||
std::string sep = "";
|
|
||||||
for (auto number : datasets.getClassesCounts(dataset)) {
|
|
||||||
oss << sep << std::setprecision(2) << fixed << (float)number / nSamples * 100.0 << "% (" << number << ")";
|
|
||||||
sep = " / ";
|
|
||||||
}
|
|
||||||
auto balance = outputBalance(oss.str());
|
|
||||||
output << balance << std::endl;
|
|
||||||
// Store data for Excel report
|
|
||||||
data[dataset] = json::object();
|
|
||||||
data[dataset]["samples"] = nSamples;
|
|
||||||
data[dataset]["features"] = datasets.getFeatures(dataset).size();
|
|
||||||
data[dataset]["classes"] = datasets.getNClasses(dataset);
|
|
||||||
data[dataset]["balance"] = oss.str();
|
|
||||||
}
|
|
||||||
numLines = num + 2;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
std::stringstream output;
|
|
||||||
json data;
|
|
||||||
int numLines = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
79
src/results/ResultsDatasetConsole.cpp
Normal file
79
src/results/ResultsDatasetConsole.cpp
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
#include "common/Colors.h"
|
||||||
|
#include "results/ResultsDataset.h"
|
||||||
|
#include "ResultsDatasetConsole.h"
|
||||||
|
namespace platform {
|
||||||
|
void ResultsDatasetsConsole::list_results(const std::string& dataset, const std::string& score, const std::string& model)
|
||||||
|
{
|
||||||
|
output.str("");
|
||||||
|
auto results = platform::ResultsDataset(dataset, model, score);
|
||||||
|
results.load();
|
||||||
|
results.sortModel();
|
||||||
|
if (results.empty()) {
|
||||||
|
output << Colors::RED() << "No results found for dataset " << dataset << " and model " << model << Colors::RESET() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int maxModel = results.maxModelSize();
|
||||||
|
int maxHyper = results.maxHyperSize();
|
||||||
|
double maxResult = results.maxResultScore();
|
||||||
|
// Build data for the Report
|
||||||
|
json data = json::object();
|
||||||
|
data["results"] = json::array();
|
||||||
|
data["max_models"] = json::object(); // Max score per model
|
||||||
|
for (const auto& result : results) {
|
||||||
|
auto results = result.getData();
|
||||||
|
if (!data["max_models"].contains(result.getModel())) {
|
||||||
|
data["max_models"][result.getModel()] = 0;
|
||||||
|
}
|
||||||
|
for (const auto& item : results["results"]) {
|
||||||
|
if (item["dataset"] == dataset) {
|
||||||
|
|
||||||
|
// Store data for Excel report
|
||||||
|
json res = json::object();
|
||||||
|
res["date"] = result.getDate();
|
||||||
|
res["time"] = result.getTime();
|
||||||
|
res["model"] = result.getModel();
|
||||||
|
res["score"] = item["score"].get<double>();
|
||||||
|
res["hyperparameters"] = item["hyperparameters"].dump();
|
||||||
|
data["results"].push_back(res);
|
||||||
|
if (item["score"].get<double>() > data["max_models"][result.getModel()]) {
|
||||||
|
data["max_models"][result.getModel()] = item["score"].get<double>();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// List the results
|
||||||
|
//
|
||||||
|
output << Colors::GREEN() << "Results of dataset " << dataset << " - for " << model << " model" << std::endl;
|
||||||
|
output << "There are " << results.size() << " results" << std::endl;
|
||||||
|
output << Colors::GREEN() << " # " << std::setw(maxModel + 1) << std::left << "Model" << "Date Time Score Hyperparameters" << std::endl;
|
||||||
|
output << "=== " << std::string(maxModel, '=') << " ========== ======== =========== " << std::string(maxHyper, '=') << std::endl;
|
||||||
|
numLines = 4;
|
||||||
|
auto i = 0;
|
||||||
|
for (const auto& item : data["results"]) {
|
||||||
|
auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN();
|
||||||
|
auto score = item["score"].get<double>();
|
||||||
|
color = score == data["max_models"][item["model"].get<std::string>()] ? Colors::YELLOW() : color;
|
||||||
|
color = score == maxResult ? Colors::RED() : color;
|
||||||
|
output << color << std::setw(3) << std::fixed << std::right << i++ << " ";
|
||||||
|
output << std::setw(maxModel) << std::left << item["model"].get<std::string>() << " ";
|
||||||
|
output << color << item["date"].get<std::string>() << " ";
|
||||||
|
output << color << item["time"].get<std::string>() << " ";
|
||||||
|
output << std::setw(11) << std::setprecision(9) << std::fixed << score << " ";
|
||||||
|
output << item["hyperparameters"].get<std::string>() << std::endl;
|
||||||
|
numLines++;
|
||||||
|
}
|
||||||
|
data["dataset"] = dataset;
|
||||||
|
data["score"] = score;
|
||||||
|
data["model"] = model;
|
||||||
|
data["lengths"]["maxModel"] = maxModel;
|
||||||
|
data["lengths"]["maxHyper"] = maxHyper;
|
||||||
|
data["maxResult"] = maxResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
27
src/results/ResultsDatasetConsole.h
Normal file
27
src/results/ResultsDatasetConsole.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <locale>
|
||||||
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include "results/ResultsDataset.h"
|
||||||
|
|
||||||
|
namespace platform {
|
||||||
|
class ResultsDatasetsConsole {
|
||||||
|
public:
|
||||||
|
ResultsDatasetsConsole() = default;
|
||||||
|
~ResultsDatasetsConsole() = default;
|
||||||
|
std::string getOutput() const { return output.str(); }
|
||||||
|
int getNumLines() const { return numLines; }
|
||||||
|
json getData() { return data; }
|
||||||
|
void list_results(const std::string& dataset, const std::string& score, const std::string& model);
|
||||||
|
private:
|
||||||
|
std::stringstream output;
|
||||||
|
json data;
|
||||||
|
int numLines = 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@@ -1,95 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <locale>
|
|
||||||
#include <map>
|
|
||||||
#include <sstream>
|
|
||||||
#include <nlohmann/json.hpp>
|
|
||||||
#include "common/Colors.h"
|
|
||||||
#include "results/ResultsDataset.h"
|
|
||||||
|
|
||||||
namespace platform {
|
|
||||||
class ResultsDatasetsConsole {
|
|
||||||
public:
|
|
||||||
ResultsDatasetsConsole() = default;
|
|
||||||
~ResultsDatasetsConsole() = default;
|
|
||||||
std::string getOutput() const { return output.str(); }
|
|
||||||
int getNumLines() const { return numLines; }
|
|
||||||
json& getData() { return data; }
|
|
||||||
void list_results(const std::string& dataset, const std::string& score, const std::string& model)
|
|
||||||
{
|
|
||||||
auto results = platform::ResultsDataset(dataset, model, score);
|
|
||||||
results.load();
|
|
||||||
results.sortModel();
|
|
||||||
if (results.empty()) {
|
|
||||||
std::cerr << Colors::RED() << "No results found for dataset " << dataset << " and model " << model << Colors::RESET() << std::endl;
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
int maxModel = results.maxModelSize();
|
|
||||||
int maxHyper = results.maxHyperSize();
|
|
||||||
double maxResult = results.maxResultScore();
|
|
||||||
// Build data for the Report
|
|
||||||
json data = json::object();
|
|
||||||
data["results"] = json::array();
|
|
||||||
data["max_models"] = json::object(); // Max score per model
|
|
||||||
for (const auto& result : results) {
|
|
||||||
auto results = result.getData();
|
|
||||||
if (!data["max_models"].contains(result.getModel())) {
|
|
||||||
data["max_models"][result.getModel()] = 0;
|
|
||||||
}
|
|
||||||
for (const auto& item : results["results"]) {
|
|
||||||
if (item["dataset"] == dataset) {
|
|
||||||
|
|
||||||
// Store data for Excel report
|
|
||||||
json res = json::object();
|
|
||||||
res["date"] = result.getDate();
|
|
||||||
res["time"] = result.getTime();
|
|
||||||
res["model"] = result.getModel();
|
|
||||||
res["score"] = item["score"].get<double>();
|
|
||||||
res["hyperparameters"] = item["hyperparameters"].dump();
|
|
||||||
data["results"].push_back(res);
|
|
||||||
if (item["score"].get<double>() > data["max_models"][result.getModel()]) {
|
|
||||||
data["max_models"][result.getModel()] = item["score"].get<double>();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//
|
|
||||||
// List the results
|
|
||||||
//
|
|
||||||
output << Colors::GREEN() << "Results of dataset " << dataset << " - for " << model << " model" << std::endl;
|
|
||||||
output << "There are " << results.size() << " results" << std::endl;
|
|
||||||
output << Colors::GREEN() << " # " << std::setw(maxModel + 1) << std::left << "Model" << "Date Time Score Hyperparameters" << std::endl;
|
|
||||||
output << "=== " << std::string(maxModel, '=') << " ========== ======== =========== " << std::string(maxHyper, '=') << std::endl;
|
|
||||||
numLines = 4;
|
|
||||||
auto i = 0;
|
|
||||||
for (const auto& item : data["results"]) {
|
|
||||||
auto color = (i % 2) ? Colors::BLUE() : Colors::CYAN();
|
|
||||||
auto score = item["score"].get<double>();
|
|
||||||
color = score == data["max_models"][item["model"].get<std::string>()] ? Colors::YELLOW() : color;
|
|
||||||
color = score == maxResult ? Colors::RED() : color;
|
|
||||||
output << color << std::setw(3) << std::fixed << std::right << i++ << " ";
|
|
||||||
output << std::setw(maxModel) << std::left << item["model"].get<std::string>() << " ";
|
|
||||||
output << color << item["date"].get<std::string>() << " ";
|
|
||||||
output << color << item["time"].get<std::string>() << " ";
|
|
||||||
output << std::setw(11) << std::setprecision(9) << std::fixed << score << " ";
|
|
||||||
output << item["hyperparameters"].get<std::string>() << std::endl;
|
|
||||||
numLines++;
|
|
||||||
}
|
|
||||||
data["dataset"] = dataset;
|
|
||||||
data["score"] = score;
|
|
||||||
data["model"] = model;
|
|
||||||
data["lengths"]["maxModel"] = maxModel;
|
|
||||||
data["lengths"]["maxHyper"] = maxHyper;
|
|
||||||
data["maxResult"] = maxResult;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
std::stringstream output;
|
|
||||||
json data;
|
|
||||||
int numLines = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user