BayesNet/tests/TestBayesMetrics.cc

63 lines
2.4 KiB
C++
Raw Normal View History

#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_approx.hpp>
#include <catch2/generators/catch_generators.hpp>
#include "BayesMetrics.h"
2023-10-04 23:14:16 +00:00
#include "TestUtils.h"
using namespace std;
2023-10-05 13:45:36 +00:00
TEST_CASE("Metrics Test", "[BayesNet]")
{
2023-10-04 23:14:16 +00:00
string file_name = GENERATE("glass", "iris", "ecoli", "diabetes");
2023-10-05 09:45:00 +00:00
map<string, pair<int, vector<int>>> resultsKBest = {
2023-10-05 13:45:36 +00:00
{"glass", {7, { 0, 1, 7, 6, 3, 5, 2 }}},
{"iris", {3, { 0, 3, 2 }} },
{"ecoli", {6, { 2, 4, 1, 0, 6, 5 }}},
{"diabetes", {2, { 7, 1 }}}
};
map<string, double> resultsMI = {
{"glass", 0.12805398},
{"iris", 0.3158139948},
{"ecoli", 0.0089431099},
{"diabetes", 0.0345470614}
};
map<string, vector<pair<int, int>>> resultsMST = {
2023-10-05 23:14:55 +00:00
{"glass", {{0,6}, {0,5}, {0,3}, {5,1}, {5,8}, {6,2}, {6,7}, {7,4}}},
2023-10-05 13:45:36 +00:00
{"iris", {{0,1},{0,2},{1,3}}},
{"ecoli", {{0,1}, {0,2}, {1,5}, {1,3}, {5,6}, {5,4}}},
{"diabetes", {{0,7}, {0,2}, {0,6}, {2,3}, {3,4}, {3,5}, {4,1}}}
2023-10-04 23:14:16 +00:00
};
auto [XDisc, yDisc, featuresDisc, classNameDisc, statesDisc] = loadDataset(file_name, true, true);
int classNumStates = statesDisc.at(classNameDisc).size();
auto yresized = torch::transpose(yDisc.view({ yDisc.size(0), 1 }), 0, 1);
torch::Tensor dataset = torch::cat({ XDisc, yresized }, 0);
int nSamples = dataset.size(1);
2023-10-05 13:45:36 +00:00
double epsilon = 1e-5;
torch::Tensor weights = torch::full({ nSamples }, 1.0 / nSamples, torch::kDouble);
bayesnet::Metrics metrics(dataset, featuresDisc, classNameDisc, classNumStates);
2023-10-04 23:14:16 +00:00
SECTION("Test Constructor")
{
2023-10-04 23:14:16 +00:00
REQUIRE(metrics.getScoresKBest().size() == 0);
}
SECTION("Test SelectKBestWeighted")
{
2023-10-05 09:45:00 +00:00
vector<int> kBest = metrics.SelectKBestWeighted(weights, true, resultsKBest.at(file_name).first);
REQUIRE(kBest.size() == resultsKBest.at(file_name).first);
REQUIRE(kBest == resultsKBest.at(file_name).second);
}
2023-10-05 13:45:36 +00:00
SECTION("Test Mutual Information")
{
2023-10-05 09:45:00 +00:00
auto result = metrics.mutualInformation(dataset.index({ 1, "..." }), dataset.index({ 2, "..." }), weights);
2023-10-05 13:45:36 +00:00
REQUIRE(result == Catch::Approx(resultsMI.at(file_name)).epsilon(epsilon));
}
SECTION("Test Maximum Spanning Tree")
{
auto weights_matrix = metrics.conditionalEdge(weights);
auto result = metrics.maximumSpanningTree(featuresDisc, weights_matrix, 0);
REQUIRE(result == resultsMST.at(file_name));
}
}