From 7c3e315ae70b5e57a64c4dfdeaef8101b40c4602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Tue, 29 Aug 2023 18:20:55 +0200 Subject: [PATCH] Add Linux specific options to compile --- .clang-uml | 26 ++++-- .gitignore | 1 + CMakeLists.txt | 2 +- Makefile | 3 + TAN_iris.dot | 12 --- sample/sample.cc | 8 +- src/Platform/Datasets.cc | 1 + src/Platform/Experiment.cc | 2 +- src/Platform/Folding.cc | 168 +++++++++++++++++++------------------ src/Platform/Folding.h | 61 +++++++------- 10 files changed, 146 insertions(+), 138 deletions(-) delete mode 100644 TAN_iris.dot diff --git a/.clang-uml b/.clang-uml index 29fd166..a94c7c5 100644 --- a/.clang-uml +++ b/.clang-uml @@ -1,19 +1,31 @@ compilation_database_dir: build output_directory: puml diagrams: - myproject_class: + BayesNet: type: class glob: - - src/bayesnet/*.cc - - src/platform/*.cc + - src/BayesNet/*.cc + - src/Platform/*.cc using_namespace: bayesnet include: namespaces: - bayesnet - platform - exclude: - namespaces: - - myproject::detail plantuml: after: - - 'note left of {{ alias("MyProjectMain") }}: Main class of myproject library.' + - "note left of {{ alias(\"MyProjectMain\") }}: Main class of myproject library." + sequence: + type: sequence + glob: + - src/Platform/main.cc + combine_free_functions_into_file_participants: true + using_namespace: + - std + - bayesnet + - platform + include: + paths: + - src/BayesNet + - src/Platform + start_from: + - function: main(int,const char **) diff --git a/.gitignore b/.gitignore index 54717f5..8855507 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ build/ *.dSYM/** cmake-build*/** .idea +puml/** diff --git a/CMakeLists.txt b/CMakeLists.txt index a187279..6b0f675 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") option(ENABLE_CLANG_TIDY "Enable to add clang tidy." OFF) option(ENABLE_TESTING "Unit testing build" OFF) option(CODE_COVERAGE "Collect coverage from test library" OFF) - +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") # CMakes modules # -------------- set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH}) diff --git a/Makefile b/Makefile index ca6da18..8e3165e 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,9 @@ clean: ## Clean the debug info find . -name "*.gcda" -print0 | xargs -0 rm @echo ">>> Done"; +clang-uml: ## Create uml class and sequence diagrams + clang-uml -p --add-compile-flag -I /usr/lib/gcc/x86_64-redhat-linux/8/include/ + debug: ## Build a debug version of the project @echo ">>> Building Debug BayesNet ..."; @if [ -d ./build ]; then rm -rf ./build; fi diff --git a/TAN_iris.dot b/TAN_iris.dot deleted file mode 100644 index d3ce2cc..0000000 --- a/TAN_iris.dot +++ /dev/null @@ -1,12 +0,0 @@ -digraph BayesNet { -label= -fontsize=30 -fontcolor=blue -labelloc=t -layout=circo - class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] - class -> sepallength class -> sepalwidth class -> petallength class -> petalwidth petallength [shape=circle] - petallength -> sepallength petalwidth [shape=circle] - sepallength [shape=circle] - sepallength -> sepalwidth sepalwidth [shape=circle] - sepalwidth -> petalwidth } diff --git a/sample/sample.cc b/sample/sample.cc index d6bcf34..9d7175f 100644 --- a/sample/sample.cc +++ b/sample/sample.cc @@ -10,7 +10,7 @@ #include "Folding.h" #include "Models.h" #include "modelRegister.h" - +#include using namespace std; @@ -195,11 +195,11 @@ int main(int argc, char** argv) Xt.index_put_({ i, "..." }, torch::tensor(Xd[i], torch::kInt32)); } float total_score = 0, total_score_train = 0, score_train, score_test; - Fold* fold; + platform::Fold* fold; if (stratified) - fold = new StratifiedKFold(nFolds, y, seed); + fold = new platform::StratifiedKFold(nFolds, y, seed); else - fold = new KFold(nFolds, y.size(), seed); + fold = new platform::KFold(nFolds, y.size(), seed); for (auto i = 0; i < nFolds; ++i) { auto [train, test] = fold->getFold(i); cout << "Fold: " << i + 1 << endl; diff --git a/src/Platform/Datasets.cc b/src/Platform/Datasets.cc index b187be8..2239a46 100644 --- a/src/Platform/Datasets.cc +++ b/src/Platform/Datasets.cc @@ -1,6 +1,7 @@ #include "Datasets.h" #include "platformUtils.h" #include "ArffFiles.h" +#include namespace platform { void Datasets::load() { diff --git a/src/Platform/Experiment.cc b/src/Platform/Experiment.cc index 09674ab..ac26972 100644 --- a/src/Platform/Experiment.cc +++ b/src/Platform/Experiment.cc @@ -2,7 +2,7 @@ #include "Datasets.h" #include "Models.h" #include "ReportConsole.h" - +#include namespace platform { using json = nlohmann::json; string get_date() diff --git a/src/Platform/Folding.cc b/src/Platform/Folding.cc index 7c59bce..48e03dd 100644 --- a/src/Platform/Folding.cc +++ b/src/Platform/Folding.cc @@ -1,95 +1,97 @@ #include "Folding.h" #include #include -Fold::Fold(int k, int n, int seed) : k(k), n(n), seed(seed) -{ - random_device rd; - random_seed = default_random_engine(seed == -1 ? rd() : seed); - srand(seed == -1 ? time(0) : seed); -} -KFold::KFold(int k, int n, int seed) : Fold(k, n, seed), indices(vector(n)) -{ - iota(begin(indices), end(indices), 0); // fill with 0, 1, ..., n - 1 - shuffle(indices.begin(), indices.end(), random_seed); -} -pair, vector> KFold::getFold(int nFold) -{ - if (nFold >= k || nFold < 0) { - throw out_of_range("nFold (" + to_string(nFold) + ") must be less than k (" + to_string(k) + ")"); +namespace platform { + Fold::Fold(int k, int n, int seed) : k(k), n(n), seed(seed) + { + random_device rd; + random_seed = default_random_engine(seed == -1 ? rd() : seed); + srand(seed == -1 ? time(0) : seed); } - int nTest = n / k; - auto train = vector(); - auto test = vector(); - for (int i = 0; i < n; i++) { - if (i >= nTest * nFold && i < nTest * (nFold + 1)) { - test.push_back(indices[i]); - } else { - train.push_back(indices[i]); - } - } - return { train, test }; -} -StratifiedKFold::StratifiedKFold(int k, torch::Tensor& y, int seed) : Fold(k, y.numel(), seed) -{ - n = y.numel(); - this->y = vector(y.data_ptr(), y.data_ptr() + n); - build(); -} -StratifiedKFold::StratifiedKFold(int k, const vector& y, int seed) - : Fold(k, y.size(), seed) -{ - this->y = y; - n = y.size(); - build(); -} -void StratifiedKFold::build() -{ - stratified_indices = vector>(k); - int fold_size = n / k; - // Compute class counts and indices - auto class_indices = map>(); - vector class_counts(*max_element(y.begin(), y.end()) + 1, 0); - for (auto i = 0; i < n; ++i) { - class_counts[y[i]]++; - class_indices[y[i]].push_back(i); - } - // Shuffle class indices - for (auto& [cls, indices] : class_indices) { + KFold::KFold(int k, int n, int seed) : Fold(k, n, seed), indices(vector(n)) + { + iota(begin(indices), end(indices), 0); // fill with 0, 1, ..., n - 1 shuffle(indices.begin(), indices.end(), random_seed); } - // Assign indices to folds - for (auto label = 0; label < class_counts.size(); ++label) { - auto num_samples_to_take = class_counts[label] / k; - if (num_samples_to_take == 0) - continue; - auto remainder_samples_to_take = class_counts[label] % k; - for (auto fold = 0; fold < k; ++fold) { - auto it = next(class_indices[label].begin(), num_samples_to_take); - move(class_indices[label].begin(), it, back_inserter(stratified_indices[fold])); // ## - class_indices[label].erase(class_indices[label].begin(), it); + pair, vector> KFold::getFold(int nFold) + { + if (nFold >= k || nFold < 0) { + throw out_of_range("nFold (" + to_string(nFold) + ") must be less than k (" + to_string(k) + ")"); } - while (remainder_samples_to_take > 0) { - int fold = (rand() % static_cast(k)); - if (stratified_indices[fold].size() == fold_size + 1) { - continue; + int nTest = n / k; + auto train = vector(); + auto test = vector(); + for (int i = 0; i < n; i++) { + if (i >= nTest * nFold && i < nTest * (nFold + 1)) { + test.push_back(indices[i]); + } else { + train.push_back(indices[i]); + } + } + return { train, test }; + } + StratifiedKFold::StratifiedKFold(int k, torch::Tensor& y, int seed) : Fold(k, y.numel(), seed) + { + n = y.numel(); + this->y = vector(y.data_ptr(), y.data_ptr() + n); + build(); + } + StratifiedKFold::StratifiedKFold(int k, const vector& y, int seed) + : Fold(k, y.size(), seed) + { + this->y = y; + n = y.size(); + build(); + } + void StratifiedKFold::build() + { + stratified_indices = vector>(k); + int fold_size = n / k; + // Compute class counts and indices + auto class_indices = map>(); + vector class_counts(*max_element(y.begin(), y.end()) + 1, 0); + for (auto i = 0; i < n; ++i) { + class_counts[y[i]]++; + class_indices[y[i]].push_back(i); + } + // Shuffle class indices + for (auto& [cls, indices] : class_indices) { + shuffle(indices.begin(), indices.end(), random_seed); + } + // Assign indices to folds + for (auto label = 0; label < class_counts.size(); ++label) { + auto num_samples_to_take = class_counts[label] / k; + if (num_samples_to_take == 0) + continue; + auto remainder_samples_to_take = class_counts[label] % k; + for (auto fold = 0; fold < k; ++fold) { + auto it = next(class_indices[label].begin(), num_samples_to_take); + move(class_indices[label].begin(), it, back_inserter(stratified_indices[fold])); // ## + class_indices[label].erase(class_indices[label].begin(), it); + } + while (remainder_samples_to_take > 0) { + int fold = (rand() % static_cast(k)); + if (stratified_indices[fold].size() == fold_size + 1) { + continue; + } + auto it = next(class_indices[label].begin(), 1); + stratified_indices[fold].push_back(*class_indices[label].begin()); + class_indices[label].erase(class_indices[label].begin(), it); + remainder_samples_to_take--; } - auto it = next(class_indices[label].begin(), 1); - stratified_indices[fold].push_back(*class_indices[label].begin()); - class_indices[label].erase(class_indices[label].begin(), it); - remainder_samples_to_take--; } } -} -pair, vector> StratifiedKFold::getFold(int nFold) -{ - if (nFold >= k || nFold < 0) { - throw out_of_range("nFold (" + to_string(nFold) + ") must be less than k (" + to_string(k) + ")"); + pair, vector> StratifiedKFold::getFold(int nFold) + { + if (nFold >= k || nFold < 0) { + throw out_of_range("nFold (" + to_string(nFold) + ") must be less than k (" + to_string(k) + ")"); + } + vector test_indices = stratified_indices[nFold]; + vector train_indices; + for (int i = 0; i < k; ++i) { + if (i == nFold) continue; + train_indices.insert(train_indices.end(), stratified_indices[i].begin(), stratified_indices[i].end()); + } + return { train_indices, test_indices }; } - vector test_indices = stratified_indices[nFold]; - vector train_indices; - for (int i = 0; i < k; ++i) { - if (i == nFold) continue; - train_indices.insert(train_indices.end(), stratified_indices[i].begin(), stratified_indices[i].end()); - } - return { train_indices, test_indices }; } \ No newline at end of file diff --git a/src/Platform/Folding.h b/src/Platform/Folding.h index eaf0c4b..81b6ba2 100644 --- a/src/Platform/Folding.h +++ b/src/Platform/Folding.h @@ -4,34 +4,35 @@ #include #include using namespace std; - -class Fold { -protected: - int k; - int n; - int seed; - default_random_engine random_seed; -public: - Fold(int k, int n, int seed = -1); - virtual pair, vector> getFold(int nFold) = 0; - virtual ~Fold() = default; - int getNumberOfFolds() { return k; } -}; -class KFold : public Fold { -private: - vector indices; -public: - KFold(int k, int n, int seed = -1); - pair, vector> getFold(int nFold) override; -}; -class StratifiedKFold : public Fold { -private: - vector y; - vector> stratified_indices; - void build(); -public: - StratifiedKFold(int k, const vector& y, int seed = -1); - StratifiedKFold(int k, torch::Tensor& y, int seed = -1); - pair, vector> getFold(int nFold) override; -}; +namespace platform { + class Fold { + protected: + int k; + int n; + int seed; + default_random_engine random_seed; + public: + Fold(int k, int n, int seed = -1); + virtual pair, vector> getFold(int nFold) = 0; + virtual ~Fold() = default; + int getNumberOfFolds() { return k; } + }; + class KFold : public Fold { + private: + vector indices; + public: + KFold(int k, int n, int seed = -1); + pair, vector> getFold(int nFold) override; + }; + class StratifiedKFold : public Fold { + private: + vector y; + vector> stratified_indices; + void build(); + public: + StratifiedKFold(int k, const vector& y, int seed = -1); + StratifiedKFold(int k, torch::Tensor& y, int seed = -1); + pair, vector> getFold(int nFold) override; + }; +} #endif \ No newline at end of file