diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f3d262..8fda348 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add the Library logo generated with to README.md +- Add link to the coverage report in the README.md coverage label ## [1.0.5] 2024-04-20 diff --git a/Makefile b/Makefile index 7985da3..3cbac96 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ app_targets = BayesNet test_targets = TestBayesNet clang-uml = clang-uml plantuml = plantuml +gcovr = gcovr +lcov = lcov +genhtml = genhtml dot = dot n_procs = -j 16 @@ -112,24 +115,27 @@ test: ## Run tests (opt="-s") to verbose output the tests, (opt="-c='Test Maximu coverage: ## Run tests and generate coverage report (build/index.html) @echo ">>> Building tests with coverage..." + @which $(gcovr) || (echo ">>> Please install gcovr"; exit 1) @$(MAKE) test - @gcovr $(f_debug)/tests + @$(gcovr) $(f_debug)/tests @echo ">>> Done"; viewcoverage: ## Run tests, generate coverage report and upload it to codecov (build/index.html) + @which $(lcov) || (echo ">>> Please install lcov"; exit 1) + @which $(genhtml) || (echo ">>> Please install lcov"; exit 1) @echo ">>> Building tests with coverage..." @$(MAKE) coverage @echo ">>> Building report..." @cd $(f_debug)/tests; \ - lcov --directory . --capture --output-file coverage.info >/dev/null 2>&1; \ - lcov --remove coverage.info '/usr/*' --output-file coverage.info >/dev/null 2>&1; \ - lcov --remove coverage.info 'lib/*' --output-file coverage.info >/dev/null 2>&1; \ - lcov --remove coverage.info 'libtorch/*' --output-file coverage.info >/dev/null 2>&1; \ - lcov --remove coverage.info 'tests/*' --output-file coverage.info >/dev/null 2>&1; \ - lcov --remove coverage.info 'bayesnet/utils/loguru.*' --output-file coverage.info >/dev/null 2>&1; \ - genhtml coverage.info --output-directory coverage >/dev/null 2>&1; + $(lcov) --directory . --capture --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info '/usr/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'lib/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'libtorch/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'tests/*' --output-file coverage.info >/dev/null 2>&1; \ + $(lcov) --remove coverage.info 'bayesnet/utils/loguru.*' --ignore-errors unused --output-file coverage.info >/dev/null 2>&1 + @$(genhtml) $(f_debug)/tests/coverage.info --output-directory html >/dev/null 2>&1; @$(MAKE) updatebadge - @xdg-open $(f_debug)/tests/coverage/index.html || open $(f_debug)/tests/coverage/index.html 2>/dev/null + @xdg-open html/index.html || open html/index.html 2>/dev/null @echo ">>> Done"; updatebadge: ## Update the coverage badge in README.md diff --git a/README.md b/README.md index d21aa98..2fe225e 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,10 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)]() ![Gitea Release](https://img.shields.io/gitea/v/release/rmontanana/bayesnet?gitea_url=https://gitea.rmontanana.es:3000) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/cf3e0ac71d764650b1bf4d8d00d303b1)](https://app.codacy.com/gh/Doctorado-ML/BayesNet/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade) +[![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) -![Static Badge](https://img.shields.io/badge/Coverage-97,2%25-green) +[![Static Badge](https://img.shields.io/badge/Coverage-97,2%25-green)](html/index.html) Bayesian Network Classifiers using libtorch from scratch diff --git a/html/amber.png b/html/amber.png new file mode 100644 index 0000000..2cab170 Binary files /dev/null and b/html/amber.png differ diff --git a/html/bayesnet/BaseClassifier.h.func-c.html b/html/bayesnet/BaseClassifier.h.func-c.html new file mode 100644 index 0000000..8f1584c --- /dev/null +++ b/html/bayesnet/BaseClassifier.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/BaseClassifier.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet - BaseClassifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet14BaseClassifierD0Ev273
_ZN8bayesnet14BaseClassifierD0Ev0
_ZN8bayesnet14BaseClassifierD2Ev273
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/BaseClassifier.h.func.html b/html/bayesnet/BaseClassifier.h.func.html new file mode 100644 index 0000000..9a47e1c --- /dev/null +++ b/html/bayesnet/BaseClassifier.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/BaseClassifier.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet - BaseClassifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet14BaseClassifierD0Ev273
_ZN8bayesnet14BaseClassifierD0Ev0
_ZN8bayesnet14BaseClassifierD2Ev273
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/BaseClassifier.h.gcov.html b/html/bayesnet/BaseClassifier.h.gcov.html new file mode 100644 index 0000000..6492e92 --- /dev/null +++ b/html/bayesnet/BaseClassifier.h.gcov.html @@ -0,0 +1,121 @@ + + + + + + + LCOV - coverage.info - bayesnet/BaseClassifier.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet - BaseClassifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #pragma once
+       8              : #include <vector>
+       9              : #include <torch/torch.h>
+      10              : #include <nlohmann/json.hpp>
+      11              : namespace bayesnet {
+      12              :     enum status_t { NORMAL, WARNING, ERROR };
+      13              :     class BaseClassifier {
+      14              :     public:
+      15              :         // X is nxm std::vector, y is nx1 std::vector
+      16              :         virtual BaseClassifier& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
+      17              :         // X is nxm tensor, y is nx1 tensor
+      18              :         virtual BaseClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
+      19              :         virtual BaseClassifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) = 0;
+      20              :         virtual BaseClassifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights) = 0;
+      21          273 :         virtual ~BaseClassifier() = default;
+      22              :         torch::Tensor virtual predict(torch::Tensor& X) = 0;
+      23              :         std::vector<int> virtual predict(std::vector<std::vector<int >>& X) = 0;
+      24              :         torch::Tensor virtual predict_proba(torch::Tensor& X) = 0;
+      25              :         std::vector<std::vector<double>> virtual predict_proba(std::vector<std::vector<int >>& X) = 0;
+      26              :         status_t virtual getStatus() const = 0;
+      27              :         float virtual score(std::vector<std::vector<int>>& X, std::vector<int>& y) = 0;
+      28              :         float virtual score(torch::Tensor& X, torch::Tensor& y) = 0;
+      29              :         int virtual getNumberOfNodes()const = 0;
+      30              :         int virtual getNumberOfEdges()const = 0;
+      31              :         int virtual getNumberOfStates() const = 0;
+      32              :         int virtual getClassNumStates() const = 0;
+      33              :         std::vector<std::string> virtual show() const = 0;
+      34              :         std::vector<std::string> virtual graph(const std::string& title = "") const = 0;
+      35              :         virtual std::string getVersion() = 0;
+      36              :         std::vector<std::string> virtual topological_order() = 0;
+      37              :         std::vector<std::string> virtual getNotes() const = 0;
+      38              :         std::string virtual dump_cpt()const = 0;
+      39              :         virtual void setHyperparameters(const nlohmann::json& hyperparameters) = 0;
+      40              :         std::vector<std::string>& getValidHyperparameters() { return validHyperparameters; }
+      41              :     protected:
+      42              :         virtual void trainModel(const torch::Tensor& weights) = 0;
+      43              :         std::vector<std::string> validHyperparameters;
+      44              :     };
+      45              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.cc.func-c.html b/html/bayesnet/classifiers/Classifier.cc.func-c.html new file mode 100644 index 0000000..e5eb6a6 --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.cc.func-c.html @@ -0,0 +1,243 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %126126
Test Date:2024-04-21 16:43:29Functions:100.0 %2424
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet10Classifier17topological_orderB5cxx11Ev1
_ZNK8bayesnet10Classifier8dump_cptB5cxx11Ev1
_ZN8bayesnet10Classifier5scoreERSt6vectorIS1_IiSaIiEESaIS3_EERS3_4
_ZN8bayesnet10Classifier7predictERSt6vectorIS1_IiSaIiEESaIS3_EE4
_ZNK8bayesnet10Classifier17getNumberOfStatesEv6
_ZNK8bayesnet10Classifier4showB5cxx11Ev6
_ZN8bayesnet10Classifier18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE19
_ZN8bayesnet10Classifier3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE28
_ZN8bayesnet10Classifier5scoreERN2at6TensorES3_28
_ZN8bayesnet10Classifier3fitERSt6vectorIS1_IiSaIiEESaIS3_EERS3_RKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISD_EERKSD_RSt3mapISD_S3_St4lessISD_ESaISt4pairISI_S3_EEE32
_ZNK8bayesnet10Classifier16getNumberOfEdgesEv40
_ZNK8bayesnet10Classifier16getNumberOfNodesEv40
_ZN8bayesnet10Classifier13predict_probaERSt6vectorIS1_IiSaIiEESaIS3_EE67
_ZNK8bayesnet10Classifier17getClassNumStatesEv76
_ZN8bayesnet10Classifier12buildDatasetERN2at6TensorE77
_ZN8bayesnet10Classifier3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE99
_ZN8bayesnet10Classifier3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEERKS2_136
_ZN8bayesnet10Classifier7predictERN2at6TensorE245
_ZN8bayesnet10Classifier10trainModelERKN2at6TensorE251
_ZN8bayesnet10Classifier8addNodesEv251
_ZN8bayesnet10Classifier18checkFitParametersEv291
_ZN8bayesnet10Classifier5buildERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEERKN2at6TensorE291
_ZN8bayesnet10Classifier13predict_probaERN2at6TensorE306
_ZN8bayesnet10ClassifierC2ENS_7NetworkE413
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.cc.func.html b/html/bayesnet/classifiers/Classifier.cc.func.html new file mode 100644 index 0000000..1ccc779 --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.cc.func.html @@ -0,0 +1,243 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %126126
Test Date:2024-04-21 16:43:29Functions:100.0 %2424
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet10Classifier10trainModelERKN2at6TensorE251
_ZN8bayesnet10Classifier12buildDatasetERN2at6TensorE77
_ZN8bayesnet10Classifier13predict_probaERN2at6TensorE306
_ZN8bayesnet10Classifier13predict_probaERSt6vectorIS1_IiSaIiEESaIS3_EE67
_ZN8bayesnet10Classifier17topological_orderB5cxx11Ev1
_ZN8bayesnet10Classifier18checkFitParametersEv291
_ZN8bayesnet10Classifier18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE19
_ZN8bayesnet10Classifier3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE99
_ZN8bayesnet10Classifier3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEERKS2_136
_ZN8bayesnet10Classifier3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE28
_ZN8bayesnet10Classifier3fitERSt6vectorIS1_IiSaIiEESaIS3_EERS3_RKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISD_EERKSD_RSt3mapISD_S3_St4lessISD_ESaISt4pairISI_S3_EEE32
_ZN8bayesnet10Classifier5buildERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEERKN2at6TensorE291
_ZN8bayesnet10Classifier5scoreERN2at6TensorES3_28
_ZN8bayesnet10Classifier5scoreERSt6vectorIS1_IiSaIiEESaIS3_EERS3_4
_ZN8bayesnet10Classifier7predictERN2at6TensorE245
_ZN8bayesnet10Classifier7predictERSt6vectorIS1_IiSaIiEESaIS3_EE4
_ZN8bayesnet10Classifier8addNodesEv251
_ZN8bayesnet10ClassifierC2ENS_7NetworkE413
_ZNK8bayesnet10Classifier16getNumberOfEdgesEv40
_ZNK8bayesnet10Classifier16getNumberOfNodesEv40
_ZNK8bayesnet10Classifier17getClassNumStatesEv76
_ZNK8bayesnet10Classifier17getNumberOfStatesEv6
_ZNK8bayesnet10Classifier4showB5cxx11Ev6
_ZNK8bayesnet10Classifier8dump_cptB5cxx11Ev1
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.cc.gcov.html b/html/bayesnet/classifiers/Classifier.cc.gcov.html new file mode 100644 index 0000000..17c8089 --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.cc.gcov.html @@ -0,0 +1,270 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %126126
Test Date:2024-04-21 16:43:29Functions:100.0 %2424
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <sstream>
+       8              : #include "bayesnet/utils/bayesnetUtils.h"
+       9              : #include "Classifier.h"
+      10              : 
+      11              : namespace bayesnet {
+      12          413 :     Classifier::Classifier(Network model) : model(model), m(0), n(0), metrics(Metrics()), fitted(false) {}
+      13              :     const std::string CLASSIFIER_NOT_FITTED = "Classifier has not been fitted";
+      14          291 :     Classifier& Classifier::build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
+      15              :     {
+      16          291 :         this->features = features;
+      17          291 :         this->className = className;
+      18          291 :         this->states = states;
+      19          291 :         m = dataset.size(1);
+      20          291 :         n = features.size();
+      21          291 :         checkFitParameters();
+      22          283 :         auto n_classes = states.at(className).size();
+      23          283 :         metrics = Metrics(dataset, features, className, n_classes);
+      24          283 :         model.initialize();
+      25          283 :         buildModel(weights);
+      26          283 :         trainModel(weights);
+      27          279 :         fitted = true;
+      28          279 :         return *this;
+      29              :     }
+      30           77 :     void Classifier::buildDataset(torch::Tensor& ytmp)
+      31              :     {
+      32              :         try {
+      33           77 :             auto yresized = torch::transpose(ytmp.view({ ytmp.size(0), 1 }), 0, 1);
+      34          239 :             dataset = torch::cat({ dataset, yresized }, 0);
+      35           77 :         }
+      36            4 :         catch (const std::exception& e) {
+      37            4 :             std::stringstream oss;
+      38            4 :             oss << "* Error in X and y dimensions *\n";
+      39            4 :             oss << "X dimensions: " << dataset.sizes() << "\n";
+      40            4 :             oss << "y dimensions: " << ytmp.sizes();
+      41            4 :             throw std::runtime_error(oss.str());
+      42            8 :         }
+      43          154 :     }
+      44          251 :     void Classifier::trainModel(const torch::Tensor& weights)
+      45              :     {
+      46          251 :         model.fit(dataset, weights, features, className, states);
+      47          251 :     }
+      48              :     // X is nxm where n is the number of features and m the number of samples
+      49           28 :     Classifier& Classifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
+      50              :     {
+      51           28 :         dataset = X;
+      52           28 :         buildDataset(y);
+      53           26 :         const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
+      54           44 :         return build(features, className, states, weights);
+      55           26 :     }
+      56              :     // X is nxm where n is the number of features and m the number of samples
+      57           32 :     Classifier& Classifier::fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
+      58              :     {
+      59           32 :         dataset = torch::zeros({ static_cast<int>(X.size()), static_cast<int>(X[0].size()) }, torch::kInt32);
+      60          643 :         for (int i = 0; i < X.size(); ++i) {
+      61         2444 :             dataset.index_put_({ i, "..." }, torch::tensor(X[i], torch::kInt32));
+      62              :         }
+      63           32 :         auto ytmp = torch::tensor(y, torch::kInt32);
+      64           32 :         buildDataset(ytmp);
+      65           30 :         const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
+      66           56 :         return build(features, className, states, weights);
+      67          647 :     }
+      68           99 :     Classifier& Classifier::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
+      69              :     {
+      70           99 :         this->dataset = dataset;
+      71           99 :         const torch::Tensor weights = torch::full({ dataset.size(1) }, 1.0 / dataset.size(1), torch::kDouble);
+      72          198 :         return build(features, className, states, weights);
+      73           99 :     }
+      74          136 :     Classifier& Classifier::fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
+      75              :     {
+      76          136 :         this->dataset = dataset;
+      77          136 :         return build(features, className, states, weights);
+      78              :     }
+      79          291 :     void Classifier::checkFitParameters()
+      80              :     {
+      81          291 :         if (torch::is_floating_point(dataset)) {
+      82            2 :             throw std::invalid_argument("dataset (X, y) must be of type Integer");
+      83              :         }
+      84          289 :         if (dataset.size(0) - 1 != features.size()) {
+      85            2 :             throw std::invalid_argument("Classifier: X " + std::to_string(dataset.size(0) - 1) + " and features " + std::to_string(features.size()) + " must have the same number of features");
+      86              :         }
+      87          287 :         if (states.find(className) == states.end()) {
+      88            2 :             throw std::invalid_argument("class name not found in states");
+      89              :         }
+      90         9467 :         for (auto feature : features) {
+      91         9184 :             if (states.find(feature) == states.end()) {
+      92            2 :                 throw std::invalid_argument("feature [" + feature + "] not found in states");
+      93              :             }
+      94         9184 :         }
+      95          283 :     }
+      96          245 :     torch::Tensor Classifier::predict(torch::Tensor& X)
+      97              :     {
+      98          245 :         if (!fitted) {
+      99            4 :             throw std::logic_error(CLASSIFIER_NOT_FITTED);
+     100              :         }
+     101          241 :         return model.predict(X);
+     102              :     }
+     103            4 :     std::vector<int> Classifier::predict(std::vector<std::vector<int>>& X)
+     104              :     {
+     105            4 :         if (!fitted) {
+     106            2 :             throw std::logic_error(CLASSIFIER_NOT_FITTED);
+     107              :         }
+     108            2 :         auto m_ = X[0].size();
+     109            2 :         auto n_ = X.size();
+     110            2 :         std::vector<std::vector<int>> Xd(n_, std::vector<int>(m_, 0));
+     111           10 :         for (auto i = 0; i < n_; i++) {
+     112           16 :             Xd[i] = std::vector<int>(X[i].begin(), X[i].end());
+     113              :         }
+     114            2 :         auto yp = model.predict(Xd);
+     115            4 :         return yp;
+     116            2 :     }
+     117          306 :     torch::Tensor Classifier::predict_proba(torch::Tensor& X)
+     118              :     {
+     119          306 :         if (!fitted) {
+     120            2 :             throw std::logic_error(CLASSIFIER_NOT_FITTED);
+     121              :         }
+     122          304 :         return model.predict_proba(X);
+     123              :     }
+     124           67 :     std::vector<std::vector<double>> Classifier::predict_proba(std::vector<std::vector<int>>& X)
+     125              :     {
+     126           67 :         if (!fitted) {
+     127            2 :             throw std::logic_error(CLASSIFIER_NOT_FITTED);
+     128              :         }
+     129           65 :         auto m_ = X[0].size();
+     130           65 :         auto n_ = X.size();
+     131           65 :         std::vector<std::vector<int>> Xd(n_, std::vector<int>(m_, 0));
+     132              :         // Convert to nxm vector
+     133          974 :         for (auto i = 0; i < n_; i++) {
+     134         1818 :             Xd[i] = std::vector<int>(X[i].begin(), X[i].end());
+     135              :         }
+     136           65 :         auto yp = model.predict_proba(Xd);
+     137          130 :         return yp;
+     138           65 :     }
+     139           28 :     float Classifier::score(torch::Tensor& X, torch::Tensor& y)
+     140              :     {
+     141           28 :         torch::Tensor y_pred = predict(X);
+     142           52 :         return (y_pred == y).sum().item<float>() / y.size(0);
+     143           26 :     }
+     144            4 :     float Classifier::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
+     145              :     {
+     146            4 :         if (!fitted) {
+     147            2 :             throw std::logic_error(CLASSIFIER_NOT_FITTED);
+     148              :         }
+     149            2 :         return model.score(X, y);
+     150              :     }
+     151            6 :     std::vector<std::string> Classifier::show() const
+     152              :     {
+     153            6 :         return model.show();
+     154              :     }
+     155          251 :     void Classifier::addNodes()
+     156              :     {
+     157              :         // Add all nodes to the network
+     158         8799 :         for (const auto& feature : features) {
+     159         8548 :             model.addNode(feature);
+     160              :         }
+     161          251 :         model.addNode(className);
+     162          251 :     }
+     163           40 :     int Classifier::getNumberOfNodes() const
+     164              :     {
+     165              :         // Features does not include class
+     166           40 :         return fitted ? model.getFeatures().size() : 0;
+     167              :     }
+     168           40 :     int Classifier::getNumberOfEdges() const
+     169              :     {
+     170           40 :         return fitted ? model.getNumEdges() : 0;
+     171              :     }
+     172            6 :     int Classifier::getNumberOfStates() const
+     173              :     {
+     174            6 :         return fitted ? model.getStates() : 0;
+     175              :     }
+     176           76 :     int Classifier::getClassNumStates() const
+     177              :     {
+     178           76 :         return fitted ? model.getClassNumStates() : 0;
+     179              :     }
+     180            1 :     std::vector<std::string> Classifier::topological_order()
+     181              :     {
+     182            1 :         return model.topological_sort();
+     183              :     }
+     184            1 :     std::string Classifier::dump_cpt() const
+     185              :     {
+     186            1 :         return model.dump_cpt();
+     187              :     }
+     188           19 :     void Classifier::setHyperparameters(const nlohmann::json& hyperparameters)
+     189              :     {
+     190           19 :         if (!hyperparameters.empty()) {
+     191            2 :             throw std::invalid_argument("Invalid hyperparameters" + hyperparameters.dump());
+     192              :         }
+     193           17 :     }
+     194              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.h.func-c.html b/html/bayesnet/classifiers/Classifier.h.func-c.html new file mode 100644 index 0000000..1db8252 --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.h.func-c.html @@ -0,0 +1,117 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:80.0 %54
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet10Classifier10getVersionB5cxx11Ev8
_ZNK8bayesnet10Classifier8getNotesB5cxx11Ev20
_ZNK8bayesnet10Classifier9getStatusEv32
_ZN8bayesnet10ClassifierD0Ev273
_ZN8bayesnet10ClassifierD0Ev0
_ZN8bayesnet10ClassifierD2Ev273
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.h.func.html b/html/bayesnet/classifiers/Classifier.h.func.html new file mode 100644 index 0000000..30874c3 --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.h.func.html @@ -0,0 +1,117 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:80.0 %54
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet10Classifier10getVersionB5cxx11Ev8
_ZN8bayesnet10ClassifierD0Ev273
_ZN8bayesnet10ClassifierD0Ev0
_ZN8bayesnet10ClassifierD2Ev273
_ZNK8bayesnet10Classifier8getNotesB5cxx11Ev20
_ZNK8bayesnet10Classifier9getStatusEv32
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Classifier.h.gcov.html b/html/bayesnet/classifiers/Classifier.h.gcov.html new file mode 100644 index 0000000..04bf73a --- /dev/null +++ b/html/bayesnet/classifiers/Classifier.h.gcov.html @@ -0,0 +1,141 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Classifier.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Classifier.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:80.0 %54
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef CLASSIFIER_H
+       8              : #define CLASSIFIER_H
+       9              : #include <torch/torch.h>
+      10              : #include "bayesnet/utils/BayesMetrics.h"
+      11              : #include "bayesnet/network/Network.h"
+      12              : #include "bayesnet/BaseClassifier.h"
+      13              : 
+      14              : namespace bayesnet {
+      15              :     class Classifier : public BaseClassifier {
+      16              :     public:
+      17              :         Classifier(Network model);
+      18          273 :         virtual ~Classifier() = default;
+      19              :         Classifier& fit(std::vector<std::vector<int>>& X, std::vector<int>& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
+      20              :         Classifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
+      21              :         Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states) override;
+      22              :         Classifier& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights) override;
+      23              :         void addNodes();
+      24              :         int getNumberOfNodes() const override;
+      25              :         int getNumberOfEdges() const override;
+      26              :         int getNumberOfStates() const override;
+      27              :         int getClassNumStates() const override;
+      28              :         torch::Tensor predict(torch::Tensor& X) override;
+      29              :         std::vector<int> predict(std::vector<std::vector<int>>& X) override;
+      30              :         torch::Tensor predict_proba(torch::Tensor& X) override;
+      31              :         std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
+      32           32 :         status_t getStatus() const override { return status; }
+      33           24 :         std::string getVersion() override { return { project_version.begin(), project_version.end() }; };
+      34              :         float score(torch::Tensor& X, torch::Tensor& y) override;
+      35              :         float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
+      36              :         std::vector<std::string> show() const override;
+      37              :         std::vector<std::string> topological_order()  override;
+      38           20 :         std::vector<std::string> getNotes() const override { return notes; }
+      39              :         std::string dump_cpt() const override;
+      40              :         void setHyperparameters(const nlohmann::json& hyperparameters) override; //For classifiers that don't have hyperparameters
+      41              :     protected:
+      42              :         bool fitted;
+      43              :         unsigned int m, n; // m: number of samples, n: number of features
+      44              :         Network model;
+      45              :         Metrics metrics;
+      46              :         std::vector<std::string> features;
+      47              :         std::string className;
+      48              :         std::map<std::string, std::vector<int>> states;
+      49              :         torch::Tensor dataset; // (n+1)xm tensor
+      50              :         status_t status = NORMAL;
+      51              :         std::vector<std::string> notes; // Used to store messages occurred during the fit process
+      52              :         void checkFitParameters();
+      53              :         virtual void buildModel(const torch::Tensor& weights) = 0;
+      54              :         void trainModel(const torch::Tensor& weights) override;
+      55              :         void buildDataset(torch::Tensor& y);
+      56              :     private:
+      57              :         Classifier& build(const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
+      58              :     };
+      59              : }
+      60              : #endif
+      61              : 
+      62              : 
+      63              : 
+      64              : 
+      65              : 
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.cc.func-c.html b/html/bayesnet/classifiers/KDB.cc.func-c.html new file mode 100644 index 0000000..8c7385a --- /dev/null +++ b/html/bayesnet/classifiers/KDB.cc.func-c.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:96.3 %5452
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet3KDB5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE2
_ZN8bayesnet3KDB18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE3
_ZN8bayesnet3KDB10buildModelERKN2at6TensorE13
_ZN8bayesnet3KDBC2Eif37
_ZN8bayesnet3KDB11add_m_edgesEiRSt6vectorIiSaIiEERN2at6TensorE86
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.cc.func.html b/html/bayesnet/classifiers/KDB.cc.func.html new file mode 100644 index 0000000..19feb2a --- /dev/null +++ b/html/bayesnet/classifiers/KDB.cc.func.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:96.3 %5452
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3KDB10buildModelERKN2at6TensorE13
_ZN8bayesnet3KDB11add_m_edgesEiRSt6vectorIiSaIiEERN2at6TensorE86
_ZN8bayesnet3KDB18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE3
_ZN8bayesnet3KDBC2Eif37
_ZNK8bayesnet3KDB5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE2
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.cc.gcov.html b/html/bayesnet/classifiers/KDB.cc.gcov.html new file mode 100644 index 0000000..4694323 --- /dev/null +++ b/html/bayesnet/classifiers/KDB.cc.gcov.html @@ -0,0 +1,187 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:96.3 %5452
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "KDB.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           37 :     KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta)
+      11              :     {
+      12          111 :         validHyperparameters = { "k", "theta" };
+      13              : 
+      14          111 :     }
+      15            3 :     void KDB::setHyperparameters(const nlohmann::json& hyperparameters_)
+      16              :     {
+      17            3 :         auto hyperparameters = hyperparameters_;
+      18            3 :         if (hyperparameters.contains("k")) {
+      19            1 :             k = hyperparameters["k"];
+      20            1 :             hyperparameters.erase("k");
+      21              :         }
+      22            3 :         if (hyperparameters.contains("theta")) {
+      23            1 :             theta = hyperparameters["theta"];
+      24            1 :             hyperparameters.erase("theta");
+      25              :         }
+      26            3 :         Classifier::setHyperparameters(hyperparameters);
+      27            3 :     }
+      28           13 :     void KDB::buildModel(const torch::Tensor& weights)
+      29              :     {
+      30              :         /*
+      31              :         1. For each feature Xi, compute mutual information, I(X;C),
+      32              :         where C is the class.
+      33              :         2. Compute class conditional mutual information I(Xi;XjIC), f or each
+      34              :         pair of features Xi and Xj, where i#j.
+      35              :         3. Let the used variable list, S, be empty.
+      36              :         4. Let the DAG network being constructed, BN, begin with a single
+      37              :         class node, C.
+      38              :         5. Repeat until S includes all domain features
+      39              :         5.1. Select feature Xmax which is not in S and has the largest value
+      40              :         I(Xmax;C).
+      41              :         5.2. Add a node to BN representing Xmax.
+      42              :         5.3. Add an arc from C to Xmax in BN.
+      43              :         5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with
+      44              :         the highest value for I(Xmax;X,jC).
+      45              :         5.5. Add Xmax to S.
+      46              :         Compute the conditional probabilility infered by the structure of BN by
+      47              :         using counts from DB, and output BN.
+      48              :         */
+      49              :         // 1. For each feature Xi, compute mutual information, I(X;C),
+      50              :         // where C is the class.
+      51           13 :         addNodes();
+      52           39 :         const torch::Tensor& y = dataset.index({ -1, "..." });
+      53           13 :         std::vector<double> mi;
+      54           99 :         for (auto i = 0; i < features.size(); i++) {
+      55          258 :             torch::Tensor firstFeature = dataset.index({ i, "..." });
+      56           86 :             mi.push_back(metrics.mutualInformation(firstFeature, y, weights));
+      57           86 :         }
+      58              :         // 2. Compute class conditional mutual information I(Xi;XjIC), f or each
+      59           13 :         auto conditionalEdgeWeights = metrics.conditionalEdge(weights);
+      60              :         // 3. Let the used variable list, S, be empty.
+      61           13 :         std::vector<int> S;
+      62              :         // 4. Let the DAG network being constructed, BN, begin with a single
+      63              :         // class node, C.
+      64              :         // 5. Repeat until S includes all domain features
+      65              :         // 5.1. Select feature Xmax which is not in S and has the largest value
+      66              :         // I(Xmax;C).
+      67           13 :         auto order = argsort(mi);
+      68           99 :         for (auto idx : order) {
+      69              :             // 5.2. Add a node to BN representing Xmax.
+      70              :             // 5.3. Add an arc from C to Xmax in BN.
+      71           86 :             model.addEdge(className, features[idx]);
+      72              :             // 5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with
+      73              :             // the highest value for I(Xmax;X,jC).
+      74           86 :             add_m_edges(idx, S, conditionalEdgeWeights);
+      75              :             // 5.5. Add Xmax to S.
+      76           86 :             S.push_back(idx);
+      77              :         }
+      78          112 :     }
+      79           86 :     void KDB::add_m_edges(int idx, std::vector<int>& S, torch::Tensor& weights)
+      80              :     {
+      81           86 :         auto n_edges = std::min(k, static_cast<int>(S.size()));
+      82           86 :         auto cond_w = clone(weights);
+      83           86 :         bool exit_cond = k == 0;
+      84           86 :         int num = 0;
+      85          251 :         while (!exit_cond) {
+      86          660 :             auto max_minfo = argmax(cond_w.index({ idx, "..." })).item<int>();
+      87          165 :             auto belongs = find(S.begin(), S.end(), max_minfo) != S.end();
+      88          441 :             if (belongs && cond_w.index({ idx, max_minfo }).item<float>() > theta) {
+      89              :                 try {
+      90           80 :                     model.addEdge(features[max_minfo], features[idx]);
+      91           80 :                     num++;
+      92              :                 }
+      93            0 :                 catch (const std::invalid_argument& e) {
+      94              :                     // Loops are not allowed
+      95            0 :                 }
+      96              :             }
+      97          660 :             cond_w.index_put_({ idx, max_minfo }, -1);
+      98          495 :             auto candidates_mask = cond_w.index({ idx, "..." }).gt(theta);
+      99          165 :             auto candidates = candidates_mask.nonzero();
+     100          165 :             exit_cond = num == n_edges || candidates.size(0) == 0;
+     101          165 :         }
+     102          673 :     }
+     103            2 :     std::vector<std::string> KDB::graph(const std::string& title) const
+     104              :     {
+     105            2 :         std::string header{ title };
+     106            2 :         if (title == "KDB") {
+     107            2 :             header += " (k=" + std::to_string(k) + ", theta=" + std::to_string(theta) + ")";
+     108              :         }
+     109            4 :         return model.graph(header);
+     110            2 :     }
+     111              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.h.func-c.html b/html/bayesnet/classifiers/KDB.h.func-c.html new file mode 100644 index 0000000..9956de3 --- /dev/null +++ b/html/bayesnet/classifiers/KDB.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3KDBD0Ev11
_ZN8bayesnet3KDBD0Ev2
_ZN8bayesnet3KDBD2Ev9
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.h.func.html b/html/bayesnet/classifiers/KDB.h.func.html new file mode 100644 index 0000000..e4959a3 --- /dev/null +++ b/html/bayesnet/classifiers/KDB.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3KDBD0Ev11
_ZN8bayesnet3KDBD0Ev2
_ZN8bayesnet3KDBD2Ev9
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDB.h.gcov.html b/html/bayesnet/classifiers/KDB.h.gcov.html new file mode 100644 index 0000000..3cf9e99 --- /dev/null +++ b/html/bayesnet/classifiers/KDB.h.gcov.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDB.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDB.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef KDB_H
+       8              : #define KDB_H
+       9              : #include <torch/torch.h>
+      10              : #include "bayesnet/utils/bayesnetUtils.h"
+      11              : #include "Classifier.h"
+      12              : namespace bayesnet {
+      13              :     class KDB : public Classifier {
+      14              :     private:
+      15              :         int k;
+      16              :         float theta;
+      17              :         void add_m_edges(int idx, std::vector<int>& S, torch::Tensor& weights);
+      18              :     protected:
+      19              :         void buildModel(const torch::Tensor& weights) override;
+      20              :     public:
+      21              :         explicit KDB(int k, float theta = 0.03);
+      22           11 :         virtual ~KDB() = default;
+      23              :         void setHyperparameters(const nlohmann::json& hyperparameters_) override;
+      24              :         std::vector<std::string> graph(const std::string& name = "KDB") const override;
+      25              :     };
+      26              : }
+      27              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.cc.func-c.html b/html/bayesnet/classifiers/KDBLd.cc.func-c.html new file mode 100644 index 0000000..59bfcea --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.cc.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet5KDBLd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZN8bayesnet5KDBLd7predictERN2at6TensorE4
_ZN8bayesnet5KDBLd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet5KDBLdC2Ei17
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.cc.func.html b/html/bayesnet/classifiers/KDBLd.cc.func.html new file mode 100644 index 0000000..f97fcaa --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.cc.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5KDBLd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet5KDBLd7predictERN2at6TensorE4
_ZN8bayesnet5KDBLdC2Ei17
_ZNK8bayesnet5KDBLd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.cc.gcov.html b/html/bayesnet/classifiers/KDBLd.cc.gcov.html new file mode 100644 index 0000000..76d836e --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.cc.gcov.html @@ -0,0 +1,111 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "KDBLd.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           17 :     KDBLd::KDBLd(int k) : KDB(k), Proposal(dataset, features, className) {}
+      11            5 :     KDBLd& KDBLd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      12              :     {
+      13            5 :         checkInput(X_, y_);
+      14            5 :         features = features_;
+      15            5 :         className = className_;
+      16            5 :         Xf = X_;
+      17            5 :         y = y_;
+      18              :         // Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
+      19            5 :         states = fit_local_discretization(y);
+      20              :         // We have discretized the input data
+      21              :         // 1st we need to fit the model to build the normal KDB structure, KDB::fit initializes the base Bayesian network
+      22            5 :         KDB::fit(dataset, features, className, states);
+      23            5 :         states = localDiscretizationProposal(states, model);
+      24            5 :         return *this;
+      25              :     }
+      26            4 :     torch::Tensor KDBLd::predict(torch::Tensor& X)
+      27              :     {
+      28            4 :         auto Xt = prepareX(X);
+      29            8 :         return KDB::predict(Xt);
+      30            4 :     }
+      31            1 :     std::vector<std::string> KDBLd::graph(const std::string& name) const
+      32              :     {
+      33            1 :         return KDB::graph(name);
+      34              :     }
+      35              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.h.func-c.html b/html/bayesnet/classifiers/KDBLd.h.func-c.html new file mode 100644 index 0000000..982874f --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5KDBLdD0Ev5
_ZN8bayesnet5KDBLdD0Ev2
_ZN8bayesnet5KDBLdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.h.func.html b/html/bayesnet/classifiers/KDBLd.h.func.html new file mode 100644 index 0000000..3bebc1a --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5KDBLdD0Ev5
_ZN8bayesnet5KDBLdD0Ev2
_ZN8bayesnet5KDBLdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/KDBLd.h.gcov.html b/html/bayesnet/classifiers/KDBLd.h.gcov.html new file mode 100644 index 0000000..64879d1 --- /dev/null +++ b/html/bayesnet/classifiers/KDBLd.h.gcov.html @@ -0,0 +1,100 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/KDBLd.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - KDBLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef KDBLD_H
+       8              : #define KDBLD_H
+       9              : #include "Proposal.h"
+      10              : #include "KDB.h"
+      11              : 
+      12              : namespace bayesnet {
+      13              :     class KDBLd : public KDB, public Proposal {
+      14              :     private:
+      15              :     public:
+      16              :         explicit KDBLd(int k);
+      17            5 :         virtual ~KDBLd() = default;
+      18              :         KDBLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
+      19              :         std::vector<std::string> graph(const std::string& name = "KDB") const override;
+      20              :         torch::Tensor predict(torch::Tensor& X) override;
+      21              :         static inline std::string version() { return "0.0.1"; };
+      22              :     };
+      23              : }
+      24              : #endif // !KDBLD_H
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Proposal.cc.func-c.html b/html/bayesnet/classifiers/Proposal.cc.func-c.html new file mode 100644 index 0000000..f64bdf7 --- /dev/null +++ b/html/bayesnet/classifiers/Proposal.cc.func-c.html @@ -0,0 +1,145 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Proposal.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Proposal.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.7 %8684
Test Date:2024-04-21 16:43:29Functions:88.9 %98
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet8Proposal8prepareXERN2at6TensorE42
_ZN8bayesnet8ProposalD0Ev50
_ZN8bayesnet8ProposalD0Ev0
_ZN8bayesnet8ProposalD2Ev50
_ZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkE53
_ZN8bayesnet8Proposal10checkInputERKN2at6TensorES4_57
_ZN8bayesnet8Proposal24fit_local_discretizationB5cxx11ERKN2at6TensorE58
_ZN8bayesnet8ProposalC2ERN2at6TensorERSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERSA_106
_ZZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkEENKUlRKT_E0_clIS7_EEDaSO_343
_ZZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkEENKUlRKT_E_clIPNS_4NodeEEEDaSO_674
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Proposal.cc.func.html b/html/bayesnet/classifiers/Proposal.cc.func.html new file mode 100644 index 0000000..6800d0c --- /dev/null +++ b/html/bayesnet/classifiers/Proposal.cc.func.html @@ -0,0 +1,145 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Proposal.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Proposal.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.7 %8684
Test Date:2024-04-21 16:43:29Functions:88.9 %98
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet8Proposal10checkInputERKN2at6TensorES4_57
_ZN8bayesnet8Proposal24fit_local_discretizationB5cxx11ERKN2at6TensorE58
_ZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkE53
_ZN8bayesnet8Proposal8prepareXERN2at6TensorE42
_ZN8bayesnet8ProposalC2ERN2at6TensorERSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERSA_106
_ZN8bayesnet8ProposalD0Ev50
_ZN8bayesnet8ProposalD0Ev0
_ZN8bayesnet8ProposalD2Ev50
_ZZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkEENKUlRKT_E0_clIS7_EEDaSO_343
_ZZN8bayesnet8Proposal27localDiscretizationProposalERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERNS_7NetworkEENKUlRKT_E_clIPNS_4NodeEEEDaSO_674
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/Proposal.cc.gcov.html b/html/bayesnet/classifiers/Proposal.cc.gcov.html new file mode 100644 index 0000000..4e6db9a --- /dev/null +++ b/html/bayesnet/classifiers/Proposal.cc.gcov.html @@ -0,0 +1,192 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/Proposal.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - Proposal.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.7 %8684
Test Date:2024-04-21 16:43:29Functions:88.9 %98
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <ArffFiles.h>
+       8              : #include "Proposal.h"
+       9              : 
+      10              : namespace bayesnet {
+      11          106 :     Proposal::Proposal(torch::Tensor& dataset_, std::vector<std::string>& features_, std::string& className_) : pDataset(dataset_), pFeatures(features_), pClassName(className_) {}
+      12           50 :     Proposal::~Proposal()
+      13              :     {
+      14          474 :         for (auto& [key, value] : discretizers) {
+      15          424 :             delete value;
+      16              :         }
+      17           50 :     }
+      18           57 :     void Proposal::checkInput(const torch::Tensor& X, const torch::Tensor& y)
+      19              :     {
+      20           57 :         if (!torch::is_floating_point(X)) {
+      21            0 :             throw std::invalid_argument("X must be a floating point tensor");
+      22              :         }
+      23           57 :         if (torch::is_floating_point(y)) {
+      24            0 :             throw std::invalid_argument("y must be an integer tensor");
+      25              :         }
+      26           57 :     }
+      27           53 :     map<std::string, std::vector<int>> Proposal::localDiscretizationProposal(const map<std::string, std::vector<int>>& oldStates, Network& model)
+      28              :     {
+      29              :         // order of local discretization is important. no good 0, 1, 2...
+      30              :         // although we rediscretize features after the local discretization of every feature
+      31           53 :         auto order = model.topological_sort();
+      32           53 :         auto& nodes = model.getNodes();
+      33           53 :         map<std::string, std::vector<int>> states = oldStates;
+      34           53 :         std::vector<int> indicesToReDiscretize;
+      35           53 :         bool upgrade = false; // Flag to check if we need to upgrade the model
+      36          444 :         for (auto feature : order) {
+      37          391 :             auto nodeParents = nodes[feature]->getParents();
+      38          391 :             if (nodeParents.size() < 2) continue; // Only has class as parent
+      39          331 :             upgrade = true;
+      40          331 :             int index = find(pFeatures.begin(), pFeatures.end(), feature) - pFeatures.begin();
+      41          331 :             indicesToReDiscretize.push_back(index); // We need to re-discretize this feature
+      42          331 :             std::vector<std::string> parents;
+      43         1005 :             transform(nodeParents.begin(), nodeParents.end(), back_inserter(parents), [](const auto& p) { return p->getName(); });
+      44              :             // Remove class as parent as it will be added later
+      45          331 :             parents.erase(remove(parents.begin(), parents.end(), pClassName), parents.end());
+      46              :             // Get the indices of the parents
+      47          331 :             std::vector<int> indices;
+      48          331 :             indices.push_back(-1); // Add class index
+      49          674 :             transform(parents.begin(), parents.end(), back_inserter(indices), [&](const auto& p) {return find(pFeatures.begin(), pFeatures.end(), p) - pFeatures.begin(); });
+      50              :             // Now we fit the discretizer of the feature, conditioned on its parents and the class i.e. discretizer.fit(X[index], X[indices] + y)
+      51          331 :             std::vector<std::string> yJoinParents(Xf.size(1));
+      52         1005 :             for (auto idx : indices) {
+      53       239660 :                 for (int i = 0; i < Xf.size(1); ++i) {
+      54       716958 :                     yJoinParents[i] += to_string(pDataset.index({ idx, i }).item<int>());
+      55              :                 }
+      56              :             }
+      57          331 :             auto arff = ArffFiles();
+      58          331 :             auto yxv = arff.factorize(yJoinParents);
+      59          662 :             auto xvf_ptr = Xf.index({ index }).data_ptr<float>();
+      60          331 :             auto xvf = std::vector<mdlp::precision_t>(xvf_ptr, xvf_ptr + Xf.size(1));
+      61          331 :             discretizers[feature]->fit(xvf, yxv);
+      62          451 :         }
+      63           53 :         if (upgrade) {
+      64              :             // Discretize again X (only the affected indices) with the new fitted discretizers
+      65          384 :             for (auto index : indicesToReDiscretize) {
+      66          662 :                 auto Xt_ptr = Xf.index({ index }).data_ptr<float>();
+      67          331 :                 auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
+      68         1324 :                 pDataset.index_put_({ index, "..." }, torch::tensor(discretizers[pFeatures[index]]->transform(Xt)));
+      69          331 :                 auto xStates = std::vector<int>(discretizers[pFeatures[index]]->getCutPoints().size() + 1);
+      70          331 :                 iota(xStates.begin(), xStates.end(), 0);
+      71              :                 //Update new states of the feature/node
+      72          331 :                 states[pFeatures[index]] = xStates;
+      73          331 :             }
+      74           53 :             const torch::Tensor weights = torch::full({ pDataset.size(1) }, 1.0 / pDataset.size(1), torch::kDouble);
+      75           53 :             model.fit(pDataset, weights, pFeatures, pClassName, states);
+      76           53 :         }
+      77          106 :         return states;
+      78       240032 :     }
+      79           58 :     map<std::string, std::vector<int>> Proposal::fit_local_discretization(const torch::Tensor& y)
+      80              :     {
+      81              :         // Discretize the continuous input data and build pDataset (Classifier::dataset)
+      82           58 :         int m = Xf.size(1);
+      83           58 :         int n = Xf.size(0);
+      84           58 :         map<std::string, std::vector<int>> states;
+      85           58 :         pDataset = torch::zeros({ n + 1, m }, torch::kInt32);
+      86           58 :         auto yv = std::vector<int>(y.data_ptr<int>(), y.data_ptr<int>() + y.size(0));
+      87              :         // discretize input data by feature(row)
+      88          486 :         for (auto i = 0; i < pFeatures.size(); ++i) {
+      89          428 :             auto* discretizer = new mdlp::CPPFImdlp();
+      90          856 :             auto Xt_ptr = Xf.index({ i }).data_ptr<float>();
+      91          428 :             auto Xt = std::vector<float>(Xt_ptr, Xt_ptr + Xf.size(1));
+      92          428 :             discretizer->fit(Xt, yv);
+      93         1712 :             pDataset.index_put_({ i, "..." }, torch::tensor(discretizer->transform(Xt)));
+      94          428 :             auto xStates = std::vector<int>(discretizer->getCutPoints().size() + 1);
+      95          428 :             iota(xStates.begin(), xStates.end(), 0);
+      96          428 :             states[pFeatures[i]] = xStates;
+      97          428 :             discretizers[pFeatures[i]] = discretizer;
+      98          428 :         }
+      99           58 :         int n_classes = torch::max(y).item<int>() + 1;
+     100           58 :         auto yStates = std::vector<int>(n_classes);
+     101           58 :         iota(yStates.begin(), yStates.end(), 0);
+     102           58 :         states[pClassName] = yStates;
+     103          174 :         pDataset.index_put_({ n, "..." }, y);
+     104          116 :         return states;
+     105          972 :     }
+     106           42 :     torch::Tensor Proposal::prepareX(torch::Tensor& X)
+     107              :     {
+     108           42 :         auto Xtd = torch::zeros_like(X, torch::kInt32);
+     109          344 :         for (int i = 0; i < X.size(0); ++i) {
+     110          302 :             auto Xt = std::vector<float>(X[i].data_ptr<float>(), X[i].data_ptr<float>() + X.size(1));
+     111          302 :             auto Xd = discretizers[pFeatures[i]]->transform(Xt);
+     112          906 :             Xtd.index_put_({ i }, torch::tensor(Xd, torch::kInt32));
+     113          302 :         }
+     114           42 :         return Xtd;
+     115          302 :     }
+     116              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.cc.func-c.html b/html/bayesnet/classifiers/SPODE.cc.func-c.html new file mode 100644 index 0000000..12d1c42 --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.cc.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1010
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet5SPODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE17
_ZN8bayesnet5SPODE10buildModelERKN2at6TensorE225
_ZN8bayesnet5SPODEC2Ei252
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.cc.func.html b/html/bayesnet/classifiers/SPODE.cc.func.html new file mode 100644 index 0000000..f748bb9 --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.cc.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1010
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5SPODE10buildModelERKN2at6TensorE225
_ZN8bayesnet5SPODEC2Ei252
_ZNK8bayesnet5SPODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE17
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.cc.gcov.html b/html/bayesnet/classifiers/SPODE.cc.gcov.html new file mode 100644 index 0000000..c87bd98 --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.cc.gcov.html @@ -0,0 +1,107 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1010
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "SPODE.h"
+       8              : 
+       9              : namespace bayesnet {
+      10              : 
+      11          252 :     SPODE::SPODE(int root) : Classifier(Network()), root(root) {}
+      12              : 
+      13          225 :     void SPODE::buildModel(const torch::Tensor& weights)
+      14              :     {
+      15              :         // 0. Add all nodes to the model
+      16          225 :         addNodes();
+      17              :         // 1. Add edges from the class node to all other nodes
+      18              :         // 2. Add edges from the root node to all other nodes
+      19         8611 :         for (int i = 0; i < static_cast<int>(features.size()); ++i) {
+      20         8386 :             model.addEdge(className, features[i]);
+      21         8386 :             if (i != root) {
+      22         8161 :                 model.addEdge(features[root], features[i]);
+      23              :             }
+      24              :         }
+      25          225 :     }
+      26           17 :     std::vector<std::string> SPODE::graph(const std::string& name) const
+      27              :     {
+      28           17 :         return model.graph(name);
+      29              :     }
+      30              : 
+      31              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.h.func-c.html b/html/bayesnet/classifiers/SPODE.h.func-c.html new file mode 100644 index 0000000..ebcd40a --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5SPODED0Ev401
_ZN8bayesnet5SPODED0Ev180
_ZN8bayesnet5SPODED2Ev221
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.h.func.html b/html/bayesnet/classifiers/SPODE.h.func.html new file mode 100644 index 0000000..7130d03 --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5SPODED0Ev401
_ZN8bayesnet5SPODED0Ev180
_ZN8bayesnet5SPODED2Ev221
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODE.h.gcov.html b/html/bayesnet/classifiers/SPODE.h.gcov.html new file mode 100644 index 0000000..1444ca4 --- /dev/null +++ b/html/bayesnet/classifiers/SPODE.h.gcov.html @@ -0,0 +1,99 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODE.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef SPODE_H
+       8              : #define SPODE_H
+       9              : #include "Classifier.h"
+      10              : 
+      11              : namespace bayesnet {
+      12              :     class SPODE : public Classifier {
+      13              :     private:
+      14              :         int root;
+      15              :     protected:
+      16              :         void buildModel(const torch::Tensor& weights) override;
+      17              :     public:
+      18              :         explicit SPODE(int root);
+      19          401 :         virtual ~SPODE() = default;
+      20              :         std::vector<std::string> graph(const std::string& name = "SPODE") const override;
+      21              :     };
+      22              : }
+      23              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.cc.func-c.html b/html/bayesnet/classifiers/SPODELd.cc.func-c.html new file mode 100644 index 0000000..ed19e92 --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.cc.func-c.html @@ -0,0 +1,117 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2626
Test Date:2024-04-21 16:43:29Functions:100.0 %66
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7SPODELd3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE2
_ZNK8bayesnet7SPODELd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE9
_ZN8bayesnet7SPODELd7predictERN2at6TensorE34
_ZN8bayesnet7SPODELd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE42
_ZN8bayesnet7SPODELd9commonFitERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEE43
_ZN8bayesnet7SPODELdC2Ei55
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.cc.func.html b/html/bayesnet/classifiers/SPODELd.cc.func.html new file mode 100644 index 0000000..b651695 --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.cc.func.html @@ -0,0 +1,117 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2626
Test Date:2024-04-21 16:43:29Functions:100.0 %66
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7SPODELd3fitERN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE2
_ZN8bayesnet7SPODELd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE42
_ZN8bayesnet7SPODELd7predictERN2at6TensorE34
_ZN8bayesnet7SPODELd9commonFitERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEE43
_ZN8bayesnet7SPODELdC2Ei55
_ZNK8bayesnet7SPODELd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE9
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.cc.gcov.html b/html/bayesnet/classifiers/SPODELd.cc.gcov.html new file mode 100644 index 0000000..c69df71 --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.cc.gcov.html @@ -0,0 +1,126 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2626
Test Date:2024-04-21 16:43:29Functions:100.0 %66
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "SPODELd.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           55 :     SPODELd::SPODELd(int root) : SPODE(root), Proposal(dataset, features, className) {}
+      11           42 :     SPODELd& SPODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      12              :     {
+      13           42 :         checkInput(X_, y_);
+      14           42 :         Xf = X_;
+      15           42 :         y = y_;
+      16           42 :         return commonFit(features_, className_, states_);
+      17              :     }
+      18              : 
+      19            2 :     SPODELd& SPODELd::fit(torch::Tensor& dataset, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      20              :     {
+      21            2 :         if (!torch::is_floating_point(dataset)) {
+      22            1 :             throw std::runtime_error("Dataset must be a floating point tensor");
+      23              :         }
+      24            4 :         Xf = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." }).clone();
+      25            3 :         y = dataset.index({ -1, "..." }).clone().to(torch::kInt32);
+      26            1 :         return commonFit(features_, className_, states_);
+      27            3 :     }
+      28              : 
+      29           43 :     SPODELd& SPODELd::commonFit(const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      30              :     {
+      31           43 :         features = features_;
+      32           43 :         className = className_;
+      33              :         // Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
+      34           43 :         states = fit_local_discretization(y);
+      35              :         // We have discretized the input data
+      36              :         // 1st we need to fit the model to build the normal SPODE structure, SPODE::fit initializes the base Bayesian network
+      37           43 :         SPODE::fit(dataset, features, className, states);
+      38           43 :         states = localDiscretizationProposal(states, model);
+      39           43 :         return *this;
+      40              :     }
+      41           34 :     torch::Tensor SPODELd::predict(torch::Tensor& X)
+      42              :     {
+      43           34 :         auto Xt = prepareX(X);
+      44           68 :         return SPODE::predict(Xt);
+      45           34 :     }
+      46            9 :     std::vector<std::string> SPODELd::graph(const std::string& name) const
+      47              :     {
+      48            9 :         return SPODE::graph(name);
+      49              :     }
+      50              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.h.func-c.html b/html/bayesnet/classifiers/SPODELd.h.func-c.html new file mode 100644 index 0000000..2fed6aa --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7SPODELdD0Ev80
_ZN8bayesnet7SPODELdD0Ev39
_ZN8bayesnet7SPODELdD2Ev41
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.h.func.html b/html/bayesnet/classifiers/SPODELd.h.func.html new file mode 100644 index 0000000..16b2df8 --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7SPODELdD0Ev80
_ZN8bayesnet7SPODELdD0Ev39
_ZN8bayesnet7SPODELdD2Ev41
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/SPODELd.h.gcov.html b/html/bayesnet/classifiers/SPODELd.h.gcov.html new file mode 100644 index 0000000..46a58ea --- /dev/null +++ b/html/bayesnet/classifiers/SPODELd.h.gcov.html @@ -0,0 +1,101 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/SPODELd.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - SPODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef SPODELD_H
+       8              : #define SPODELD_H
+       9              : #include "SPODE.h"
+      10              : #include "Proposal.h"
+      11              : 
+      12              : namespace bayesnet {
+      13              :     class SPODELd : public SPODE, public Proposal {
+      14              :     public:
+      15              :         explicit SPODELd(int root);
+      16           80 :         virtual ~SPODELd() = default;
+      17              :         SPODELd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
+      18              :         SPODELd& fit(torch::Tensor& dataset, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
+      19              :         SPODELd& commonFit(const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states);
+      20              :         std::vector<std::string> graph(const std::string& name = "SPODE") const override;
+      21              :         torch::Tensor predict(torch::Tensor& X) override;
+      22              :         static inline std::string version() { return "0.0.1"; };
+      23              :     };
+      24              : }
+      25              : #endif // !SPODELD_H
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.cc.func-c.html b/html/bayesnet/classifiers/TAN.cc.func-c.html new file mode 100644 index 0000000..a7e588d --- /dev/null +++ b/html/bayesnet/classifiers/TAN.cc.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2323
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet3TAN5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE2
_ZN8bayesnet3TAN10buildModelERKN2at6TensorE13
_ZN8bayesnet3TANC2Ev47
_ZZN8bayesnet3TAN10buildModelERKN2at6TensorEENKUlRKT_RKT0_E_clISt4pairIifESE_EEDaS7_SA_162
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.cc.func.html b/html/bayesnet/classifiers/TAN.cc.func.html new file mode 100644 index 0000000..2d412e5 --- /dev/null +++ b/html/bayesnet/classifiers/TAN.cc.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2323
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3TAN10buildModelERKN2at6TensorE13
_ZN8bayesnet3TANC2Ev47
_ZNK8bayesnet3TAN5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE2
_ZZN8bayesnet3TAN10buildModelERKN2at6TensorEENKUlRKT_RKT0_E_clISt4pairIifESE_EEDaS7_SA_162
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.cc.gcov.html b/html/bayesnet/classifiers/TAN.cc.gcov.html new file mode 100644 index 0000000..f7d1c21 --- /dev/null +++ b/html/bayesnet/classifiers/TAN.cc.gcov.html @@ -0,0 +1,121 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2323
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "TAN.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           47 :     TAN::TAN() : Classifier(Network()) {}
+      11              : 
+      12           13 :     void TAN::buildModel(const torch::Tensor& weights)
+      13              :     {
+      14              :         // 0. Add all nodes to the model
+      15           13 :         addNodes();
+      16              :         // 1. Compute mutual information between each feature and the class and set the root node
+      17              :         // as the highest mutual information with the class
+      18           13 :         auto mi = std::vector <std::pair<int, float >>();
+      19           39 :         torch::Tensor class_dataset = dataset.index({ -1, "..." });
+      20           89 :         for (int i = 0; i < static_cast<int>(features.size()); ++i) {
+      21          228 :             torch::Tensor feature_dataset = dataset.index({ i, "..." });
+      22           76 :             auto mi_value = metrics.mutualInformation(class_dataset, feature_dataset, weights);
+      23           76 :             mi.push_back({ i, mi_value });
+      24           76 :         }
+      25          175 :         sort(mi.begin(), mi.end(), [](const auto& left, const auto& right) {return left.second < right.second;});
+      26           13 :         auto root = mi[mi.size() - 1].first;
+      27              :         // 2. Compute mutual information between each feature and the class
+      28           13 :         auto weights_matrix = metrics.conditionalEdge(weights);
+      29              :         // 3. Compute the maximum spanning tree
+      30           13 :         auto mst = metrics.maximumSpanningTree(features, weights_matrix, root);
+      31              :         // 4. Add edges from the maximum spanning tree to the model
+      32           76 :         for (auto i = 0; i < mst.size(); ++i) {
+      33           63 :             auto [from, to] = mst[i];
+      34           63 :             model.addEdge(features[from], features[to]);
+      35              :         }
+      36              :         // 5. Add edges from the class to all features
+      37           89 :         for (auto feature : features) {
+      38           76 :             model.addEdge(className, feature);
+      39           76 :         }
+      40          102 :     }
+      41            2 :     std::vector<std::string> TAN::graph(const std::string& title) const
+      42              :     {
+      43            2 :         return model.graph(title);
+      44              :     }
+      45              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.h.func-c.html b/html/bayesnet/classifiers/TAN.h.func-c.html new file mode 100644 index 0000000..62d2630 --- /dev/null +++ b/html/bayesnet/classifiers/TAN.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3TAND0Ev19
_ZN8bayesnet3TAND0Ev3
_ZN8bayesnet3TAND2Ev16
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.h.func.html b/html/bayesnet/classifiers/TAN.h.func.html new file mode 100644 index 0000000..4bfe7ed --- /dev/null +++ b/html/bayesnet/classifiers/TAN.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3TAND0Ev19
_ZN8bayesnet3TAND0Ev3
_ZN8bayesnet3TAND2Ev16
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TAN.h.gcov.html b/html/bayesnet/classifiers/TAN.h.gcov.html new file mode 100644 index 0000000..30c107d --- /dev/null +++ b/html/bayesnet/classifiers/TAN.h.gcov.html @@ -0,0 +1,97 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TAN.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TAN.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef TAN_H
+       8              : #define TAN_H
+       9              : #include "Classifier.h"
+      10              : namespace bayesnet {
+      11              :     class TAN : public Classifier {
+      12              :     private:
+      13              :     protected:
+      14              :         void buildModel(const torch::Tensor& weights) override;
+      15              :     public:
+      16              :         TAN();
+      17           19 :         virtual ~TAN() = default;
+      18              :         std::vector<std::string> graph(const std::string& name = "TAN") const override;
+      19              :     };
+      20              : }
+      21              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.cc.func-c.html b/html/bayesnet/classifiers/TANLd.cc.func-c.html new file mode 100644 index 0000000..3a21441 --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.cc.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet5TANLd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZN8bayesnet5TANLd7predictERN2at6TensorE4
_ZN8bayesnet5TANLd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet5TANLdC2Ev17
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.cc.func.html b/html/bayesnet/classifiers/TANLd.cc.func.html new file mode 100644 index 0000000..c159191 --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.cc.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5TANLd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet5TANLd7predictERN2at6TensorE4
_ZN8bayesnet5TANLdC2Ev17
_ZNK8bayesnet5TANLd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.cc.gcov.html b/html/bayesnet/classifiers/TANLd.cc.gcov.html new file mode 100644 index 0000000..79e628f --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.cc.gcov.html @@ -0,0 +1,112 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %1717
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "TANLd.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           17 :     TANLd::TANLd() : TAN(), Proposal(dataset, features, className) {}
+      11            5 :     TANLd& TANLd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      12              :     {
+      13            5 :         checkInput(X_, y_);
+      14            5 :         features = features_;
+      15            5 :         className = className_;
+      16            5 :         Xf = X_;
+      17            5 :         y = y_;
+      18              :         // Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
+      19            5 :         states = fit_local_discretization(y);
+      20              :         // We have discretized the input data
+      21              :         // 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network
+      22            5 :         TAN::fit(dataset, features, className, states);
+      23            5 :         states = localDiscretizationProposal(states, model);
+      24            5 :         return *this;
+      25              : 
+      26              :     }
+      27            4 :     torch::Tensor TANLd::predict(torch::Tensor& X)
+      28              :     {
+      29            4 :         auto Xt = prepareX(X);
+      30            8 :         return TAN::predict(Xt);
+      31            4 :     }
+      32            1 :     std::vector<std::string> TANLd::graph(const std::string& name) const
+      33              :     {
+      34            1 :         return TAN::graph(name);
+      35              :     }
+      36              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.h.func-c.html b/html/bayesnet/classifiers/TANLd.h.func-c.html new file mode 100644 index 0000000..e2d3cd2 --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5TANLdD0Ev5
_ZN8bayesnet5TANLdD0Ev2
_ZN8bayesnet5TANLdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.h.func.html b/html/bayesnet/classifiers/TANLd.h.func.html new file mode 100644 index 0000000..e625fdf --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5TANLdD0Ev5
_ZN8bayesnet5TANLdD0Ev2
_ZN8bayesnet5TANLdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/TANLd.h.gcov.html b/html/bayesnet/classifiers/TANLd.h.gcov.html new file mode 100644 index 0000000..eb76b52 --- /dev/null +++ b/html/bayesnet/classifiers/TANLd.h.gcov.html @@ -0,0 +1,100 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers/TANLd.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiers - TANLd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef TANLD_H
+       8              : #define TANLD_H
+       9              : #include "TAN.h"
+      10              : #include "Proposal.h"
+      11              : 
+      12              : namespace bayesnet {
+      13              :     class TANLd : public TAN, public Proposal {
+      14              :     private:
+      15              :     public:
+      16              :         TANLd();
+      17            5 :         virtual ~TANLd() = default;
+      18              :         TANLd& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, map<std::string, std::vector<int>>& states) override;
+      19              :         std::vector<std::string> graph(const std::string& name = "TAN") const override;
+      20              :         torch::Tensor predict(torch::Tensor& X) override;
+      21              :         static inline std::string version() { return "0.0.1"; };
+      22              :     };
+      23              : }
+      24              : #endif // !TANLD_H
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/index-sort-f.html b/html/bayesnet/classifiers/index-sort-f.html new file mode 100644 index 0000000..10a5e27 --- /dev/null +++ b/html/bayesnet/classifiers/index-sort-f.html @@ -0,0 +1,273 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiersCoverageTotalHit
Test:coverage.infoLines:98.9 %369365
Test Date:2024-04-21 16:43:29Functions:97.4 %7674
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Classifier.h +
100.0%
+
100.0 %4480.0 %54
Proposal.cc +
97.7%97.7%
+
97.7 %868488.9 %98
KDB.h +
100.0%
+
100.0 %11100.0 %22
KDBLd.h +
100.0%
+
100.0 %11100.0 %22
SPODE.h +
100.0%
+
100.0 %11100.0 %22
SPODELd.h +
100.0%
+
100.0 %11100.0 %22
TAN.h +
100.0%
+
100.0 %11100.0 %22
TANLd.h +
100.0%
+
100.0 %11100.0 %22
SPODE.cc +
100.0%
+
100.0 %1010100.0 %33
KDBLd.cc +
100.0%
+
100.0 %1717100.0 %44
TAN.cc +
100.0%
+
100.0 %2323100.0 %44
TANLd.cc +
100.0%
+
100.0 %1717100.0 %44
KDB.cc +
96.3%96.3%
+
96.3 %5452100.0 %55
SPODELd.cc +
100.0%
+
100.0 %2626100.0 %66
Classifier.cc +
100.0%
+
100.0 %126126100.0 %2424
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/index-sort-l.html b/html/bayesnet/classifiers/index-sort-l.html new file mode 100644 index 0000000..5886fe8 --- /dev/null +++ b/html/bayesnet/classifiers/index-sort-l.html @@ -0,0 +1,273 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiersCoverageTotalHit
Test:coverage.infoLines:98.9 %369365
Test Date:2024-04-21 16:43:29Functions:97.4 %7674
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
KDB.cc +
96.3%96.3%
+
96.3 %5452100.0 %55
Proposal.cc +
97.7%97.7%
+
97.7 %868488.9 %98
KDB.h +
100.0%
+
100.0 %11100.0 %22
KDBLd.h +
100.0%
+
100.0 %11100.0 %22
SPODE.h +
100.0%
+
100.0 %11100.0 %22
SPODELd.h +
100.0%
+
100.0 %11100.0 %22
TAN.h +
100.0%
+
100.0 %11100.0 %22
TANLd.h +
100.0%
+
100.0 %11100.0 %22
Classifier.h +
100.0%
+
100.0 %4480.0 %54
SPODE.cc +
100.0%
+
100.0 %1010100.0 %33
KDBLd.cc +
100.0%
+
100.0 %1717100.0 %44
TANLd.cc +
100.0%
+
100.0 %1717100.0 %44
TAN.cc +
100.0%
+
100.0 %2323100.0 %44
SPODELd.cc +
100.0%
+
100.0 %2626100.0 %66
Classifier.cc +
100.0%
+
100.0 %126126100.0 %2424
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/classifiers/index.html b/html/bayesnet/classifiers/index.html new file mode 100644 index 0000000..9f03109 --- /dev/null +++ b/html/bayesnet/classifiers/index.html @@ -0,0 +1,273 @@ + + + + + + + LCOV - coverage.info - bayesnet/classifiers + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/classifiersCoverageTotalHit
Test:coverage.infoLines:98.9 %369365
Test Date:2024-04-21 16:43:29Functions:97.4 %7674
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Classifier.cc +
100.0%
+
100.0 %126126100.0 %2424
Classifier.h +
100.0%
+
100.0 %4480.0 %54
KDB.cc +
96.3%96.3%
+
96.3 %5452100.0 %55
KDB.h +
100.0%
+
100.0 %11100.0 %22
KDBLd.cc +
100.0%
+
100.0 %1717100.0 %44
KDBLd.h +
100.0%
+
100.0 %11100.0 %22
Proposal.cc +
97.7%97.7%
+
97.7 %868488.9 %98
SPODE.cc +
100.0%
+
100.0 %1010100.0 %33
SPODE.h +
100.0%
+
100.0 %11100.0 %22
SPODELd.cc +
100.0%
+
100.0 %2626100.0 %66
SPODELd.h +
100.0%
+
100.0 %11100.0 %22
TAN.cc +
100.0%
+
100.0 %2323100.0 %44
TAN.h +
100.0%
+
100.0 %11100.0 %22
TANLd.cc +
100.0%
+
100.0 %1717100.0 %44
TANLd.h +
100.0%
+
100.0 %11100.0 %22
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.cc.func-c.html b/html/bayesnet/ensembles/AODE.cc.func-c.html new file mode 100644 index 0000000..8420b9d --- /dev/null +++ b/html/bayesnet/ensembles/AODE.cc.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2020
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4AODE18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE1
_ZNK8bayesnet4AODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZN8bayesnet4AODE10buildModelERKN2at6TensorE6
_ZN8bayesnet4AODEC2Eb19
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.cc.func.html b/html/bayesnet/ensembles/AODE.cc.func.html new file mode 100644 index 0000000..f87dca6 --- /dev/null +++ b/html/bayesnet/ensembles/AODE.cc.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2020
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4AODE10buildModelERKN2at6TensorE6
_ZN8bayesnet4AODE18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE1
_ZN8bayesnet4AODEC2Eb19
_ZNK8bayesnet4AODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.cc.gcov.html b/html/bayesnet/ensembles/AODE.cc.gcov.html new file mode 100644 index 0000000..fc36dbe --- /dev/null +++ b/html/bayesnet/ensembles/AODE.cc.gcov.html @@ -0,0 +1,114 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2020
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "AODE.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           19 :     AODE::AODE(bool predict_voting) : Ensemble(predict_voting)
+      11              :     {
+      12           38 :         validHyperparameters = { "predict_voting" };
+      13              : 
+      14           57 :     }
+      15            1 :     void AODE::setHyperparameters(const nlohmann::json& hyperparameters_)
+      16              :     {
+      17            1 :         auto hyperparameters = hyperparameters_;
+      18            1 :         if (hyperparameters.contains("predict_voting")) {
+      19            1 :             predict_voting = hyperparameters["predict_voting"];
+      20            1 :             hyperparameters.erase("predict_voting");
+      21              :         }
+      22            1 :         Classifier::setHyperparameters(hyperparameters);
+      23            1 :     }
+      24            6 :     void AODE::buildModel(const torch::Tensor& weights)
+      25              :     {
+      26            6 :         models.clear();
+      27            6 :         significanceModels.clear();
+      28           47 :         for (int i = 0; i < features.size(); ++i) {
+      29           41 :             models.push_back(std::make_unique<SPODE>(i));
+      30              :         }
+      31            6 :         n_models = models.size();
+      32            6 :         significanceModels = std::vector<double>(n_models, 1.0);
+      33            6 :     }
+      34            1 :     std::vector<std::string> AODE::graph(const std::string& title) const
+      35              :     {
+      36            1 :         return Ensemble::graph(title);
+      37              :     }
+      38              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.h.func-c.html b/html/bayesnet/ensembles/AODE.h.func-c.html new file mode 100644 index 0000000..dc70522 --- /dev/null +++ b/html/bayesnet/ensembles/AODE.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4AODED0Ev7
_ZN8bayesnet4AODED0Ev2
_ZN8bayesnet4AODED2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.h.func.html b/html/bayesnet/ensembles/AODE.h.func.html new file mode 100644 index 0000000..d8cd0b6 --- /dev/null +++ b/html/bayesnet/ensembles/AODE.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4AODED0Ev7
_ZN8bayesnet4AODED0Ev2
_ZN8bayesnet4AODED2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODE.h.gcov.html b/html/bayesnet/ensembles/AODE.h.gcov.html new file mode 100644 index 0000000..2e1cc74 --- /dev/null +++ b/html/bayesnet/ensembles/AODE.h.gcov.html @@ -0,0 +1,98 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODE.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef AODE_H
+       8              : #define AODE_H
+       9              : #include "bayesnet/classifiers/SPODE.h"
+      10              : #include "Ensemble.h"
+      11              : namespace bayesnet {
+      12              :     class AODE : public Ensemble {
+      13              :     public:
+      14              :         AODE(bool predict_voting = false);
+      15            7 :         virtual ~AODE() {};
+      16              :         void setHyperparameters(const nlohmann::json& hyperparameters) override;
+      17              :         std::vector<std::string> graph(const std::string& title = "AODE") const override;
+      18              :     protected:
+      19              :         void buildModel(const torch::Tensor& weights) override;
+      20              :     };
+      21              : }
+      22              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.cc.func-c.html b/html/bayesnet/ensembles/AODELd.cc.func-c.html new file mode 100644 index 0000000..bbbf180 --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.cc.func-c.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2424
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet6AODELd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZN8bayesnet6AODELd10buildModelERKN2at6TensorE5
_ZN8bayesnet6AODELd10trainModelERKN2at6TensorE5
_ZN8bayesnet6AODELd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet6AODELdC2Eb17
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.cc.func.html b/html/bayesnet/ensembles/AODELd.cc.func.html new file mode 100644 index 0000000..81b9613 --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.cc.func.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2424
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet6AODELd10buildModelERKN2at6TensorE5
_ZN8bayesnet6AODELd10trainModelERKN2at6TensorE5
_ZN8bayesnet6AODELd3fitERN2at6TensorES3_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISA_EERKSA_RSt3mapISA_S4_IiSaIiEESt4lessISA_ESaISt4pairISF_SJ_EEE5
_ZN8bayesnet6AODELdC2Eb17
_ZNK8bayesnet6AODELd5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.cc.gcov.html b/html/bayesnet/ensembles/AODELd.cc.gcov.html new file mode 100644 index 0000000..476adf4 --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.cc.gcov.html @@ -0,0 +1,123 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %2424
Test Date:2024-04-21 16:43:29Functions:100.0 %55
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "AODELd.h"
+       8              : 
+       9              : namespace bayesnet {
+      10           17 :     AODELd::AODELd(bool predict_voting) : Ensemble(predict_voting), Proposal(dataset, features, className)
+      11              :     {
+      12           17 :     }
+      13            5 :     AODELd& AODELd::fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_)
+      14              :     {
+      15            5 :         checkInput(X_, y_);
+      16            5 :         features = features_;
+      17            5 :         className = className_;
+      18            5 :         Xf = X_;
+      19            5 :         y = y_;
+      20              :         // Fills std::vectors Xv & yv with the data from tensors X_ (discretized) & y
+      21            5 :         states = fit_local_discretization(y);
+      22              :         // We have discretized the input data
+      23              :         // 1st we need to fit the model to build the normal TAN structure, TAN::fit initializes the base Bayesian network
+      24            5 :         Ensemble::fit(dataset, features, className, states);
+      25            5 :         return *this;
+      26              : 
+      27              :     }
+      28            5 :     void AODELd::buildModel(const torch::Tensor& weights)
+      29              :     {
+      30            5 :         models.clear();
+      31           42 :         for (int i = 0; i < features.size(); ++i) {
+      32           37 :             models.push_back(std::make_unique<SPODELd>(i));
+      33              :         }
+      34            5 :         n_models = models.size();
+      35            5 :         significanceModels = std::vector<double>(n_models, 1.0);
+      36            5 :     }
+      37            5 :     void AODELd::trainModel(const torch::Tensor& weights)
+      38              :     {
+      39           42 :         for (const auto& model : models) {
+      40           37 :             model->fit(Xf, y, features, className, states);
+      41              :         }
+      42            5 :     }
+      43            1 :     std::vector<std::string> AODELd::graph(const std::string& name) const
+      44              :     {
+      45            1 :         return Ensemble::graph(name);
+      46              :     }
+      47              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.h.func-c.html b/html/bayesnet/ensembles/AODELd.h.func-c.html new file mode 100644 index 0000000..fc910db --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet6AODELdD0Ev5
_ZN8bayesnet6AODELdD0Ev2
_ZN8bayesnet6AODELdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.h.func.html b/html/bayesnet/ensembles/AODELd.h.func.html new file mode 100644 index 0000000..a450927 --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet6AODELdD0Ev5
_ZN8bayesnet6AODELdD0Ev2
_ZN8bayesnet6AODELdD2Ev3
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/AODELd.h.gcov.html b/html/bayesnet/ensembles/AODELd.h.gcov.html new file mode 100644 index 0000000..bb7bef6 --- /dev/null +++ b/html/bayesnet/ensembles/AODELd.h.gcov.html @@ -0,0 +1,101 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/AODELd.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - AODELd.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef AODELD_H
+       8              : #define AODELD_H
+       9              : #include "bayesnet/classifiers/Proposal.h"
+      10              : #include "bayesnet/classifiers/SPODELd.h"
+      11              : #include "Ensemble.h"
+      12              : 
+      13              : namespace bayesnet {
+      14              :     class AODELd : public Ensemble, public Proposal {
+      15              :     public:
+      16              :         AODELd(bool predict_voting = true);
+      17            5 :         virtual ~AODELd() = default;
+      18              :         AODELd& fit(torch::Tensor& X_, torch::Tensor& y_, const std::vector<std::string>& features_, const std::string& className_, map<std::string, std::vector<int>>& states_) override;
+      19              :         std::vector<std::string> graph(const std::string& name = "AODELd") const override;
+      20              :     protected:
+      21              :         void trainModel(const torch::Tensor& weights) override;
+      22              :         void buildModel(const torch::Tensor& weights) override;
+      23              :     };
+      24              : }
+      25              : #endif // !AODELD_H
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.cc.func-c.html b/html/bayesnet/ensembles/BoostAODE.cc.func-c.html new file mode 100644 index 0000000..6d9ab37 --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.cc.func-c.html @@ -0,0 +1,138 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:99.1 %218216
Test Date:2024-04-21 16:43:29Functions:100.0 %99
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet9BoostAODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZN8bayesnet9BoostAODE20update_weights_blockEiRN2at6TensorES3_5
_ZN8bayesnet9BoostAODE16initializeModelsEv8
_ZN8bayesnet9BoostAODE18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE20
_ZN8bayesnet9BoostAODE10buildModelERKN2at6TensorE21
_ZN8bayesnet9BoostAODE10trainModelERKN2at6TensorE21
_ZN8bayesnet9BoostAODEC2Eb41
_ZN8bayesnet14update_weightsERN2at6TensorES2_S2_108
_ZZN8bayesnet9BoostAODE10trainModelERKN2at6TensorEENKUlT_E_clIiEEDaS5_2691
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.cc.func.html b/html/bayesnet/ensembles/BoostAODE.cc.func.html new file mode 100644 index 0000000..66352ee --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.cc.func.html @@ -0,0 +1,138 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:99.1 %218216
Test Date:2024-04-21 16:43:29Functions:100.0 %99
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet14update_weightsERN2at6TensorES2_S2_108
_ZN8bayesnet9BoostAODE10buildModelERKN2at6TensorE21
_ZN8bayesnet9BoostAODE10trainModelERKN2at6TensorE21
_ZN8bayesnet9BoostAODE16initializeModelsEv8
_ZN8bayesnet9BoostAODE18setHyperparametersERKN8nlohmann16json_abi_v3_11_310basic_jsonISt3mapSt6vectorNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEblmdSaNS2_14adl_serializerES5_IhSaIhEEvEE20
_ZN8bayesnet9BoostAODE20update_weights_blockEiRN2at6TensorES3_5
_ZN8bayesnet9BoostAODEC2Eb41
_ZNK8bayesnet9BoostAODE5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE1
_ZZN8bayesnet9BoostAODE10trainModelERKN2at6TensorEENKUlT_E_clIiEEDaS5_2691
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.cc.gcov.html b/html/bayesnet/ensembles/BoostAODE.cc.gcov.html new file mode 100644 index 0000000..7c1f567 --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.cc.gcov.html @@ -0,0 +1,449 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:99.1 %218216
Test Date:2024-04-21 16:43:29Functions:100.0 %99
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <set>
+       8              : #include <functional>
+       9              : #include <limits.h>
+      10              : #include <tuple>
+      11              : #include <folding.hpp>
+      12              : #include "bayesnet/feature_selection/CFS.h"
+      13              : #include "bayesnet/feature_selection/FCBF.h"
+      14              : #include "bayesnet/feature_selection/IWSS.h"
+      15              : #include "BoostAODE.h"
+      16              : 
+      17              : namespace bayesnet {
+      18              : 
+      19           41 :     BoostAODE::BoostAODE(bool predict_voting) : Ensemble(predict_voting)
+      20              :     {
+      21          410 :         validHyperparameters = {
+      22              :             "maxModels", "bisection", "order", "convergence", "threshold",
+      23              :             "select_features", "maxTolerance", "predict_voting", "block_update"
+      24          410 :         };
+      25              : 
+      26          123 :     }
+      27           21 :     void BoostAODE::buildModel(const torch::Tensor& weights)
+      28              :     {
+      29              :         // Models shall be built in trainModel
+      30           21 :         models.clear();
+      31           21 :         significanceModels.clear();
+      32           21 :         n_models = 0;
+      33              :         // Prepare the validation dataset
+      34           63 :         auto y_ = dataset.index({ -1, "..." });
+      35           21 :         if (convergence) {
+      36              :             // Prepare train & validation sets from train data
+      37           17 :             auto fold = folding::StratifiedKFold(5, y_, 271);
+      38           17 :             auto [train, test] = fold.getFold(0);
+      39           17 :             auto train_t = torch::tensor(train);
+      40           17 :             auto test_t = torch::tensor(test);
+      41              :             // Get train and validation sets
+      42           85 :             X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), train_t });
+      43           51 :             y_train = dataset.index({ -1, train_t });
+      44           85 :             X_test = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), test_t });
+      45           51 :             y_test = dataset.index({ -1, test_t });
+      46           17 :             dataset = X_train;
+      47           17 :             m = X_train.size(1);
+      48           17 :             auto n_classes = states.at(className).size();
+      49              :             // Build dataset with train data
+      50           17 :             buildDataset(y_train);
+      51           17 :             metrics = Metrics(dataset, features, className, n_classes);
+      52           17 :         } else {
+      53              :             // Use all data to train
+      54           16 :             X_train = dataset.index({ torch::indexing::Slice(0, dataset.size(0) - 1), "..." });
+      55            4 :             y_train = y_;
+      56              :         }
+      57          203 :     }
+      58           20 :     void BoostAODE::setHyperparameters(const nlohmann::json& hyperparameters_)
+      59              :     {
+      60           20 :         auto hyperparameters = hyperparameters_;
+      61           20 :         if (hyperparameters.contains("order")) {
+      62           25 :             std::vector<std::string> algos = { Orders.ASC, Orders.DESC, Orders.RAND };
+      63            5 :             order_algorithm = hyperparameters["order"];
+      64            5 :             if (std::find(algos.begin(), algos.end(), order_algorithm) == algos.end()) {
+      65            1 :                 throw std::invalid_argument("Invalid order algorithm, valid values [" + Orders.ASC + ", " + Orders.DESC + ", " + Orders.RAND + "]");
+      66              :             }
+      67            4 :             hyperparameters.erase("order");
+      68            5 :         }
+      69           19 :         if (hyperparameters.contains("convergence")) {
+      70            7 :             convergence = hyperparameters["convergence"];
+      71            7 :             hyperparameters.erase("convergence");
+      72              :         }
+      73           19 :         if (hyperparameters.contains("bisection")) {
+      74            6 :             bisection = hyperparameters["bisection"];
+      75            6 :             hyperparameters.erase("bisection");
+      76              :         }
+      77           19 :         if (hyperparameters.contains("threshold")) {
+      78            6 :             threshold = hyperparameters["threshold"];
+      79            6 :             hyperparameters.erase("threshold");
+      80              :         }
+      81           19 :         if (hyperparameters.contains("maxTolerance")) {
+      82            9 :             maxTolerance = hyperparameters["maxTolerance"];
+      83            9 :             if (maxTolerance < 1 || maxTolerance > 4)
+      84            3 :                 throw std::invalid_argument("Invalid maxTolerance value, must be greater in [1, 4]");
+      85            6 :             hyperparameters.erase("maxTolerance");
+      86              :         }
+      87           16 :         if (hyperparameters.contains("predict_voting")) {
+      88            1 :             predict_voting = hyperparameters["predict_voting"];
+      89            1 :             hyperparameters.erase("predict_voting");
+      90              :         }
+      91           16 :         if (hyperparameters.contains("select_features")) {
+      92            9 :             auto selectedAlgorithm = hyperparameters["select_features"];
+      93           45 :             std::vector<std::string> algos = { SelectFeatures.IWSS, SelectFeatures.CFS, SelectFeatures.FCBF };
+      94            9 :             selectFeatures = true;
+      95            9 :             select_features_algorithm = selectedAlgorithm;
+      96            9 :             if (std::find(algos.begin(), algos.end(), selectedAlgorithm) == algos.end()) {
+      97            1 :                 throw std::invalid_argument("Invalid selectFeatures value, valid values [" + SelectFeatures.IWSS + ", " + SelectFeatures.CFS + ", " + SelectFeatures.FCBF + "]");
+      98              :             }
+      99            8 :             hyperparameters.erase("select_features");
+     100           10 :         }
+     101           15 :         if (hyperparameters.contains("block_update")) {
+     102            2 :             block_update = hyperparameters["block_update"];
+     103            2 :             hyperparameters.erase("block_update");
+     104              :         }
+     105           15 :         Classifier::setHyperparameters(hyperparameters);
+     106           34 :     }
+     107          108 :     std::tuple<torch::Tensor&, double, bool> update_weights(torch::Tensor& ytrain, torch::Tensor& ypred, torch::Tensor& weights)
+     108              :     {
+     109          108 :         bool terminate = false;
+     110          108 :         double alpha_t = 0;
+     111          108 :         auto mask_wrong = ypred != ytrain;
+     112          108 :         auto mask_right = ypred == ytrain;
+     113          108 :         auto masked_weights = weights * mask_wrong.to(weights.dtype());
+     114          108 :         double epsilon_t = masked_weights.sum().item<double>();
+     115          108 :         if (epsilon_t > 0.5) {
+     116              :             // Inverse the weights policy (plot ln(wt))
+     117              :             // "In each round of AdaBoost, there is a sanity check to ensure that the current base 
+     118              :             // learner is better than random guess" (Zhi-Hua Zhou, 2012)
+     119            4 :             terminate = true;
+     120              :         } else {
+     121          104 :             double wt = (1 - epsilon_t) / epsilon_t;
+     122          104 :             alpha_t = epsilon_t == 0 ? 1 : 0.5 * log(wt);
+     123              :             // Step 3.2: Update weights for next classifier
+     124              :             // Step 3.2.1: Update weights of wrong samples
+     125          104 :             weights += mask_wrong.to(weights.dtype()) * exp(alpha_t) * weights;
+     126              :             // Step 3.2.2: Update weights of right samples
+     127          104 :             weights += mask_right.to(weights.dtype()) * exp(-alpha_t) * weights;
+     128              :             // Step 3.3: Normalise the weights
+     129          104 :             double totalWeights = torch::sum(weights).item<double>();
+     130          104 :             weights = weights / totalWeights;
+     131              :         }
+     132          216 :         return { weights, alpha_t, terminate };
+     133          108 :     }
+     134            5 :     std::tuple<torch::Tensor&, double, bool> BoostAODE::update_weights_block(int k, torch::Tensor& ytrain, torch::Tensor& weights)
+     135              :     {
+     136              :         /* Update Block algorithm
+     137              :             k = # of models in block
+     138              :             n_models = # of models in ensemble to make predictions
+     139              :             n_models_bak = # models saved
+     140              :             models = vector of models to make predictions
+     141              :             models_bak = models not used to make predictions
+     142              :             significances_bak = backup of significances vector
+     143              : 
+     144              :             Case list
+     145              :             A) k = 1, n_models = 1              => n = 0 , n_models = n + k
+     146              :             B) k = 1, n_models = n + 1  => n_models = n + k
+     147              :             C) k > 1, n_models = k + 1       => n= 1, n_models = n + k
+     148              :             D) k > 1, n_models = k           => n = 0, n_models = n + k
+     149              :             E) k > 1, n_models = k + n       => n_models = n + k
+     150              : 
+     151              :             A, D) n=0, k > 0, n_models == k
+     152              :             1. n_models_bak <- n_models
+     153              :             2. significances_bak <- significances
+     154              :             3. significances = vector(k, 1)
+     155              :             4. Don’t move any classifiers out of models
+     156              :             5. n_models <- k
+     157              :             6. Make prediction, compute alpha, update weights
+     158              :             7. Don’t restore any classifiers to models
+     159              :             8. significances <- significances_bak
+     160              :             9. Update last k significances
+     161              :             10. n_models <- n_models_bak
+     162              : 
+     163              :             B, C, E) n > 0, k > 0, n_models == n + k
+     164              :             1. n_models_bak <- n_models
+     165              :             2. significances_bak <- significances
+     166              :             3. significances = vector(k, 1)
+     167              :             4. Move first n classifiers to models_bak
+     168              :             5. n_models <- k
+     169              :             6. Make prediction, compute alpha, update weights
+     170              :             7. Insert classifiers in models_bak to be the first n models
+     171              :             8. significances <- significances_bak
+     172              :             9. Update last k significances
+     173              :             10. n_models <- n_models_bak
+     174              :         */
+     175              :         //
+     176              :         // Make predict with only the last k models
+     177              :         //
+     178            5 :         std::unique_ptr<Classifier> model;
+     179            5 :         std::vector<std::unique_ptr<Classifier>> models_bak;
+     180              :         // 1. n_models_bak <- n_models 2. significances_bak <- significances
+     181            5 :         auto significance_bak = significanceModels;
+     182            5 :         auto n_models_bak = n_models;
+     183              :         // 3. significances = vector(k, 1)
+     184            5 :         significanceModels = std::vector<double>(k, 1.0);
+     185              :         // 4. Move first n classifiers to models_bak
+     186              :         // backup the first n_models - k models (if n_models == k, don't backup any)
+     187           20 :         for (int i = 0; i < n_models - k; ++i) {
+     188           15 :             model = std::move(models[0]);
+     189           15 :             models.erase(models.begin());
+     190           15 :             models_bak.push_back(std::move(model));
+     191              :         }
+     192            5 :         assert(models.size() == k);
+     193              :         // 5. n_models <- k
+     194            5 :         n_models = k;
+     195              :         // 6. Make prediction, compute alpha, update weights
+     196            5 :         auto ypred = predict(X_train);
+     197              :         //
+     198              :         // Update weights
+     199              :         //
+     200              :         double alpha_t;
+     201              :         bool terminate;
+     202            5 :         std::tie(weights, alpha_t, terminate) = update_weights(y_train, ypred, weights);
+     203              :         //
+     204              :         // Restore the models if needed
+     205              :         //
+     206              :         // 7. Insert classifiers in models_bak to be the first n models
+     207              :         // if n_models_bak == k, don't restore any, because none of them were moved
+     208            5 :         if (k != n_models_bak) {
+     209              :             // Insert in the same order as they were extracted
+     210            4 :             int bak_size = models_bak.size();
+     211           19 :             for (int i = 0; i < bak_size; ++i) {
+     212           15 :                 model = std::move(models_bak[bak_size - 1 - i]);
+     213           15 :                 models_bak.erase(models_bak.end() - 1);
+     214           15 :                 models.insert(models.begin(), std::move(model));
+     215              :             }
+     216              :         }
+     217              :         // 8. significances <- significances_bak
+     218            5 :         significanceModels = significance_bak;
+     219              :         //
+     220              :         // Update the significance of the last k models
+     221              :         //
+     222              :         // 9. Update last k significances
+     223           21 :         for (int i = 0; i < k; ++i) {
+     224           16 :             significanceModels[n_models_bak - k + i] = alpha_t;
+     225              :         }
+     226              :         // 10. n_models <- n_models_bak
+     227            5 :         n_models = n_models_bak;
+     228           10 :         return { weights, alpha_t, terminate };
+     229            5 :     }
+     230            8 :     std::vector<int> BoostAODE::initializeModels()
+     231              :     {
+     232            8 :         std::vector<int> featuresUsed;
+     233            8 :         torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
+     234            8 :         int maxFeatures = 0;
+     235            8 :         if (select_features_algorithm == SelectFeatures.CFS) {
+     236            2 :             featureSelector = new CFS(dataset, features, className, maxFeatures, states.at(className).size(), weights_);
+     237            6 :         } else if (select_features_algorithm == SelectFeatures.IWSS) {
+     238            3 :             if (threshold < 0 || threshold >0.5) {
+     239            2 :                 throw std::invalid_argument("Invalid threshold value for " + SelectFeatures.IWSS + " [0, 0.5]");
+     240              :             }
+     241            1 :             featureSelector = new IWSS(dataset, features, className, maxFeatures, states.at(className).size(), weights_, threshold);
+     242            3 :         } else if (select_features_algorithm == SelectFeatures.FCBF) {
+     243            3 :             if (threshold < 1e-7 || threshold > 1) {
+     244            2 :                 throw std::invalid_argument("Invalid threshold value for " + SelectFeatures.FCBF + " [1e-7, 1]");
+     245              :             }
+     246            1 :             featureSelector = new FCBF(dataset, features, className, maxFeatures, states.at(className).size(), weights_, threshold);
+     247              :         }
+     248            4 :         featureSelector->fit();
+     249            4 :         auto cfsFeatures = featureSelector->getFeatures();
+     250            4 :         auto scores = featureSelector->getScores();
+     251           25 :         for (const int& feature : cfsFeatures) {
+     252           21 :             featuresUsed.push_back(feature);
+     253           21 :             std::unique_ptr<Classifier> model = std::make_unique<SPODE>(feature);
+     254           21 :             model->fit(dataset, features, className, states, weights_);
+     255           21 :             models.push_back(std::move(model));
+     256           21 :             significanceModels.push_back(1.0); // They will be updated later in trainModel
+     257           21 :             n_models++;
+     258           21 :         }
+     259            4 :         notes.push_back("Used features in initialization: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size()) + " with " + select_features_algorithm);
+     260            4 :         delete featureSelector;
+     261            8 :         return featuresUsed;
+     262           12 :     }
+     263           21 :     void BoostAODE::trainModel(const torch::Tensor& weights)
+     264              :     {
+     265              :         // Algorithm based on the adaboost algorithm for classification
+     266              :         // as explained in Ensemble methods (Zhi-Hua Zhou, 2012)
+     267           21 :         fitted = true;
+     268           21 :         double alpha_t = 0;
+     269           21 :         torch::Tensor weights_ = torch::full({ m }, 1.0 / m, torch::kFloat64);
+     270           21 :         bool finished = false;
+     271           21 :         std::vector<int> featuresUsed;
+     272           21 :         if (selectFeatures) {
+     273            8 :             featuresUsed = initializeModels();
+     274            4 :             auto ypred = predict(X_train);
+     275            4 :             std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
+     276              :             // Update significance of the models
+     277           25 :             for (int i = 0; i < n_models; ++i) {
+     278           21 :                 significanceModels[i] = alpha_t;
+     279              :             }
+     280            4 :             if (finished) {
+     281            0 :                 return;
+     282              :             }
+     283            4 :         }
+     284           17 :         int numItemsPack = 0; // The counter of the models inserted in the current pack
+     285              :         // Variables to control the accuracy finish condition
+     286           17 :         double priorAccuracy = 0.0;
+     287           17 :         double improvement = 1.0;
+     288           17 :         double convergence_threshold = 1e-4;
+     289           17 :         int tolerance = 0; // number of times the accuracy is lower than the convergence_threshold
+     290              :         // Step 0: Set the finish condition
+     291              :         // epsilon sub t > 0.5 => inverse the weights policy
+     292              :         // validation error is not decreasing
+     293              :         // run out of features
+     294           17 :         bool ascending = order_algorithm == Orders.ASC;
+     295           17 :         std::mt19937 g{ 173 };
+     296           99 :         while (!finished) {
+     297              :             // Step 1: Build ranking with mutual information
+     298           82 :             auto featureSelection = metrics.SelectKBestWeighted(weights_, ascending, n); // Get all the features sorted
+     299           82 :             if (order_algorithm == Orders.RAND) {
+     300            9 :                 std::shuffle(featureSelection.begin(), featureSelection.end(), g);
+     301              :             }
+     302              :             // Remove used features
+     303          164 :             featureSelection.erase(remove_if(begin(featureSelection), end(featureSelection), [&](auto x)
+     304        10764 :                 { return std::find(begin(featuresUsed), end(featuresUsed), x) != end(featuresUsed);}),
+     305           82 :                 end(featureSelection)
+     306              :             );
+     307           82 :             int k = pow(2, tolerance);
+     308           82 :             int counter = 0; // The model counter of the current pack
+     309          197 :             while (counter++ < k && featureSelection.size() > 0) {
+     310          115 :                 auto feature = featureSelection[0];
+     311          115 :                 featureSelection.erase(featureSelection.begin());
+     312          115 :                 std::unique_ptr<Classifier> model;
+     313          115 :                 model = std::make_unique<SPODE>(feature);
+     314          115 :                 model->fit(dataset, features, className, states, weights_);
+     315          115 :                 alpha_t = 0.0;
+     316          115 :                 if (!block_update) {
+     317           99 :                     auto ypred = model->predict(X_train);
+     318              :                     // Step 3.1: Compute the classifier amout of say
+     319           99 :                     std::tie(weights_, alpha_t, finished) = update_weights(y_train, ypred, weights_);
+     320           99 :                 }
+     321              :                 // Step 3.4: Store classifier and its accuracy to weigh its future vote
+     322          115 :                 numItemsPack++;
+     323          115 :                 featuresUsed.push_back(feature);
+     324          115 :                 models.push_back(std::move(model));
+     325          115 :                 significanceModels.push_back(alpha_t);
+     326          115 :                 n_models++;
+     327          115 :             }
+     328           82 :             if (block_update) {
+     329            5 :                 std::tie(weights_, alpha_t, finished) = update_weights_block(k, y_train, weights_);
+     330              :             }
+     331           82 :             if (convergence && !finished) {
+     332           49 :                 auto y_val_predict = predict(X_test);
+     333           49 :                 double accuracy = (y_val_predict == y_test).sum().item<double>() / (double)y_test.size(0);
+     334           49 :                 if (priorAccuracy == 0) {
+     335           13 :                     priorAccuracy = accuracy;
+     336              :                 } else {
+     337           36 :                     improvement = accuracy - priorAccuracy;
+     338              :                 }
+     339           49 :                 if (improvement < convergence_threshold) {
+     340           32 :                     tolerance++;
+     341              :                 } else {
+     342           17 :                     tolerance = 0; // Reset the counter if the model performs better
+     343           17 :                     numItemsPack = 0;
+     344              :                 }
+     345              :                 // Keep the best accuracy until now as the prior accuracy
+     346           49 :                 priorAccuracy = std::max(accuracy, priorAccuracy);
+     347              :                 // priorAccuracy = accuracy;
+     348           49 :             }
+     349           82 :             finished = finished || tolerance > maxTolerance || featuresUsed.size() == features.size();
+     350           82 :         }
+     351           17 :         if (tolerance > maxTolerance) {
+     352            2 :             if (numItemsPack < n_models) {
+     353            2 :                 notes.push_back("Convergence threshold reached & " + std::to_string(numItemsPack) + " models eliminated");
+     354           32 :                 for (int i = 0; i < numItemsPack; ++i) {
+     355           30 :                     significanceModels.pop_back();
+     356           30 :                     models.pop_back();
+     357           30 :                     n_models--;
+     358              :                 }
+     359              :             } else {
+     360            0 :                 notes.push_back("Convergence threshold reached & 0 models eliminated");
+     361              :             }
+     362              :         }
+     363           17 :         if (featuresUsed.size() != features.size()) {
+     364            2 :             notes.push_back("Used features in train: " + std::to_string(featuresUsed.size()) + " of " + std::to_string(features.size()));
+     365            2 :             status = WARNING;
+     366              :         }
+     367           17 :         notes.push_back("Number of models: " + std::to_string(n_models));
+     368           25 :     }
+     369            1 :     std::vector<std::string> BoostAODE::graph(const std::string& title) const
+     370              :     {
+     371            1 :         return Ensemble::graph(title);
+     372              :     }
+     373              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.h.func-c.html b/html/bayesnet/ensembles/BoostAODE.h.func-c.html new file mode 100644 index 0000000..736efb7 --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet9BoostAODED0Ev21
_ZN8bayesnet9BoostAODED0Ev2
_ZN8bayesnet9BoostAODED2Ev19
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.h.func.html b/html/bayesnet/ensembles/BoostAODE.h.func.html new file mode 100644 index 0000000..eafa4c6 --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet9BoostAODED0Ev21
_ZN8bayesnet9BoostAODED0Ev2
_ZN8bayesnet9BoostAODED2Ev19
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/BoostAODE.h.gcov.html b/html/bayesnet/ensembles/BoostAODE.h.gcov.html new file mode 100644 index 0000000..86934e5 --- /dev/null +++ b/html/bayesnet/ensembles/BoostAODE.h.gcov.html @@ -0,0 +1,125 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/BoostAODE.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - BoostAODE.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef BOOSTAODE_H
+       8              : #define BOOSTAODE_H
+       9              : #include <map>
+      10              : #include "bayesnet/classifiers/SPODE.h"
+      11              : #include "bayesnet/feature_selection/FeatureSelect.h"
+      12              : #include "Ensemble.h"
+      13              : namespace bayesnet {
+      14              :     struct {
+      15              :         std::string CFS = "CFS";
+      16              :         std::string FCBF = "FCBF";
+      17              :         std::string IWSS = "IWSS";
+      18              :     }SelectFeatures;
+      19              :     struct {
+      20              :         std::string ASC = "asc";
+      21              :         std::string DESC = "desc";
+      22              :         std::string RAND = "rand";
+      23              :     }Orders;
+      24              :     class BoostAODE : public Ensemble {
+      25              :     public:
+      26              :         BoostAODE(bool predict_voting = false);
+      27           21 :         virtual ~BoostAODE() = default;
+      28              :         std::vector<std::string> graph(const std::string& title = "BoostAODE") const override;
+      29              :         void setHyperparameters(const nlohmann::json& hyperparameters_) override;
+      30              :     protected:
+      31              :         void buildModel(const torch::Tensor& weights) override;
+      32              :         void trainModel(const torch::Tensor& weights) override;
+      33              :     private:
+      34              :         std::tuple<torch::Tensor&, double, bool> update_weights_block(int k, torch::Tensor& ytrain, torch::Tensor& weights);
+      35              :         std::vector<int> initializeModels();
+      36              :         torch::Tensor X_train, y_train, X_test, y_test;
+      37              :         // Hyperparameters
+      38              :         bool bisection = true; // if true, use bisection stratety to add k models at once to the ensemble
+      39              :         int maxTolerance = 3;
+      40              :         std::string order_algorithm; // order to process the KBest features asc, desc, rand
+      41              :         bool convergence = true; //if true, stop when the model does not improve
+      42              :         bool selectFeatures = false; // if true, use feature selection
+      43              :         std::string select_features_algorithm = Orders.DESC; // Selected feature selection algorithm
+      44              :         FeatureSelect* featureSelector = nullptr;
+      45              :         double threshold = -1;
+      46              :         bool block_update = false;
+      47              :     };
+      48              : }
+      49              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.cc.func-c.html b/html/bayesnet/ensembles/Ensemble.cc.func-c.html new file mode 100644 index 0000000..76197b5 --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.cc.func-c.html @@ -0,0 +1,250 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:98.1 %154151
Test Date:2024-04-21 16:43:29Functions:100.0 %2525
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet8Ensemble17getNumberOfStatesEv1
_ZNK8bayesnet8Ensemble4showB5cxx11Ev1
_ZNK8bayesnet8Ensemble5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE3
_ZN8bayesnet8Ensemble10trainModelERKN2at6TensorE6
_ZNK8bayesnet8Ensemble16getNumberOfEdgesEv6
_ZNK8bayesnet8Ensemble16getNumberOfNodesEv6
_ZN8bayesnet8Ensemble22predict_average_votingERSt6vectorIS1_IiSaIiEESaIS3_EE7
_ZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EE11
_ZN8bayesnet8Ensemble5scoreERSt6vectorIS1_IiSaIiEESaIS3_EERS3_12
_ZN8bayesnet8Ensemble15compute_arg_maxERSt6vectorIS1_IdSaIdEESaIS3_EE13
_ZN8bayesnet8Ensemble5scoreERN2at6TensorES3_16
_ZN8bayesnet8Ensemble7predictERSt6vectorIS1_IiSaIiEESaIS3_EE16
_ZN8bayesnet8Ensemble22predict_average_votingERN2at6TensorE20
_ZN8bayesnet8Ensemble6votingERN2at6TensorE20
_ZN8bayesnet8Ensemble13predict_probaERSt6vectorIS1_IiSaIiEESaIS3_EE24
_ZN8bayesnet8Ensemble21predict_average_probaERN2at6TensorE63
_ZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUlvE_clEv63
_ZN8bayesnet8Ensemble15compute_arg_maxERN2at6TensorE75
_ZN8bayesnet8EnsembleC2Eb77
_ZN8bayesnet8Ensemble7predictERN2at6TensorE78
_ZN8bayesnet8Ensemble13predict_probaERN2at6TensorE82
_ZZN8bayesnet8Ensemble22predict_average_votingERN2at6TensorEENKUlvE_clEv114
_ZZN8bayesnet8Ensemble21predict_average_probaERN2at6TensorEENKUlvE_clEv302
_ZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUldE_clEd8606
_ZZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUlvE_clEvENKUlddE_clEdd65366
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.cc.func.html b/html/bayesnet/ensembles/Ensemble.cc.func.html new file mode 100644 index 0000000..7959c55 --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.cc.func.html @@ -0,0 +1,250 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:98.1 %154151
Test Date:2024-04-21 16:43:29Functions:100.0 %2525
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet8Ensemble10trainModelERKN2at6TensorE6
_ZN8bayesnet8Ensemble13predict_probaERN2at6TensorE82
_ZN8bayesnet8Ensemble13predict_probaERSt6vectorIS1_IiSaIiEESaIS3_EE24
_ZN8bayesnet8Ensemble15compute_arg_maxERN2at6TensorE75
_ZN8bayesnet8Ensemble15compute_arg_maxERSt6vectorIS1_IdSaIdEESaIS3_EE13
_ZN8bayesnet8Ensemble21predict_average_probaERN2at6TensorE63
_ZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EE11
_ZN8bayesnet8Ensemble22predict_average_votingERN2at6TensorE20
_ZN8bayesnet8Ensemble22predict_average_votingERSt6vectorIS1_IiSaIiEESaIS3_EE7
_ZN8bayesnet8Ensemble5scoreERN2at6TensorES3_16
_ZN8bayesnet8Ensemble5scoreERSt6vectorIS1_IiSaIiEESaIS3_EERS3_12
_ZN8bayesnet8Ensemble6votingERN2at6TensorE20
_ZN8bayesnet8Ensemble7predictERN2at6TensorE78
_ZN8bayesnet8Ensemble7predictERSt6vectorIS1_IiSaIiEESaIS3_EE16
_ZN8bayesnet8EnsembleC2Eb77
_ZNK8bayesnet8Ensemble16getNumberOfEdgesEv6
_ZNK8bayesnet8Ensemble16getNumberOfNodesEv6
_ZNK8bayesnet8Ensemble17getNumberOfStatesEv1
_ZNK8bayesnet8Ensemble4showB5cxx11Ev1
_ZNK8bayesnet8Ensemble5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE3
_ZZN8bayesnet8Ensemble21predict_average_probaERN2at6TensorEENKUlvE_clEv302
_ZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUldE_clEd8606
_ZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUlvE_clEv63
_ZZN8bayesnet8Ensemble22predict_average_votingERN2at6TensorEENKUlvE_clEv114
_ZZZN8bayesnet8Ensemble21predict_average_probaERSt6vectorIS1_IiSaIiEESaIS3_EEENKUlvE_clEvENKUlddE_clEdd65366
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.cc.gcov.html b/html/bayesnet/ensembles/Ensemble.cc.gcov.html new file mode 100644 index 0000000..7e29967 --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.cc.gcov.html @@ -0,0 +1,298 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:98.1 %154151
Test Date:2024-04-21 16:43:29Functions:100.0 %2525
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "Ensemble.h"
+       8              : 
+       9              : namespace bayesnet {
+      10              : 
+      11           77 :     Ensemble::Ensemble(bool predict_voting) : Classifier(Network()), n_models(0), predict_voting(predict_voting)
+      12              :     {
+      13              : 
+      14           77 :     };
+      15              :     const std::string ENSEMBLE_NOT_FITTED = "Ensemble has not been fitted";
+      16            6 :     void Ensemble::trainModel(const torch::Tensor& weights)
+      17              :     {
+      18            6 :         n_models = models.size();
+      19           47 :         for (auto i = 0; i < n_models; ++i) {
+      20              :             // fit with std::vectors
+      21           41 :             models[i]->fit(dataset, features, className, states);
+      22              :         }
+      23            6 :     }
+      24           13 :     std::vector<int> Ensemble::compute_arg_max(std::vector<std::vector<double>>& X)
+      25              :     {
+      26           13 :         std::vector<int> y_pred;
+      27         2843 :         for (auto i = 0; i < X.size(); ++i) {
+      28         2830 :             auto max = std::max_element(X[i].begin(), X[i].end());
+      29         5660 :             y_pred.push_back(std::distance(X[i].begin(), max));
+      30              :         }
+      31           13 :         return y_pred;
+      32            0 :     }
+      33           75 :     torch::Tensor Ensemble::compute_arg_max(torch::Tensor& X)
+      34              :     {
+      35           75 :         auto y_pred = torch::argmax(X, 1);
+      36           75 :         return y_pred;
+      37              :     }
+      38           20 :     torch::Tensor Ensemble::voting(torch::Tensor& votes)
+      39              :     {
+      40              :         // Convert m x n_models tensor to a m x n_class_states with voting probabilities
+      41           20 :         auto y_pred_ = votes.accessor<int, 2>();
+      42           20 :         std::vector<int> y_pred_final;
+      43           20 :         int numClasses = states.at(className).size();
+      44              :         // votes is m x n_models with the prediction of every model for each sample
+      45           20 :         auto result = torch::zeros({ votes.size(0), numClasses }, torch::kFloat32);
+      46           20 :         auto sum = std::reduce(significanceModels.begin(), significanceModels.end());
+      47         5364 :         for (int i = 0; i < votes.size(0); ++i) {
+      48              :             // n_votes store in each index (value of class) the significance added by each model
+      49              :             // i.e. n_votes[0] contains how much value has the value 0 of class. That value is generated by the models predictions
+      50         5344 :             std::vector<double> n_votes(numClasses, 0.0);
+      51        42310 :             for (int j = 0; j < n_models; ++j) {
+      52        36966 :                 n_votes[y_pred_[i][j]] += significanceModels.at(j);
+      53              :             }
+      54         5344 :             result[i] = torch::tensor(n_votes);
+      55         5344 :         }
+      56              :         // To only do one division and gain precision
+      57           20 :         result /= sum;
+      58           40 :         return result;
+      59           20 :     }
+      60           24 :     std::vector<std::vector<double>> Ensemble::predict_proba(std::vector<std::vector<int>>& X)
+      61              :     {
+      62           24 :         if (!fitted) {
+      63            6 :             throw std::logic_error(ENSEMBLE_NOT_FITTED);
+      64              :         }
+      65           18 :         return predict_voting ? predict_average_voting(X) : predict_average_proba(X);
+      66              :     }
+      67           82 :     torch::Tensor Ensemble::predict_proba(torch::Tensor& X)
+      68              :     {
+      69           82 :         if (!fitted) {
+      70            6 :             throw std::logic_error(ENSEMBLE_NOT_FITTED);
+      71              :         }
+      72           76 :         return predict_voting ? predict_average_voting(X) : predict_average_proba(X);
+      73              :     }
+      74           16 :     std::vector<int> Ensemble::predict(std::vector<std::vector<int>>& X)
+      75              :     {
+      76           16 :         auto res = predict_proba(X);
+      77           24 :         return compute_arg_max(res);
+      78           12 :     }
+      79           78 :     torch::Tensor Ensemble::predict(torch::Tensor& X)
+      80              :     {
+      81           78 :         auto res = predict_proba(X);
+      82          148 :         return compute_arg_max(res);
+      83           74 :     }
+      84           63 :     torch::Tensor Ensemble::predict_average_proba(torch::Tensor& X)
+      85              :     {
+      86           63 :         auto n_states = models[0]->getClassNumStates();
+      87           63 :         torch::Tensor y_pred = torch::zeros({ X.size(1), n_states }, torch::kFloat32);
+      88           63 :         auto threads{ std::vector<std::thread>() };
+      89           63 :         std::mutex mtx;
+      90          365 :         for (auto i = 0; i < n_models; ++i) {
+      91          302 :             threads.push_back(std::thread([&, i]() {
+      92          302 :                 auto ypredict = models[i]->predict_proba(X);
+      93          302 :                 std::lock_guard<std::mutex> lock(mtx);
+      94          302 :                 y_pred += ypredict * significanceModels[i];
+      95          302 :                 }));
+      96              :         }
+      97          365 :         for (auto& thread : threads) {
+      98          302 :             thread.join();
+      99              :         }
+     100           63 :         auto sum = std::reduce(significanceModels.begin(), significanceModels.end());
+     101           63 :         y_pred /= sum;
+     102          126 :         return y_pred;
+     103           63 :     }
+     104           11 :     std::vector<std::vector<double>> Ensemble::predict_average_proba(std::vector<std::vector<int>>& X)
+     105              :     {
+     106           11 :         auto n_states = models[0]->getClassNumStates();
+     107           11 :         std::vector<std::vector<double>> y_pred(X[0].size(), std::vector<double>(n_states, 0.0));
+     108           11 :         auto threads{ std::vector<std::thread>() };
+     109           11 :         std::mutex mtx;
+     110           74 :         for (auto i = 0; i < n_models; ++i) {
+     111           63 :             threads.push_back(std::thread([&, i]() {
+     112           63 :                 auto ypredict = models[i]->predict_proba(X);
+     113           63 :                 assert(ypredict.size() == y_pred.size());
+     114           63 :                 assert(ypredict[0].size() == y_pred[0].size());
+     115           63 :                 std::lock_guard<std::mutex> lock(mtx);
+     116              :                 // Multiply each prediction by the significance of the model and then add it to the final prediction
+     117        12479 :                 for (auto j = 0; j < ypredict.size(); ++j) {
+     118        12416 :                     std::transform(y_pred[j].begin(), y_pred[j].end(), ypredict[j].begin(), y_pred[j].begin(),
+     119        77782 :                         [significanceModels = significanceModels[i]](double x, double y) { return x + y * significanceModels; });
+     120              :                 }
+     121           63 :                 }));
+     122              :         }
+     123           74 :         for (auto& thread : threads) {
+     124           63 :             thread.join();
+     125              :         }
+     126           11 :         auto sum = std::reduce(significanceModels.begin(), significanceModels.end());
+     127              :         //Divide each element of the prediction by the sum of the significances
+     128         2067 :         for (auto j = 0; j < y_pred.size(); ++j) {
+     129        10662 :             std::transform(y_pred[j].begin(), y_pred[j].end(), y_pred[j].begin(), [sum](double x) { return x / sum; });
+     130              :         }
+     131           22 :         return y_pred;
+     132           11 :     }
+     133            7 :     std::vector<std::vector<double>> Ensemble::predict_average_voting(std::vector<std::vector<int>>& X)
+     134              :     {
+     135            7 :         torch::Tensor Xt = bayesnet::vectorToTensor(X, false);
+     136            7 :         auto y_pred = predict_average_voting(Xt);
+     137            7 :         std::vector<std::vector<double>> result = tensorToVectorDouble(y_pred);
+     138           14 :         return result;
+     139            7 :     }
+     140           20 :     torch::Tensor Ensemble::predict_average_voting(torch::Tensor& X)
+     141              :     {
+     142              :         // Build a m x n_models tensor with the predictions of each model
+     143           20 :         torch::Tensor y_pred = torch::zeros({ X.size(1), n_models }, torch::kInt32);
+     144           20 :         auto threads{ std::vector<std::thread>() };
+     145           20 :         std::mutex mtx;
+     146          134 :         for (auto i = 0; i < n_models; ++i) {
+     147          114 :             threads.push_back(std::thread([&, i]() {
+     148          114 :                 auto ypredict = models[i]->predict(X);
+     149          114 :                 std::lock_guard<std::mutex> lock(mtx);
+     150          342 :                 y_pred.index_put_({ "...", i }, ypredict);
+     151          228 :                 }));
+     152              :         }
+     153          134 :         for (auto& thread : threads) {
+     154          114 :             thread.join();
+     155              :         }
+     156           40 :         return voting(y_pred);
+     157           20 :     }
+     158           16 :     float Ensemble::score(torch::Tensor& X, torch::Tensor& y)
+     159              :     {
+     160           16 :         auto y_pred = predict(X);
+     161           14 :         int correct = 0;
+     162         4746 :         for (int i = 0; i < y_pred.size(0); ++i) {
+     163         4732 :             if (y_pred[i].item<int>() == y[i].item<int>()) {
+     164         4026 :                 correct++;
+     165              :             }
+     166              :         }
+     167           28 :         return (double)correct / y_pred.size(0);
+     168           14 :     }
+     169           12 :     float Ensemble::score(std::vector<std::vector<int>>& X, std::vector<int>& y)
+     170              :     {
+     171           12 :         auto y_pred = predict(X);
+     172           10 :         int correct = 0;
+     173         2534 :         for (int i = 0; i < y_pred.size(); ++i) {
+     174         2524 :             if (y_pred[i] == y[i]) {
+     175         2173 :                 correct++;
+     176              :             }
+     177              :         }
+     178           20 :         return (double)correct / y_pred.size();
+     179           10 :     }
+     180            1 :     std::vector<std::string> Ensemble::show() const
+     181              :     {
+     182            1 :         auto result = std::vector<std::string>();
+     183            5 :         for (auto i = 0; i < n_models; ++i) {
+     184            4 :             auto res = models[i]->show();
+     185            4 :             result.insert(result.end(), res.begin(), res.end());
+     186            4 :         }
+     187            1 :         return result;
+     188            0 :     }
+     189            3 :     std::vector<std::string> Ensemble::graph(const std::string& title) const
+     190              :     {
+     191            3 :         auto result = std::vector<std::string>();
+     192           20 :         for (auto i = 0; i < n_models; ++i) {
+     193           17 :             auto res = models[i]->graph(title + "_" + std::to_string(i));
+     194           17 :             result.insert(result.end(), res.begin(), res.end());
+     195           17 :         }
+     196            3 :         return result;
+     197            0 :     }
+     198            6 :     int Ensemble::getNumberOfNodes() const
+     199              :     {
+     200            6 :         int nodes = 0;
+     201           43 :         for (auto i = 0; i < n_models; ++i) {
+     202           37 :             nodes += models[i]->getNumberOfNodes();
+     203              :         }
+     204            6 :         return nodes;
+     205              :     }
+     206            6 :     int Ensemble::getNumberOfEdges() const
+     207              :     {
+     208            6 :         int edges = 0;
+     209           43 :         for (auto i = 0; i < n_models; ++i) {
+     210           37 :             edges += models[i]->getNumberOfEdges();
+     211              :         }
+     212            6 :         return edges;
+     213              :     }
+     214            1 :     int Ensemble::getNumberOfStates() const
+     215              :     {
+     216            1 :         int nstates = 0;
+     217            5 :         for (auto i = 0; i < n_models; ++i) {
+     218            4 :             nstates += models[i]->getNumberOfStates();
+     219              :         }
+     220            1 :         return nstates;
+     221              :     }
+     222              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.h.func-c.html b/html/bayesnet/ensembles/Ensemble.h.func-c.html new file mode 100644 index 0000000..65cb7ae --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.h.func-c.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %55
Test Date:2024-04-21 16:43:29Functions:75.0 %43
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZNK8bayesnet8Ensemble8dump_cptB5cxx11Ev2
_ZN8bayesnet8Ensemble17topological_orderB5cxx11Ev3
_ZN8bayesnet8EnsembleD0Ev27
_ZN8bayesnet8EnsembleD0Ev0
_ZN8bayesnet8EnsembleD2Ev27
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.h.func.html b/html/bayesnet/ensembles/Ensemble.h.func.html new file mode 100644 index 0000000..12ee4d1 --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.h.func.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %55
Test Date:2024-04-21 16:43:29Functions:75.0 %43
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet8Ensemble17topological_orderB5cxx11Ev3
_ZN8bayesnet8EnsembleD0Ev27
_ZN8bayesnet8EnsembleD0Ev0
_ZN8bayesnet8EnsembleD2Ev27
_ZNK8bayesnet8Ensemble8dump_cptB5cxx11Ev2
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/Ensemble.h.gcov.html b/html/bayesnet/ensembles/Ensemble.h.gcov.html new file mode 100644 index 0000000..0bd3673 --- /dev/null +++ b/html/bayesnet/ensembles/Ensemble.h.gcov.html @@ -0,0 +1,129 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles/Ensemble.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensembles - Ensemble.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %55
Test Date:2024-04-21 16:43:29Functions:75.0 %43
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef ENSEMBLE_H
+       8              : #define ENSEMBLE_H
+       9              : #include <torch/torch.h>
+      10              : #include "bayesnet/utils/BayesMetrics.h"
+      11              : #include "bayesnet/utils/bayesnetUtils.h"
+      12              : #include "bayesnet/classifiers/Classifier.h"
+      13              : 
+      14              : namespace bayesnet {
+      15              :     class Ensemble : public Classifier {
+      16              :     public:
+      17              :         Ensemble(bool predict_voting = true);
+      18           27 :         virtual ~Ensemble() = default;
+      19              :         torch::Tensor predict(torch::Tensor& X) override;
+      20              :         std::vector<int> predict(std::vector<std::vector<int>>& X) override;
+      21              :         torch::Tensor predict_proba(torch::Tensor& X) override;
+      22              :         std::vector<std::vector<double>> predict_proba(std::vector<std::vector<int>>& X) override;
+      23              :         float score(torch::Tensor& X, torch::Tensor& y) override;
+      24              :         float score(std::vector<std::vector<int>>& X, std::vector<int>& y) override;
+      25              :         int getNumberOfNodes() const override;
+      26              :         int getNumberOfEdges() const override;
+      27              :         int getNumberOfStates() const override;
+      28              :         std::vector<std::string> show() const override;
+      29              :         std::vector<std::string> graph(const std::string& title) const override;
+      30            3 :         std::vector<std::string> topological_order()  override
+      31              :         {
+      32            3 :             return std::vector<std::string>();
+      33              :         }
+      34            2 :         std::string dump_cpt() const override
+      35              :         {
+      36            4 :             return "";
+      37              :         }
+      38              :     protected:
+      39              :         torch::Tensor predict_average_voting(torch::Tensor& X);
+      40              :         std::vector<std::vector<double>> predict_average_voting(std::vector<std::vector<int>>& X);
+      41              :         torch::Tensor predict_average_proba(torch::Tensor& X);
+      42              :         std::vector<std::vector<double>> predict_average_proba(std::vector<std::vector<int>>& X);
+      43              :         torch::Tensor compute_arg_max(torch::Tensor& X);
+      44              :         std::vector<int> compute_arg_max(std::vector<std::vector<double>>& X);
+      45              :         torch::Tensor voting(torch::Tensor& votes);
+      46              :         unsigned n_models;
+      47              :         std::vector<std::unique_ptr<Classifier>> models;
+      48              :         std::vector<double> significanceModels;
+      49              :         void trainModel(const torch::Tensor& weights) override;
+      50              :         bool predict_voting;
+      51              :     };
+      52              : }
+      53              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/index-sort-f.html b/html/bayesnet/ensembles/index-sort-f.html new file mode 100644 index 0000000..df51294 --- /dev/null +++ b/html/bayesnet/ensembles/index-sort-f.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensemblesCoverageTotalHit
Test:coverage.infoLines:98.8 %424419
Test Date:2024-04-21 16:43:29Functions:98.1 %5352
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Ensemble.h +
100.0%
+
100.0 %5575.0 %43
AODE.h +
100.0%
+
100.0 %11100.0 %22
AODELd.h +
100.0%
+
100.0 %11100.0 %22
BoostAODE.h +
100.0%
+
100.0 %11100.0 %22
AODE.cc +
100.0%
+
100.0 %2020100.0 %44
AODELd.cc +
100.0%
+
100.0 %2424100.0 %55
BoostAODE.cc +
99.1%99.1%
+
99.1 %218216100.0 %99
Ensemble.cc +
98.1%98.1%
+
98.1 %154151100.0 %2525
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/index-sort-l.html b/html/bayesnet/ensembles/index-sort-l.html new file mode 100644 index 0000000..637943b --- /dev/null +++ b/html/bayesnet/ensembles/index-sort-l.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensemblesCoverageTotalHit
Test:coverage.infoLines:98.8 %424419
Test Date:2024-04-21 16:43:29Functions:98.1 %5352
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Ensemble.cc +
98.1%98.1%
+
98.1 %154151100.0 %2525
BoostAODE.cc +
99.1%99.1%
+
99.1 %218216100.0 %99
AODE.h +
100.0%
+
100.0 %11100.0 %22
AODELd.h +
100.0%
+
100.0 %11100.0 %22
BoostAODE.h +
100.0%
+
100.0 %11100.0 %22
Ensemble.h +
100.0%
+
100.0 %5575.0 %43
AODE.cc +
100.0%
+
100.0 %2020100.0 %44
AODELd.cc +
100.0%
+
100.0 %2424100.0 %55
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/ensembles/index.html b/html/bayesnet/ensembles/index.html new file mode 100644 index 0000000..c19334b --- /dev/null +++ b/html/bayesnet/ensembles/index.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/ensembles + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/ensemblesCoverageTotalHit
Test:coverage.infoLines:98.8 %424419
Test Date:2024-04-21 16:43:29Functions:98.1 %5352
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
AODE.cc +
100.0%
+
100.0 %2020100.0 %44
AODE.h +
100.0%
+
100.0 %11100.0 %22
AODELd.cc +
100.0%
+
100.0 %2424100.0 %55
AODELd.h +
100.0%
+
100.0 %11100.0 %22
BoostAODE.cc +
99.1%99.1%
+
99.1 %218216100.0 %99
BoostAODE.h +
100.0%
+
100.0 %11100.0 %22
Ensemble.cc +
98.1%98.1%
+
98.1 %154151100.0 %2525
Ensemble.h +
100.0%
+
100.0 %5575.0 %43
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.cc.func-c.html b/html/bayesnet/feature_selection/CFS.cc.func-c.html new file mode 100644 index 0000000..9db3561 --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.cc.func-c.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.8 %4544
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3CFS3fitEv6
_ZN8bayesnet3CFS24computeContinueConditionERKSt6vectorIiSaIiEE28
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.cc.func.html b/html/bayesnet/feature_selection/CFS.cc.func.html new file mode 100644 index 0000000..36fbee8 --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.cc.func.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.8 %4544
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3CFS24computeContinueConditionERKSt6vectorIiSaIiEE28
_ZN8bayesnet3CFS3fitEv6
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.cc.gcov.html b/html/bayesnet/feature_selection/CFS.cc.gcov.html new file mode 100644 index 0000000..a227d60 --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.cc.gcov.html @@ -0,0 +1,154 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.8 %4544
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <limits>
+       8              : #include "bayesnet/utils/bayesnetUtils.h"
+       9              : #include "CFS.h"
+      10              : namespace bayesnet {
+      11            6 :     void CFS::fit()
+      12              :     {
+      13            6 :         initialize();
+      14            6 :         computeSuLabels();
+      15            6 :         auto featureOrder = argsort(suLabels); // sort descending order
+      16            6 :         auto continueCondition = true;
+      17            6 :         auto feature = featureOrder[0];
+      18            6 :         selectedFeatures.push_back(feature);
+      19            6 :         selectedScores.push_back(suLabels[feature]);
+      20            6 :         featureOrder.erase(featureOrder.begin());
+      21           34 :         while (continueCondition) {
+      22           28 :             double merit = std::numeric_limits<double>::lowest();
+      23           28 :             int bestFeature = -1;
+      24          164 :             for (auto feature : featureOrder) {
+      25          136 :                 selectedFeatures.push_back(feature);
+      26              :                 // Compute merit with selectedFeatures
+      27          136 :                 auto meritNew = computeMeritCFS();
+      28          136 :                 if (meritNew > merit) {
+      29           57 :                     merit = meritNew;
+      30           57 :                     bestFeature = feature;
+      31              :                 }
+      32          136 :                 selectedFeatures.pop_back();
+      33              :             }
+      34           28 :             if (bestFeature == -1) {
+      35              :                 // meritNew has to be nan due to constant features
+      36            0 :                 break;
+      37              :             }
+      38           28 :             selectedFeatures.push_back(bestFeature);
+      39           28 :             selectedScores.push_back(merit);
+      40           28 :             featureOrder.erase(remove(featureOrder.begin(), featureOrder.end(), bestFeature), featureOrder.end());
+      41           28 :             continueCondition = computeContinueCondition(featureOrder);
+      42              :         }
+      43            6 :         fitted = true;
+      44            6 :     }
+      45           28 :     bool CFS::computeContinueCondition(const std::vector<int>& featureOrder)
+      46              :     {
+      47           28 :         if (selectedFeatures.size() == maxFeatures || featureOrder.size() == 0) {
+      48            1 :             return false;
+      49              :         }
+      50           27 :         if (selectedScores.size() >= 5) {
+      51              :             /*
+      52              :             "To prevent the best first search from exploring the entire
+      53              :             feature subset search space, a stopping criterion is imposed.
+      54              :             The search will terminate if five consecutive fully expanded
+      55              :             subsets show no improvement over the current best subset."
+      56              :             as stated in Mark A.Hall Thesis
+      57              :             */
+      58           10 :             double item_ant = std::numeric_limits<double>::lowest();
+      59           10 :             int num = 0;
+      60           10 :             std::vector<double> lastFive(selectedScores.end() - 5, selectedScores.end());
+      61           40 :             for (auto item : lastFive) {
+      62           35 :                 if (item_ant == std::numeric_limits<double>::lowest()) {
+      63           10 :                     item_ant = item;
+      64              :                 }
+      65           35 :                 if (item > item_ant) {
+      66            5 :                     break;
+      67              :                 } else {
+      68           30 :                     num++;
+      69           30 :                     item_ant = item;
+      70              :                 }
+      71              :             }
+      72           10 :             if (num == 5) {
+      73            5 :                 return false;
+      74              :             }
+      75           10 :         }
+      76           22 :         return true;
+      77              :     }
+      78              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.h.func-c.html b/html/bayesnet/feature_selection/CFS.h.func-c.html new file mode 100644 index 0000000..cf1c59c --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.h.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3CFSC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_6
_ZN8bayesnet3CFSD0Ev12
_ZN8bayesnet3CFSD0Ev6
_ZN8bayesnet3CFSD2Ev6
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.h.func.html b/html/bayesnet/feature_selection/CFS.h.func.html new file mode 100644 index 0000000..ae092ed --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.h.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3CFSC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_6
_ZN8bayesnet3CFSD0Ev12
_ZN8bayesnet3CFSD0Ev6
_ZN8bayesnet3CFSD2Ev6
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/CFS.h.gcov.html b/html/bayesnet/feature_selection/CFS.h.gcov.html new file mode 100644 index 0000000..6578a15 --- /dev/null +++ b/html/bayesnet/feature_selection/CFS.h.gcov.html @@ -0,0 +1,102 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/CFS.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - CFS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %44
Test Date:2024-04-21 16:43:29Functions:100.0 %33
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef CFS_H
+       8              : #define CFS_H
+       9              : #include <torch/torch.h>
+      10              : #include <vector>
+      11              : #include "bayesnet/feature_selection/FeatureSelect.h"
+      12              : namespace bayesnet {
+      13              :     class CFS : public FeatureSelect {
+      14              :     public:
+      15              :         // dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
+      16            6 :         CFS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights) :
+      17            6 :             FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights)
+      18              :         {
+      19            6 :         }
+      20           12 :         virtual ~CFS() {};
+      21              :         void fit() override;
+      22              :     private:
+      23              :         bool computeContinueCondition(const std::vector<int>& featureOrder);
+      24              :     };
+      25              : }
+      26              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.cc.func-c.html b/html/bayesnet/feature_selection/FCBF.cc.func-c.html new file mode 100644 index 0000000..055a209 --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.cc.func-c.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %2624
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4FCBF3fitEv5
_ZN8bayesnet4FCBFC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_d7
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.cc.func.html b/html/bayesnet/feature_selection/FCBF.cc.func.html new file mode 100644 index 0000000..e122521 --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.cc.func.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %2624
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4FCBF3fitEv5
_ZN8bayesnet4FCBFC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_d7
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.cc.gcov.html b/html/bayesnet/feature_selection/FCBF.cc.gcov.html new file mode 100644 index 0000000..f274fbc --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.cc.gcov.html @@ -0,0 +1,126 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %2624
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "bayesnet/utils/bayesnetUtils.h"
+       8              : #include "FCBF.h"
+       9              : namespace bayesnet {
+      10              : 
+      11            7 :     FCBF::FCBF(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold) :
+      12            7 :         FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
+      13              :     {
+      14            7 :         if (threshold < 1e-7) {
+      15            2 :             throw std::invalid_argument("Threshold cannot be less than 1e-7");
+      16              :         }
+      17            7 :     }
+      18            5 :     void FCBF::fit()
+      19              :     {
+      20            5 :         initialize();
+      21            5 :         computeSuLabels();
+      22            5 :         auto featureOrder = argsort(suLabels); // sort descending order
+      23            5 :         auto featureOrderCopy = featureOrder;
+      24           42 :         for (const auto& feature : featureOrder) {
+      25              :             // Don't self compare
+      26           37 :             featureOrderCopy.erase(featureOrderCopy.begin());
+      27           37 :             if (suLabels.at(feature) == 0.0) {
+      28              :                 // The feature has been removed from the list
+      29           16 :                 continue;
+      30              :             }
+      31           21 :             if (suLabels.at(feature) < threshold) {
+      32            0 :                 break;
+      33              :             }
+      34              :             // Remove redundant features
+      35          116 :             for (const auto& featureCopy : featureOrderCopy) {
+      36           95 :                 double value = computeSuFeatures(feature, featureCopy);
+      37           95 :                 if (value >= suLabels.at(featureCopy)) {
+      38              :                     // Remove feature from list
+      39           33 :                     suLabels[featureCopy] = 0.0;
+      40              :                 }
+      41              :             }
+      42           21 :             selectedFeatures.push_back(feature);
+      43           21 :             selectedScores.push_back(suLabels[feature]);
+      44           21 :             if (selectedFeatures.size() == maxFeatures) {
+      45            0 :                 break;
+      46              :             }
+      47              :         }
+      48            5 :         fitted = true;
+      49            5 :     }
+      50              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.h.func-c.html b/html/bayesnet/feature_selection/FCBF.h.func-c.html new file mode 100644 index 0000000..fba4f09 --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4FCBFD0Ev10
_ZN8bayesnet4FCBFD0Ev5
_ZN8bayesnet4FCBFD2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.h.func.html b/html/bayesnet/feature_selection/FCBF.h.func.html new file mode 100644 index 0000000..a2731be --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4FCBFD0Ev10
_ZN8bayesnet4FCBFD0Ev5
_ZN8bayesnet4FCBFD2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FCBF.h.gcov.html b/html/bayesnet/feature_selection/FCBF.h.gcov.html new file mode 100644 index 0000000..14216e2 --- /dev/null +++ b/html/bayesnet/feature_selection/FCBF.h.gcov.html @@ -0,0 +1,99 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FCBF.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FCBF.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef FCBF_H
+       8              : #define FCBF_H
+       9              : #include <torch/torch.h>
+      10              : #include <vector>
+      11              : #include "bayesnet/feature_selection/FeatureSelect.h"
+      12              : namespace bayesnet {
+      13              :     class FCBF : public FeatureSelect {
+      14              :     public:
+      15              :         // dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
+      16              :         FCBF(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold);
+      17           10 :         virtual ~FCBF() {};
+      18              :         void fit() override;
+      19              :     private:
+      20              :         double threshold = -1;
+      21              :     };
+      22              : }
+      23              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.cc.func-c.html b/html/bayesnet/feature_selection/FeatureSelect.cc.func-c.html new file mode 100644 index 0000000..84eaaeb --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.cc.func-c.html @@ -0,0 +1,131 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %4442
Test Date:2024-04-21 16:43:29Functions:100.0 %88
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet13FeatureSelect10initializeEv16
_ZN8bayesnet13FeatureSelect15computeSuLabelsEv16
_ZNK8bayesnet13FeatureSelect11getFeaturesEv16
_ZNK8bayesnet13FeatureSelect9getScoresEv16
_ZN8bayesnet13FeatureSelectC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_22
_ZN8bayesnet13FeatureSelect15computeMeritCFSEv158
_ZN8bayesnet13FeatureSelect22symmetricalUncertaintyEii411
_ZN8bayesnet13FeatureSelect17computeSuFeaturesEii980
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.cc.func.html b/html/bayesnet/feature_selection/FeatureSelect.cc.func.html new file mode 100644 index 0000000..8f5eafe --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.cc.func.html @@ -0,0 +1,131 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %4442
Test Date:2024-04-21 16:43:29Functions:100.0 %88
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet13FeatureSelect10initializeEv16
_ZN8bayesnet13FeatureSelect15computeMeritCFSEv158
_ZN8bayesnet13FeatureSelect15computeSuLabelsEv16
_ZN8bayesnet13FeatureSelect17computeSuFeaturesEii980
_ZN8bayesnet13FeatureSelect22symmetricalUncertaintyEii411
_ZN8bayesnet13FeatureSelectC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_22
_ZNK8bayesnet13FeatureSelect11getFeaturesEv16
_ZNK8bayesnet13FeatureSelect9getScoresEv16
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.cc.gcov.html b/html/bayesnet/feature_selection/FeatureSelect.cc.gcov.html new file mode 100644 index 0000000..12fbe0f --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.cc.gcov.html @@ -0,0 +1,160 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %4442
Test Date:2024-04-21 16:43:29Functions:100.0 %88
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <limits>
+       8              : #include "bayesnet/utils/bayesnetUtils.h"
+       9              : #include "FeatureSelect.h"
+      10              : namespace bayesnet {
+      11           22 :     FeatureSelect::FeatureSelect(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights) :
+      12           22 :         Metrics(samples, features, className, classNumStates), maxFeatures(maxFeatures == 0 ? samples.size(0) - 1 : maxFeatures), weights(weights)
+      13              : 
+      14              :     {
+      15           22 :     }
+      16           16 :     void FeatureSelect::initialize()
+      17              :     {
+      18           16 :         selectedFeatures.clear();
+      19           16 :         selectedScores.clear();
+      20           16 :     }
+      21          411 :     double FeatureSelect::symmetricalUncertainty(int a, int b)
+      22              :     {
+      23              :         /*
+      24              :         Compute symmetrical uncertainty. Normalize* information gain (mutual
+      25              :         information) with the entropies of the features in order to compensate
+      26              :         the bias due to high cardinality features. *Range [0, 1]
+      27              :         (https://www.sciencedirect.com/science/article/pii/S0020025519303603)
+      28              :         */
+      29         1233 :         auto x = samples.index({ a, "..." });
+      30         1233 :         auto y = samples.index({ b, "..." });
+      31          411 :         auto mu = mutualInformation(x, y, weights);
+      32          411 :         auto hx = entropy(x, weights);
+      33          411 :         auto hy = entropy(y, weights);
+      34          411 :         return 2.0 * mu / (hx + hy);
+      35         1233 :     }
+      36           16 :     void FeatureSelect::computeSuLabels()
+      37              :     {
+      38              :         // Compute Simmetrical Uncertainty between features and labels
+      39              :         // https://en.wikipedia.org/wiki/Symmetric_uncertainty
+      40          135 :         for (int i = 0; i < features.size(); ++i) {
+      41          119 :             suLabels.push_back(symmetricalUncertainty(i, -1));
+      42              :         }
+      43           16 :     }
+      44          980 :     double FeatureSelect::computeSuFeatures(const int firstFeature, const int secondFeature)
+      45              :     {
+      46              :         // Compute Simmetrical Uncertainty between features
+      47              :         // https://en.wikipedia.org/wiki/Symmetric_uncertainty
+      48              :         try {
+      49          980 :             return suFeatures.at({ firstFeature, secondFeature });
+      50              :         }
+      51          292 :         catch (const std::out_of_range& e) {
+      52          292 :             double result = symmetricalUncertainty(firstFeature, secondFeature);
+      53          292 :             suFeatures[{firstFeature, secondFeature}] = result;
+      54          292 :             return result;
+      55          292 :         }
+      56              :     }
+      57          158 :     double FeatureSelect::computeMeritCFS()
+      58              :     {
+      59          158 :         double rcf = 0;
+      60          727 :         for (auto feature : selectedFeatures) {
+      61          569 :             rcf += suLabels[feature];
+      62              :         }
+      63          158 :         double rff = 0;
+      64          158 :         int n = selectedFeatures.size();
+      65         1043 :         for (const auto& item : doCombinations(selectedFeatures)) {
+      66          885 :             rff += computeSuFeatures(item.first, item.second);
+      67          158 :         }
+      68          158 :         return rcf / sqrt(n + (n * n - n) * rff);
+      69              :     }
+      70           16 :     std::vector<int> FeatureSelect::getFeatures() const
+      71              :     {
+      72           16 :         if (!fitted) {
+      73            0 :             throw std::runtime_error("FeatureSelect not fitted");
+      74              :         }
+      75           16 :         return selectedFeatures;
+      76              :     }
+      77           16 :     std::vector<double> FeatureSelect::getScores() const
+      78              :     {
+      79           16 :         if (!fitted) {
+      80            0 :             throw std::runtime_error("FeatureSelect not fitted");
+      81              :         }
+      82           16 :         return selectedScores;
+      83              :     }
+      84              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.h.func-c.html b/html/bayesnet/feature_selection/FeatureSelect.h.func-c.html new file mode 100644 index 0000000..7b1bbbf --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet13FeatureSelectD0Ev22
_ZN8bayesnet13FeatureSelectD0Ev0
_ZN8bayesnet13FeatureSelectD2Ev22
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.h.func.html b/html/bayesnet/feature_selection/FeatureSelect.h.func.html new file mode 100644 index 0000000..24e2380 --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet13FeatureSelectD0Ev22
_ZN8bayesnet13FeatureSelectD0Ev0
_ZN8bayesnet13FeatureSelectD2Ev22
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/FeatureSelect.h.gcov.html b/html/bayesnet/feature_selection/FeatureSelect.h.gcov.html new file mode 100644 index 0000000..5812114 --- /dev/null +++ b/html/bayesnet/feature_selection/FeatureSelect.h.gcov.html @@ -0,0 +1,112 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/FeatureSelect.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - FeatureSelect.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef FEATURE_SELECT_H
+       8              : #define FEATURE_SELECT_H
+       9              : #include <torch/torch.h>
+      10              : #include <vector>
+      11              : #include "bayesnet/utils/BayesMetrics.h"
+      12              : namespace bayesnet {
+      13              :     class FeatureSelect : public Metrics {
+      14              :     public:
+      15              :         // dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
+      16              :         FeatureSelect(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights);
+      17           22 :         virtual ~FeatureSelect() {};
+      18              :         virtual void fit() = 0;
+      19              :         std::vector<int> getFeatures() const;
+      20              :         std::vector<double> getScores() const;
+      21              :     protected:
+      22              :         void initialize();
+      23              :         void computeSuLabels();
+      24              :         double computeSuFeatures(const int a, const int b);
+      25              :         double symmetricalUncertainty(int a, int b);
+      26              :         double computeMeritCFS();
+      27              :         const torch::Tensor& weights;
+      28              :         int maxFeatures;
+      29              :         std::vector<int> selectedFeatures;
+      30              :         std::vector<double> selectedScores;
+      31              :         std::vector<double> suLabels;
+      32              :         std::map<std::pair<int, int>, double> suFeatures;
+      33              :         bool fitted = false;
+      34              :     };
+      35              : }
+      36              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.cc.func-c.html b/html/bayesnet/feature_selection/IWSS.cc.func-c.html new file mode 100644 index 0000000..fd76cca --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.cc.func-c.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:93.3 %3028
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4IWSS3fitEv5
_ZN8bayesnet4IWSSC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_d9
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.cc.func.html b/html/bayesnet/feature_selection/IWSS.cc.func.html new file mode 100644 index 0000000..14e0666 --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.cc.func.html @@ -0,0 +1,89 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:93.3 %3028
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4IWSS3fitEv5
_ZN8bayesnet4IWSSC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_iiS4_d9
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.cc.gcov.html b/html/bayesnet/feature_selection/IWSS.cc.gcov.html new file mode 100644 index 0000000..24a540c --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.cc.gcov.html @@ -0,0 +1,129 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:93.3 %3028
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <limits>
+       8              : #include "bayesnet/utils/bayesnetUtils.h"
+       9              : #include "IWSS.h"
+      10              : namespace bayesnet {
+      11            9 :     IWSS::IWSS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold) :
+      12            9 :         FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
+      13              :     {
+      14            9 :         if (threshold < 0 || threshold > .5) {
+      15            4 :             throw std::invalid_argument("Threshold has to be in [0, 0.5]");
+      16              :         }
+      17            9 :     }
+      18            5 :     void IWSS::fit()
+      19              :     {
+      20            5 :         initialize();
+      21            5 :         computeSuLabels();
+      22            5 :         auto featureOrder = argsort(suLabels); // sort descending order
+      23            5 :         auto featureOrderCopy = featureOrder;
+      24              :         // Add first and second features to result
+      25              :         //     First with its own score
+      26            5 :         auto first_feature = pop_first(featureOrderCopy);
+      27            5 :         selectedFeatures.push_back(first_feature);
+      28            5 :         selectedScores.push_back(suLabels.at(first_feature));
+      29              :         //     Second with the score of the candidates
+      30            5 :         selectedFeatures.push_back(pop_first(featureOrderCopy));
+      31            5 :         auto merit = computeMeritCFS();
+      32            5 :         selectedScores.push_back(merit);
+      33           17 :         for (const auto feature : featureOrderCopy) {
+      34           17 :             selectedFeatures.push_back(feature);
+      35              :             // Compute merit with selectedFeatures
+      36           17 :             auto meritNew = computeMeritCFS();
+      37           17 :             double delta = merit != 0.0 ? std::abs(merit - meritNew) / merit : 0.0;
+      38           17 :             if (meritNew > merit || delta < threshold) {
+      39           12 :                 if (meritNew > merit) {
+      40            0 :                     merit = meritNew;
+      41              :                 }
+      42           12 :                 selectedScores.push_back(meritNew);
+      43              :             } else {
+      44            5 :                 selectedFeatures.pop_back();
+      45            5 :                 break;
+      46              :             }
+      47           12 :             if (selectedFeatures.size() == maxFeatures) {
+      48            0 :                 break;
+      49              :             }
+      50              :         }
+      51            5 :         fitted = true;
+      52            5 :     }
+      53              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.h.func-c.html b/html/bayesnet/feature_selection/IWSS.h.func-c.html new file mode 100644 index 0000000..0e3ebcc --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.h.func-c.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4IWSSD0Ev10
_ZN8bayesnet4IWSSD0Ev5
_ZN8bayesnet4IWSSD2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.h.func.html b/html/bayesnet/feature_selection/IWSS.h.func.html new file mode 100644 index 0000000..acdd456 --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.h.func.html @@ -0,0 +1,96 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4IWSSD0Ev10
_ZN8bayesnet4IWSSD0Ev5
_ZN8bayesnet4IWSSD2Ev5
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/IWSS.h.gcov.html b/html/bayesnet/feature_selection/IWSS.h.gcov.html new file mode 100644 index 0000000..205f28f --- /dev/null +++ b/html/bayesnet/feature_selection/IWSS.h.gcov.html @@ -0,0 +1,99 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection/IWSS.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selection - IWSS.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %22
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef IWSS_H
+       8              : #define IWSS_H
+       9              : #include <vector>
+      10              : #include <torch/torch.h>
+      11              : #include "FeatureSelect.h"
+      12              : namespace bayesnet {
+      13              :     class IWSS : public FeatureSelect {
+      14              :     public:
+      15              :         // dataset is a n+1xm tensor of integers where dataset[-1] is the y std::vector
+      16              :         IWSS(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int maxFeatures, const int classNumStates, const torch::Tensor& weights, const double threshold);
+      17           10 :         virtual ~IWSS() {};
+      18              :         void fit() override;
+      19              :     private:
+      20              :         double threshold = -1;
+      21              :     };
+      22              : }
+      23              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/index-sort-f.html b/html/bayesnet/feature_selection/index-sort-f.html new file mode 100644 index 0000000..c5d80b2 --- /dev/null +++ b/html/bayesnet/feature_selection/index-sort-f.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selectionCoverageTotalHit
Test:coverage.infoLines:95.4 %152145
Test Date:2024-04-21 16:43:29Functions:95.7 %2322
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
FeatureSelect.h +
100.0%
+
100.0 %1150.0 %21
CFS.cc +
97.8%97.8%
+
97.8 %4544100.0 %22
FCBF.cc +
92.3%92.3%
+
92.3 %2624100.0 %22
FCBF.h +
100.0%
+
100.0 %11100.0 %22
IWSS.cc +
93.3%93.3%
+
93.3 %3028100.0 %22
IWSS.h +
100.0%
+
100.0 %11100.0 %22
CFS.h +
100.0%
+
100.0 %44100.0 %33
FeatureSelect.cc +
95.5%95.5%
+
95.5 %4442100.0 %88
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/index-sort-l.html b/html/bayesnet/feature_selection/index-sort-l.html new file mode 100644 index 0000000..96f5f93 --- /dev/null +++ b/html/bayesnet/feature_selection/index-sort-l.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selectionCoverageTotalHit
Test:coverage.infoLines:95.4 %152145
Test Date:2024-04-21 16:43:29Functions:95.7 %2322
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
FCBF.cc +
92.3%92.3%
+
92.3 %2624100.0 %22
IWSS.cc +
93.3%93.3%
+
93.3 %3028100.0 %22
FeatureSelect.cc +
95.5%95.5%
+
95.5 %4442100.0 %88
CFS.cc +
97.8%97.8%
+
97.8 %4544100.0 %22
FCBF.h +
100.0%
+
100.0 %11100.0 %22
FeatureSelect.h +
100.0%
+
100.0 %1150.0 %21
IWSS.h +
100.0%
+
100.0 %11100.0 %22
CFS.h +
100.0%
+
100.0 %44100.0 %33
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/feature_selection/index.html b/html/bayesnet/feature_selection/index.html new file mode 100644 index 0000000..df773f3 --- /dev/null +++ b/html/bayesnet/feature_selection/index.html @@ -0,0 +1,189 @@ + + + + + + + LCOV - coverage.info - bayesnet/feature_selection + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/feature_selectionCoverageTotalHit
Test:coverage.infoLines:95.4 %152145
Test Date:2024-04-21 16:43:29Functions:95.7 %2322
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
CFS.cc +
97.8%97.8%
+
97.8 %4544100.0 %22
CFS.h +
100.0%
+
100.0 %44100.0 %33
FCBF.cc +
92.3%92.3%
+
92.3 %2624100.0 %22
FCBF.h +
100.0%
+
100.0 %11100.0 %22
FeatureSelect.cc +
95.5%95.5%
+
95.5 %4442100.0 %88
FeatureSelect.h +
100.0%
+
100.0 %1150.0 %21
IWSS.cc +
93.3%93.3%
+
93.3 %3028100.0 %22
IWSS.h +
100.0%
+
100.0 %11100.0 %22
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/index-sort-f.html b/html/bayesnet/index-sort-f.html new file mode 100644 index 0000000..be8ff89 --- /dev/null +++ b/html/bayesnet/index-sort-f.html @@ -0,0 +1,105 @@ + + + + + + + LCOV - coverage.info - bayesnet + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnetCoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
BaseClassifier.h +
100.0%
+
100.0 %1150.0 %21
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/index-sort-l.html b/html/bayesnet/index-sort-l.html new file mode 100644 index 0000000..1e2b677 --- /dev/null +++ b/html/bayesnet/index-sort-l.html @@ -0,0 +1,105 @@ + + + + + + + LCOV - coverage.info - bayesnet + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnetCoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
BaseClassifier.h +
100.0%
+
100.0 %1150.0 %21
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/index.html b/html/bayesnet/index.html new file mode 100644 index 0000000..e2abc60 --- /dev/null +++ b/html/bayesnet/index.html @@ -0,0 +1,105 @@ + + + + + + + LCOV - coverage.info - bayesnet + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnetCoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:50.0 %21
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
BaseClassifier.h +
100.0%
+
100.0 %1150.0 %21
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.cc.func-c.html b/html/bayesnet/network/Network.cc.func-c.html new file mode 100644 index 0000000..594a181 --- /dev/null +++ b/html/bayesnet/network/Network.cc.func-c.html @@ -0,0 +1,355 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.6 %297290
Test Date:2024-04-21 16:43:29Functions:100.0 %4040
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Network3fitERKN2at6TensorES4_S4_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_RKSt3mapISB_S5_IiSaIiEESt4lessISB_ESaISt4pairISG_SK_EEE1
_ZN8bayesnet7NetworkC2Ef2
_ZNK8bayesnet7Network8dump_cptB5cxx11Ev2
_ZN8bayesnet7Network5scoreERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_5
_ZNK8bayesnet7Network4showB5cxx11Ev7
_ZN8bayesnet7Network10getSamplesEv12
_ZN8bayesnet7Network7predictERKSt6vectorIS1_IiSaIiEESaIS3_EE12
_ZNK8bayesnet7Network9getStatesEv12
_ZN8bayesnet7Network3fitERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_RKS1_IdSaIdEERKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISJ_EERKSJ_RKSt3mapISJ_S3_St4lessISJ_ESaISt4pairISO_S3_EEE21
_ZNK8bayesnet7Network5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE22
_ZNK8bayesnet7Network11getNumEdgesEv48
_ZNK8bayesnet7Network11getFeaturesB5cxx11Ev52
_ZN8bayesnet7Network16topological_sortB5cxx11Ev55
_ZNK8bayesnet7Network8getEdgesB5cxx11Ev59
_ZN8bayesnet7Network13predict_probaERKSt6vectorIS1_IiSaIiEESaIS3_EE68
_ZN8bayesnet7Network7predictERKN2at6TensorE245
_ZN8bayesnet7Network10initializeEv286
_ZN8bayesnet7Network13predict_probaERKN2at6TensorE304
_ZN8bayesnet7Network3fitERKN2at6TensorES4_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_RKSt3mapISB_S5_IiSaIiEESt4lessISB_ESaISt4pairISG_SK_EEE305
_ZN8bayesnet7Network11completeFitERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERKN2at6TensorE313
_ZN8bayesnet7Network9setStatesERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEE313
_ZN8bayesnet7Network12checkFitDataEiiiRKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RKSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEERKN2at6TensorE327
_ZN8bayesnet7NetworkC2ERKS0_414
_ZNK8bayesnet7Network13getMaxThreadsEv417
_ZN8bayesnet7NetworkC2Ev435
_ZNK8bayesnet7Network17getClassNumStatesEv496
_ZN8bayesnet7Network14predict_tensorERKN2at6TensorEb549
_ZN8bayesnet7Network7addNodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE8878
_ZZN8bayesnet7Network11completeFitERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERKN2at6TensorEENKUlvE_clEv9288
_ZZN8bayesnet7Network9setStatesERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEEENKUlRSE_E_clESK_9288
_ZN8bayesnet7Network14predict_sampleERKSt6vectorIiSaIiEE13606
_ZN8bayesnet7Network7addEdgeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_16946
_ZN8bayesnet7Network8isCyclicERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt13unordered_setIS6_St4hashIS6_ESt8equal_toIS6_ESaIS6_EESG_22324
_ZN8bayesnet7Network14predict_sampleERKN2at6TensorE95840
_ZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE109442
_ZN8bayesnet7Network13computeFactorERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE437768
_ZZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlRKdE_clESH_437768
_ZZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlvE_clEv437768
_ZNK8bayesnet7Network12getClassNameB5cxx11Ev437774
_ZN8bayesnet7Network8getNodesB5cxx11Ev437841
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.cc.func.html b/html/bayesnet/network/Network.cc.func.html new file mode 100644 index 0000000..20dc02f --- /dev/null +++ b/html/bayesnet/network/Network.cc.func.html @@ -0,0 +1,355 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.6 %297290
Test Date:2024-04-21 16:43:29Functions:100.0 %4040
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Network10getSamplesEv12
_ZN8bayesnet7Network10initializeEv286
_ZN8bayesnet7Network11completeFitERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERKN2at6TensorE313
_ZN8bayesnet7Network12checkFitDataEiiiRKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKS7_RKSt3mapIS7_S1_IiSaIiEESt4lessIS7_ESaISt4pairISC_SG_EEERKN2at6TensorE327
_ZN8bayesnet7Network13computeFactorERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE437768
_ZN8bayesnet7Network13predict_probaERKN2at6TensorE304
_ZN8bayesnet7Network13predict_probaERKSt6vectorIS1_IiSaIiEESaIS3_EE68
_ZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE109442
_ZN8bayesnet7Network14predict_sampleERKN2at6TensorE95840
_ZN8bayesnet7Network14predict_sampleERKSt6vectorIiSaIiEE13606
_ZN8bayesnet7Network14predict_tensorERKN2at6TensorEb549
_ZN8bayesnet7Network16topological_sortB5cxx11Ev55
_ZN8bayesnet7Network3fitERKN2at6TensorES4_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_RKSt3mapISB_S5_IiSaIiEESt4lessISB_ESaISt4pairISG_SK_EEE305
_ZN8bayesnet7Network3fitERKN2at6TensorES4_S4_RKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_RKSt3mapISB_S5_IiSaIiEESt4lessISB_ESaISt4pairISG_SK_EEE1
_ZN8bayesnet7Network3fitERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_RKS1_IdSaIdEERKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISJ_EERKSJ_RKSt3mapISJ_S3_St4lessISJ_ESaISt4pairISO_S3_EEE21
_ZN8bayesnet7Network5scoreERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_5
_ZN8bayesnet7Network7addEdgeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES8_16946
_ZN8bayesnet7Network7addNodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE8878
_ZN8bayesnet7Network7predictERKN2at6TensorE245
_ZN8bayesnet7Network7predictERKSt6vectorIS1_IiSaIiEESaIS3_EE12
_ZN8bayesnet7Network8getNodesB5cxx11Ev437841
_ZN8bayesnet7Network8isCyclicERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERSt13unordered_setIS6_St4hashIS6_ESt8equal_toIS6_ESaIS6_EESG_22324
_ZN8bayesnet7Network9setStatesERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEE313
_ZN8bayesnet7NetworkC2ERKS0_414
_ZN8bayesnet7NetworkC2Ef2
_ZN8bayesnet7NetworkC2Ev435
_ZNK8bayesnet7Network11getFeaturesB5cxx11Ev52
_ZNK8bayesnet7Network11getNumEdgesEv48
_ZNK8bayesnet7Network12getClassNameB5cxx11Ev437774
_ZNK8bayesnet7Network13getMaxThreadsEv417
_ZNK8bayesnet7Network17getClassNumStatesEv496
_ZNK8bayesnet7Network4showB5cxx11Ev7
_ZNK8bayesnet7Network5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE22
_ZNK8bayesnet7Network8dump_cptB5cxx11Ev2
_ZNK8bayesnet7Network8getEdgesB5cxx11Ev59
_ZNK8bayesnet7Network9getStatesEv12
_ZZN8bayesnet7Network11completeFitERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEERKN2at6TensorEENKUlvE_clEv9288
_ZZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlRKdE_clESH_437768
_ZZN8bayesnet7Network14exactInferenceERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlvE_clEv437768
_ZZN8bayesnet7Network9setStatesERKSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIiSaIiEESt4lessIS7_ESaISt4pairIKS7_SA_EEEENKUlRSE_E_clESK_9288
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.cc.gcov.html b/html/bayesnet/network/Network.cc.gcov.html new file mode 100644 index 0000000..801bed2 --- /dev/null +++ b/html/bayesnet/network/Network.cc.gcov.html @@ -0,0 +1,509 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:97.6 %297290
Test Date:2024-04-21 16:43:29Functions:100.0 %4040
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <thread>
+       8              : #include <mutex>
+       9              : #include <sstream>
+      10              : #include "Network.h"
+      11              : #include "bayesnet/utils/bayesnetUtils.h"
+      12              : namespace bayesnet {
+      13          435 :     Network::Network() : fitted{ false }, maxThreads{ 0.95 }, classNumStates{ 0 }, laplaceSmoothing{ 0 }
+      14              :     {
+      15          435 :     }
+      16            2 :     Network::Network(float maxT) : fitted{ false }, maxThreads{ maxT }, classNumStates{ 0 }, laplaceSmoothing{ 0 }
+      17              :     {
+      18              : 
+      19            2 :     }
+      20          414 :     Network::Network(const Network& other) : laplaceSmoothing(other.laplaceSmoothing), features(other.features), className(other.className), classNumStates(other.getClassNumStates()),
+      21          828 :         maxThreads(other.getMaxThreads()), fitted(other.fitted), samples(other.samples)
+      22              :     {
+      23          414 :         if (samples.defined())
+      24            1 :             samples = samples.clone();
+      25          419 :         for (const auto& node : other.nodes) {
+      26            5 :             nodes[node.first] = std::make_unique<Node>(*node.second);
+      27              :         }
+      28          414 :     }
+      29          286 :     void Network::initialize()
+      30              :     {
+      31          286 :         features.clear();
+      32          286 :         className = "";
+      33          286 :         classNumStates = 0;
+      34          286 :         fitted = false;
+      35          286 :         nodes.clear();
+      36          286 :         samples = torch::Tensor();
+      37          286 :     }
+      38          417 :     float Network::getMaxThreads() const
+      39              :     {
+      40          417 :         return maxThreads;
+      41              :     }
+      42           12 :     torch::Tensor& Network::getSamples()
+      43              :     {
+      44           12 :         return samples;
+      45              :     }
+      46         8878 :     void Network::addNode(const std::string& name)
+      47              :     {
+      48         8878 :         if (name == "") {
+      49            2 :             throw std::invalid_argument("Node name cannot be empty");
+      50              :         }
+      51         8876 :         if (nodes.find(name) != nodes.end()) {
+      52            0 :             return;
+      53              :         }
+      54         8876 :         if (find(features.begin(), features.end(), name) == features.end()) {
+      55         8876 :             features.push_back(name);
+      56              :         }
+      57         8876 :         nodes[name] = std::make_unique<Node>(name);
+      58              :     }
+      59           52 :     std::vector<std::string> Network::getFeatures() const
+      60              :     {
+      61           52 :         return features;
+      62              :     }
+      63          496 :     int Network::getClassNumStates() const
+      64              :     {
+      65          496 :         return classNumStates;
+      66              :     }
+      67           12 :     int Network::getStates() const
+      68              :     {
+      69           12 :         int result = 0;
+      70           72 :         for (auto& node : nodes) {
+      71           60 :             result += node.second->getNumStates();
+      72              :         }
+      73           12 :         return result;
+      74              :     }
+      75       437774 :     std::string Network::getClassName() const
+      76              :     {
+      77       437774 :         return className;
+      78              :     }
+      79        22324 :     bool Network::isCyclic(const std::string& nodeId, std::unordered_set<std::string>& visited, std::unordered_set<std::string>& recStack)
+      80              :     {
+      81        22324 :         if (visited.find(nodeId) == visited.end()) // if node hasn't been visited yet
+      82              :         {
+      83        22324 :             visited.insert(nodeId);
+      84        22324 :             recStack.insert(nodeId);
+      85        27702 :             for (Node* child : nodes[nodeId]->getChildren()) {
+      86         5384 :                 if (visited.find(child->getName()) == visited.end() && isCyclic(child->getName(), visited, recStack))
+      87            6 :                     return true;
+      88         5380 :                 if (recStack.find(child->getName()) != recStack.end())
+      89            2 :                     return true;
+      90              :             }
+      91              :         }
+      92        22318 :         recStack.erase(nodeId); // remove node from recursion stack before function ends
+      93        22318 :         return false;
+      94              :     }
+      95        16946 :     void Network::addEdge(const std::string& parent, const std::string& child)
+      96              :     {
+      97        16946 :         if (nodes.find(parent) == nodes.end()) {
+      98            2 :             throw std::invalid_argument("Parent node " + parent + " does not exist");
+      99              :         }
+     100        16944 :         if (nodes.find(child) == nodes.end()) {
+     101            2 :             throw std::invalid_argument("Child node " + child + " does not exist");
+     102              :         }
+     103              :         // Temporarily add edge to check for cycles
+     104        16942 :         nodes[parent]->addChild(nodes[child].get());
+     105        16942 :         nodes[child]->addParent(nodes[parent].get());
+     106        16942 :         std::unordered_set<std::string> visited;
+     107        16942 :         std::unordered_set<std::string> recStack;
+     108        16942 :         if (isCyclic(nodes[child]->getName(), visited, recStack)) // if adding this edge forms a cycle
+     109              :         {
+     110              :             // remove problematic edge
+     111            2 :             nodes[parent]->removeChild(nodes[child].get());
+     112            2 :             nodes[child]->removeParent(nodes[parent].get());
+     113            2 :             throw std::invalid_argument("Adding this edge forms a cycle in the graph.");
+     114              :         }
+     115        16944 :     }
+     116       437841 :     std::map<std::string, std::unique_ptr<Node>>& Network::getNodes()
+     117              :     {
+     118       437841 :         return nodes;
+     119              :     }
+     120          327 :     void Network::checkFitData(int n_samples, int n_features, int n_samples_y, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
+     121              :     {
+     122          327 :         if (weights.size(0) != n_samples) {
+     123            2 :             throw std::invalid_argument("Weights (" + std::to_string(weights.size(0)) + ") must have the same number of elements as samples (" + std::to_string(n_samples) + ") in Network::fit");
+     124              :         }
+     125          325 :         if (n_samples != n_samples_y) {
+     126            2 :             throw std::invalid_argument("X and y must have the same number of samples in Network::fit (" + std::to_string(n_samples) + " != " + std::to_string(n_samples_y) + ")");
+     127              :         }
+     128          323 :         if (n_features != featureNames.size()) {
+     129            2 :             throw std::invalid_argument("X and features must have the same number of features in Network::fit (" + std::to_string(n_features) + " != " + std::to_string(featureNames.size()) + ")");
+     130              :         }
+     131          321 :         if (features.size() == 0) {
+     132            2 :             throw std::invalid_argument("The network has not been initialized. You must call addNode() before calling fit()");
+     133              :         }
+     134          319 :         if (n_features != features.size() - 1) {
+     135            2 :             throw std::invalid_argument("X and local features must have the same number of features in Network::fit (" + std::to_string(n_features) + " != " + std::to_string(features.size() - 1) + ")");
+     136              :         }
+     137          317 :         if (find(features.begin(), features.end(), className) == features.end()) {
+     138            2 :             throw std::invalid_argument("Class Name not found in Network::features");
+     139              :         }
+     140         9296 :         for (auto& feature : featureNames) {
+     141         8983 :             if (find(features.begin(), features.end(), feature) == features.end()) {
+     142            2 :                 throw std::invalid_argument("Feature " + feature + " not found in Network::features");
+     143              :             }
+     144         8981 :             if (states.find(feature) == states.end()) {
+     145            0 :                 throw std::invalid_argument("Feature " + feature + " not found in states");
+     146              :             }
+     147              :         }
+     148          313 :     }
+     149          313 :     void Network::setStates(const std::map<std::string, std::vector<int>>& states)
+     150              :     {
+     151              :         // Set states to every Node in the network
+     152          313 :         for_each(features.begin(), features.end(), [this, &states](const std::string& feature) {
+     153         9288 :             nodes.at(feature)->setNumStates(states.at(feature).size());
+     154         9288 :             });
+     155          313 :         classNumStates = nodes.at(className)->getNumStates();
+     156          313 :     }
+     157              :     // X comes in nxm, where n is the number of features and m the number of samples
+     158            1 :     void Network::fit(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
+     159              :     {
+     160            1 :         checkFitData(X.size(1), X.size(0), y.size(0), featureNames, className, states, weights);
+     161            1 :         this->className = className;
+     162            1 :         torch::Tensor ytmp = torch::transpose(y.view({ y.size(0), 1 }), 0, 1);
+     163            3 :         samples = torch::cat({ X , ytmp }, 0);
+     164            5 :         for (int i = 0; i < featureNames.size(); ++i) {
+     165           12 :             auto row_feature = X.index({ i, "..." });
+     166            4 :         }
+     167            1 :         completeFit(states, weights);
+     168            6 :     }
+     169          305 :     void Network::fit(const torch::Tensor& samples, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
+     170              :     {
+     171          305 :         checkFitData(samples.size(1), samples.size(0) - 1, samples.size(1), featureNames, className, states, weights);
+     172          305 :         this->className = className;
+     173          305 :         this->samples = samples;
+     174          305 :         completeFit(states, weights);
+     175          305 :     }
+     176              :     // input_data comes in nxm, where n is the number of features and m the number of samples
+     177           21 :     void Network::fit(const std::vector<std::vector<int>>& input_data, const std::vector<int>& labels, const std::vector<double>& weights_, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states)
+     178              :     {
+     179           21 :         const torch::Tensor weights = torch::tensor(weights_, torch::kFloat64);
+     180           21 :         checkFitData(input_data[0].size(), input_data.size(), labels.size(), featureNames, className, states, weights);
+     181            7 :         this->className = className;
+     182              :         // Build tensor of samples (nxm) (n+1 because of the class)
+     183            7 :         samples = torch::zeros({ static_cast<int>(input_data.size() + 1), static_cast<int>(input_data[0].size()) }, torch::kInt32);
+     184           35 :         for (int i = 0; i < featureNames.size(); ++i) {
+     185          112 :             samples.index_put_({ i, "..." }, torch::tensor(input_data[i], torch::kInt32));
+     186              :         }
+     187           28 :         samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
+     188            7 :         completeFit(states, weights);
+     189           56 :     }
+     190          313 :     void Network::completeFit(const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights)
+     191              :     {
+     192          313 :         setStates(states);
+     193          313 :         laplaceSmoothing = 1.0 / samples.size(1); // To use in CPT computation
+     194          313 :         std::vector<std::thread> threads;
+     195         9601 :         for (auto& node : nodes) {
+     196         9288 :             threads.emplace_back([this, &node, &weights]() {
+     197         9288 :                 node.second->computeCPT(samples, features, laplaceSmoothing, weights);
+     198         9288 :                 });
+     199              :         }
+     200         9601 :         for (auto& thread : threads) {
+     201         9288 :             thread.join();
+     202              :         }
+     203          313 :         fitted = true;
+     204          313 :     }
+     205          549 :     torch::Tensor Network::predict_tensor(const torch::Tensor& samples, const bool proba)
+     206              :     {
+     207          549 :         if (!fitted) {
+     208            2 :             throw std::logic_error("You must call fit() before calling predict()");
+     209              :         }
+     210          547 :         torch::Tensor result;
+     211          547 :         result = torch::zeros({ samples.size(1), classNumStates }, torch::kFloat64);
+     212        96385 :         for (int i = 0; i < samples.size(1); ++i) {
+     213       287520 :             const torch::Tensor sample = samples.index({ "...", i });
+     214        95840 :             auto psample = predict_sample(sample);
+     215        95838 :             auto temp = torch::tensor(psample, torch::kFloat64);
+     216              :             //            result.index_put_({ i, "..." }, torch::tensor(predict_sample(sample), torch::kFloat64));
+     217       287514 :             result.index_put_({ i, "..." }, temp);
+     218        95840 :         }
+     219          545 :         if (proba)
+     220          304 :             return result;
+     221          482 :         return result.argmax(1);
+     222       192225 :     }
+     223              :     // Return mxn tensor of probabilities
+     224          304 :     torch::Tensor Network::predict_proba(const torch::Tensor& samples)
+     225              :     {
+     226          304 :         return predict_tensor(samples, true);
+     227              :     }
+     228              : 
+     229              :     // Return mxn tensor of probabilities
+     230          245 :     torch::Tensor Network::predict(const torch::Tensor& samples)
+     231              :     {
+     232          245 :         return predict_tensor(samples, false);
+     233              :     }
+     234              : 
+     235              :     // Return mx1 std::vector of predictions
+     236              :     // tsamples is nxm std::vector of samples
+     237           12 :     std::vector<int> Network::predict(const std::vector<std::vector<int>>& tsamples)
+     238              :     {
+     239           12 :         if (!fitted) {
+     240            4 :             throw std::logic_error("You must call fit() before calling predict()");
+     241              :         }
+     242            8 :         std::vector<int> predictions;
+     243            8 :         std::vector<int> sample;
+     244          891 :         for (int row = 0; row < tsamples[0].size(); ++row) {
+     245          885 :             sample.clear();
+     246         6563 :             for (int col = 0; col < tsamples.size(); ++col) {
+     247         5678 :                 sample.push_back(tsamples[col][row]);
+     248              :             }
+     249          885 :             std::vector<double> classProbabilities = predict_sample(sample);
+     250              :             // Find the class with the maximum posterior probability
+     251          883 :             auto maxElem = max_element(classProbabilities.begin(), classProbabilities.end());
+     252          883 :             int predictedClass = distance(classProbabilities.begin(), maxElem);
+     253          883 :             predictions.push_back(predictedClass);
+     254          883 :         }
+     255           12 :         return predictions;
+     256           10 :     }
+     257              :     // Return mxn std::vector of probabilities
+     258              :     // tsamples is nxm std::vector of samples
+     259           68 :     std::vector<std::vector<double>> Network::predict_proba(const std::vector<std::vector<int>>& tsamples)
+     260              :     {
+     261           68 :         if (!fitted) {
+     262            2 :             throw std::logic_error("You must call fit() before calling predict_proba()");
+     263              :         }
+     264           66 :         std::vector<std::vector<double>> predictions;
+     265           66 :         std::vector<int> sample;
+     266        12787 :         for (int row = 0; row < tsamples[0].size(); ++row) {
+     267        12721 :             sample.clear();
+     268       193587 :             for (int col = 0; col < tsamples.size(); ++col) {
+     269       180866 :                 sample.push_back(tsamples[col][row]);
+     270              :             }
+     271        12721 :             predictions.push_back(predict_sample(sample));
+     272              :         }
+     273          132 :         return predictions;
+     274           66 :     }
+     275            5 :     double Network::score(const std::vector<std::vector<int>>& tsamples, const std::vector<int>& labels)
+     276              :     {
+     277            5 :         std::vector<int> y_pred = predict(tsamples);
+     278            3 :         int correct = 0;
+     279          581 :         for (int i = 0; i < y_pred.size(); ++i) {
+     280          578 :             if (y_pred[i] == labels[i]) {
+     281          486 :                 correct++;
+     282              :             }
+     283              :         }
+     284            6 :         return (double)correct / y_pred.size();
+     285            3 :     }
+     286              :     // Return 1xn std::vector of probabilities
+     287        13606 :     std::vector<double> Network::predict_sample(const std::vector<int>& sample)
+     288              :     {
+     289              :         // Ensure the sample size is equal to the number of features
+     290        13606 :         if (sample.size() != features.size() - 1) {
+     291            4 :             throw std::invalid_argument("Sample size (" + std::to_string(sample.size()) +
+     292            6 :                 ") does not match the number of features (" + std::to_string(features.size() - 1) + ")");
+     293              :         }
+     294        13604 :         std::map<std::string, int> evidence;
+     295       200142 :         for (int i = 0; i < sample.size(); ++i) {
+     296       186538 :             evidence[features[i]] = sample[i];
+     297              :         }
+     298        27208 :         return exactInference(evidence);
+     299        13604 :     }
+     300              :     // Return 1xn std::vector of probabilities
+     301        95840 :     std::vector<double> Network::predict_sample(const torch::Tensor& sample)
+     302              :     {
+     303              :         // Ensure the sample size is equal to the number of features
+     304        95840 :         if (sample.size(0) != features.size() - 1) {
+     305            4 :             throw std::invalid_argument("Sample size (" + std::to_string(sample.size(0)) +
+     306            6 :                 ") does not match the number of features (" + std::to_string(features.size() - 1) + ")");
+     307              :         }
+     308        95838 :         std::map<std::string, int> evidence;
+     309      2448008 :         for (int i = 0; i < sample.size(0); ++i) {
+     310      2352170 :             evidence[features[i]] = sample[i].item<int>();
+     311              :         }
+     312       191676 :         return exactInference(evidence);
+     313        95838 :     }
+     314       437768 :     double Network::computeFactor(std::map<std::string, int>& completeEvidence)
+     315              :     {
+     316       437768 :         double result = 1.0;
+     317      6084992 :         for (auto& node : getNodes()) {
+     318      5647224 :             result *= node.second->getFactorValue(completeEvidence);
+     319              :         }
+     320       437768 :         return result;
+     321              :     }
+     322       109442 :     std::vector<double> Network::exactInference(std::map<std::string, int>& evidence)
+     323              :     {
+     324       109442 :         std::vector<double> result(classNumStates, 0.0);
+     325       109442 :         std::vector<std::thread> threads;
+     326       109442 :         std::mutex mtx;
+     327       547210 :         for (int i = 0; i < classNumStates; ++i) {
+     328       437768 :             threads.emplace_back([this, &result, &evidence, i, &mtx]() {
+     329       437768 :                 auto completeEvidence = std::map<std::string, int>(evidence);
+     330       437768 :                 completeEvidence[getClassName()] = i;
+     331       437768 :                 double factor = computeFactor(completeEvidence);
+     332       437768 :                 std::lock_guard<std::mutex> lock(mtx);
+     333       437768 :                 result[i] = factor;
+     334       437768 :                 });
+     335              :         }
+     336       547210 :         for (auto& thread : threads) {
+     337       437768 :             thread.join();
+     338              :         }
+     339              :         // Normalize result
+     340       109442 :         double sum = accumulate(result.begin(), result.end(), 0.0);
+     341       547210 :         transform(result.begin(), result.end(), result.begin(), [sum](const double& value) { return value / sum; });
+     342       218884 :         return result;
+     343       109442 :     }
+     344            7 :     std::vector<std::string> Network::show() const
+     345              :     {
+     346            7 :         std::vector<std::string> result;
+     347              :         // Draw the network
+     348           40 :         for (auto& node : nodes) {
+     349           33 :             std::string line = node.first + " -> ";
+     350           77 :             for (auto child : node.second->getChildren()) {
+     351           44 :                 line += child->getName() + ", ";
+     352              :             }
+     353           33 :             result.push_back(line);
+     354           33 :         }
+     355            7 :         return result;
+     356            0 :     }
+     357           22 :     std::vector<std::string> Network::graph(const std::string& title) const
+     358              :     {
+     359           22 :         auto output = std::vector<std::string>();
+     360           22 :         auto prefix = "digraph BayesNet {\nlabel=<BayesNet ";
+     361           22 :         auto suffix = ">\nfontsize=30\nfontcolor=blue\nlabelloc=t\nlayout=circo\n";
+     362           22 :         std::string header = prefix + title + suffix;
+     363           22 :         output.push_back(header);
+     364          175 :         for (auto& node : nodes) {
+     365          153 :             auto result = node.second->graph(className);
+     366          153 :             output.insert(output.end(), result.begin(), result.end());
+     367          153 :         }
+     368           22 :         output.push_back("}\n");
+     369           44 :         return output;
+     370           22 :     }
+     371           59 :     std::vector<std::pair<std::string, std::string>> Network::getEdges() const
+     372              :     {
+     373           59 :         auto edges = std::vector<std::pair<std::string, std::string>>();
+     374          937 :         for (const auto& node : nodes) {
+     375          878 :             auto head = node.first;
+     376         2456 :             for (const auto& child : node.second->getChildren()) {
+     377         1578 :                 auto tail = child->getName();
+     378         1578 :                 edges.push_back({ head, tail });
+     379         1578 :             }
+     380          878 :         }
+     381           59 :         return edges;
+     382            0 :     }
+     383           48 :     int Network::getNumEdges() const
+     384              :     {
+     385           48 :         return getEdges().size();
+     386              :     }
+     387           55 :     std::vector<std::string> Network::topological_sort()
+     388              :     {
+     389              :         /* Check if al the fathers of every node are before the node */
+     390           55 :         auto result = features;
+     391           55 :         result.erase(remove(result.begin(), result.end(), className), result.end());
+     392           55 :         bool ending{ false };
+     393          157 :         while (!ending) {
+     394          102 :             ending = true;
+     395          951 :             for (auto feature : features) {
+     396          849 :                 auto fathers = nodes[feature]->getParents();
+     397         2250 :                 for (const auto& father : fathers) {
+     398         1401 :                     auto fatherName = father->getName();
+     399         1401 :                     if (fatherName == className) {
+     400          745 :                         continue;
+     401              :                     }
+     402              :                     // Check if father is placed before the actual feature
+     403          656 :                     auto it = find(result.begin(), result.end(), fatherName);
+     404          656 :                     if (it != result.end()) {
+     405          656 :                         auto it2 = find(result.begin(), result.end(), feature);
+     406          656 :                         if (it2 != result.end()) {
+     407          656 :                             if (distance(it, it2) < 0) {
+     408              :                                 // if it is not, insert it before the feature
+     409           61 :                                 result.erase(remove(result.begin(), result.end(), fatherName), result.end());
+     410           61 :                                 result.insert(it2, fatherName);
+     411           61 :                                 ending = false;
+     412              :                             }
+     413              :                         } else {
+     414            0 :                             throw std::logic_error("Error in topological sort because of node " + feature + " is not in result");
+     415              :                         }
+     416              :                     } else {
+     417            0 :                         throw std::logic_error("Error in topological sort because of node father " + fatherName + " is not in result");
+     418              :                     }
+     419         1401 :                 }
+     420          849 :             }
+     421              :         }
+     422           55 :         return result;
+     423            0 :     }
+     424            2 :     std::string Network::dump_cpt() const
+     425              :     {
+     426            2 :         std::stringstream oss;
+     427           12 :         for (auto& node : nodes) {
+     428           10 :             oss << "* " << node.first << ": (" << node.second->getNumStates() << ") : " << node.second->getCPT().sizes() << std::endl;
+     429           10 :             oss << node.second->getCPT() << std::endl;
+     430              :         }
+     431            4 :         return oss.str();
+     432            2 :     }
+     433              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.h.func-c.html b/html/bayesnet/network/Network.h.func-c.html new file mode 100644 index 0000000..7b5a131 --- /dev/null +++ b/html/bayesnet/network/Network.h.func-c.html @@ -0,0 +1,82 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ +
+ + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7NetworkD2Ev711
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.h.func.html b/html/bayesnet/network/Network.h.func.html new file mode 100644 index 0000000..4076ad4 --- /dev/null +++ b/html/bayesnet/network/Network.h.func.html @@ -0,0 +1,82 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ +
+ + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7NetworkD2Ev711
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Network.h.gcov.html b/html/bayesnet/network/Network.h.gcov.html new file mode 100644 index 0000000..2ef5b1b --- /dev/null +++ b/html/bayesnet/network/Network.h.gcov.html @@ -0,0 +1,145 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Network.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Network.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef NETWORK_H
+       8              : #define NETWORK_H
+       9              : #include <map>
+      10              : #include <vector>
+      11              : #include "bayesnet/config.h"
+      12              : #include "Node.h"
+      13              : 
+      14              : namespace bayesnet {
+      15              :     class Network {
+      16              :     public:
+      17              :         Network();
+      18              :         explicit Network(float);
+      19              :         explicit Network(const Network&);
+      20          711 :         ~Network() = default;
+      21              :         torch::Tensor& getSamples();
+      22              :         float getMaxThreads() const;
+      23              :         void addNode(const std::string&);
+      24              :         void addEdge(const std::string&, const std::string&);
+      25              :         std::map<std::string, std::unique_ptr<Node>>& getNodes();
+      26              :         std::vector<std::string> getFeatures() const;
+      27              :         int getStates() const;
+      28              :         std::vector<std::pair<std::string, std::string>> getEdges() const;
+      29              :         int getNumEdges() const;
+      30              :         int getClassNumStates() const;
+      31              :         std::string getClassName() const;
+      32              :         /*
+      33              :         Notice: Nodes have to be inserted in the same order as they are in the dataset, i.e., first node is first column and so on.
+      34              :         */
+      35              :         void fit(const std::vector<std::vector<int>>& input_data, const std::vector<int>& labels, const std::vector<double>& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
+      36              :         void fit(const torch::Tensor& X, const torch::Tensor& y, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
+      37              :         void fit(const torch::Tensor& samples, const torch::Tensor& weights, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states);
+      38              :         std::vector<int> predict(const std::vector<std::vector<int>>&); // Return mx1 std::vector of predictions
+      39              :         torch::Tensor predict(const torch::Tensor&); // Return mx1 tensor of predictions
+      40              :         torch::Tensor predict_tensor(const torch::Tensor& samples, const bool proba);
+      41              :         std::vector<std::vector<double>> predict_proba(const std::vector<std::vector<int>>&); // Return mxn std::vector of probabilities
+      42              :         torch::Tensor predict_proba(const torch::Tensor&); // Return mxn tensor of probabilities
+      43              :         double score(const std::vector<std::vector<int>>&, const std::vector<int>&);
+      44              :         std::vector<std::string> topological_sort();
+      45              :         std::vector<std::string> show() const;
+      46              :         std::vector<std::string> graph(const std::string& title) const; // Returns a std::vector of std::strings representing the graph in graphviz format
+      47              :         void initialize();
+      48              :         std::string dump_cpt() const;
+      49              :         inline std::string version() { return  { project_version.begin(), project_version.end() }; }
+      50              :     private:
+      51              :         std::map<std::string, std::unique_ptr<Node>> nodes;
+      52              :         bool fitted;
+      53              :         float maxThreads = 0.95;
+      54              :         int classNumStates;
+      55              :         std::vector<std::string> features; // Including classname
+      56              :         std::string className;
+      57              :         double laplaceSmoothing;
+      58              :         torch::Tensor samples; // n+1xm tensor used to fit the model
+      59              :         bool isCyclic(const std::string&, std::unordered_set<std::string>&, std::unordered_set<std::string>&);
+      60              :         std::vector<double> predict_sample(const std::vector<int>&);
+      61              :         std::vector<double> predict_sample(const torch::Tensor&);
+      62              :         std::vector<double> exactInference(std::map<std::string, int>&);
+      63              :         double computeFactor(std::map<std::string, int>&);
+      64              :         void completeFit(const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
+      65              :         void checkFitData(int n_features, int n_samples, int n_samples_y, const std::vector<std::string>& featureNames, const std::string& className, const std::map<std::string, std::vector<int>>& states, const torch::Tensor& weights);
+      66              :         void setStates(const std::map<std::string, std::vector<int>>&);
+      67              :     };
+      68              : }
+      69              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Node.cc.func-c.html b/html/bayesnet/network/Node.cc.func-c.html new file mode 100644 index 0000000..032dde5 --- /dev/null +++ b/html/bayesnet/network/Node.cc.func-c.html @@ -0,0 +1,215 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Node.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Node.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %8884
Test Date:2024-04-21 16:43:29Functions:100.0 %2020
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4Node5clearEv1
_ZN8bayesnet4Node11removeChildEPS0_3
_ZN8bayesnet4Node12removeParentEPS0_3
_ZN8bayesnet4Node12combinationsERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EE5
_ZN8bayesnet4Node7minFillEv5
_ZN8bayesnet4Node6getCPTEv105
_ZN8bayesnet4Node5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE153
_ZZN8bayesnet4Node5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEENKUlRKT_E_clIPS0_EEDaSB_241
_ZN8bayesnet4Node10getParentsEv1268
_ZN8bayesnet4NodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE8887
_ZN8bayesnet4Node10computeCPTERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEdS4_9288
_ZN8bayesnet4Node12setNumStatesEi9288
_ZN8bayesnet4Node9addParentEPS0_16951
_ZN8bayesnet4Node8addChildEPS0_16953
_ZZN8bayesnet4Node10computeCPTERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEdS4_ENKUlRKT_E_clIPS0_EEDaSI_17649
_ZNK8bayesnet4Node12getNumStatesEv18032
_ZN8bayesnet4Node11getChildrenEv23263
_ZN8bayesnet4Node14getFactorValueERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE5647224
_ZZN8bayesnet4Node14getFactorValueERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlRKT_E_clIPS0_EEDaSI_9977196
_ZNK8bayesnet4Node7getNameB5cxx11Ev13264685
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Node.cc.func.html b/html/bayesnet/network/Node.cc.func.html new file mode 100644 index 0000000..3eca79f --- /dev/null +++ b/html/bayesnet/network/Node.cc.func.html @@ -0,0 +1,215 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Node.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Node.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %8884
Test Date:2024-04-21 16:43:29Functions:100.0 %2020
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet4Node10computeCPTERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEdS4_9288
_ZN8bayesnet4Node10getParentsEv1268
_ZN8bayesnet4Node11getChildrenEv23263
_ZN8bayesnet4Node11removeChildEPS0_3
_ZN8bayesnet4Node12combinationsERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EE5
_ZN8bayesnet4Node12removeParentEPS0_3
_ZN8bayesnet4Node12setNumStatesEi9288
_ZN8bayesnet4Node14getFactorValueERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEE5647224
_ZN8bayesnet4Node5clearEv1
_ZN8bayesnet4Node5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE153
_ZN8bayesnet4Node6getCPTEv105
_ZN8bayesnet4Node7minFillEv5
_ZN8bayesnet4Node8addChildEPS0_16953
_ZN8bayesnet4Node9addParentEPS0_16951
_ZN8bayesnet4NodeC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE8887
_ZNK8bayesnet4Node12getNumStatesEv18032
_ZNK8bayesnet4Node7getNameB5cxx11Ev13264685
_ZZN8bayesnet4Node10computeCPTERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EEdS4_ENKUlRKT_E_clIPS0_EEDaSI_17649
_ZZN8bayesnet4Node14getFactorValueERSt3mapINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiSt4lessIS7_ESaISt4pairIKS7_iEEEENKUlRKT_E_clIPS0_EEDaSI_9977196
_ZZN8bayesnet4Node5graphERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEENKUlRKT_E_clIPS0_EEDaSB_241
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/Node.cc.gcov.html b/html/bayesnet/network/Node.cc.gcov.html new file mode 100644 index 0000000..88a4c74 --- /dev/null +++ b/html/bayesnet/network/Node.cc.gcov.html @@ -0,0 +1,217 @@ + + + + + + + LCOV - coverage.info - bayesnet/network/Node.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/network - Node.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:95.5 %8884
Test Date:2024-04-21 16:43:29Functions:100.0 %2020
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "Node.h"
+       8              : 
+       9              : namespace bayesnet {
+      10              : 
+      11         8887 :     Node::Node(const std::string& name)
+      12         8887 :         : name(name), numStates(0), cpTable(torch::Tensor()), parents(std::vector<Node*>()), children(std::vector<Node*>())
+      13              :     {
+      14         8887 :     }
+      15            1 :     void Node::clear()
+      16              :     {
+      17            1 :         parents.clear();
+      18            1 :         children.clear();
+      19            1 :         cpTable = torch::Tensor();
+      20            1 :         dimensions.clear();
+      21            1 :         numStates = 0;
+      22            1 :     }
+      23     13264685 :     std::string Node::getName() const
+      24              :     {
+      25     13264685 :         return name;
+      26              :     }
+      27        16951 :     void Node::addParent(Node* parent)
+      28              :     {
+      29        16951 :         parents.push_back(parent);
+      30        16951 :     }
+      31            3 :     void Node::removeParent(Node* parent)
+      32              :     {
+      33            3 :         parents.erase(std::remove(parents.begin(), parents.end(), parent), parents.end());
+      34            3 :     }
+      35            3 :     void Node::removeChild(Node* child)
+      36              :     {
+      37            3 :         children.erase(std::remove(children.begin(), children.end(), child), children.end());
+      38            3 :     }
+      39        16953 :     void Node::addChild(Node* child)
+      40              :     {
+      41        16953 :         children.push_back(child);
+      42        16953 :     }
+      43         1268 :     std::vector<Node*>& Node::getParents()
+      44              :     {
+      45         1268 :         return parents;
+      46              :     }
+      47        23263 :     std::vector<Node*>& Node::getChildren()
+      48              :     {
+      49        23263 :         return children;
+      50              :     }
+      51        18032 :     int Node::getNumStates() const
+      52              :     {
+      53        18032 :         return numStates;
+      54              :     }
+      55         9288 :     void Node::setNumStates(int numStates)
+      56              :     {
+      57         9288 :         this->numStates = numStates;
+      58         9288 :     }
+      59          105 :     torch::Tensor& Node::getCPT()
+      60              :     {
+      61          105 :         return cpTable;
+      62              :     }
+      63              :     /*
+      64              :      The MinFill criterion is a heuristic for variable elimination.
+      65              :      The variable that minimizes the number of edges that need to be added to the graph to make it triangulated.
+      66              :      This is done by counting the number of edges that need to be added to the graph if the variable is eliminated.
+      67              :      The variable with the minimum number of edges is chosen.
+      68              :      Here this is done computing the length of the combinations of the node neighbors taken 2 by 2.
+      69              :     */
+      70            5 :     unsigned Node::minFill()
+      71              :     {
+      72            5 :         std::unordered_set<std::string> neighbors;
+      73           13 :         for (auto child : children) {
+      74            8 :             neighbors.emplace(child->getName());
+      75              :         }
+      76           12 :         for (auto parent : parents) {
+      77            7 :             neighbors.emplace(parent->getName());
+      78              :         }
+      79            5 :         auto source = std::vector<std::string>(neighbors.begin(), neighbors.end());
+      80           10 :         return combinations(source).size();
+      81            5 :     }
+      82            5 :     std::vector<std::pair<std::string, std::string>> Node::combinations(const std::vector<std::string>& source)
+      83              :     {
+      84            5 :         std::vector<std::pair<std::string, std::string>> result;
+      85           20 :         for (int i = 0; i < source.size(); ++i) {
+      86           15 :             std::string temp = source[i];
+      87           31 :             for (int j = i + 1; j < source.size(); ++j) {
+      88           16 :                 result.push_back({ temp, source[j] });
+      89              :             }
+      90           15 :         }
+      91            5 :         return result;
+      92            0 :     }
+      93         9288 :     void Node::computeCPT(const torch::Tensor& dataset, const std::vector<std::string>& features, const double laplaceSmoothing, const torch::Tensor& weights)
+      94              :     {
+      95         9288 :         dimensions.clear();
+      96              :         // Get dimensions of the CPT
+      97         9288 :         dimensions.push_back(numStates);
+      98        26937 :         transform(parents.begin(), parents.end(), back_inserter(dimensions), [](const auto& parent) { return parent->getNumStates(); });
+      99              : 
+     100              :         // Create a tensor of zeros with the dimensions of the CPT
+     101         9288 :         cpTable = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
+     102              :         // Fill table with counts
+     103         9288 :         auto pos = find(features.begin(), features.end(), name);
+     104         9288 :         if (pos == features.end()) {
+     105            0 :             throw std::logic_error("Feature " + name + " not found in dataset");
+     106              :         }
+     107         9288 :         int name_index = pos - features.begin();
+     108      1756738 :         for (int n_sample = 0; n_sample < dataset.size(1); ++n_sample) {
+     109      1747450 :             c10::List<c10::optional<at::Tensor>> coordinates;
+     110      5242350 :             coordinates.push_back(dataset.index({ name_index, n_sample }));
+     111      4997840 :             for (auto parent : parents) {
+     112      3250390 :                 pos = find(features.begin(), features.end(), parent->getName());
+     113      3250390 :                 if (pos == features.end()) {
+     114            0 :                     throw std::logic_error("Feature parent " + parent->getName() + " not found in dataset");
+     115              :                 }
+     116      3250390 :                 int parent_index = pos - features.begin();
+     117      9751170 :                 coordinates.push_back(dataset.index({ parent_index, n_sample }));
+     118              :             }
+     119              :             // Increment the count of the corresponding coordinate
+     120      3494900 :             cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + weights.index({ n_sample }).item<double>());
+     121      1747450 :         }
+     122              :         // Normalize the counts
+     123         9288 :         cpTable = cpTable / cpTable.sum(0);
+     124      6754578 :     }
+     125      5647224 :     float Node::getFactorValue(std::map<std::string, int>& evidence)
+     126              :     {
+     127      5647224 :         c10::List<c10::optional<at::Tensor>> coordinates;
+     128              :         // following predetermined order of indices in the cpTable (see Node.h)
+     129      5647224 :         coordinates.push_back(at::tensor(evidence[name]));
+     130     15624420 :         transform(parents.begin(), parents.end(), std::back_inserter(coordinates), [&evidence](const auto& parent) { return at::tensor(evidence[parent->getName()]); });
+     131     11294448 :         return cpTable.index({ coordinates }).item<float>();
+     132      5647224 :     }
+     133          153 :     std::vector<std::string> Node::graph(const std::string& className)
+     134              :     {
+     135          153 :         auto output = std::vector<std::string>();
+     136          153 :         auto suffix = name == className ? ", fontcolor=red, fillcolor=lightblue, style=filled " : "";
+     137          153 :         output.push_back(name + " [shape=circle" + suffix + "] \n");
+     138          394 :         transform(children.begin(), children.end(), back_inserter(output), [this](const auto& child) { return name + " -> " + child->getName(); });
+     139          153 :         return output;
+     140            0 :     }
+     141              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/index-sort-f.html b/html/bayesnet/network/index-sort-f.html new file mode 100644 index 0000000..b39ff17 --- /dev/null +++ b/html/bayesnet/network/index-sort-f.html @@ -0,0 +1,129 @@ + + + + + + + LCOV - coverage.info - bayesnet/network + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/networkCoverageTotalHit
Test:coverage.infoLines:97.2 %386375
Test Date:2024-04-21 16:43:29Functions:100.0 %6161
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Network.h +
100.0%
+
100.0 %11100.0 %11
Node.cc +
95.5%95.5%
+
95.5 %8884100.0 %2020
Network.cc +
97.6%97.6%
+
97.6 %297290100.0 %4040
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/index-sort-l.html b/html/bayesnet/network/index-sort-l.html new file mode 100644 index 0000000..e7c5e99 --- /dev/null +++ b/html/bayesnet/network/index-sort-l.html @@ -0,0 +1,129 @@ + + + + + + + LCOV - coverage.info - bayesnet/network + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/networkCoverageTotalHit
Test:coverage.infoLines:97.2 %386375
Test Date:2024-04-21 16:43:29Functions:100.0 %6161
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Node.cc +
95.5%95.5%
+
95.5 %8884100.0 %2020
Network.cc +
97.6%97.6%
+
97.6 %297290100.0 %4040
Network.h +
100.0%
+
100.0 %11100.0 %11
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/network/index.html b/html/bayesnet/network/index.html new file mode 100644 index 0000000..47b6d99 --- /dev/null +++ b/html/bayesnet/network/index.html @@ -0,0 +1,129 @@ + + + + + + + LCOV - coverage.info - bayesnet/network + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/networkCoverageTotalHit
Test:coverage.infoLines:97.2 %386375
Test Date:2024-04-21 16:43:29Functions:100.0 %6161
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
Network.cc +
97.6%97.6%
+
97.6 %297290100.0 %4040
Network.h +
100.0%
+
100.0 %11100.0 %11
Node.cc +
95.5%95.5%
+
95.5 %8884100.0 %2020
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.cc.func-c.html b/html/bayesnet/utils/BayesMetrics.cc.func-c.html new file mode 100644 index 0000000..7e2f43d --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.cc.func-c.html @@ -0,0 +1,159 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.5 %120111
Test Date:2024-04-21 16:43:29Functions:91.7 %1211
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Metrics22conditionalEdgeWeightsERSt6vectorIfSaIfEE0
_ZNK8bayesnet7Metrics14getScoresKBestEv8
_ZN8bayesnet7MetricsC2ERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_RKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISF_EERKSF_i16
_ZN8bayesnet7Metrics19maximumSpanningTreeERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKN2at6TensorEi29
_ZN8bayesnet7Metrics15conditionalEdgeERKN2at6TensorE34
_ZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbj90
_ZN8bayesnet7MetricsC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_i338
_ZZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbjENKUliiE_clEii453
_ZN8bayesnet7Metrics17mutualInformationERKN2at6TensorES4_S4_7684
_ZN8bayesnet7Metrics18conditionalEntropyERKN2at6TensorES4_S4_7684
_ZN8bayesnet7Metrics7entropyERKN2at6TensorES4_8506
_ZZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbjENKUliiE0_clEii12619
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.cc.func.html b/html/bayesnet/utils/BayesMetrics.cc.func.html new file mode 100644 index 0000000..a500dca --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.cc.func.html @@ -0,0 +1,159 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.5 %120111
Test Date:2024-04-21 16:43:29Functions:91.7 %1211
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Metrics15conditionalEdgeERKN2at6TensorE34
_ZN8bayesnet7Metrics17mutualInformationERKN2at6TensorES4_S4_7684
_ZN8bayesnet7Metrics18conditionalEntropyERKN2at6TensorES4_S4_7684
_ZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbj90
_ZN8bayesnet7Metrics19maximumSpanningTreeERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKN2at6TensorEi29
_ZN8bayesnet7Metrics22conditionalEdgeWeightsERSt6vectorIfSaIfEE0
_ZN8bayesnet7Metrics7entropyERKN2at6TensorES4_8506
_ZN8bayesnet7MetricsC2ERKN2at6TensorERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISB_EERKSB_i338
_ZN8bayesnet7MetricsC2ERKSt6vectorIS1_IiSaIiEESaIS3_EERKS3_RKS1_INSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaISF_EERKSF_i16
_ZNK8bayesnet7Metrics14getScoresKBestEv8
_ZZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbjENKUliiE0_clEii12619
_ZZN8bayesnet7Metrics19SelectKBestWeightedERKN2at6TensorEbjENKUliiE_clEii453
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.cc.gcov.html b/html/bayesnet/utils/BayesMetrics.cc.gcov.html new file mode 100644 index 0000000..57438ce --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.cc.gcov.html @@ -0,0 +1,245 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.5 %120111
Test Date:2024-04-21 16:43:29Functions:91.7 %1211
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include "Mst.h"
+       8              : #include "BayesMetrics.h"
+       9              : namespace bayesnet {
+      10              :     //samples is n+1xm tensor used to fit the model
+      11          338 :     Metrics::Metrics(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int classNumStates)
+      12          338 :         : samples(samples)
+      13          338 :         , features(features)
+      14          338 :         , className(className)
+      15          338 :         , classNumStates(classNumStates)
+      16              :     {
+      17          338 :     }
+      18              :     //samples is n+1xm std::vector used to fit the model
+      19           16 :     Metrics::Metrics(const std::vector<std::vector<int>>& vsamples, const std::vector<int>& labels, const std::vector<std::string>& features, const std::string& className, const int classNumStates)
+      20           16 :         : features(features)
+      21           16 :         , className(className)
+      22           16 :         , classNumStates(classNumStates)
+      23           32 :         , samples(torch::zeros({ static_cast<int>(vsamples.size() + 1), static_cast<int>(vsamples[0].size()) }, torch::kInt32))
+      24              :     {
+      25          128 :         for (int i = 0; i < vsamples.size(); ++i) {
+      26          448 :             samples.index_put_({ i,  "..." }, torch::tensor(vsamples[i], torch::kInt32));
+      27              :         }
+      28           64 :         samples.index_put_({ -1, "..." }, torch::tensor(labels, torch::kInt32));
+      29          144 :     }
+      30           90 :     std::vector<int> Metrics::SelectKBestWeighted(const torch::Tensor& weights, bool ascending, unsigned k)
+      31              :     {
+      32              :         // Return the K Best features 
+      33           90 :         auto n = features.size();
+      34           90 :         if (k == 0) {
+      35            0 :             k = n;
+      36              :         }
+      37              :         // compute scores
+      38           90 :         scoresKBest.clear();
+      39           90 :         featuresKBest.clear();
+      40          270 :         auto label = samples.index({ -1, "..." });
+      41         2837 :         for (int i = 0; i < n; ++i) {
+      42         8241 :             scoresKBest.push_back(mutualInformation(label, samples.index({ i, "..." }), weights));
+      43         2747 :             featuresKBest.push_back(i);
+      44              :         }
+      45              :         // sort & reduce scores and features
+      46           90 :         if (ascending) {
+      47           19 :             sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
+      48          453 :                 { return scoresKBest[i] < scoresKBest[j]; });
+      49           19 :             sort(scoresKBest.begin(), scoresKBest.end(), std::less<double>());
+      50           19 :             if (k < n) {
+      51           28 :                 for (int i = 0; i < n - k; ++i) {
+      52           20 :                     featuresKBest.erase(featuresKBest.begin());
+      53           20 :                     scoresKBest.erase(scoresKBest.begin());
+      54              :                 }
+      55              :             }
+      56              :         } else {
+      57           71 :             sort(featuresKBest.begin(), featuresKBest.end(), [&](int i, int j)
+      58        12619 :                 { return scoresKBest[i] > scoresKBest[j]; });
+      59           71 :             sort(scoresKBest.begin(), scoresKBest.end(), std::greater<double>());
+      60           71 :             featuresKBest.resize(k);
+      61           71 :             scoresKBest.resize(k);
+      62              :         }
+      63          180 :         return featuresKBest;
+      64         2927 :     }
+      65            8 :     std::vector<double> Metrics::getScoresKBest() const
+      66              :     {
+      67            8 :         return scoresKBest;
+      68              :     }
+      69              : 
+      70           34 :     torch::Tensor Metrics::conditionalEdge(const torch::Tensor& weights)
+      71              :     {
+      72           34 :         auto result = std::vector<double>();
+      73           34 :         auto source = std::vector<std::string>(features);
+      74           34 :         source.push_back(className);
+      75           34 :         auto combinations = doCombinations(source);
+      76              :         // Compute class prior
+      77           34 :         auto margin = torch::zeros({ classNumStates }, torch::kFloat);
+      78          184 :         for (int value = 0; value < classNumStates; ++value) {
+      79          600 :             auto mask = samples.index({ -1,  "..." }) == value;
+      80          150 :             margin[value] = mask.sum().item<double>() / samples.size(1);
+      81          150 :         }
+      82          918 :         for (auto [first, second] : combinations) {
+      83          884 :             int index_first = find(features.begin(), features.end(), first) - features.begin();
+      84          884 :             int index_second = find(features.begin(), features.end(), second) - features.begin();
+      85          884 :             double accumulated = 0;
+      86         5240 :             for (int value = 0; value < classNumStates; ++value) {
+      87        17424 :                 auto mask = samples.index({ -1, "..." }) == value;
+      88        13068 :                 auto first_dataset = samples.index({ index_first, mask });
+      89        13068 :                 auto second_dataset = samples.index({ index_second, mask });
+      90         8712 :                 auto weights_dataset = weights.index({ mask });
+      91         8712 :                 auto mi = mutualInformation(first_dataset, second_dataset, weights_dataset);
+      92         4356 :                 auto pb = margin[value].item<double>();
+      93         4356 :                 accumulated += pb * mi;
+      94         4356 :             }
+      95          884 :             result.push_back(accumulated);
+      96          884 :         }
+      97           34 :         long n_vars = source.size();
+      98           34 :         auto matrix = torch::zeros({ n_vars, n_vars });
+      99           34 :         auto indices = torch::triu_indices(n_vars, n_vars, 1);
+     100          918 :         for (auto i = 0; i < result.size(); ++i) {
+     101          884 :             auto x = indices[0][i];
+     102          884 :             auto y = indices[1][i];
+     103          884 :             matrix[x][y] = result[i];
+     104          884 :             matrix[y][x] = result[i];
+     105          884 :         }
+     106           68 :         return matrix;
+     107        21964 :     }
+     108              :     // To use in Python
+     109            0 :     std::vector<float> Metrics::conditionalEdgeWeights(std::vector<float>& weights_)
+     110              :     {
+     111            0 :         const torch::Tensor weights = torch::tensor(weights_);
+     112            0 :         auto matrix = conditionalEdge(weights);
+     113            0 :         std::vector<float> v(matrix.data_ptr<float>(), matrix.data_ptr<float>() + matrix.numel());
+     114            0 :         return v;
+     115            0 :     }
+     116         8506 :     double Metrics::entropy(const torch::Tensor& feature, const torch::Tensor& weights)
+     117              :     {
+     118         8506 :         torch::Tensor counts = feature.bincount(weights);
+     119         8506 :         double totalWeight = counts.sum().item<double>();
+     120         8506 :         torch::Tensor probs = counts.to(torch::kFloat) / totalWeight;
+     121         8506 :         torch::Tensor logProbs = torch::log(probs);
+     122         8506 :         torch::Tensor entropy = -probs * logProbs;
+     123        17012 :         return entropy.nansum().item<double>();
+     124         8506 :     }
+     125              :     // H(Y|X) = sum_{x in X} p(x) H(Y|X=x)
+     126         7684 :     double Metrics::conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
+     127              :     {
+     128         7684 :         int numSamples = firstFeature.sizes()[0];
+     129         7684 :         torch::Tensor featureCounts = secondFeature.bincount(weights);
+     130         7684 :         std::unordered_map<int, std::unordered_map<int, double>> jointCounts;
+     131         7684 :         double totalWeight = 0;
+     132       991784 :         for (auto i = 0; i < numSamples; i++) {
+     133       984100 :             jointCounts[secondFeature[i].item<int>()][firstFeature[i].item<int>()] += weights[i].item<double>();
+     134       984100 :             totalWeight += weights[i].item<float>();
+     135              :         }
+     136         7684 :         if (totalWeight == 0)
+     137            0 :             return 0;
+     138         7684 :         double entropyValue = 0;
+     139        26852 :         for (int value = 0; value < featureCounts.sizes()[0]; ++value) {
+     140        19168 :             double p_f = featureCounts[value].item<double>() / totalWeight;
+     141        19168 :             double entropy_f = 0;
+     142        57129 :             for (auto& [label, jointCount] : jointCounts[value]) {
+     143        37961 :                 double p_l_f = jointCount / featureCounts[value].item<double>();
+     144        37961 :                 if (p_l_f > 0) {
+     145        37961 :                     entropy_f -= p_l_f * log(p_l_f);
+     146              :                 } else {
+     147            0 :                     entropy_f = 0;
+     148              :                 }
+     149              :             }
+     150        19168 :             entropyValue += p_f * entropy_f;
+     151              :         }
+     152         7684 :         return entropyValue;
+     153         7684 :     }
+     154              :     // I(X;Y) = H(Y) - H(Y|X)
+     155         7684 :     double Metrics::mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights)
+     156              :     {
+     157         7684 :         return entropy(firstFeature, weights) - conditionalEntropy(firstFeature, secondFeature, weights);
+     158              :     }
+     159              :     /*
+     160              :     Compute the maximum spanning tree considering the weights as distances
+     161              :     and the indices of the weights as nodes of this square matrix using
+     162              :     Kruskal algorithm
+     163              :     */
+     164           29 :     std::vector<std::pair<int, int>> Metrics::maximumSpanningTree(const std::vector<std::string>& features, const torch::Tensor& weights, const int root)
+     165              :     {
+     166           29 :         auto mst = MST(features, weights, root);
+     167           58 :         return mst.maximumSpanningTree();
+     168           29 :     }
+     169              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.h.func-c.html b/html/bayesnet/utils/BayesMetrics.h.func-c.html new file mode 100644 index 0000000..3efc16e --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.h.func-c.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %1312
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Metrics9pop_firstIiEET_RSt6vectorIS2_SaIS2_EE10
_ZN8bayesnet7Metrics14doCombinationsIiEESt6vectorISt4pairIT_S4_ESaIS5_EERKS2_IS4_SaIS4_EE192
_ZN8bayesnet7Metrics14doCombinationsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESt6vectorISt4pairIT_SA_ESaISB_EERKS8_ISA_SaISA_EE34
_ZN8bayesnet7Metrics14doCombinationsIiEESt6vectorISt4pairIT_S4_ESaIS5_EERKS2_IS4_SaIS4_EE158
_ZN8bayesnet7MetricsC2Ev413
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.h.func.html b/html/bayesnet/utils/BayesMetrics.h.func.html new file mode 100644 index 0000000..c9ce940 --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.h.func.html @@ -0,0 +1,110 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %1312
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet7Metrics14doCombinationsIiEESt6vectorISt4pairIT_S4_ESaIS5_EERKS2_IS4_SaIS4_EE192
_ZN8bayesnet7Metrics14doCombinationsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEESt6vectorISt4pairIT_SA_ESaISB_EERKS8_ISA_SaISA_EE34
_ZN8bayesnet7Metrics14doCombinationsIiEESt6vectorISt4pairIT_S4_ESaIS5_EERKS2_IS4_SaIS4_EE158
_ZN8bayesnet7Metrics9pop_firstIiEET_RSt6vectorIS2_SaIS2_EE10
_ZN8bayesnet7MetricsC2Ev413
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/BayesMetrics.h.gcov.html b/html/bayesnet/utils/BayesMetrics.h.gcov.html new file mode 100644 index 0000000..8f74599 --- /dev/null +++ b/html/bayesnet/utils/BayesMetrics.h.gcov.html @@ -0,0 +1,131 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/BayesMetrics.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - BayesMetrics.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.3 %1312
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef BAYESNET_METRICS_H
+       8              : #define BAYESNET_METRICS_H
+       9              : #include <vector>
+      10              : #include <string>
+      11              : #include <torch/torch.h>
+      12              : namespace bayesnet {
+      13              :     class Metrics {
+      14              :     public:
+      15          413 :         Metrics() = default;
+      16              :         Metrics(const torch::Tensor& samples, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
+      17              :         Metrics(const std::vector<std::vector<int>>& vsamples, const std::vector<int>& labels, const std::vector<std::string>& features, const std::string& className, const int classNumStates);
+      18              :         std::vector<int> SelectKBestWeighted(const torch::Tensor& weights, bool ascending = false, unsigned k = 0);
+      19              :         std::vector<double> getScoresKBest() const;
+      20              :         double mutualInformation(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
+      21              :         std::vector<float> conditionalEdgeWeights(std::vector<float>& weights); // To use in Python
+      22              :         torch::Tensor conditionalEdge(const torch::Tensor& weights);
+      23              :         std::vector<std::pair<int, int>> maximumSpanningTree(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
+      24              :     protected:
+      25              :         torch::Tensor samples; // n+1xm torch::Tensor used to fit the model where samples[-1] is the y std::vector
+      26              :         std::string className;
+      27              :         double entropy(const torch::Tensor& feature, const torch::Tensor& weights);
+      28              :         std::vector<std::string> features;
+      29              :         template <class T>
+      30          192 :         std::vector<std::pair<T, T>> doCombinations(const std::vector<T>& source)
+      31              :         {
+      32          192 :             std::vector<std::pair<T, T>> result;
+      33         1013 :             for (int i = 0; i < source.size(); ++i) {
+      34          821 :                 T temp = source[i];
+      35         2590 :                 for (int j = i + 1; j < source.size(); ++j) {
+      36         1769 :                     result.push_back({ temp, source[j] });
+      37              :                 }
+      38              :             }
+      39          192 :             return result;
+      40            0 :         }
+      41              :         template <class T>
+      42           10 :         T pop_first(std::vector<T>& v)
+      43              :         {
+      44           10 :             T temp = v[0];
+      45           10 :             v.erase(v.begin());
+      46           10 :             return temp;
+      47              :         }
+      48              :     private:
+      49              :         int classNumStates = 0;
+      50              :         std::vector<double> scoresKBest;
+      51              :         std::vector<int> featuresKBest; // sorted indices of the features
+      52              :         double conditionalEntropy(const torch::Tensor& firstFeature, const torch::Tensor& secondFeature, const torch::Tensor& weights);
+      53              :     };
+      54              : }
+      55              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.cc.func-c.html b/html/bayesnet/utils/Mst.cc.func-c.html new file mode 100644 index 0000000..1083dcf --- /dev/null +++ b/html/bayesnet/utils/Mst.cc.func-c.html @@ -0,0 +1,145 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:94.1 %6864
Test Date:2024-04-21 16:43:29Functions:100.0 %1010
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet3MST19maximumSpanningTreeEv29
_ZN8bayesnet3MSTC2ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKN2at6TensorEi29
_ZN8bayesnet5Graph17kruskal_algorithmEv29
_ZN8bayesnet5GraphC2Ei29
_ZN8bayesnet7reorderESt6vectorISt4pairIfS1_IiiEESaIS3_EEi29
_ZN8bayesnet13insertElementERNSt7__cxx114listIiSaIiEEEi159
_ZN8bayesnet5Graph9union_setEii159
_ZN8bayesnet5Graph7addEdgeEiif576
_ZZN8bayesnet5Graph17kruskal_algorithmEvENKUlRKT_RKT0_E_clISt4pairIfS9_IiiEESB_EEDaS3_S6_2213
_ZN8bayesnet5Graph8find_setEi2663
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.cc.func.html b/html/bayesnet/utils/Mst.cc.func.html new file mode 100644 index 0000000..bdace1b --- /dev/null +++ b/html/bayesnet/utils/Mst.cc.func.html @@ -0,0 +1,145 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:94.1 %6864
Test Date:2024-04-21 16:43:29Functions:100.0 %1010
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet13insertElementERNSt7__cxx114listIiSaIiEEEi159
_ZN8bayesnet3MST19maximumSpanningTreeEv29
_ZN8bayesnet3MSTC2ERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EERKN2at6TensorEi29
_ZN8bayesnet5Graph17kruskal_algorithmEv29
_ZN8bayesnet5Graph7addEdgeEiif576
_ZN8bayesnet5Graph8find_setEi2663
_ZN8bayesnet5Graph9union_setEii159
_ZN8bayesnet5GraphC2Ei29
_ZN8bayesnet7reorderESt6vectorISt4pairIfS1_IiiEESaIS3_EEi29
_ZZN8bayesnet5Graph17kruskal_algorithmEvENKUlRKT_RKT0_E_clISt4pairIfS9_IiiEESB_EEDaS3_S6_2213
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.cc.gcov.html b/html/bayesnet/utils/Mst.cc.gcov.html new file mode 100644 index 0000000..ca89f3e --- /dev/null +++ b/html/bayesnet/utils/Mst.cc.gcov.html @@ -0,0 +1,196 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:94.1 %6864
Test Date:2024-04-21 16:43:29Functions:100.0 %1010
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #include <sstream>
+       8              : #include <vector>
+       9              : #include <list>
+      10              : #include "Mst.h"
+      11              : /*
+      12              :     Based on the code from https://www.softwaretestinghelp.com/minimum-spanning-tree-tutorial/
+      13              : 
+      14              : */
+      15              : 
+      16              : namespace bayesnet {
+      17           58 :     Graph::Graph(int V) : V(V), parent(std::vector<int>(V))
+      18              :     {
+      19          217 :         for (int i = 0; i < V; i++)
+      20          188 :             parent[i] = i;
+      21           29 :         G.clear();
+      22           29 :         T.clear();
+      23           29 :     }
+      24          576 :     void Graph::addEdge(int u, int v, float wt)
+      25              :     {
+      26          576 :         G.push_back({ wt, { u, v } });
+      27          576 :     }
+      28         2663 :     int Graph::find_set(int i)
+      29              :     {
+      30              :         // If i is the parent of itself
+      31         2663 :         if (i == parent[i])
+      32         1152 :             return i;
+      33              :         else
+      34              :             //else recursively find the parent of i
+      35         1511 :             return find_set(parent[i]);
+      36              :     }
+      37          159 :     void Graph::union_set(int u, int v)
+      38              :     {
+      39          159 :         parent[u] = parent[v];
+      40          159 :     }
+      41           29 :     void Graph::kruskal_algorithm()
+      42              :     {
+      43              :         // sort the edges ordered on decreasing weight
+      44         2242 :         stable_sort(G.begin(), G.end(), [](const auto& left, const auto& right) {return left.first > right.first;});
+      45          605 :         for (int i = 0; i < G.size(); i++) {
+      46              :             int uSt, vEd;
+      47          576 :             uSt = find_set(G[i].second.first);
+      48          576 :             vEd = find_set(G[i].second.second);
+      49          576 :             if (uSt != vEd) {
+      50          159 :                 T.push_back(G[i]); // add to mst std::vector
+      51          159 :                 union_set(uSt, vEd);
+      52              :             }
+      53              :         }
+      54           29 :     }
+      55              : 
+      56          159 :     void insertElement(std::list<int>& variables, int variable)
+      57              :     {
+      58          159 :         if (std::find(variables.begin(), variables.end(), variable) == variables.end()) {
+      59          159 :             variables.push_front(variable);
+      60              :         }
+      61          159 :     }
+      62              : 
+      63           29 :     std::vector<std::pair<int, int>> reorder(std::vector<std::pair<float, std::pair<int, int>>> T, int root_original)
+      64              :     {
+      65              :         // Create the edges of a DAG from the MST
+      66              :         // replacing unordered_set with list because unordered_set cannot guarantee the order of the elements inserted
+      67           29 :         auto result = std::vector<std::pair<int, int>>();
+      68           29 :         auto visited = std::vector<int>();
+      69           29 :         auto nextVariables = std::list<int>();
+      70           29 :         nextVariables.push_front(root_original);
+      71          217 :         while (nextVariables.size() > 0) {
+      72          188 :             int root = nextVariables.front();
+      73          188 :             nextVariables.pop_front();
+      74          664 :             for (int i = 0; i < T.size(); ++i) {
+      75          476 :                 auto [weight, edge] = T[i];
+      76          476 :                 auto [from, to] = edge;
+      77          476 :                 if (from == root || to == root) {
+      78          159 :                     visited.insert(visited.begin(), i);
+      79          159 :                     if (from == root) {
+      80          106 :                         result.push_back({ from, to });
+      81          106 :                         insertElement(nextVariables, to);
+      82              :                     } else {
+      83           53 :                         result.push_back({ to, from });
+      84           53 :                         insertElement(nextVariables, from);
+      85              :                     }
+      86              :                 }
+      87              :             }
+      88              :             // Remove visited
+      89          347 :             for (int i = 0; i < visited.size(); ++i) {
+      90          159 :                 T.erase(T.begin() + visited[i]);
+      91              :             }
+      92          188 :             visited.clear();
+      93              :         }
+      94           29 :         if (T.size() > 0) {
+      95            0 :             for (int i = 0; i < T.size(); ++i) {
+      96            0 :                 auto [weight, edge] = T[i];
+      97            0 :                 auto [from, to] = edge;
+      98            0 :                 result.push_back({ from, to });
+      99              :             }
+     100              :         }
+     101           58 :         return result;
+     102           29 :     }
+     103              : 
+     104           29 :     MST::MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root) : features(features), weights(weights), root(root) {}
+     105           29 :     std::vector<std::pair<int, int>> MST::maximumSpanningTree()
+     106              :     {
+     107           29 :         auto num_features = features.size();
+     108           29 :         Graph g(num_features);
+     109              :         // Make a complete graph
+     110          188 :         for (int i = 0; i < num_features - 1; ++i) {
+     111          735 :             for (int j = i + 1; j < num_features; ++j) {
+     112          576 :                 g.addEdge(i, j, weights[i][j].item<float>());
+     113              :             }
+     114              :         }
+     115           29 :         g.kruskal_algorithm();
+     116           29 :         auto mst = g.get_mst();
+     117           58 :         return reorder(mst, root);
+     118           29 :     }
+     119              : 
+     120              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.h.func-c.html b/html/bayesnet/utils/Mst.h.func-c.html new file mode 100644 index 0000000..b5801f5 --- /dev/null +++ b/html/bayesnet/utils/Mst.h.func-c.html @@ -0,0 +1,82 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ +
+ + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5Graph7get_mstEv29
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.h.func.html b/html/bayesnet/utils/Mst.h.func.html new file mode 100644 index 0000000..6350287 --- /dev/null +++ b/html/bayesnet/utils/Mst.h.func.html @@ -0,0 +1,82 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.h - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ +
+ + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet5Graph7get_mstEv29
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/Mst.h.gcov.html b/html/bayesnet/utils/Mst.h.gcov.html new file mode 100644 index 0000000..202ec88 --- /dev/null +++ b/html/bayesnet/utils/Mst.h.gcov.html @@ -0,0 +1,114 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/Mst.h + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - Mst.h (source / functions)CoverageTotalHit
Test:coverage.infoLines:100.0 %11
Test Date:2024-04-21 16:43:29Functions:100.0 %11
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : #ifndef MST_H
+       8              : #define MST_H
+       9              : #include <vector>
+      10              : #include <string>
+      11              : #include <torch/torch.h>
+      12              : namespace bayesnet {
+      13              :     class MST {
+      14              :     public:
+      15              :         MST() = default;
+      16              :         MST(const std::vector<std::string>& features, const torch::Tensor& weights, const int root);
+      17              :         std::vector<std::pair<int, int>> maximumSpanningTree();
+      18              :     private:
+      19              :         torch::Tensor weights;
+      20              :         std::vector<std::string> features;
+      21              :         int root = 0;
+      22              :     };
+      23              :     class Graph {
+      24              :     public:
+      25              :         explicit Graph(int V);
+      26              :         void addEdge(int u, int v, float wt);
+      27              :         int find_set(int i);
+      28              :         void union_set(int u, int v);
+      29              :         void kruskal_algorithm();
+      30           29 :         std::vector <std::pair<float, std::pair<int, int>>> get_mst() { return T; }
+      31              :     private:
+      32              :         int V;      // number of nodes in graph
+      33              :         std::vector <std::pair<float, std::pair<int, int>>> G; // std::vector for graph
+      34              :         std::vector <std::pair<float, std::pair<int, int>>> T; // std::vector for mst
+      35              :         std::vector<int> parent;
+      36              :     };
+      37              : }
+      38              : #endif
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/bayesnetUtils.cc.func-c.html b/html/bayesnet/utils/bayesnetUtils.cc.func-c.html new file mode 100644 index 0000000..27e5275 --- /dev/null +++ b/html/bayesnet/utils/bayesnetUtils.cc.func-c.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/bayesnetUtils.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - bayesnetUtils.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.0 %2523
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet20tensorToVectorDoubleERN2at6TensorE7
_ZN8bayesnet14vectorToTensorERSt6vectorIS0_IiSaIiEESaIS2_EEb9
_ZN8bayesnet7argsortERSt6vectorIdSaIdEE29
_ZZN8bayesnet7argsortERSt6vectorIdSaIdEEENKUliiE_clEii567
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/bayesnetUtils.cc.func.html b/html/bayesnet/utils/bayesnetUtils.cc.func.html new file mode 100644 index 0000000..0ade43a --- /dev/null +++ b/html/bayesnet/utils/bayesnetUtils.cc.func.html @@ -0,0 +1,103 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/bayesnetUtils.cc - functions + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - bayesnetUtils.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.0 %2523
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Function Name Sort by function nameHit count Sort by function hit count
_ZN8bayesnet14vectorToTensorERSt6vectorIS0_IiSaIiEESaIS2_EEb9
_ZN8bayesnet20tensorToVectorDoubleERN2at6TensorE7
_ZN8bayesnet7argsortERSt6vectorIdSaIdEE29
_ZZN8bayesnet7argsortERSt6vectorIdSaIdEEENKUliiE_clEii567
+
+
+ + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/bayesnetUtils.cc.gcov.html b/html/bayesnet/utils/bayesnetUtils.cc.gcov.html new file mode 100644 index 0000000..627c05a --- /dev/null +++ b/html/bayesnet/utils/bayesnetUtils.cc.gcov.html @@ -0,0 +1,120 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils/bayesnetUtils.cc + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utils - bayesnetUtils.cc (source / functions)CoverageTotalHit
Test:coverage.infoLines:92.0 %2523
Test Date:2024-04-21 16:43:29Functions:100.0 %44
+
+ + + + + + + + +

+
            Line data    Source code
+
+       1              : // ***************************************************************
+       2              : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
+       3              : // SPDX-FileType: SOURCE
+       4              : // SPDX-License-Identifier: MIT
+       5              : // ***************************************************************
+       6              : 
+       7              : 
+       8              : #include "bayesnetUtils.h"
+       9              : namespace bayesnet {
+      10              :     // Return the indices in descending order
+      11           29 :     std::vector<int> argsort(std::vector<double>& nums)
+      12              :     {
+      13           29 :         int n = nums.size();
+      14           29 :         std::vector<int> indices(n);
+      15           29 :         iota(indices.begin(), indices.end(), 0);
+      16          596 :         sort(indices.begin(), indices.end(), [&nums](int i, int j) {return nums[i] > nums[j];});
+      17           29 :         return indices;
+      18            0 :     }
+      19            7 :     std::vector<std::vector<double>> tensorToVectorDouble(torch::Tensor& dtensor)
+      20              :     {
+      21              :         // convert mxn tensor to mxn std::vector
+      22            7 :         std::vector<std::vector<double>> result;
+      23              :         // Iterate over cols
+      24         1803 :         for (int i = 0; i < dtensor.size(0); ++i) {
+      25         5388 :             auto col_tensor = dtensor.index({ i, "..." });
+      26         1796 :             auto col = std::vector<double>(col_tensor.data_ptr<float>(), col_tensor.data_ptr<float>() + dtensor.size(1));
+      27         1796 :             result.push_back(col);
+      28         1796 :         }
+      29            7 :         return result;
+      30         1796 :     }
+      31            9 :     torch::Tensor vectorToTensor(std::vector<std::vector<int>>& vector, bool transpose)
+      32              :     {
+      33              :         // convert nxm std::vector to mxn tensor if transpose
+      34            9 :         long int m = transpose ? vector[0].size() : vector.size();
+      35            9 :         long int n = transpose ? vector.size() : vector[0].size();
+      36            9 :         auto tensor = torch::zeros({ m, n }, torch::kInt32);
+      37           59 :         for (int i = 0; i < m; ++i) {
+      38        12481 :             for (int j = 0; j < n; ++j) {
+      39        12431 :                 tensor[i][j] = transpose ? vector[j][i] : vector[i][j];
+      40              :             }
+      41              :         }
+      42            9 :         return tensor;
+      43            0 :     }
+      44              : }
+        
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/index-sort-f.html b/html/bayesnet/utils/index-sort-f.html new file mode 100644 index 0000000..23233b4 --- /dev/null +++ b/html/bayesnet/utils/index-sort-f.html @@ -0,0 +1,153 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utilsCoverageTotalHit
Test:coverage.infoLines:93.0 %227211
Test Date:2024-04-21 16:43:29Functions:96.8 %3130
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
BayesMetrics.cc +
92.5%92.5%
+
92.5 %12011191.7 %1211
Mst.h +
100.0%
+
100.0 %11100.0 %11
BayesMetrics.h +
92.3%92.3%
+
92.3 %1312100.0 %44
bayesnetUtils.cc +
92.0%92.0%
+
92.0 %2523100.0 %44
Mst.cc +
94.1%94.1%
+
94.1 %6864100.0 %1010
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/index-sort-l.html b/html/bayesnet/utils/index-sort-l.html new file mode 100644 index 0000000..7fe8316 --- /dev/null +++ b/html/bayesnet/utils/index-sort-l.html @@ -0,0 +1,153 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utilsCoverageTotalHit
Test:coverage.infoLines:93.0 %227211
Test Date:2024-04-21 16:43:29Functions:96.8 %3130
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
bayesnetUtils.cc +
92.0%92.0%
+
92.0 %2523100.0 %44
BayesMetrics.h +
92.3%92.3%
+
92.3 %1312100.0 %44
BayesMetrics.cc +
92.5%92.5%
+
92.5 %12011191.7 %1211
Mst.cc +
94.1%94.1%
+
94.1 %6864100.0 %1010
Mst.h +
100.0%
+
100.0 %11100.0 %11
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/bayesnet/utils/index.html b/html/bayesnet/utils/index.html new file mode 100644 index 0000000..264a0a1 --- /dev/null +++ b/html/bayesnet/utils/index.html @@ -0,0 +1,153 @@ + + + + + + + LCOV - coverage.info - bayesnet/utils + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top level - bayesnet/utilsCoverageTotalHit
Test:coverage.infoLines:93.0 %227211
Test Date:2024-04-21 16:43:29Functions:96.8 %3130
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Filename Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
BayesMetrics.cc +
92.5%92.5%
+
92.5 %12011191.7 %1211
BayesMetrics.h +
92.3%92.3%
+
92.3 %1312100.0 %44
Mst.cc +
94.1%94.1%
+
94.1 %6864100.0 %1010
Mst.h +
100.0%
+
100.0 %11100.0 %11
bayesnetUtils.cc +
92.0%92.0%
+
92.0 %2523100.0 %44
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/cmd_line b/html/cmd_line new file mode 100644 index 0000000..80b2ab2 --- /dev/null +++ b/html/cmd_line @@ -0,0 +1 @@ +genhtml build_debug/tests/coverage.info --output-directory html diff --git a/html/emerald.png b/html/emerald.png new file mode 100644 index 0000000..38ad4f4 Binary files /dev/null and b/html/emerald.png differ diff --git a/html/gcov.css b/html/gcov.css new file mode 100644 index 0000000..f329042 --- /dev/null +++ b/html/gcov.css @@ -0,0 +1,1073 @@ +/* All views: initial background and text color */ +body +{ + color: #000000; + background-color: #ffffff; +} + +/* All views: standard link format*/ +a:link +{ + color: #284fa8; + text-decoration: underline; +} + +/* All views: standard link - visited format */ +a:visited +{ + color: #00cb40; + text-decoration: underline; +} + +/* All views: standard link - activated format */ +a:active +{ + color: #ff0040; + text-decoration: underline; +} + +/* All views: main title format */ +td.title +{ + text-align: center; + padding-bottom: 10px; + font-family: sans-serif; + font-size: 20pt; + font-style: italic; + font-weight: bold; +} +/* "Line coverage date bins" leader */ +td.subTableHeader +{ + text-align: center; + padding-bottom: 6px; + font-family: sans-serif; + font-weight: bold; + vertical-align: center; +} + +/* All views: header item format */ +td.headerItem +{ + text-align: right; + padding-right: 6px; + font-family: sans-serif; + font-weight: bold; + vertical-align: top; + white-space: nowrap; +} + +/* All views: header item value format */ +td.headerValue +{ + text-align: left; + color: #284fa8; + font-family: sans-serif; + font-weight: bold; + white-space: nowrap; +} + +/* All views: header item coverage table heading */ +td.headerCovTableHead +{ + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; +} + +/* All views: header item coverage table entry */ +td.headerCovTableEntry +{ + text-align: right; + color: #284fa8; + font-family: sans-serif; + font-weight: bold; + white-space: nowrap; + padding-left: 12px; + padding-right: 4px; + background-color: #dae7fe; +} + +/* All views: header item coverage table entry for high coverage rate */ +td.headerCovTableEntryHi +{ + text-align: right; + color: #000000; + font-family: sans-serif; + font-weight: bold; + white-space: nowrap; + padding-left: 12px; + padding-right: 4px; + background-color: #a7fc9d; +} + +/* All views: header item coverage table entry for medium coverage rate */ +td.headerCovTableEntryMed +{ + text-align: right; + color: #000000; + font-family: sans-serif; + font-weight: bold; + white-space: nowrap; + padding-left: 12px; + padding-right: 4px; + background-color: #ffea20; +} + +/* All views: header item coverage table entry for ow coverage rate */ +td.headerCovTableEntryLo +{ + text-align: right; + color: #000000; + font-family: sans-serif; + font-weight: bold; + white-space: nowrap; + padding-left: 12px; + padding-right: 4px; + background-color: #ff0000; +} + +/* All views: header legend value for legend entry */ +td.headerValueLeg +{ + text-align: left; + color: #000000; + font-family: sans-serif; + font-size: 80%; + white-space: nowrap; + padding-top: 4px; +} + +/* All views: color of horizontal ruler */ +td.ruler +{ + background-color: #6688d4; +} + +/* All views: version string format */ +td.versionInfo +{ + text-align: center; + padding-top: 2px; + font-family: sans-serif; + font-style: italic; +} + +/* Directory view/File view (all)/Test case descriptions: + table headline format */ +td.tableHead +{ + text-align: center; + color: #ffffff; + background-color: #6688d4; + font-family: sans-serif; + font-size: 120%; + font-weight: bold; + white-space: nowrap; + padding-left: 4px; + padding-right: 4px; +} + +span.tableHeadSort +{ + padding-right: 4px; +} + +/* Directory view/File view (all): filename entry format */ +td.coverFile +{ + text-align: left; + padding-left: 10px; + padding-right: 20px; + color: #284fa8; + background-color: #dae7fe; + font-family: monospace; +} + +/* Directory view/File view (all): filename entry format */ +td.overallOwner +{ + text-align: center; + font-weight: bold; + font-family: sans-serif; + background-color: #dae7fe; + padding-right: 10px; + padding-left: 10px; +} + +/* Directory view/File view (all): filename entry format */ +td.ownerName +{ + text-align: right; + font-style: italic; + font-family: sans-serif; + background-color: #E5DBDB; + padding-right: 10px; + padding-left: 20px; +} + +/* Directory view/File view (all): bar-graph entry format*/ +td.coverBar +{ + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; +} + +/* Directory view/File view (all): bar-graph entry format*/ +td.owner_coverBar +{ + padding-left: 10px; + padding-right: 10px; + background-color: #E5DBDB; +} + +/* Directory view/File view (all): bar-graph outline color */ +td.coverBarOutline +{ + background-color: #000000; +} + +/* Directory view/File view (all): percentage entry for files with + high coverage rate */ +td.coverPerHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #a7fc9d; + font-weight: bold; + font-family: sans-serif; +} + +/* 'owner' entry: slightly lighter color than 'coverPerHi' */ +td.owner_coverPerHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #82E0AA; + font-weight: bold; + font-family: sans-serif; +} + +/* Directory view/File view (all): line count entry */ +td.coverNumDflt +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; + white-space: nowrap; + font-family: sans-serif; +} + +/* td background color and font for the 'owner' section of the table */ +td.ownerTla +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #E5DBDB; + white-space: nowrap; + font-family: sans-serif; + font-style: italic; +} + +/* Directory view/File view (all): line count entry for files with + high coverage rate */ +td.coverNumHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #a7fc9d; + white-space: nowrap; + font-family: sans-serif; +} + +td.owner_coverNumHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #82E0AA; + white-space: nowrap; + font-family: sans-serif; +} + +/* Directory view/File view (all): percentage entry for files with + medium coverage rate */ +td.coverPerMed +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #ffea20; + font-weight: bold; + font-family: sans-serif; +} + +td.owner_coverPerMed +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #F9E79F; + font-weight: bold; + font-family: sans-serif; +} + +/* Directory view/File view (all): line count entry for files with + medium coverage rate */ +td.coverNumMed +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #ffea20; + white-space: nowrap; + font-family: sans-serif; +} + +td.owner_coverNumMed +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #F9E79F; + white-space: nowrap; + font-family: sans-serif; +} + +/* Directory view/File view (all): percentage entry for files with + low coverage rate */ +td.coverPerLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #ff0000; + font-weight: bold; + font-family: sans-serif; +} + +td.owner_coverPerLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #EC7063; + font-weight: bold; + font-family: sans-serif; +} + +/* Directory view/File view (all): line count entry for files with + low coverage rate */ +td.coverNumLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #ff0000; + white-space: nowrap; + font-family: sans-serif; +} + +td.owner_coverNumLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #EC7063; + white-space: nowrap; + font-family: sans-serif; +} + +/* File view (all): "show/hide details" link format */ +a.detail:link +{ + color: #b8d0ff; + font-size:80%; +} + +/* File view (all): "show/hide details" link - visited format */ +a.detail:visited +{ + color: #b8d0ff; + font-size:80%; +} + +/* File view (all): "show/hide details" link - activated format */ +a.detail:active +{ + color: #ffffff; + font-size:80%; +} + +/* File view (detail): test name entry */ +td.testName +{ + text-align: right; + padding-right: 10px; + background-color: #dae7fe; + font-family: sans-serif; +} + +/* File view (detail): test percentage entry */ +td.testPer +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; + font-family: sans-serif; +} + +/* File view (detail): test lines count entry */ +td.testNum +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; + font-family: sans-serif; +} + +/* Test case descriptions: test name format*/ +dt +{ + font-family: sans-serif; + font-weight: bold; +} + +/* Test case descriptions: description table body */ +td.testDescription +{ + padding-top: 10px; + padding-left: 30px; + padding-bottom: 10px; + padding-right: 30px; + background-color: #dae7fe; +} + +/* Source code view: function entry */ +td.coverFn +{ + text-align: left; + padding-left: 10px; + padding-right: 20px; + color: #284fa8; + background-color: #dae7fe; + font-family: monospace; +} + +/* Source code view: function entry zero count*/ +td.coverFnLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #ff0000; + font-weight: bold; + font-family: sans-serif; +} + +/* Source code view: function entry nonzero count*/ +td.coverFnHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; + font-weight: bold; + font-family: sans-serif; +} + +td.coverFnAlias +{ + text-align: right; + padding-left: 10px; + padding-right: 20px; + color: #284fa8; + /* make this a slightly different color than the leader - otherwise, + otherwise the alias is hard to distinguish in the table */ + background-color: #E5DBDB; /* very light pale grey/blue */ + font-family: monospace; +} + +/* Source code view: function entry zero count*/ +td.coverFnAliasLo +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #EC7063; /* lighter red */ + font-family: sans-serif; +} + +/* Source code view: function entry nonzero count*/ +td.coverFnAliasHi +{ + text-align: right; + padding-left: 10px; + padding-right: 10px; + background-color: #dae7fe; + font-weight: bold; + font-family: sans-serif; +} + +/* Source code view: source code format */ +pre.source +{ + font-family: monospace; + white-space: pre; + margin-top: 2px; +} + +/* Source code view: line number format */ +span.lineNum +{ + background-color: #efe383; +} + +/* Source code view: format for Cov legend */ +span.coverLegendCov +{ + padding-left: 10px; + padding-right: 10px; + padding-bottom: 2px; + background-color: #cad7fe; +} + +/* Source code view: format for NoCov legend */ +span.coverLegendNoCov +{ + padding-left: 10px; + padding-right: 10px; + padding-bottom: 2px; + background-color: #ff6230; +} + +/* Source code view: format for the source code heading line */ +pre.sourceHeading +{ + white-space: pre; + font-family: monospace; + font-weight: bold; + margin: 0px; +} + +/* All views: header legend value for low rate */ +td.headerValueLegL +{ + font-family: sans-serif; + text-align: center; + white-space: nowrap; + padding-left: 4px; + padding-right: 2px; + background-color: #ff0000; + font-size: 80%; +} + +/* All views: header legend value for med rate */ +td.headerValueLegM +{ + font-family: sans-serif; + text-align: center; + white-space: nowrap; + padding-left: 2px; + padding-right: 2px; + background-color: #ffea20; + font-size: 80%; +} + +/* All views: header legend value for hi rate */ +td.headerValueLegH +{ + font-family: sans-serif; + text-align: center; + white-space: nowrap; + padding-left: 2px; + padding-right: 4px; + background-color: #a7fc9d; + font-size: 80%; +} + +/* All views except source code view: legend format for low coverage */ +span.coverLegendCovLo +{ + padding-left: 10px; + padding-right: 10px; + padding-top: 2px; + background-color: #ff0000; +} + +/* All views except source code view: legend format for med coverage */ +span.coverLegendCovMed +{ + padding-left: 10px; + padding-right: 10px; + padding-top: 2px; + background-color: #ffea20; +} + +/* All views except source code view: legend format for hi coverage */ +span.coverLegendCovHi +{ + padding-left: 10px; + padding-right: 10px; + padding-top: 2px; + background-color: #a7fc9d; +} + +a.branchTla:link +{ + color: #000000; +} + +a.branchTla:visited +{ + color: #000000; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered New Code (+ => 0): +Newly added code is not tested" */ +td.tlaUNC +{ + text-align: right; + background-color: #FF6230; +} +td.tlaBgUNC { + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered New Code (+ => 0): +Newly added code is not tested" */ +span.tlaUNC +{ + text-align: left; + background-color: #FF6230; +} +span.tlaBgUNC { + background-color: #FF6230; +} +a.tlaBgUNC { + background-color: #FF6230; + color: #000000; +} + +td.headerCovTableHeadUNC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Lost Baseline Coverage (1 => 0): +Unchanged code is no longer tested" */ +td.tlaLBC +{ + text-align: right; + background-color: #FF6230; +} +td.tlaBgLBC { + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Lost Baseline Coverage (1 => 0): +Unchanged code is no longer tested" */ +span.tlaLBC +{ + text-align: left; + background-color: #FF6230; +} +span.tlaBgLBC { + background-color: #FF6230; +} +a.tlaBgLBC { + background-color: #FF6230; + color: #000000; +} + +td.headerCovTableHeadLBC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered Included Code (# => 0): +Previously unused code is untested" */ +td.tlaUIC +{ + text-align: right; + background-color: #FF6230; +} +td.tlaBgUIC { + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered Included Code (# => 0): +Previously unused code is untested" */ +span.tlaUIC +{ + text-align: left; + background-color: #FF6230; +} +span.tlaBgUIC { + background-color: #FF6230; +} +a.tlaBgUIC { + background-color: #FF6230; + color: #000000; +} + +td.headerCovTableHeadUIC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered Baseline Code (0 => 0): +Unchanged code was untested before, is untested now" */ +td.tlaUBC +{ + text-align: right; + background-color: #FF6230; +} +td.tlaBgUBC { + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Uncovered Baseline Code (0 => 0): +Unchanged code was untested before, is untested now" */ +span.tlaUBC +{ + text-align: left; + background-color: #FF6230; +} +span.tlaBgUBC { + background-color: #FF6230; +} +a.tlaBgUBC { + background-color: #FF6230; + color: #000000; +} + +td.headerCovTableHeadUBC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FF6230; +} + +/* Source code view/table entry backround: format for lines classified as "Gained Baseline Coverage (0 => 1): +Unchanged code is tested now" */ +td.tlaGBC +{ + text-align: right; + background-color: #CAD7FE; +} +td.tlaBgGBC { + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Gained Baseline Coverage (0 => 1): +Unchanged code is tested now" */ +span.tlaGBC +{ + text-align: left; + background-color: #CAD7FE; +} +span.tlaBgGBC { + background-color: #CAD7FE; +} +a.tlaBgGBC { + background-color: #CAD7FE; + color: #000000; +} + +td.headerCovTableHeadGBC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Gained Included Coverage (# => 1): +Previously unused code is tested now" */ +td.tlaGIC +{ + text-align: right; + background-color: #CAD7FE; +} +td.tlaBgGIC { + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Gained Included Coverage (# => 1): +Previously unused code is tested now" */ +span.tlaGIC +{ + text-align: left; + background-color: #CAD7FE; +} +span.tlaBgGIC { + background-color: #CAD7FE; +} +a.tlaBgGIC { + background-color: #CAD7FE; + color: #000000; +} + +td.headerCovTableHeadGIC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Gained New Coverage (+ => 1): +Newly added code is tested" */ +td.tlaGNC +{ + text-align: right; + background-color: #CAD7FE; +} +td.tlaBgGNC { + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Gained New Coverage (+ => 1): +Newly added code is tested" */ +span.tlaGNC +{ + text-align: left; + background-color: #CAD7FE; +} +span.tlaBgGNC { + background-color: #CAD7FE; +} +a.tlaBgGNC { + background-color: #CAD7FE; + color: #000000; +} + +td.headerCovTableHeadGNC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Covered Baseline Code (1 => 1): +Unchanged code was tested before and is still tested" */ +td.tlaCBC +{ + text-align: right; + background-color: #CAD7FE; +} +td.tlaBgCBC { + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Covered Baseline Code (1 => 1): +Unchanged code was tested before and is still tested" */ +span.tlaCBC +{ + text-align: left; + background-color: #CAD7FE; +} +span.tlaBgCBC { + background-color: #CAD7FE; +} +a.tlaBgCBC { + background-color: #CAD7FE; + color: #000000; +} + +td.headerCovTableHeadCBC { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #CAD7FE; +} + +/* Source code view/table entry backround: format for lines classified as "Excluded Uncovered Baseline (0 => #): +Previously untested code is unused now" */ +td.tlaEUB +{ + text-align: right; + background-color: #FFFFFF; +} +td.tlaBgEUB { + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Excluded Uncovered Baseline (0 => #): +Previously untested code is unused now" */ +span.tlaEUB +{ + text-align: left; + background-color: #FFFFFF; +} +span.tlaBgEUB { + background-color: #FFFFFF; +} +a.tlaBgEUB { + background-color: #FFFFFF; + color: #000000; +} + +td.headerCovTableHeadEUB { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Excluded Covered Baseline (1 => #): +Previously tested code is unused now" */ +td.tlaECB +{ + text-align: right; + background-color: #FFFFFF; +} +td.tlaBgECB { + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Excluded Covered Baseline (1 => #): +Previously tested code is unused now" */ +span.tlaECB +{ + text-align: left; + background-color: #FFFFFF; +} +span.tlaBgECB { + background-color: #FFFFFF; +} +a.tlaBgECB { + background-color: #FFFFFF; + color: #000000; +} + +td.headerCovTableHeadECB { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Deleted Uncovered Baseline (0 => -): +Previously untested code has been deleted" */ +td.tlaDUB +{ + text-align: right; + background-color: #FFFFFF; +} +td.tlaBgDUB { + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Deleted Uncovered Baseline (0 => -): +Previously untested code has been deleted" */ +span.tlaDUB +{ + text-align: left; + background-color: #FFFFFF; +} +span.tlaBgDUB { + background-color: #FFFFFF; +} +a.tlaBgDUB { + background-color: #FFFFFF; + color: #000000; +} + +td.headerCovTableHeadDUB { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Deleted Covered Baseline (1 => -): +Previously tested code has been deleted" */ +td.tlaDCB +{ + text-align: right; + background-color: #FFFFFF; +} +td.tlaBgDCB { + background-color: #FFFFFF; +} + +/* Source code view/table entry backround: format for lines classified as "Deleted Covered Baseline (1 => -): +Previously tested code has been deleted" */ +span.tlaDCB +{ + text-align: left; + background-color: #FFFFFF; +} +span.tlaBgDCB { + background-color: #FFFFFF; +} +a.tlaBgDCB { + background-color: #FFFFFF; + color: #000000; +} + +td.headerCovTableHeadDCB { + text-align: center; + padding-right: 6px; + padding-left: 6px; + padding-bottom: 0px; + font-family: sans-serif; + white-space: nowrap; + background-color: #FFFFFF; +} + +/* Source code view: format for date/owner bin that is not hit */ +span.missBins +{ + background-color: #ff0000 /* red */ +} diff --git a/html/glass.png b/html/glass.png new file mode 100644 index 0000000..e1abc00 Binary files /dev/null and b/html/glass.png differ diff --git a/html/index-sort-f.html b/html/index-sort-f.html new file mode 100644 index 0000000..af7fb3a --- /dev/null +++ b/html/index-sort-f.html @@ -0,0 +1,165 @@ + + + + + + + LCOV - coverage.info + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top levelCoverageTotalHit
Test:coverage.infoLines:97.2 %15591516
Test Date:2024-04-21 16:43:29Functions:97.6 %246240
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Directory Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
bayesnet +
100.0%
+
100.0 %1150.0 %21
bayesnet/feature_selection +
95.4%95.4%
+
95.4 %15214595.7 %2322
bayesnet/utils +
93.0%93.0%
+
93.0 %22721196.8 %3130
bayesnet/classifiers +
98.9%98.9%
+
98.9 %36936597.4 %7674
bayesnet/ensembles +
98.8%98.8%
+
98.8 %42441998.1 %5352
bayesnet/network +
97.2%97.2%
+
97.2 %386375100.0 %6161
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/index-sort-l.html b/html/index-sort-l.html new file mode 100644 index 0000000..c0c4366 --- /dev/null +++ b/html/index-sort-l.html @@ -0,0 +1,165 @@ + + + + + + + LCOV - coverage.info + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top levelCoverageTotalHit
Test:coverage.infoLines:97.2 %15591516
Test Date:2024-04-21 16:43:29Functions:97.6 %246240
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Directory Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
bayesnet/utils +
93.0%93.0%
+
93.0 %22721196.8 %3130
bayesnet/feature_selection +
95.4%95.4%
+
95.4 %15214595.7 %2322
bayesnet/network +
97.2%97.2%
+
97.2 %386375100.0 %6161
bayesnet/ensembles +
98.8%98.8%
+
98.8 %42441998.1 %5352
bayesnet/classifiers +
98.9%98.9%
+
98.9 %36936597.4 %7674
bayesnet +
100.0%
+
100.0 %1150.0 %21
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..380826b --- /dev/null +++ b/html/index.html @@ -0,0 +1,165 @@ + + + + + + + LCOV - coverage.info + + + + + + + + + + + + + + +
LCOV - code coverage report
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Current view:top levelCoverageTotalHit
Test:coverage.infoLines:97.2 %15591516
Test Date:2024-04-21 16:43:29Functions:97.6 %246240
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Directory Sort by file nameLine Coverage Sort by line coverageFunction Coverage Sort by function coverage
Rate Total Hit Rate Total Hit
bayesnet +
100.0%
+
100.0 %1150.0 %21
bayesnet/classifiers +
98.9%98.9%
+
98.9 %36936597.4 %7674
bayesnet/ensembles +
98.8%98.8%
+
98.8 %42441998.1 %5352
bayesnet/feature_selection +
95.4%95.4%
+
95.4 %15214595.7 %2322
bayesnet/network +
97.2%97.2%
+
97.2 %386375100.0 %6161
bayesnet/utils +
93.0%93.0%
+
93.0 %22721196.8 %3130
+
+
+ + + + +
Generated by: LCOV version 2.0-1
+
+ + + diff --git a/html/ruby.png b/html/ruby.png new file mode 100644 index 0000000..991b6d4 Binary files /dev/null and b/html/ruby.png differ diff --git a/html/snow.png b/html/snow.png new file mode 100644 index 0000000..2cdae10 Binary files /dev/null and b/html/snow.png differ diff --git a/html/updown.png b/html/updown.png new file mode 100644 index 0000000..aa56a23 Binary files /dev/null and b/html/updown.png differ diff --git a/update_coverage.py b/update_coverage.py index da99463..7ad537b 100644 --- a/update_coverage.py +++ b/update_coverage.py @@ -20,15 +20,13 @@ if value < 90: print("⛔Coverage is less than 90%. I won't update the badge.") sys.exit(1) percentage = output.decode("utf-8").strip().replace(".", ",") -coverage_line = ( - f"![Static Badge](https://img.shields.io/badge/Coverage-{percentage}25-green)" -) +coverage_line = f"[![Static Badge](https://img.shields.io/badge/Coverage-{percentage}25-green)](html/index.html)" # Update README.md with open(readme_file, "r") as f: lines = f.readlines() with open(readme_file, "w") as f: for line in lines: - if "Coverage" in line: + if "img.shields.io/badge/Coverage" in line: f.write(coverage_line + "\n") else: f.write(line)