Add some tests

This commit is contained in:
Ricardo Montañana Gómez 2023-07-18 13:44:08 +02:00
parent 57dab6d709
commit 1a21015492
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
6 changed files with 86 additions and 55 deletions

View File

@ -21,6 +21,9 @@ namespace bayesnet {
}
void Network::addNode(string name, int numStates)
{
if (find(features.begin(), features.end(), name) == features.end()) {
features.push_back(name);
}
if (nodes.find(name) != nodes.end()) {
// if node exists update its number of states
nodes[name]->setNumStates(numStates);

View File

@ -1,3 +1,29 @@
//
// Created by Ricardo Montañana Gómez on 18/7/23.
//
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <string>
#include "../src/KDB.h"
#include "utils.h"
TEST_CASE("Test Bayesian Network")
{
auto[Xd, y, features, className, states] = loadFile("iris");
SECTION("Test Update Nodes")
{
auto net = bayesnet::Network();
net.addNode("A", 3);
REQUIRE(net.getStates() == 3);
net.addNode("A", 5);
REQUIRE(net.getStates() == 5);
}
SECTION("Test get features")
{
auto net = bayesnet::Network();
net.addNode("A", 3);
net.addNode("B", 5);
REQUIRE(net.getFeatures() == vector<string>{"A", "B"});
net.addNode("C", 2);
REQUIRE(net.getFeatures() == vector<string>{"A", "B", "C"});
}
}

View File

@ -2,7 +2,7 @@ if(ENABLE_TESTING)
set(TEST_MAIN "unit_tests")
set(TEST_SOURCES main.cc ../sample/ArffFiles.cc ../sample/CPPFImdlp.cpp ../sample/Metrics.cpp
../src/utils.cc ../src/Network.cc ../src/Node.cc ../src/Metrics.cc ../src/BaseClassifier.cc ../src/KDB.cc
../src/TAN.cc ../src/SPODE.cc ../src/Ensemble.cc ../src/AODE.cc ../src/Mst.cc)
../src/TAN.cc ../src/SPODE.cc ../src/Ensemble.cc ../src/AODE.cc ../src/Mst.cc BayesNetwork.cc utils.cc utils.h)
add_executable(${TEST_MAIN} ${TEST_SOURCES})
target_link_libraries(${TEST_MAIN} PUBLIC "${TORCH_LIBRARIES}" Catch2::Catch2WithMain)
add_test(NAME ${TEST_MAIN} COMMAND ${TEST_MAIN})

View File

@ -5,35 +5,14 @@
#include <vector>
#include <map>
#include <string>
#include <torch/torch.h>
#include "../sample/ArffFiles.h"
#include "../sample/CPPFImdlp.h"
#include "../src/KDB.h"
#include "../src/TAN.h"
#include "../src/SPODE.h"
#include "../src/AODE.h"
const string PATH = "data/";
using namespace std;
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t>& X, mdlp::labels_t& y, vector<string> features)
{
vector<mdlp::labels_t>Xd;
map<string, int> maxes;
auto fimdlp = mdlp::CPPFImdlp();
for (int i = 0; i < X.size(); i++) {
fimdlp.fit(X[i], y);
mdlp::labels_t& xd = fimdlp.transform(X[i]);
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
Xd.push_back(xd);
}
return { Xd, maxes };
}
#include "utils.h"
TEST_CASE("Test Bayesian Classifiers score", "[BayesNet]")
{
auto path = "../../data/";
map <pair<string, string>, float> scores = {
{{"diabetes", "AODE"}, 0.811198}, {{"diabetes", "KDB"}, 0.852865}, {{"diabetes", "SPODE"}, 0.802083}, {{"diabetes", "TAN"}, 0.821615},
{{"ecoli", "AODE"}, 0.889881}, {{"ecoli", "KDB"}, 0.889881}, {{"ecoli", "SPODE"}, 0.880952}, {{"ecoli", "TAN"}, 0.892857},
@ -42,27 +21,8 @@ TEST_CASE("Test Bayesian Classifiers score", "[BayesNet]")
};
string file_name = GENERATE("glass", "iris", "ecoli", "diabetes");
auto handler = ArffFiles();
handler.load(path + static_cast<string>(file_name) + ".arff");
// Get Dataset X, y
vector<mdlp::samples_t>& X = handler.getX();
mdlp::labels_t& y = handler.getY();
// Get className & Features
auto className = handler.getClassName();
vector<string> features;
for (auto feature : handler.getAttributes()) {
features.push_back(feature.first);
}
// Discretize Dataset
vector<mdlp::labels_t> Xd;
map<string, int> maxes;
tie(Xd, maxes) = discretize(X, y, features);
maxes[className] = *max_element(y.begin(), y.end()) + 1;
map<string, vector<int>> states;
for (auto feature : features) {
states[feature] = vector<int>(maxes[feature]);
}
states[className] = vector<int>(maxes[className]);
auto[Xd, y, features, className, states] = loadFile(file_name);
SECTION("Test TAN classifier (" + file_name + ")")
{
auto clf = bayesnet::TAN();

View File

@ -1,3 +1,40 @@
//
// Created by Ricardo Montañana Gómez on 18/7/23.
//
#include "utils.h"
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t> &X, mdlp::labels_t &y, vector<string> features) {
vector<mdlp::labels_t> Xd;
map<string, int> maxes;
auto fimdlp = mdlp::CPPFImdlp();
for (int i = 0; i < X.size(); i++) {
fimdlp.fit(X[i], y);
mdlp::labels_t &xd = fimdlp.transform(X[i]);
maxes[features[i]] = *max_element(xd.begin(), xd.end()) + 1;
Xd.push_back(xd);
}
return {Xd, maxes};
}
tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>>
loadFile(string name) {
auto handler = ArffFiles();
handler.load(PATH + static_cast<string>(name) + ".arff");
// Get Dataset X, y
vector<mdlp::samples_t> &X = handler.getX();
mdlp::labels_t &y = handler.getY();
// Get className & Features
auto className = handler.getClassName();
vector<string> features;
for (auto feature: handler.getAttributes()) {
features.push_back(feature.first);
}
// Discretize Dataset
vector<mdlp::labels_t> Xd;
map<string, int> maxes;
tie(Xd, maxes) = discretize(X, y, features);
maxes[className] = *max_element(y.begin(), y. end()) + 1;
map<string, vector<int>> states;
for (auto feature: features) {
states[feature] = vector<int>(maxes[feature]);
}
states[className] = vector<int>(maxes[className]);
return {Xd, y, features, className, states};
}

View File

@ -1,8 +1,13 @@
//
// Created by Ricardo Montañana Gómez on 18/7/23.
//
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include "../sample/ArffFiles.h"
#include "../sample/CPPFImdlp.h"
#ifndef BAYESNET_UTILS_H
#define BAYESNET_UTILS_H
using namespace std;
const string PATH = "../../data/";
pair<vector<mdlp::labels_t>, map<string, int>> discretize(vector<mdlp::samples_t> &X, mdlp::labels_t &y, vector<string> features);
tuple<vector<vector<int>>, vector<int>, vector<string>, string, map<string, vector<int>>> loadFile(string name);
#endif //BAYESNET_UTILS_H