Refactor library folders
Add paginators per output type in b_manage
This commit is contained in:
78
src/reports/DatasetsConsole.hpp
Normal file
78
src/reports/DatasetsConsole.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#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;
|
||||
};
|
||||
}
|
||||
|
48
src/reports/DatasetsExcel.cpp
Normal file
48
src/reports/DatasetsExcel.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include "DatasetsExcel.h"
|
||||
|
||||
namespace platform {
|
||||
DatasetsExcel::DatasetsExcel()
|
||||
{
|
||||
file_name = "datasets.xlsx";
|
||||
workbook = workbook_new(getFileName().c_str());
|
||||
createFormats();
|
||||
setProperties("Datasets");
|
||||
}
|
||||
DatasetsExcel::~DatasetsExcel()
|
||||
{
|
||||
workbook_close(workbook);
|
||||
}
|
||||
void DatasetsExcel::report(json& data)
|
||||
{
|
||||
int datasetNameSize = 25; // Min size of the column
|
||||
int balanceSize = 75; // Min size of the column
|
||||
worksheet = workbook_add_worksheet(workbook, "Datasets");
|
||||
// Header
|
||||
worksheet_merge_range(worksheet, 0, 0, 0, 5, "Datasets", styles["headerFirst"]);
|
||||
// Body header
|
||||
row = 2;
|
||||
int col = 0;
|
||||
for (const auto& name : { "Nº", "Dataset", "Samples", "Features", "Classes", "Balance" }) {
|
||||
writeString(row, col++, name, "bodyHeader");
|
||||
}
|
||||
// Body
|
||||
for (auto& [key, value] : data.items()) {
|
||||
row++;
|
||||
if (key.size() > datasetNameSize) {
|
||||
datasetNameSize = key.size();
|
||||
}
|
||||
writeInt(row, 0, row - 3, "ints");
|
||||
writeString(row, 1, key.c_str(), "text");
|
||||
writeInt(row, 2, value["samples"], "ints");
|
||||
writeInt(row, 3, value["features"], "ints");
|
||||
writeInt(row, 4, value["classes"], "ints");
|
||||
writeString(row, 5, value["balance"].get<std::string>().c_str(), "text");
|
||||
}
|
||||
// Format columns
|
||||
worksheet_freeze_panes(worksheet, 3, 2);
|
||||
std::vector<int> columns_sizes = { 5, datasetNameSize, 10, 10, 10, balanceSize };
|
||||
for (int i = 0; i < columns_sizes.size(); ++i) {
|
||||
worksheet_set_column(worksheet, i, i, columns_sizes.at(i), NULL);
|
||||
}
|
||||
}
|
||||
}
|
16
src/reports/DatasetsExcel.h
Normal file
16
src/reports/DatasetsExcel.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "reports/ExcelFile.h"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
namespace platform {
|
||||
|
||||
class DatasetsExcel : public ExcelFile {
|
||||
public:
|
||||
DatasetsExcel();
|
||||
~DatasetsExcel();
|
||||
void report(json& data);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user