Fix tests to 97%

This commit is contained in:
2024-11-22 19:12:43 +01:00
parent b168e42fea
commit 3728bcb7d3
7 changed files with 51 additions and 36 deletions

View File

@@ -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) [![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-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 Bayesian Network Classifiers using libtorch from scratch

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")
@@ -373,7 +373,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 +394,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 +419,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 +455,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);
} }
@@ -549,19 +549,32 @@ 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));
}
}
}

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

@@ -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" }