Add mdlp as library in lib/
Fix tests to reach 99.1% of coverage

Reviewed-on: #31
This commit is contained in:
2024-11-23 17:22:41 +00:00
parent f0f3d9ad6e
commit 86f2bc44fc
26 changed files with 5183 additions and 261 deletions

3
.gitmodules vendored
View File

@@ -16,3 +16,6 @@
[submodule "tests/lib/Files"] [submodule "tests/lib/Files"]
path = tests/lib/Files path = tests/lib/Files
url = https://github.com/rmontanana/ArffFiles url = https://github.com/rmontanana/ArffFiles
[submodule "lib/mdlp"]
path = lib/mdlp
url = https://github.com/rmontanana/mdlp

2
.vscode/launch.json vendored
View File

@@ -16,7 +16,7 @@
"name": "test", "name": "test",
"program": "${workspaceFolder}/build_Debug/tests/TestBayesNet", "program": "${workspaceFolder}/build_Debug/tests/TestBayesNet",
"args": [ "args": [
"[Network]" "No features selected"
], ],
"cwd": "${workspaceFolder}/build_Debug/tests" "cwd": "${workspaceFolder}/build_Debug/tests"
}, },

View File

@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Fixed
- Prevent existing edges to be added to the network in the `add_edge` method.
- Don't allow to add nodes or edges on already fiited networks.
- Number of threads spawned
- Network class tests
### Added ### Added
- Library logo generated with <https://openart.ai> to README.md - Library logo generated with <https://openart.ai> to README.md
@@ -19,12 +26,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add tests to check the correct version of the mdlp, folding and json libraries. - Add tests to check the correct version of the mdlp, folding and json libraries.
- Library documentation generated with Doxygen. - Library documentation generated with Doxygen.
- Link to documentation in the README.md. - Link to documentation in the README.md.
- Three types of smoothing the Bayesian Network OLD_LAPLACE, LAPLACE and CESTNIK. - Three types of smoothing the Bayesian Network ORIGINAL, LAPLACE and CESTNIK.
### Internal ### Internal
- Fixed doxygen optional dependency
- Add env parallel variable to Makefile
- Add CountingSemaphore class to manage the number of threads spawned.
- Ignore CUDA language in CMake CodeCoverage module. - Ignore CUDA language in CMake CodeCoverage module.
- Remove mdlp library from submodules and add it as a dependency. - Update mdlp library as a git submodule.
- Create library ShuffleArffFile to limit the number of samples with a parameter and shuffle them. - Create library ShuffleArffFile to limit the number of samples with a parameter and shuffle them.
- Refactor catch2 library location to test/lib - Refactor catch2 library location to test/lib
- Refactor loadDataset function in tests. - Refactor loadDataset function in tests.

View File

@@ -65,15 +65,9 @@ endif (ENABLE_CLANG_TIDY)
# External libraries - dependencies of BayesNet # External libraries - dependencies of BayesNet
# --------------------------------------------- # ---------------------------------------------
find_library(FImdlp NAMES libfimdlp.a REQUIRED)
find_path(FImdlp_INCLUDE_DIRS REQUIRED NAMES fimdlp)
message(STATUS "FImdlp=${FImdlp}")
message(STATUS "FImdlp_INCLUDE_DIRS=${FImdlp_INCLUDE_DIRS}")
# include(FetchContent) # include(FetchContent)
add_git_submodule("lib/json") add_git_submodule("lib/json")
add_git_submodule("lib/mdlp")
# Subdirectories # Subdirectories
# -------------- # --------------

View File

@@ -43,7 +43,7 @@ setup: ## Install dependencies for tests and coverage
fi fi
@echo "* You should install plantuml & graphviz for the diagrams" @echo "* You should install plantuml & graphviz for the diagrams"
diagrams: ## Create an UML class diagram & depnendency of the project (diagrams/BayesNet.png) diagrams: ## Create an UML class diagram & dependency of the project (diagrams/BayesNet.png)
@which $(plantuml) || (echo ">>> Please install plantuml"; exit 1) @which $(plantuml) || (echo ">>> Please install plantuml"; exit 1)
@which $(dot) || (echo ">>> Please install graphviz"; exit 1) @which $(dot) || (echo ">>> Please install graphviz"; exit 1)
@which $(clang-uml) || (echo ">>> Please install clang-uml"; exit 1) @which $(clang-uml) || (echo ">>> Please install clang-uml"; exit 1)

View File

@@ -7,9 +7,9 @@
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_BayesNet&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_BayesNet&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_BayesNet&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=rmontanana_BayesNet&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=rmontanana_BayesNet)
![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/bayesnet?gitea_url=https://gitea.rmontanana.es:3000&logo=gitea) ![Gitea Last Commit](https://img.shields.io/gitea/last-commit/rmontanana/bayesnet?gitea_url=https://gitea.rmontanana.es:3000&logo=gitea)
[![Coverage Badge](https://img.shields.io/badge/Coverage-97,1%25-green)](html/index.html) [![Coverage Badge](https://img.shields.io/badge/Coverage-99,1%25-green)](html/index.html)
Bayesian Network Classifiers using libtorch from scratch Bayesian Network Classifiers library
## Dependencies ## Dependencies
@@ -71,6 +71,8 @@ make sample fname=tests/data/glass.arff
#### - AODE #### - AODE
#### - A2DE
#### - [BoostAODE](docs/BoostAODE.md) #### - [BoostAODE](docs/BoostAODE.md)
#### - BoostA2DE #### - BoostA2DE

View File

@@ -4,10 +4,9 @@ include_directories(
${BayesNet_SOURCE_DIR}/lib/json/include ${BayesNet_SOURCE_DIR}/lib/json/include
${BayesNet_SOURCE_DIR} ${BayesNet_SOURCE_DIR}
${CMAKE_BINARY_DIR}/configured_files/include ${CMAKE_BINARY_DIR}/configured_files/include
${FImdlp_INCLUDE_DIRS}
) )
file(GLOB_RECURSE Sources "*.cc") file(GLOB_RECURSE Sources "*.cc")
add_library(BayesNet ${Sources}) add_library(BayesNet ${Sources})
target_link_libraries(BayesNet ${FImdlp} "${TORCH_LIBRARIES}") target_link_libraries(BayesNet fimdlp "${TORCH_LIBRARIES}")

View File

@@ -9,7 +9,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <torch/torch.h> #include <torch/torch.h>
#include <fimdlp/CPPFImdlp.h> #include <CPPFImdlp.h>
#include "bayesnet/network/Network.h" #include "bayesnet/network/Network.h"
#include "Classifier.h" #include "Classifier.h"

View File

@@ -59,6 +59,9 @@ namespace bayesnet {
std::vector<int> featuresUsed; std::vector<int> featuresUsed;
if (selectFeatures) { if (selectFeatures) {
featuresUsed = initializeModels(smoothing); featuresUsed = initializeModels(smoothing);
if (featuresUsed.size() == 0) {
return;
}
auto ypred = predict(X_train); auto ypred = predict(X_train);
std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_); std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
// Update significance of the models // Update significance of the models

View File

@@ -209,7 +209,7 @@ namespace bayesnet {
pthread_setname_np(threadName.c_str()); pthread_setname_np(threadName.c_str());
#endif #endif
double numStates = static_cast<double>(node.second->getNumStates()); double numStates = static_cast<double>(node.second->getNumStates());
double smoothing_factor = 0.0; double smoothing_factor;
switch (smoothing) { switch (smoothing) {
case Smoothing_t::ORIGINAL: case Smoothing_t::ORIGINAL:
smoothing_factor = 1.0 / n_samples; smoothing_factor = 1.0 / n_samples;
@@ -221,7 +221,7 @@ namespace bayesnet {
smoothing_factor = 1 / numStates; smoothing_factor = 1 / numStates;
break; break;
default: default:
throw std::invalid_argument("Smoothing method not recognized " + std::to_string(static_cast<int>(smoothing))); smoothing_factor = 0.0; // No smoothing
} }
node.second->computeCPT(samples, features, smoothing_factor, weights); node.second->computeCPT(samples, features, smoothing_factor, weights);
semaphore.release(); semaphore.release();
@@ -234,16 +234,6 @@ namespace bayesnet {
for (auto& thread : threads) { for (auto& thread : threads) {
thread.join(); thread.join();
} }
// std::fstream file;
// file.open("cpt.txt", std::fstream::out | std::fstream::app);
// file << std::string(80, '*') << std::endl;
// for (const auto& item : graph("Test")) {
// file << item << std::endl;
// }
// file << std::string(80, '-') << std::endl;
// file << dump_cpt() << std::endl;
// file << std::string(80, '=') << std::endl;
// file.close();
fitted = true; fitted = true;
} }
torch::Tensor Network::predict_tensor(const torch::Tensor& samples, const bool proba) torch::Tensor Network::predict_tensor(const torch::Tensor& samples, const bool proba)

View File

@@ -53,14 +53,14 @@ namespace bayesnet {
} }
} }
void insertElement(std::list<int>& variables, int variable) void MST::insertElement(std::list<int>& variables, int variable)
{ {
if (std::find(variables.begin(), variables.end(), variable) == variables.end()) { if (std::find(variables.begin(), variables.end(), variable) == variables.end()) {
variables.push_front(variable); variables.push_front(variable);
} }
} }
std::vector<std::pair<int, int>> reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original) std::vector<std::pair<int, int>> MST::reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original)
{ {
// Create the edges of a DAG from the MST // Create the edges of a DAG from the MST
// replacing unordered_set with list because unordered_set cannot guarantee the order of the elements inserted // replacing unordered_set with list because unordered_set cannot guarantee the order of the elements inserted

View File

@@ -14,6 +14,8 @@ namespace bayesnet {
public: public:
MST() = default; MST() = default;
MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root); MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
void insertElement(std::list<int>& variables, int variable);
std::vector<std::pair<int, int>> reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original);
std::vector<std::pair<int, int>> maximumSpanningTree(); std::vector<std::pair<int, int>> maximumSpanningTree();
private: private:
torch::Tensor weights; torch::Tensor weights;

1
lib/mdlp Submodule

Submodule lib/mdlp added at 7d62d6af4a

View File

@@ -8,7 +8,6 @@ find_package(Torch REQUIRED)
find_library(BayesNet NAMES libBayesNet BayesNet libBayesNet.a REQUIRED) find_library(BayesNet NAMES libBayesNet BayesNet libBayesNet.a REQUIRED)
find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet) find_path(Bayesnet_INCLUDE_DIRS REQUIRED NAMES bayesnet)
find_library(FImdlp NAMES libfimdlp.a PATHS REQUIRED) find_library(FImdlp NAMES libfimdlp.a PATHS REQUIRED)
find_path(FImdlp_INCLUDE_DIRS REQUIRED NAMES fimdlp)
message(STATUS "FImdlp=${FImdlp}") message(STATUS "FImdlp=${FImdlp}")
message(STATUS "FImdlp_INCLUDE_DIRS=${FImdlp_INCLUDE_DIRS}") message(STATUS "FImdlp_INCLUDE_DIRS=${FImdlp_INCLUDE_DIRS}")
@@ -23,4 +22,4 @@ include_directories(
) )
add_executable(bayesnet_sample sample.cc) add_executable(bayesnet_sample sample.cc)
target_link_libraries(bayesnet_sample ${FImdlp} "${TORCH_LIBRARIES}" "${BayesNet}") target_link_libraries(bayesnet_sample fimdlp "${TORCH_LIBRARIES}" "${BayesNet}")

View File

@@ -5,7 +5,7 @@
// *************************************************************** // ***************************************************************
#include <ArffFiles.hpp> #include <ArffFiles.hpp>
#include <fimdlp/CPPFImdlp.h> #include <CPPFImdlp.h>
#include <bayesnet/ensembles/BoostAODE.h> #include <bayesnet/ensembles/BoostAODE.h>
std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y) std::vector<mdlp::labels_t> discretizeDataset(std::vector<mdlp::samples_t>& X, mdlp::labels_t& y)

View File

@@ -6,13 +6,12 @@ if(ENABLE_TESTING)
${BayesNet_SOURCE_DIR}/lib/json/include ${BayesNet_SOURCE_DIR}/lib/json/include
${BayesNet_SOURCE_DIR} ${BayesNet_SOURCE_DIR}
${CMAKE_BINARY_DIR}/configured_files/include ${CMAKE_BINARY_DIR}/configured_files/include
${FImdlp_INCLUDE_DIRS}
) )
file(GLOB_RECURSE BayesNet_SOURCES "${BayesNet_SOURCE_DIR}/bayesnet/*.cc") file(GLOB_RECURSE BayesNet_SOURCES "${BayesNet_SOURCE_DIR}/bayesnet/*.cc")
add_executable(TestBayesNet TestBayesNetwork.cc TestBayesNode.cc TestBayesClassifier.cc add_executable(TestBayesNet TestBayesNetwork.cc TestBayesNode.cc TestBayesClassifier.cc
TestBayesModels.cc TestBayesMetrics.cc TestFeatureSelection.cc TestBoostAODE.cc TestA2DE.cc TestBayesModels.cc TestBayesMetrics.cc TestFeatureSelection.cc TestBoostAODE.cc TestA2DE.cc
TestUtils.cc TestBayesEnsemble.cc TestModulesVersions.cc TestBoostA2DE.cc ${BayesNet_SOURCES}) TestUtils.cc TestBayesEnsemble.cc TestModulesVersions.cc TestBoostA2DE.cc TestMST.cc ${BayesNet_SOURCES})
target_link_libraries(TestBayesNet PUBLIC "${TORCH_LIBRARIES}" mdlp PRIVATE Catch2::Catch2WithMain) target_link_libraries(TestBayesNet PUBLIC "${TORCH_LIBRARIES}" fimdlp PRIVATE Catch2::Catch2WithMain)
add_test(NAME BayesNetworkTest COMMAND TestBayesNet) add_test(NAME BayesNetworkTest COMMAND TestBayesNet)
add_test(NAME A2DE COMMAND TestBayesNet "[A2DE]") add_test(NAME A2DE COMMAND TestBayesNet "[A2DE]")
add_test(NAME BoostA2DE COMMAND TestBayesNet "[BoostA2DE]") add_test(NAME BoostA2DE COMMAND TestBayesNet "[BoostA2DE]")
@@ -25,4 +24,5 @@ if(ENABLE_TESTING)
add_test(NAME Modules COMMAND TestBayesNet "[Modules]") add_test(NAME Modules COMMAND TestBayesNet "[Modules]")
add_test(NAME Network COMMAND TestBayesNet "[Network]") add_test(NAME Network COMMAND TestBayesNet "[Network]")
add_test(NAME Node COMMAND TestBayesNet "[Node]") add_test(NAME Node COMMAND TestBayesNet "[Node]")
add_test(NAME MST COMMAND TestBayesNet "[MST]")
endif(ENABLE_TESTING) endif(ENABLE_TESTING)

View File

@@ -45,5 +45,5 @@ TEST_CASE("Test graph", "[A2DE]")
auto graph = clf.graph(); auto graph = clf.graph();
REQUIRE(graph.size() == 78); REQUIRE(graph.size() == 78);
REQUIRE(graph[0] == "digraph BayesNet {\nlabel=<BayesNet A2DE_0>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n"); REQUIRE(graph[0] == "digraph BayesNet {\nlabel=<BayesNet A2DE_0>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n");
REQUIRE(graph[1] == "class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n"); REQUIRE(graph[1] == "\"class\" [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n");
} }

View File

@@ -85,7 +85,7 @@ TEST_CASE("Dump_cpt", "[Classifier]")
auto raw = RawDatasets("iris", true); auto raw = RawDatasets("iris", true);
model.fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing); model.fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing);
auto cpt = model.dump_cpt(); auto cpt = model.dump_cpt();
REQUIRE(cpt.size() == 1713); REQUIRE(cpt.size() == 1718);
} }
TEST_CASE("Not fitted model", "[Classifier]") TEST_CASE("Not fitted model", "[Classifier]")
{ {

View File

@@ -27,13 +27,13 @@ TEST_CASE("Test Bayesian Classifiers score & version", "[Models]")
map <pair<std::string, std::string>, float> scores{ map <pair<std::string, std::string>, float> scores{
// Diabetes // Diabetes
{{"diabetes", "AODE"}, 0.82161}, {{"diabetes", "KDB"}, 0.852865}, {{"diabetes", "SPODE"}, 0.802083}, {{"diabetes", "TAN"}, 0.821615}, {{"diabetes", "AODE"}, 0.82161}, {{"diabetes", "KDB"}, 0.852865}, {{"diabetes", "SPODE"}, 0.802083}, {{"diabetes", "TAN"}, 0.821615},
{{"diabetes", "AODELd"}, 0.8138f}, {{"diabetes", "KDBLd"}, 0.80208f}, {{"diabetes", "SPODELd"}, 0.78646f}, {{"diabetes", "TANLd"}, 0.8099f}, {{"diabetes", "BoostAODE"}, 0.83984f}, {{"diabetes", "AODELd"}, 0.8125f}, {{"diabetes", "KDBLd"}, 0.80208f}, {{"diabetes", "SPODELd"}, 0.7890625f}, {{"diabetes", "TANLd"}, 0.803385437f}, {{"diabetes", "BoostAODE"}, 0.83984f},
// Ecoli // Ecoli
{{"ecoli", "AODE"}, 0.889881}, {{"ecoli", "KDB"}, 0.889881}, {{"ecoli", "SPODE"}, 0.880952}, {{"ecoli", "TAN"}, 0.892857}, {{"ecoli", "AODE"}, 0.889881}, {{"ecoli", "KDB"}, 0.889881}, {{"ecoli", "SPODE"}, 0.880952}, {{"ecoli", "TAN"}, 0.892857},
{{"ecoli", "AODELd"}, 0.8869f}, {{"ecoli", "KDBLd"}, 0.875f}, {{"ecoli", "SPODELd"}, 0.84226f}, {{"ecoli", "TANLd"}, 0.86905f}, {{"ecoli", "BoostAODE"}, 0.89583f}, {{"ecoli", "AODELd"}, 0.875f}, {{"ecoli", "KDBLd"}, 0.880952358f}, {{"ecoli", "SPODELd"}, 0.839285731f}, {{"ecoli", "TANLd"}, 0.848214269f}, {{"ecoli", "BoostAODE"}, 0.89583f},
// Glass // Glass
{{"glass", "AODE"}, 0.79439}, {{"glass", "KDB"}, 0.827103}, {{"glass", "SPODE"}, 0.775701}, {{"glass", "TAN"}, 0.827103}, {{"glass", "AODE"}, 0.79439}, {{"glass", "KDB"}, 0.827103}, {{"glass", "SPODE"}, 0.775701}, {{"glass", "TAN"}, 0.827103},
{{"glass", "AODELd"}, 0.79439f}, {{"glass", "KDBLd"}, 0.85047f}, {{"glass", "SPODELd"}, 0.79439f}, {{"glass", "TANLd"}, 0.86449f}, {{"glass", "BoostAODE"}, 0.84579f}, {{"glass", "AODELd"}, 0.799065411f}, {{"glass", "KDBLd"}, 0.82710278f}, {{"glass", "SPODELd"}, 0.780373812f}, {{"glass", "TANLd"}, 0.869158864f}, {{"glass", "BoostAODE"}, 0.84579f},
// Iris // Iris
{{"iris", "AODE"}, 0.973333}, {{"iris", "KDB"}, 0.973333}, {{"iris", "SPODE"}, 0.973333}, {{"iris", "TAN"}, 0.973333}, {{"iris", "AODE"}, 0.973333}, {{"iris", "KDB"}, 0.973333}, {{"iris", "SPODE"}, 0.973333}, {{"iris", "TAN"}, 0.973333},
{{"iris", "AODELd"}, 0.973333}, {{"iris", "KDBLd"}, 0.973333}, {{"iris", "SPODELd"}, 0.96f}, {{"iris", "TANLd"}, 0.97333f}, {{"iris", "BoostAODE"}, 0.98f} {{"iris", "AODELd"}, 0.973333}, {{"iris", "KDBLd"}, 0.973333}, {{"iris", "SPODELd"}, 0.96f}, {{"iris", "TANLd"}, 0.97333f}, {{"iris", "BoostAODE"}, 0.98f}
@@ -71,10 +71,10 @@ TEST_CASE("Test Bayesian Classifiers score & version", "[Models]")
TEST_CASE("Models features & Graph", "[Models]") TEST_CASE("Models features & Graph", "[Models]")
{ {
auto graph = std::vector<std::string>({ "digraph BayesNet {\nlabel=<BayesNet Test>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n", auto graph = std::vector<std::string>({ "digraph BayesNet {\nlabel=<BayesNet Test>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n",
"class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n", "\"class\" [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n",
"class -> sepallength", "class -> sepalwidth", "class -> petallength", "class -> petalwidth", "petallength [shape=circle] \n", "\"class\" -> \"sepallength\"", "\"class\" -> \"sepalwidth\"", "\"class\" -> \"petallength\"", "\"class\" -> \"petalwidth\"", "\"petallength\" [shape=circle] \n",
"petallength -> sepallength", "petalwidth [shape=circle] \n", "sepallength [shape=circle] \n", "\"petallength\" -> \"sepallength\"", "\"petalwidth\" [shape=circle] \n", "\"sepallength\" [shape=circle] \n",
"sepallength -> sepalwidth", "sepalwidth [shape=circle] \n", "sepalwidth -> petalwidth", "}\n" "\"sepallength\" -> \"sepalwidth\"", "\"sepalwidth\" [shape=circle] \n", "\"sepalwidth\" -> \"petalwidth\"", "}\n"
} }
); );
SECTION("Test TAN") SECTION("Test TAN")
@@ -96,7 +96,7 @@ TEST_CASE("Models features & Graph", "[Models]")
clf.fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing); clf.fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing);
REQUIRE(clf.getNumberOfNodes() == 5); REQUIRE(clf.getNumberOfNodes() == 5);
REQUIRE(clf.getNumberOfEdges() == 7); REQUIRE(clf.getNumberOfEdges() == 7);
REQUIRE(clf.getNumberOfStates() == 19); REQUIRE(clf.getNumberOfStates() == 27);
REQUIRE(clf.getClassNumStates() == 3); REQUIRE(clf.getClassNumStates() == 3);
REQUIRE(clf.show() == std::vector<std::string>{"class -> sepallength, sepalwidth, petallength, petalwidth, ", "petallength -> sepallength, ", "petalwidth -> ", "sepallength -> sepalwidth, ", "sepalwidth -> petalwidth, "}); REQUIRE(clf.show() == std::vector<std::string>{"class -> sepallength, sepalwidth, petallength, petalwidth, ", "petallength -> sepallength, ", "petalwidth -> ", "sepallength -> sepalwidth, ", "sepalwidth -> petalwidth, "});
REQUIRE(clf.graph("Test") == graph); REQUIRE(clf.graph("Test") == graph);

View File

@@ -186,11 +186,11 @@ TEST_CASE("Test Bayesian Network", "[Network]")
auto str = net.graph("Test Graph"); auto str = net.graph("Test Graph");
REQUIRE(str.size() == 7); REQUIRE(str.size() == 7);
REQUIRE(str[0] == "digraph BayesNet {\nlabel=<BayesNet Test Graph>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n"); REQUIRE(str[0] == "digraph BayesNet {\nlabel=<BayesNet Test Graph>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n");
REQUIRE(str[1] == "A [shape=circle] \n"); REQUIRE(str[1] == "\"A\" [shape=circle] \n");
REQUIRE(str[2] == "A -> B"); REQUIRE(str[2] == "\"A\" -> \"B\"");
REQUIRE(str[3] == "A -> C"); REQUIRE(str[3] == "\"A\" -> \"C\"");
REQUIRE(str[4] == "B [shape=circle] \n"); REQUIRE(str[4] == "\"B\" [shape=circle] \n");
REQUIRE(str[5] == "C [shape=circle] \n"); REQUIRE(str[5] == "\"C\" [shape=circle] \n");
REQUIRE(str[6] == "}\n"); REQUIRE(str[6] == "}\n");
} }
SECTION("Test predict") SECTION("Test predict")
@@ -257,9 +257,9 @@ TEST_CASE("Test Bayesian Network", "[Network]")
REQUIRE(node->getCPT().equal(node2->getCPT())); REQUIRE(node->getCPT().equal(node2->getCPT()));
} }
} }
SECTION("Test oddities") SECTION("Network oddities")
{ {
INFO("Test oddities"); INFO("Network oddities");
buildModel(net, raw.features, raw.className); buildModel(net, raw.features, raw.className);
// predict without fitting // predict without fitting
std::vector<std::vector<int>> test = { {1, 2, 0, 1, 1}, {0, 1, 2, 0, 1}, {0, 0, 0, 0, 1}, {2, 2, 2, 2, 1} }; std::vector<std::vector<int>> test = { {1, 2, 0, 1, 1}, {0, 1, 2, 0, 1}, {0, 0, 0, 0, 1}, {2, 2, 2, 2, 1} };
@@ -329,6 +329,14 @@ TEST_CASE("Test Bayesian Network", "[Network]")
std::string invalid_state = "Feature sepallength not found in states"; std::string invalid_state = "Feature sepallength not found in states";
REQUIRE_THROWS_AS(net4.fit(raw.Xv, raw.yv, raw.weightsv, raw.features, raw.className, std::map<std::string, std::vector<int>>(), raw.smoothing), std::invalid_argument); REQUIRE_THROWS_AS(net4.fit(raw.Xv, raw.yv, raw.weightsv, raw.features, raw.className, std::map<std::string, std::vector<int>>(), raw.smoothing), std::invalid_argument);
REQUIRE_THROWS_WITH(net4.fit(raw.Xv, raw.yv, raw.weightsv, raw.features, raw.className, std::map<std::string, std::vector<int>>(), raw.smoothing), invalid_state); REQUIRE_THROWS_WITH(net4.fit(raw.Xv, raw.yv, raw.weightsv, raw.features, raw.className, std::map<std::string, std::vector<int>>(), raw.smoothing), invalid_state);
// Try to add node or edge to a fitted network
auto net5 = bayesnet::Network();
buildModel(net5, raw.features, raw.className);
net5.fit(raw.Xv, raw.yv, raw.weightsv, raw.features, raw.className, raw.states, raw.smoothing);
REQUIRE_THROWS_AS(net5.addNode("A"), std::logic_error);
REQUIRE_THROWS_WITH(net5.addNode("A"), "Cannot add node to a fitted network. Initialize first.");
REQUIRE_THROWS_AS(net5.addEdge("A", "B"), std::logic_error);
REQUIRE_THROWS_WITH(net5.addEdge("A", "B"), "Cannot add edge to a fitted network. Initialize first.");
} }
} }
@@ -373,7 +381,7 @@ TEST_CASE("Dump CPT", "[Network]")
0.3333 0.3333
0.3333 0.3333
0.3333 0.3333
[ CPUFloatType{3} ] [ CPUDoubleType{3} ]
* petallength: (4) : [4, 3, 3] * petallength: (4) : [4, 3, 3]
(1,.,.) = (1,.,.) =
0.9388 0.1000 0.2000 0.9388 0.1000 0.2000
@@ -394,7 +402,7 @@ TEST_CASE("Dump CPT", "[Network]")
0.0204 0.1000 0.2000 0.0204 0.1000 0.2000
0.1250 0.0526 0.1667 0.1250 0.0526 0.1667
0.2000 0.0606 0.8235 0.2000 0.0606 0.8235
[ CPUFloatType{4,3,3} ] [ CPUDoubleType{4,3,3} ]
* petalwidth: (3) : [3, 6, 3] * petalwidth: (3) : [3, 6, 3]
(1,.,.) = (1,.,.) =
0.5000 0.0417 0.0714 0.5000 0.0417 0.0714
@@ -419,12 +427,12 @@ TEST_CASE("Dump CPT", "[Network]")
0.1111 0.0909 0.8000 0.1111 0.0909 0.8000
0.0667 0.2000 0.8667 0.0667 0.2000 0.8667
0.0303 0.2500 0.7500 0.0303 0.2500 0.7500
[ CPUFloatType{3,6,3} ] [ CPUDoubleType{3,6,3} ]
* sepallength: (3) : [3, 3] * sepallength: (3) : [3, 3]
0.8679 0.1321 0.0377 0.8679 0.1321 0.0377
0.0943 0.3019 0.0566 0.0943 0.3019 0.0566
0.0377 0.5660 0.9057 0.0377 0.5660 0.9057
[ CPUFloatType{3,3} ] [ CPUDoubleType{3,3} ]
* sepalwidth: (6) : [6, 3, 3] * sepalwidth: (6) : [6, 3, 3]
(1,.,.) = (1,.,.) =
0.0392 0.5000 0.2857 0.0392 0.5000 0.2857
@@ -455,7 +463,7 @@ TEST_CASE("Dump CPT", "[Network]")
0.5098 0.0833 0.1429 0.5098 0.0833 0.1429
0.5000 0.0476 0.1250 0.5000 0.0476 0.1250
0.2857 0.0571 0.1132 0.2857 0.0571 0.1132
[ CPUFloatType{6,3,3} ] [ CPUDoubleType{6,3,3} ]
)"; )";
REQUIRE(res == expected); REQUIRE(res == expected);
} }
@@ -525,6 +533,7 @@ TEST_CASE("Test Smoothing A", "[Network]")
} }
} }
} }
TEST_CASE("Test Smoothing B", "[Network]") TEST_CASE("Test Smoothing B", "[Network]")
{ {
auto net = bayesnet::Network(); auto net = bayesnet::Network();
@@ -549,19 +558,41 @@ TEST_CASE("Test Smoothing B", "[Network]")
{ "C", {0, 1} } { "C", {0, 1} }
}; };
auto weights = std::vector<double>(C.size(), 1); auto weights = std::vector<double>(C.size(), 1);
// Simple // See https://www.overleaf.com/read/tfnhpfysfkfx#2d576c example for calculations
std::cout << "LAPLACE\n"; INFO("Test Smoothing B - Laplace");
net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::LAPLACE); net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::LAPLACE);
std::cout << net.dump_cpt(); auto laplace_values = std::vector<std::vector<float>>({ {0.377418, 0.622582}, {0.217821, 0.782179} });
std::cout << "Predict proba of {0, 1, 2} y {1, 2, 3} = " << net.predict_proba({ {0, 1}, {1, 2}, {2, 3} }) << std::endl; auto laplace_score = net.predict_proba({ {0, 1}, {1, 2}, {2, 3} });
std::cout << "ORIGINAL\n"; for (auto i = 0; i < 2; ++i) {
for (auto j = 0; j < 2; ++j) {
REQUIRE(laplace_score.at(i).at(j) == Catch::Approx(laplace_values.at(i).at(j)).margin(threshold));
}
}
INFO("Test Smoothing B - Original");
net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::ORIGINAL); net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::ORIGINAL);
std::cout << net.dump_cpt(); auto original_values = std::vector<std::vector<float>>({ {0.344769, 0.655231}, {0.0421263, 0.957874} });
std::cout << "Predict proba of {0, 1, 2} y {1, 2, 3} = " << net.predict_proba({ {0, 1}, {1, 2}, {2, 3} }) << std::endl; auto original_score = net.predict_proba({ {0, 1}, {1, 2}, {2, 3} });
std::cout << "CESTNIK\n"; for (auto i = 0; i < 2; ++i) {
for (auto j = 0; j < 2; ++j) {
REQUIRE(original_score.at(i).at(j) == Catch::Approx(original_values.at(i).at(j)).margin(threshold));
}
}
INFO("Test Smoothing B - Cestnik");
net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::CESTNIK); net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::CESTNIK);
std::cout << net.dump_cpt(); auto cestnik_values = std::vector<std::vector<float>>({ {0.353422, 0.646578}, {0.12364, 0.87636} });
std::cout << "Predict proba of {0, 1, 2} y {1, 2, 3} = " << net.predict_proba({ {0, 1}, {1, 2}, {2, 3} }) << std::endl; auto cestnik_score = net.predict_proba({ {0, 1}, {1, 2}, {2, 3} });
for (auto i = 0; i < 2; ++i) {
for (auto j = 0; j < 2; ++j) {
} REQUIRE(cestnik_score.at(i).at(j) == Catch::Approx(cestnik_values.at(i).at(j)).margin(threshold));
}
}
INFO("Test Smoothing B - No smoothing");
net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::NONE);
auto nosmooth_values = std::vector<std::vector<float>>({ {0.342465753, 0.65753424}, {0.0, 1.0} });
auto nosmooth_score = net.predict_proba({ {0, 1}, {1, 2}, {2, 3} });
for (auto i = 0; i < 2; ++i) {
for (auto j = 0; j < 2; ++j) {
REQUIRE(nosmooth_score.at(i).at(j) == Catch::Approx(nosmooth_values.at(i).at(j)).margin(threshold));
}
}
}

View File

@@ -62,15 +62,17 @@ TEST_CASE("Test Node computeCPT", "[Node]")
// Create a vector with the names of the classes // Create a vector with the names of the classes
auto className = std::string("Class"); auto className = std::string("Class");
// weights // weights
auto weights = torch::tensor({ 1.0, 1.0, 1.0, 1.0 }); auto weights = torch::tensor({ 1.0, 1.0, 1.0, 1.0 }, torch::kDouble);
std::vector<bayesnet::Node> nodes; std::vector<bayesnet::Node> nodes;
for (int i = 0; i < features.size(); i++) { for (int i = 0; i < features.size(); i++) {
auto node = bayesnet::Node(features[i]); auto node = bayesnet::Node(features[i]);
node.setNumStates(states[i]); node.setNumStates(states[i]);
nodes.push_back(node); nodes.push_back(node);
} }
// Create node class with 2 states
nodes.push_back(bayesnet::Node(className)); nodes.push_back(bayesnet::Node(className));
nodes[features.size()].setNumStates(2); nodes[features.size()].setNumStates(2);
// The network is c->f1, f2, f3 y f1->f2, f3
for (int i = 0; i < features.size(); i++) { for (int i = 0; i < features.size(); i++) {
// Add class node as parent of all feature nodes // Add class node as parent of all feature nodes
nodes[i].addParent(&nodes[features.size()]); nodes[i].addParent(&nodes[features.size()]);

View File

@@ -27,189 +27,192 @@ TEST_CASE("Build basic model", "[BoostA2DE]")
auto score = clf.score(raw.Xv, raw.yv); auto score = clf.score(raw.Xv, raw.yv);
REQUIRE(score == Catch::Approx(0.919271).epsilon(raw.epsilon)); REQUIRE(score == Catch::Approx(0.919271).epsilon(raw.epsilon));
} }
// TEST_CASE("Feature_select IWSS", "[BoostAODE]") TEST_CASE("Feature_select IWSS", "[BoostA2DE]")
// { {
// auto raw = RawDatasets("glass", true); auto raw = RawDatasets("glass", true);
// auto clf = bayesnet::BoostAODE(); auto clf = bayesnet::BoostA2DE();
// clf.setHyperparameters({ {"select_features", "IWSS"}, {"threshold", 0.5 } }); clf.setHyperparameters({ {"select_features", "IWSS"}, {"threshold", 0.5 } });
// clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// REQUIRE(clf.getNumberOfNodes() == 90); REQUIRE(clf.getNumberOfNodes() == 140);
// REQUIRE(clf.getNumberOfEdges() == 153); REQUIRE(clf.getNumberOfEdges() == 294);
// REQUIRE(clf.getNotes().size() == 2); REQUIRE(clf.getNotes().size() == 4);
// REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with IWSS"); REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with IWSS");
// REQUIRE(clf.getNotes()[1] == "Number of models: 9"); REQUIRE(clf.getNotes()[1] == "Convergence threshold reached & 15 models eliminated");
// } REQUIRE(clf.getNotes()[2] == "Pairs not used in train: 2");
// TEST_CASE("Feature_select FCBF", "[BoostAODE]") REQUIRE(clf.getNotes()[3] == "Number of models: 14");
// { }
// auto raw = RawDatasets("glass", true); TEST_CASE("Feature_select FCBF", "[BoostA2DE]")
// auto clf = bayesnet::BoostAODE(); {
// clf.setHyperparameters({ {"select_features", "FCBF"}, {"threshold", 1e-7 } }); auto raw = RawDatasets("glass", true);
// clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); auto clf = bayesnet::BoostA2DE();
// REQUIRE(clf.getNumberOfNodes() == 90); clf.setHyperparameters({ {"select_features", "FCBF"}, {"threshold", 1e-7 } });
// REQUIRE(clf.getNumberOfEdges() == 153); clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// REQUIRE(clf.getNotes().size() == 2); REQUIRE(clf.getNumberOfNodes() == 110);
// REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with FCBF"); REQUIRE(clf.getNumberOfEdges() == 231);
// REQUIRE(clf.getNotes()[1] == "Number of models: 9"); REQUIRE(clf.getNotes()[0] == "Used features in initialization: 4 of 9 with FCBF");
// } REQUIRE(clf.getNotes()[1] == "Convergence threshold reached & 15 models eliminated");
// TEST_CASE("Test used features in train note and score", "[BoostAODE]") REQUIRE(clf.getNotes()[2] == "Pairs not used in train: 2");
// { REQUIRE(clf.getNotes()[3] == "Number of models: 11");
// auto raw = RawDatasets("diabetes", true); }
// auto clf = bayesnet::BoostAODE(true); TEST_CASE("Test used features in train note and score", "[BoostA2DE]")
// clf.setHyperparameters({ {
// {"order", "asc"}, auto raw = RawDatasets("diabetes", true);
// {"convergence", true}, auto clf = bayesnet::BoostA2DE(true);
// {"select_features","CFS"}, clf.setHyperparameters({
// }); {"order", "asc"},
// clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); {"convergence", true},
// REQUIRE(clf.getNumberOfNodes() == 72); {"select_features","CFS"},
// REQUIRE(clf.getNumberOfEdges() == 120); });
// REQUIRE(clf.getNotes().size() == 2); clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 8 with CFS"); REQUIRE(clf.getNumberOfNodes() == 144);
// REQUIRE(clf.getNotes()[1] == "Number of models: 8"); REQUIRE(clf.getNumberOfEdges() == 288);
// auto score = clf.score(raw.Xv, raw.yv); REQUIRE(clf.getNotes().size() == 2);
// auto scoret = clf.score(raw.Xt, raw.yt); REQUIRE(clf.getNotes()[0] == "Used features in initialization: 6 of 8 with CFS");
// REQUIRE(score == Catch::Approx(0.809895813).epsilon(raw.epsilon)); REQUIRE(clf.getNotes()[1] == "Number of models: 16");
// REQUIRE(scoret == Catch::Approx(0.809895813).epsilon(raw.epsilon)); auto score = clf.score(raw.Xv, raw.yv);
// } auto scoret = clf.score(raw.Xt, raw.yt);
// TEST_CASE("Voting vs proba", "[BoostAODE]") REQUIRE(score == Catch::Approx(0.856771).epsilon(raw.epsilon));
// { REQUIRE(scoret == Catch::Approx(0.856771).epsilon(raw.epsilon));
// auto raw = RawDatasets("iris", true); }
// auto clf = bayesnet::BoostAODE(false); TEST_CASE("Voting vs proba", "[BoostA2DE]")
// clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); {
// auto score_proba = clf.score(raw.Xv, raw.yv); auto raw = RawDatasets("iris", true);
// auto pred_proba = clf.predict_proba(raw.Xv); auto clf = bayesnet::BoostA2DE(false);
// clf.setHyperparameters({ clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// {"predict_voting",true}, auto score_proba = clf.score(raw.Xv, raw.yv);
// }); auto pred_proba = clf.predict_proba(raw.Xv);
// auto score_voting = clf.score(raw.Xv, raw.yv); clf.setHyperparameters({
// auto pred_voting = clf.predict_proba(raw.Xv); {"predict_voting",true},
// REQUIRE(score_proba == Catch::Approx(0.97333).epsilon(raw.epsilon)); });
// REQUIRE(score_voting == Catch::Approx(0.98).epsilon(raw.epsilon)); auto score_voting = clf.score(raw.Xv, raw.yv);
// REQUIRE(pred_voting[83][2] == Catch::Approx(1.0).epsilon(raw.epsilon)); auto pred_voting = clf.predict_proba(raw.Xv);
// REQUIRE(pred_proba[83][2] == Catch::Approx(0.86121525).epsilon(raw.epsilon)); REQUIRE(score_proba == Catch::Approx(0.98).epsilon(raw.epsilon));
// REQUIRE(clf.dump_cpt() == ""); REQUIRE(score_voting == Catch::Approx(0.946667).epsilon(raw.epsilon));
// REQUIRE(clf.topological_order() == std::vector<std::string>()); REQUIRE(pred_voting[83][2] == Catch::Approx(0.53508).epsilon(raw.epsilon));
// } REQUIRE(pred_proba[83][2] == Catch::Approx(0.48394).epsilon(raw.epsilon));
// TEST_CASE("Order asc, desc & random", "[BoostAODE]") REQUIRE(clf.dump_cpt() == "");
// { REQUIRE(clf.topological_order() == std::vector<std::string>());
// auto raw = RawDatasets("glass", true); }
// std::map<std::string, double> scores{ TEST_CASE("Order asc, desc & random", "[BoostA2DE]")
// {"asc", 0.83645f }, { "desc", 0.84579f }, { "rand", 0.84112 } {
// }; auto raw = RawDatasets("glass", true);
// for (const std::string& order : { "asc", "desc", "rand" }) { std::map<std::string, double> scores{
// auto clf = bayesnet::BoostAODE(); {"asc", 0.752336f }, { "desc", 0.813084f }, { "rand", 0.850467 }
// clf.setHyperparameters({ };
// {"order", order}, for (const std::string& order : { "asc", "desc", "rand" }) {
// {"bisection", false}, auto clf = bayesnet::BoostA2DE();
// {"maxTolerance", 1}, clf.setHyperparameters({
// {"convergence", false}, {"order", order},
// }); {"bisection", false},
// clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing); {"maxTolerance", 1},
// auto score = clf.score(raw.Xv, raw.yv); {"convergence", false},
// auto scoret = clf.score(raw.Xt, raw.yt); });
// INFO("BoostAODE order: " + order); clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// REQUIRE(score == Catch::Approx(scores[order]).epsilon(raw.epsilon)); auto score = clf.score(raw.Xv, raw.yv);
// REQUIRE(scoret == Catch::Approx(scores[order]).epsilon(raw.epsilon)); auto scoret = clf.score(raw.Xt, raw.yt);
// } INFO("BoostA2DE order: " + order);
// } REQUIRE(score == Catch::Approx(scores[order]).epsilon(raw.epsilon));
// TEST_CASE("Oddities", "[BoostAODE]") REQUIRE(scoret == Catch::Approx(scores[order]).epsilon(raw.epsilon));
// { }
// auto clf = bayesnet::BoostAODE(); }
// auto raw = RawDatasets("iris", true); TEST_CASE("Oddities2", "[BoostA2DE]")
// auto bad_hyper = nlohmann::json{ {
// { { "order", "duck" } }, auto clf = bayesnet::BoostA2DE();
// { { "select_features", "duck" } }, auto raw = RawDatasets("iris", true);
// { { "maxTolerance", 0 } }, auto bad_hyper = nlohmann::json{
// { { "maxTolerance", 5 } }, { { "order", "duck" } },
// }; { { "select_features", "duck" } },
// for (const auto& hyper : bad_hyper.items()) { { { "maxTolerance", 0 } },
// INFO("BoostAODE hyper: " + hyper.value().dump()); { { "maxTolerance", 5 } },
// REQUIRE_THROWS_AS(clf.setHyperparameters(hyper.value()), std::invalid_argument); };
// } for (const auto& hyper : bad_hyper.items()) {
// REQUIRE_THROWS_AS(clf.setHyperparameters({ {"maxTolerance", 0 } }), std::invalid_argument); INFO("BoostA2DE hyper: " + hyper.value().dump());
// auto bad_hyper_fit = nlohmann::json{ REQUIRE_THROWS_AS(clf.setHyperparameters(hyper.value()), std::invalid_argument);
// { { "select_features","IWSS" }, { "threshold", -0.01 } }, }
// { { "select_features","IWSS" }, { "threshold", 0.51 } }, REQUIRE_THROWS_AS(clf.setHyperparameters({ {"maxTolerance", 0 } }), std::invalid_argument);
// { { "select_features","FCBF" }, { "threshold", 1e-8 } }, auto bad_hyper_fit = nlohmann::json{
// { { "select_features","FCBF" }, { "threshold", 1.01 } }, { { "select_features","IWSS" }, { "threshold", -0.01 } },
// }; { { "select_features","IWSS" }, { "threshold", 0.51 } },
// for (const auto& hyper : bad_hyper_fit.items()) { { { "select_features","FCBF" }, { "threshold", 1e-8 } },
// INFO("BoostAODE hyper: " + hyper.value().dump()); { { "select_features","FCBF" }, { "threshold", 1.01 } },
// clf.setHyperparameters(hyper.value()); };
// REQUIRE_THROWS_AS(clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing, std::invalid_argument); for (const auto& hyper : bad_hyper_fit.items()) {
// } INFO("BoostA2DE hyper: " + hyper.value().dump());
// } clf.setHyperparameters(hyper.value());
REQUIRE_THROWS_AS(clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing), std::invalid_argument);
// TEST_CASE("Bisection Best", "[BoostAODE]") }
// { }
// auto clf = bayesnet::BoostAODE(); TEST_CASE("No features selected", "[BoostA2DE]")
// auto raw = RawDatasets("kdd_JapaneseVowels", true, 1200, true, false); {
// clf.setHyperparameters({ // Check that the note "No features selected in initialization" is added
// {"bisection", true}, //
// {"maxTolerance", 3}, auto raw = RawDatasets("iris", true);
// {"convergence", true}, auto clf = bayesnet::BoostA2DE();
// {"block_update", false}, clf.setHyperparameters({ {"select_features","FCBF"}, {"threshold", 1 } });
// {"convergence_best", false}, clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// }); REQUIRE(clf.getNotes().size() == 1);
// clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing); REQUIRE(clf.getNotes()[0] == "No features selected in initialization");
// REQUIRE(clf.getNumberOfNodes() == 210); }
// REQUIRE(clf.getNumberOfEdges() == 378); TEST_CASE("Bisection Best", "[BoostA2DE]")
// REQUIRE(clf.getNotes().size() == 1); {
// REQUIRE(clf.getNotes().at(0) == "Number of models: 14"); auto clf = bayesnet::BoostA2DE();
// auto score = clf.score(raw.X_test, raw.y_test); auto raw = RawDatasets("kdd_JapaneseVowels", true, 1200, true, false);
// auto scoret = clf.score(raw.X_test, raw.y_test); clf.setHyperparameters({
// REQUIRE(score == Catch::Approx(0.991666675f).epsilon(raw.epsilon)); {"bisection", true},
// REQUIRE(scoret == Catch::Approx(0.991666675f).epsilon(raw.epsilon)); {"maxTolerance", 3},
// } {"convergence", true},
// TEST_CASE("Bisection Best vs Last", "[BoostAODE]") {"block_update", false},
// { {"convergence_best", false},
// auto raw = RawDatasets("kdd_JapaneseVowels", true, 1500, true, false); });
// auto clf = bayesnet::BoostAODE(true); clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing);
// auto hyperparameters = nlohmann::json{ REQUIRE(clf.getNumberOfNodes() == 480);
// {"bisection", true}, REQUIRE(clf.getNumberOfEdges() == 1152);
// {"maxTolerance", 3}, REQUIRE(clf.getNotes().size() == 3);
// {"convergence", true}, REQUIRE(clf.getNotes().at(0) == "Convergence threshold reached & 15 models eliminated");
// {"convergence_best", true}, REQUIRE(clf.getNotes().at(1) == "Pairs not used in train: 83");
// }; REQUIRE(clf.getNotes().at(2) == "Number of models: 32");
// clf.setHyperparameters(hyperparameters); auto score = clf.score(raw.X_test, raw.y_test);
// clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing); auto scoret = clf.score(raw.X_test, raw.y_test);
// auto score_best = clf.score(raw.X_test, raw.y_test); REQUIRE(score == Catch::Approx(0.966667f).epsilon(raw.epsilon));
// REQUIRE(score_best == Catch::Approx(0.980000019f).epsilon(raw.epsilon)); REQUIRE(scoret == Catch::Approx(0.966667f).epsilon(raw.epsilon));
// // Now we will set the hyperparameter to use the last accuracy }
// hyperparameters["convergence_best"] = false; TEST_CASE("Block Update", "[BoostA2DE]")
// clf.setHyperparameters(hyperparameters); {
// clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing); auto clf = bayesnet::BoostA2DE();
// auto score_last = clf.score(raw.X_test, raw.y_test); auto raw = RawDatasets("spambase", true, 500);
// REQUIRE(score_last == Catch::Approx(0.976666689f).epsilon(raw.epsilon)); clf.setHyperparameters({
// } {"bisection", true},
{"block_update", true},
// TEST_CASE("Block Update", "[BoostAODE]") {"maxTolerance", 3},
// { {"convergence", true},
// auto clf = bayesnet::BoostAODE(); });
// auto raw = RawDatasets("mfeat-factors", true, 500); clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing);
// clf.setHyperparameters({ REQUIRE(clf.getNumberOfNodes() == 58);
// {"bisection", true}, REQUIRE(clf.getNumberOfEdges() == 165);
// {"block_update", true}, REQUIRE(clf.getNotes().size() == 3);
// {"maxTolerance", 3}, REQUIRE(clf.getNotes()[0] == "Convergence threshold reached & 15 models eliminated");
// {"convergence", true}, REQUIRE(clf.getNotes()[1] == "Pairs not used in train: 1588");
// }); REQUIRE(clf.getNotes()[2] == "Number of models: 1");
// clf.fit(raw.X_train, raw.y_train, raw.features, raw.className, raw.states, raw.smoothing); auto score = clf.score(raw.X_test, raw.y_test);
// REQUIRE(clf.getNumberOfNodes() == 868); auto scoret = clf.score(raw.X_test, raw.y_test);
// REQUIRE(clf.getNumberOfEdges() == 1724); REQUIRE(score == Catch::Approx(1.0f).epsilon(raw.epsilon));
// REQUIRE(clf.getNotes().size() == 3); REQUIRE(scoret == Catch::Approx(1.0f).epsilon(raw.epsilon));
// REQUIRE(clf.getNotes()[0] == "Convergence threshold reached & 15 models eliminated"); //
// REQUIRE(clf.getNotes()[1] == "Used features in train: 19 of 216"); // std::cout << "Number of nodes " << clf.getNumberOfNodes() << std::endl;
// REQUIRE(clf.getNotes()[2] == "Number of models: 4"); // std::cout << "Number of edges " << clf.getNumberOfEdges() << std::endl;
// auto score = clf.score(raw.X_test, raw.y_test); // std::cout << "Notes size " << clf.getNotes().size() << std::endl;
// auto scoret = clf.score(raw.X_test, raw.y_test); // for (auto note : clf.getNotes()) {
// REQUIRE(score == Catch::Approx(0.99f).epsilon(raw.epsilon)); // std::cout << note << std::endl;
// REQUIRE(scoret == Catch::Approx(0.99f).epsilon(raw.epsilon)); // }
// // // std::cout << "Score " << score << std::endl;
// // std::cout << "Number of nodes " << clf.getNumberOfNodes() << std::endl; }
// // std::cout << "Number of edges " << clf.getNumberOfEdges() << std::endl; TEST_CASE("Test graph b2a2de", "[BoostA2DE]")
// // std::cout << "Notes size " << clf.getNotes().size() << std::endl; {
// // for (auto note : clf.getNotes()) { auto raw = RawDatasets("iris", true);
// // std::cout << note << std::endl; auto clf = bayesnet::BoostA2DE();
// // } clf.fit(raw.Xv, raw.yv, raw.features, raw.className, raw.states, raw.smoothing);
// // std::cout << "Score " << score << std::endl; auto graph = clf.graph();
// } REQUIRE(graph.size() == 26);
REQUIRE(graph[0] == "digraph BayesNet {\nlabel=<BayesNet BoostA2DE_0>\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n");
REQUIRE(graph[1] == "\"class\" [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n");
}

72
tests/TestMST.cc Normal file
View File

@@ -0,0 +1,72 @@
// ***************************************************************
// SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: MIT
// ***************************************************************
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <string>
#include <vector>
#include "TestUtils.h"
#include "bayesnet/utils/Mst.h"
TEST_CASE("MST::insertElement tests", "[MST]")
{
bayesnet::MST mst({}, torch::tensor({}), 0);
SECTION("Insert into an empty list")
{
std::list<int> variables;
mst.insertElement(variables, 5);
REQUIRE(variables == std::list<int>{5});
}
SECTION("Insert a non-duplicate element")
{
std::list<int> variables = { 1, 2, 3 };
mst.insertElement(variables, 4);
REQUIRE(variables == std::list<int>{4, 1, 2, 3});
}
SECTION("Insert a duplicate element")
{
std::list<int> variables = { 1, 2, 3 };
mst.insertElement(variables, 2);
REQUIRE(variables == std::list<int>{1, 2, 3});
}
}
TEST_CASE("MST::reorder tests", "[MST]")
{
bayesnet::MST mst({}, torch::tensor({}), 0);
SECTION("Reorder simple graph")
{
std::vector<std::pair<float, std::pair<int, int>>> T = { {2.0, {1, 2}}, {1.0, {0, 1}} };
auto result = mst.reorder(T, 0);
REQUIRE(result == std::vector<std::pair<int, int>>{{0, 1}, { 1, 2 }});
}
SECTION("Reorder with disconnected graph")
{
std::vector<std::pair<float, std::pair<int, int>>> T = { {2.0, {1, 2}}, {1.0, {0, 1}} };
auto result = mst.reorder(T, 0);
REQUIRE(result == std::vector<std::pair<int, int>>{{0, 1}, { 2, 3 }});
}
}
TEST_CASE("MST::maximumSpanningTree tests", "[MST]")
{
std::vector<std::string> features = { "A", "B", "C" };
auto weights = torch::tensor({
{0.0, 1.0, 2.0},
{1.0, 0.0, 3.0},
{2.0, 3.0, 0.0}
});
bayesnet::MST mst(features, weights, 0);
SECTION("MST of a complete graph")
{
auto result = mst.maximumSpanningTree();
REQUIRE(result.size() == 2); // Un MST para 3 nodos tiene 2 aristas
}
}

View File

@@ -7,7 +7,7 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp> #include <catch2/matchers/catch_matchers.hpp>
#include <string> #include <string>
#include <fimdlp/CPPFImdlp.h> #include <CPPFImdlp.h>
#include <folding.hpp> #include <folding.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#define TO_STR2(x) #x #define TO_STR2(x) #x
@@ -16,7 +16,7 @@
#include "TestUtils.h" #include "TestUtils.h"
std::map<std::string, std::string> modules = { std::map<std::string, std::string> modules = {
{ "mdlp", "2.0.0" }, { "mdlp", "2.0.1" },
{ "Folding", "1.1.0" }, { "Folding", "1.1.0" },
{ "json", "3.11" }, { "json", "3.11" },
{ "ArffFiles", "1.1.0" } { "ArffFiles", "1.1.0" }

View File

@@ -12,7 +12,7 @@
#include <map> #include <map>
#include <tuple> #include <tuple>
#include <ArffFiles.hpp> #include <ArffFiles.hpp>
#include <fimdlp/CPPFImdlp.h> #include <CPPFImdlp.h>
#include <folding.hpp> #include <folding.hpp>
#include <bayesnet/network/Network.h> #include <bayesnet/network/Network.h>

4811
tests/data/spambase.arff Executable file

File diff suppressed because it is too large Load Diff