From 506ef34c6f67331eaa4fe88e4fa1553299f92d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Sat, 5 Aug 2023 20:29:05 +0200 Subject: [PATCH] Add report output to main --- src/BayesNet/KDB.cc | 1 - src/Platform/CMakeLists.txt | 2 +- src/Platform/Experiment.cc | 8 +++++ src/Platform/Experiment.h | 1 + src/Platform/Report.cc | 66 +++++++++++++++++++++++++++++++++++++ src/Platform/Report.h | 23 +++++++++++++ src/Platform/main.cc | 2 +- 7 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/Platform/Report.cc create mode 100644 src/Platform/Report.h diff --git a/src/BayesNet/KDB.cc b/src/BayesNet/KDB.cc index c1611c5..a0ab434 100644 --- a/src/BayesNet/KDB.cc +++ b/src/BayesNet/KDB.cc @@ -35,7 +35,6 @@ namespace bayesnet { } // 2. Compute class conditional mutual information I(Xi;XjIC), f or each auto conditionalEdgeWeights = metrics.conditionalEdge(); - cout << "Conditional edge weights: " << conditionalEdgeWeights << endl; // 3. Let the used variable list, S, be empty. vector S; // 4. Let the DAG network being constructed, BN, begin with a single diff --git a/src/Platform/CMakeLists.txt b/src/Platform/CMakeLists.txt index 7de4c29..3b13abc 100644 --- a/src/Platform/CMakeLists.txt +++ b/src/Platform/CMakeLists.txt @@ -4,5 +4,5 @@ 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) +add_executable(main main.cc Folding.cc platformUtils.cc Experiment.cc Datasets.cc Models.cc Report.cc) target_link_libraries(main BayesNet ArffFiles mdlp "${TORCH_LIBRARIES}") \ No newline at end of file diff --git a/src/Platform/Experiment.cc b/src/Platform/Experiment.cc index a79216c..cdd6da6 100644 --- a/src/Platform/Experiment.cc +++ b/src/Platform/Experiment.cc @@ -1,6 +1,7 @@ #include "Experiment.h" #include "Datasets.h" #include "Models.h" +#include "Report.h" namespace platform { using json = nlohmann::json; @@ -86,6 +87,13 @@ namespace platform { file.close(); } + void Experiment::report() + { + json data = build_json(); + Report report(data); + report.show(); + } + void Experiment::show() { json data = build_json(); diff --git a/src/Platform/Experiment.h b/src/Platform/Experiment.h index 4305316..e2f1936 100644 --- a/src/Platform/Experiment.h +++ b/src/Platform/Experiment.h @@ -108,6 +108,7 @@ namespace platform { void cross_validation(const string& path, const string& fileName); void go(vector filesToProcess, const string& path); void show(); + void report(); }; } #endif \ No newline at end of file diff --git a/src/Platform/Report.cc b/src/Platform/Report.cc new file mode 100644 index 0000000..ecff1a6 --- /dev/null +++ b/src/Platform/Report.cc @@ -0,0 +1,66 @@ +#include "Report.h" + +namespace platform { + string headerLine(const string& text) + { + int n = MAXL - text.length() - 3; + return "* " + text + string(n, ' ') + "*\n"; + } + string Report::fromVector(const string& key) + { + string result = ""; + + for (auto& item : data[key]) { + result += to_string(item) + ", "; + } + return "[" + result.substr(0, result.length() - 2) + "]"; + } + string fVector(const json& data) + { + string result = ""; + for (const auto& item : data) { + result += to_string(item) + ", "; + } + return "[" + result.substr(0, result.length() - 2) + "]"; + } + void Report::show() + { + header(); + body(); + } + void Report::header() + { + cout << string(MAXL, '*') << endl; + cout << headerLine("Report " + data["model"].get() + " ver. " + data["version"].get() + " with " + to_string(data["folds"].get()) + " Folds cross validation and " + to_string(data["seeds"].size()) + " random seeds. " + data["date"].get() + " " + data["time"].get()); + cout << headerLine(data["title"].get()); + cout << headerLine("Random seeds: " + fromVector("seeds") + " Stratified: " + (data["stratified"].get() ? "True" : "False")); + cout << headerLine("Execution took " + to_string(data["duration"].get()) + " seconds, " + to_string(data["duration"].get() / 3600) + " hours, on " + data["platform"].get()); + cout << headerLine("Score is " + data["score_name"].get()); + cout << string(MAXL, '*') << endl; + cout << endl; + } + void Report::body() + { + cout << "Dataset Sampl. Feat. Cls Nodes Edges States Score Time Hyperparameters" << endl; + cout << "============================== ====== ===== === ======= ======= ======= =============== ================= ===============" << endl; + for (const auto& r : data["results"]) { + cout << setw(30) << left << r["dataset"].get() << " "; + cout << setw(6) << right << r["samples"].get() << " "; + cout << setw(5) << right << r["features"].get() << " "; + cout << setw(3) << right << r["classes"].get() << " "; + cout << setw(7) << right << r["nodes"].get() << " "; + cout << setw(7) << right << r["leaves"].get() << " "; + cout << setw(7) << right << r["depth"].get() << " "; + cout << setw(8) << right << setprecision(6) << fixed << r["score_test"].get() << "±" << setw(6) << setprecision(4) << fixed << r["score_test_std"].get() << " "; + cout << setw(10) << right << setprecision(6) << fixed << r["test_time"].get() << "±" << setw(6) << setprecision(4) << fixed << r["test_time_std"].get() << " "; + cout << " " << r["hyperparameters"].get(); + cout << endl; + cout << string(MAXL, '*') << endl; + cout << headerLine("Train scores: " + fVector(r["scores_train"])); + cout << headerLine("Test scores: " + fVector(r["scores_test"])); + cout << headerLine("Train times: " + fVector(r["times_train"])); + cout << headerLine("Test times: " + fVector(r["times_test"])); + cout << string(MAXL, '*') << endl; + } + } +} \ No newline at end of file diff --git a/src/Platform/Report.h b/src/Platform/Report.h new file mode 100644 index 0000000..c6ea8a1 --- /dev/null +++ b/src/Platform/Report.h @@ -0,0 +1,23 @@ +#ifndef REPORT_H +#define REPORT_H +#include +#include +#include + +using json = nlohmann::json; +const int MAXL = 121; +namespace platform { + using namespace std; + class Report { + public: + explicit Report(json data_) { data = data_; }; + virtual ~Report() = default; + void show(); + private: + void header(); + void body(); + string fromVector(const string& key); + json data; + }; +}; +#endif \ No newline at end of file diff --git a/src/Platform/main.cc b/src/Platform/main.cc index 9a41080..c50a8ef 100644 --- a/src/Platform/main.cc +++ b/src/Platform/main.cc @@ -116,7 +116,7 @@ int main(int argc, char** argv) if (saveResults) experiment.save(PATH_RESULTS); else - experiment.show(); + experiment.report(); cout << "Done!" << endl; return 0; }