From 0ec53f405f4e5410c3d3790945d88fec82cef112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Sun, 5 May 2024 11:14:01 +0200 Subject: [PATCH] Fix mistakes in feature selection in SPnDE Complete the first A2DE test Update version number --- .vscode/launch.json | 2 +- CHANGELOG.md | 9 ++++++--- CMakeLists.txt | 2 +- bayesnet/classifiers/SPnDE.cc | 3 ++- bayesnet/ensembles/A2DE.cc | 5 +++-- tests/CMakeLists.txt | 2 +- tests/TestA2DE.cc | 10 ++++------ 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 0d2525f..2a0ba63 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "name": "test", "program": "${workspaceFolder}/build_debug/tests/TestBayesNet", "args": [ - "\"Bisection Best\"" + "[A2DE]" ], "cwd": "${workspaceFolder}/build_debug/tests" }, diff --git a/CHANGELOG.md b/CHANGELOG.md index b34fba4..eafa922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Add the Library logo generated with to README.md -- Add link to the coverage report in the README.md coverage label. -- Add the *convergence_best* hyperparameter to the BoostAODE class, to control the way the prior accuracy is computed if convergence is set. Default value is *false*. +- Library logo generated with to README.md +- Link to the coverage report in the README.md coverage label. +- *convergence_best* hyperparameter to the BoostAODE class, to control the way the prior accuracy is computed if convergence is set. Default value is *false*. +- SPnDE model. +- A2DE model. ### Internal @@ -19,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Refactor catch2 library location to test/lib - Refactor loadDataset function in tests. - Remove conditionalEdgeWeights method in BayesMetrics. +- A2DE & SPnDE tests. ## [1.0.5] 2024-04-20 diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b4c6b6..3f52715 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.20) project(BayesNet - VERSION 1.0.5 + VERSION 1.0.5.1 DESCRIPTION "Bayesian Network and basic classifiers Library." HOMEPAGE_URL "https://github.com/rmontanana/bayesnet" LANGUAGES CXX diff --git a/bayesnet/classifiers/SPnDE.cc b/bayesnet/classifiers/SPnDE.cc index 4785b26..984f78c 100644 --- a/bayesnet/classifiers/SPnDE.cc +++ b/bayesnet/classifiers/SPnDE.cc @@ -16,7 +16,7 @@ namespace bayesnet { addNodes(); std::vector attributes; for (int i = 0; i < static_cast(features.size()); ++i) { - if (std::find(parents.begin(), parents.end(), i) != parents.end()) { + if (std::find(parents.begin(), parents.end(), i) == parents.end()) { attributes.push_back(i); } } @@ -25,6 +25,7 @@ namespace bayesnet { for (const auto& attribute : attributes) { model.addEdge(className, features[attribute]); for (const auto& root : parents) { + model.addEdge(features[root], features[attribute]); } } diff --git a/bayesnet/ensembles/A2DE.cc b/bayesnet/ensembles/A2DE.cc index 27e709c..3f7d348 100644 --- a/bayesnet/ensembles/A2DE.cc +++ b/bayesnet/ensembles/A2DE.cc @@ -27,10 +27,11 @@ namespace bayesnet { significanceModels.clear(); for (int i = 0; i < features.size() - 1; ++i) { for (int j = i + 1; j < features.size(); ++j) { - models.push_back(std::make_unique(std::vector({ i, j }))); + auto model = std::make_unique(std::vector({ i, j })); + models.push_back(std::move(model)); } } - n_models = models.size(); + n_models = static_cast(models.size()); significanceModels = std::vector(n_models, 1.0); } std::vector A2DE::graph(const std::string& title) const diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b51bff3..e54cc18 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,7 @@ if(ENABLE_TESTING) ) file(GLOB_RECURSE BayesNet_SOURCES "${BayesNet_SOURCE_DIR}/bayesnet/*.cc") add_executable(TestBayesNet TestBayesNetwork.cc TestBayesNode.cc TestBayesClassifier.cc - TestBayesModels.cc TestBayesMetrics.cc TestFeatureSelection.cc TestBoostAODE.cc + TestBayesModels.cc TestBayesMetrics.cc TestFeatureSelection.cc TestBoostAODE.cc TestA2DE.cc TestUtils.cc TestBayesEnsemble.cc ${BayesNet_SOURCES}) target_link_libraries(TestBayesNet PUBLIC "${TORCH_LIBRARIES}" ArffFiles mdlp PRIVATE Catch2::Catch2WithMain) add_test(NAME BayesNetworkTest COMMAND TestBayesNet) diff --git a/tests/TestA2DE.cc b/tests/TestA2DE.cc index 8cd128e..790930d 100644 --- a/tests/TestA2DE.cc +++ b/tests/TestA2DE.cc @@ -17,10 +17,8 @@ TEST_CASE("Fit and Score", "[A2DE]") auto raw = RawDatasets("glass", true); auto clf = bayesnet::A2DE(); clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states); - std::cout << "Score A2DE: " << clf.score(raw.Xv, raw.yv) << std::endl; - // REQUIRE(clf.getNumberOfNodes() == 90); - // REQUIRE(clf.getNumberOfEdges() == 153); - // REQUIRE(clf.getNotes().size() == 2); - // REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 9 with CFS"); - // REQUIRE(clf.getNotes()[1] == "Number of models: 9"); + REQUIRE(clf.score(raw.Xv, raw.yv) == Catch::Approx(0.831776).epsilon(raw.epsilon)); + REQUIRE(clf.getNumberOfNodes() == 360); + REQUIRE(clf.getNumberOfEdges() == 756); + REQUIRE(clf.getNotes().size() == 0); }