From 9a0449c12d8a03a66ade2ae5b24412a16247c47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Sat, 29 Jul 2023 19:38:42 +0200 Subject: [PATCH] Fix some lint warnings --- sample/sample.cc | 13 ++++--------- src/BayesNet/BayesMetrics.cc | 3 +-- src/BayesNet/BayesMetrics.h | 2 +- src/BayesNet/KDB.h | 2 +- src/BayesNet/Mst.h | 4 ++-- src/BayesNet/Network.h | 6 +++--- src/BayesNet/SPODE.h | 2 +- src/Platform/Models.cc | 19 ------------------- src/Platform/main.cc | 12 ++++-------- 9 files changed, 17 insertions(+), 46 deletions(-) diff --git a/sample/sample.cc b/sample/sample.cc index 502dcfc..2d7efa4 100644 --- a/sample/sample.cc +++ b/sample/sample.cc @@ -148,9 +148,9 @@ int main(int argc, char** argv) // Get className & Features auto className = handler.getClassName(); vector features; - for (auto feature : handler.getAttributes()) { - features.push_back(feature.first); - } + auto attributes = handler.getAttributes(); + transform(attributes.begin(), attributes.end(), back_inserter(features), + [](const pair& item) { return item.first; }); // Discretize Dataset auto [Xd, maxes] = discretize(X, y, features); maxes[className] = *max_element(y.begin(), y.end()) + 1; @@ -159,12 +159,7 @@ int main(int argc, char** argv) states[feature] = vector(maxes[feature]); } states[className] = vector(maxes[className]); - auto classifiers = map({ - { "AODE", new bayesnet::AODE() }, { "KDB", new bayesnet::KDB(2) }, - { "SPODE", new bayesnet::SPODE(2) }, { "TAN", new bayesnet::TAN() } - } - ); - bayesnet::BaseClassifier* clf = classifiers[model_name]; + auto clf = platform::Models::instance()->create(model_name); clf->fit(Xd, y, features, className, states); auto score = clf->score(Xd, y); auto lines = clf->show(); diff --git a/src/BayesNet/BayesMetrics.cc b/src/BayesNet/BayesMetrics.cc index 6671995..be15f07 100644 --- a/src/BayesNet/BayesMetrics.cc +++ b/src/BayesNet/BayesMetrics.cc @@ -12,8 +12,8 @@ namespace bayesnet { : features(features) , className(className) , classNumStates(classNumStates) + , samples(torch::zeros({ static_cast(vsamples[0].size()), static_cast(vsamples.size() + 1) }, torch::kInt32)) { - samples = torch::zeros({ static_cast(vsamples[0].size()), static_cast(vsamples.size() + 1) }, torch::kInt32); for (int i = 0; i < vsamples.size(); ++i) { samples.index_put_({ "...", i }, torch::tensor(vsamples[i], torch::kInt32)); } @@ -123,7 +123,6 @@ namespace bayesnet { */ vector> Metrics::maximumSpanningTree(vector features, Tensor& weights, int root) { - auto result = vector>(); auto mst = MST(features, weights, root); return mst.maximumSpanningTree(); } diff --git a/src/BayesNet/BayesMetrics.h b/src/BayesNet/BayesMetrics.h index f8557a0..427b1d4 100644 --- a/src/BayesNet/BayesMetrics.h +++ b/src/BayesNet/BayesMetrics.h @@ -11,7 +11,7 @@ namespace bayesnet { Tensor samples; vector features; string className; - int classNumStates; + int classNumStates = 0; public: Metrics() = default; Metrics(Tensor&, vector&, string&, int); diff --git a/src/BayesNet/KDB.h b/src/BayesNet/KDB.h index b0790da..e3f257f 100644 --- a/src/BayesNet/KDB.h +++ b/src/BayesNet/KDB.h @@ -13,7 +13,7 @@ namespace bayesnet { protected: void train() override; public: - KDB(int k, float theta = 0.03); + explicit KDB(int k, float theta = 0.03); virtual ~KDB() {}; vector graph(string name = "KDB") override; }; diff --git a/src/BayesNet/Mst.h b/src/BayesNet/Mst.h index 15b0dbb..71a46a5 100644 --- a/src/BayesNet/Mst.h +++ b/src/BayesNet/Mst.h @@ -10,7 +10,7 @@ namespace bayesnet { private: Tensor weights; vector features; - int root; + int root = 0; public: MST() = default; MST(vector& features, Tensor& weights, int root); @@ -23,7 +23,7 @@ namespace bayesnet { vector >> T; // vector for mst vector parent; public: - Graph(int V); + explicit Graph(int V); void addEdge(int u, int v, float wt); int find_set(int i); void union_set(int u, int v); diff --git a/src/BayesNet/Network.h b/src/BayesNet/Network.h index c8d832d..ffa4f67 100644 --- a/src/BayesNet/Network.h +++ b/src/BayesNet/Network.h @@ -27,9 +27,9 @@ namespace bayesnet { void completeFit(); public: Network(); - Network(float, int); - Network(float); - Network(Network&); + explicit Network(float, int); + explicit Network(float); + explicit Network(Network&); torch::Tensor& getSamples(); float getmaxThreads(); void addNode(string, int); diff --git a/src/BayesNet/SPODE.h b/src/BayesNet/SPODE.h index 0f422a7..30f0b46 100644 --- a/src/BayesNet/SPODE.h +++ b/src/BayesNet/SPODE.h @@ -9,7 +9,7 @@ namespace bayesnet { protected: void train() override; public: - SPODE(int root); + explicit SPODE(int root); virtual ~SPODE() {}; vector graph(string name = "SPODE") override; }; diff --git a/src/Platform/Models.cc b/src/Platform/Models.cc index aa23a2d..df1b517 100644 --- a/src/Platform/Models.cc +++ b/src/Platform/Models.cc @@ -2,25 +2,6 @@ namespace platform { using namespace std; // Idea from: https://www.codeproject.com/Articles/567242/AplusC-2b-2bplusObjectplusFactory - // shared_ptr Models::createInstance(const string& name) - // { - // bayesnet::BaseClassifier* instance = nullptr; - // if (name == "AODE") { - // instance = new bayesnet::AODE(); - // } else if (name == "KDB") { - // instance = new bayesnet::KDB(2); - // } else if (name == "SPODE") { - // instance = new bayesnet::SPODE(2); - // } else if (name == "TAN") { - // instance = new bayesnet::TAN(); - // } else { - // throw runtime_error("Model " + name + " not found"); - // } - // if (instance != nullptr) - // return shared_ptr(instance); - // else - // return nullptr; - // } Models* Models::factory = nullptr;; Models* Models::instance() { diff --git a/src/Platform/main.cc b/src/Platform/main.cc index 29c8505..d9dfb40 100644 --- a/src/Platform/main.cc +++ b/src/Platform/main.cc @@ -76,10 +76,6 @@ argparse::ArgumentParser manageArguments(int argc, char** argv) } return program; } -void registerModels() -{ - -} int main(int argc, char** argv) { @@ -92,7 +88,7 @@ int main(int argc, char** argv) auto stratified = program.get("stratified"); auto n_folds = program.get("folds"); auto seeds = program.get>("seeds"); - vector filesToProcess; + vector filesToTest; auto datasets = platform::Datasets(path, true, platform::ARFF); auto title = program.get("title"); if (file_name != "") { @@ -103,9 +99,9 @@ int main(int argc, char** argv) if (title == "") { title = "Test " + file_name + " " + model_name + " " + to_string(n_folds) + " folds"; } - filesToProcess.push_back(file_name); + filesToTest.push_back(file_name); } else { - filesToProcess = platform::Datasets(path, true, platform::ARFF).getNames(); + filesToTest = platform::Datasets(path, true, platform::ARFF).getNames(); saveResults = true; } @@ -121,7 +117,7 @@ int main(int argc, char** argv) } platform::Timer timer; timer.start(); - experiment.go(filesToProcess, path); + experiment.go(filesToTest, path); experiment.setDuration(timer.getDuration()); if (saveResults) experiment.save(PATH_RESULTS);