diff --git a/README.md b/README.md index 7b8e34b..bdc77a3 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ [![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) ![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-96,8%25-green)](html/index.html) +[![Coverage Badge](https://img.shields.io/badge/Coverage-97,0%25-green)](html/index.html) Bayesian Network Classifiers using libtorch from scratch diff --git a/tests/TestA2DE.cc b/tests/TestA2DE.cc index 60b876f..1640a89 100644 --- a/tests/TestA2DE.cc +++ b/tests/TestA2DE.cc @@ -45,5 +45,5 @@ TEST_CASE("Test graph", "[A2DE]") auto graph = clf.graph(); REQUIRE(graph.size() == 78); REQUIRE(graph[0] == "digraph BayesNet {\nlabel=\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"); } diff --git a/tests/TestBayesClassifier.cc b/tests/TestBayesClassifier.cc index ce5887b..506bf26 100644 --- a/tests/TestBayesClassifier.cc +++ b/tests/TestBayesClassifier.cc @@ -85,7 +85,7 @@ TEST_CASE("Dump_cpt", "[Classifier]") auto raw = RawDatasets("iris", true); model.fit(raw.Xt, raw.yt, raw.features, raw.className, raw.states, raw.smoothing); auto cpt = model.dump_cpt(); - REQUIRE(cpt.size() == 1713); + REQUIRE(cpt.size() == 1718); } TEST_CASE("Not fitted model", "[Classifier]") { diff --git a/tests/TestBayesModels.cc b/tests/TestBayesModels.cc index cbc56a9..e5113a2 100644 --- a/tests/TestBayesModels.cc +++ b/tests/TestBayesModels.cc @@ -27,13 +27,13 @@ TEST_CASE("Test Bayesian Classifiers score & version", "[Models]") map , float> scores{ // Diabetes {{"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", "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", "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", "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} @@ -71,10 +71,10 @@ TEST_CASE("Test Bayesian Classifiers score & version", "[Models]") TEST_CASE("Models features & Graph", "[Models]") { auto graph = std::vector({ "digraph BayesNet {\nlabel=\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n", - "class [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n", - "class -> sepallength", "class -> sepalwidth", "class -> petallength", "class -> petalwidth", "petallength [shape=circle] \n", - "petallength -> sepallength", "petalwidth [shape=circle] \n", "sepallength [shape=circle] \n", - "sepallength -> sepalwidth", "sepalwidth [shape=circle] \n", "sepalwidth -> petalwidth", "}\n" + "\"class\" [shape=circle, fontcolor=red, fillcolor=lightblue, style=filled ] \n", + "\"class\" -> \"sepallength\"", "\"class\" -> \"sepalwidth\"", "\"class\" -> \"petallength\"", "\"class\" -> \"petalwidth\"", "\"petallength\" [shape=circle] \n", + "\"petallength\" -> \"sepallength\"", "\"petalwidth\" [shape=circle] \n", "\"sepallength\" [shape=circle] \n", + "\"sepallength\" -> \"sepalwidth\"", "\"sepalwidth\" [shape=circle] \n", "\"sepalwidth\" -> \"petalwidth\"", "}\n" } ); 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); REQUIRE(clf.getNumberOfNodes() == 5); REQUIRE(clf.getNumberOfEdges() == 7); - REQUIRE(clf.getNumberOfStates() == 19); + REQUIRE(clf.getNumberOfStates() == 27); REQUIRE(clf.getClassNumStates() == 3); REQUIRE(clf.show() == std::vector{"class -> sepallength, sepalwidth, petallength, petalwidth, ", "petallength -> sepallength, ", "petalwidth -> ", "sepallength -> sepalwidth, ", "sepalwidth -> petalwidth, "}); REQUIRE(clf.graph("Test") == graph); diff --git a/tests/TestBayesNetwork.cc b/tests/TestBayesNetwork.cc index 9a22214..2adcbfd 100644 --- a/tests/TestBayesNetwork.cc +++ b/tests/TestBayesNetwork.cc @@ -186,11 +186,11 @@ TEST_CASE("Test Bayesian Network", "[Network]") auto str = net.graph("Test Graph"); REQUIRE(str.size() == 7); REQUIRE(str[0] == "digraph BayesNet {\nlabel=\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n"); - REQUIRE(str[1] == "A [shape=circle] \n"); - REQUIRE(str[2] == "A -> B"); - REQUIRE(str[3] == "A -> C"); - REQUIRE(str[4] == "B [shape=circle] \n"); - REQUIRE(str[5] == "C [shape=circle] \n"); + REQUIRE(str[1] == "\"A\" [shape=circle] \n"); + REQUIRE(str[2] == "\"A\" -> \"B\""); + REQUIRE(str[3] == "\"A\" -> \"C\""); + REQUIRE(str[4] == "\"B\" [shape=circle] \n"); + REQUIRE(str[5] == "\"C\" [shape=circle] \n"); REQUIRE(str[6] == "}\n"); } SECTION("Test predict") @@ -373,7 +373,7 @@ TEST_CASE("Dump CPT", "[Network]") 0.3333 0.3333 0.3333 -[ CPUFloatType{3} ] +[ CPUDoubleType{3} ] * petallength: (4) : [4, 3, 3] (1,.,.) = 0.9388 0.1000 0.2000 @@ -394,7 +394,7 @@ TEST_CASE("Dump CPT", "[Network]") 0.0204 0.1000 0.2000 0.1250 0.0526 0.1667 0.2000 0.0606 0.8235 -[ CPUFloatType{4,3,3} ] +[ CPUDoubleType{4,3,3} ] * petalwidth: (3) : [3, 6, 3] (1,.,.) = 0.5000 0.0417 0.0714 @@ -419,12 +419,12 @@ TEST_CASE("Dump CPT", "[Network]") 0.1111 0.0909 0.8000 0.0667 0.2000 0.8667 0.0303 0.2500 0.7500 -[ CPUFloatType{3,6,3} ] +[ CPUDoubleType{3,6,3} ] * sepallength: (3) : [3, 3] 0.8679 0.1321 0.0377 0.0943 0.3019 0.0566 0.0377 0.5660 0.9057 -[ CPUFloatType{3,3} ] +[ CPUDoubleType{3,3} ] * sepalwidth: (6) : [6, 3, 3] (1,.,.) = 0.0392 0.5000 0.2857 @@ -455,7 +455,7 @@ TEST_CASE("Dump CPT", "[Network]") 0.5098 0.0833 0.1429 0.5000 0.0476 0.1250 0.2857 0.0571 0.1132 -[ CPUFloatType{6,3,3} ] +[ CPUDoubleType{6,3,3} ] )"; REQUIRE(res == expected); } @@ -549,19 +549,32 @@ TEST_CASE("Test Smoothing B", "[Network]") { "C", {0, 1} } }; auto weights = std::vector(C.size(), 1); - // Simple - std::cout << "LAPLACE\n"; + // See https://www.overleaf.com/read/tfnhpfysfkfx#2d576c example for calculations + INFO("Test Smoothing B - Laplace"); net.fit(Data, C, weights, { "X", "Y", "Z" }, "C", states, bayesnet::Smoothing_t::LAPLACE); - std::cout << net.dump_cpt(); - std::cout << "Predict proba of {0, 1, 2} y {1, 2, 3} = " << net.predict_proba({ {0, 1}, {1, 2}, {2, 3} }) << std::endl; - std::cout << "ORIGINAL\n"; + auto laplace_values = std::vector>({ {0.377418, 0.622582}, {0.217821, 0.782179} }); + auto laplace_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(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); - std::cout << net.dump_cpt(); - std::cout << "Predict proba of {0, 1, 2} y {1, 2, 3} = " << net.predict_proba({ {0, 1}, {1, 2}, {2, 3} }) << std::endl; - std::cout << "CESTNIK\n"; + auto original_values = std::vector>({ {0.344769, 0.655231}, {0.0421263, 0.957874} }); + auto original_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(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); - std::cout << net.dump_cpt(); - 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_values = std::vector>({ {0.353422, 0.646578}, {0.12364, 0.87636} }); + 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)); + } + } +} \ No newline at end of file diff --git a/tests/TestBayesNode.cc b/tests/TestBayesNode.cc index dd564f7..8f910ea 100644 --- a/tests/TestBayesNode.cc +++ b/tests/TestBayesNode.cc @@ -62,15 +62,17 @@ TEST_CASE("Test Node computeCPT", "[Node]") // Create a vector with the names of the classes auto className = std::string("Class"); // 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 nodes; for (int i = 0; i < features.size(); i++) { auto node = bayesnet::Node(features[i]); node.setNumStates(states[i]); nodes.push_back(node); } + // Create node class with 2 states nodes.push_back(bayesnet::Node(className)); nodes[features.size()].setNumStates(2); + // The network is c->f1, f2, f3 y f1->f2, f3 for (int i = 0; i < features.size(); i++) { // Add class node as parent of all feature nodes nodes[i].addParent(&nodes[features.size()]); diff --git a/tests/TestModulesVersions.cc b/tests/TestModulesVersions.cc index fdcaa05..b53167f 100644 --- a/tests/TestModulesVersions.cc +++ b/tests/TestModulesVersions.cc @@ -16,7 +16,7 @@ #include "TestUtils.h" std::map modules = { - { "mdlp", "2.0.0" }, + { "mdlp", "2.0.1" }, { "Folding", "1.1.0" }, { "json", "3.11" }, { "ArffFiles", "1.1.0" }