LCOV - code coverage report
Current view: top level - bayesnet/feature_selection - IWSS.cc (source / functions) Coverage Total Hit
Test: BayesNet Coverage Report Lines: 96.7 % 30 29
Test Date: 2024-05-06 17:54:04 Functions: 100.0 % 2 2
Legend: Lines: hit not hit

            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           87 :     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           87 :         FeatureSelect(samples, features, className, maxFeatures, classNumStates, weights), threshold(threshold)
      13              :     {
      14           87 :         if (threshold < 0 || threshold > .5) {
      15           40 :             throw std::invalid_argument("Threshold has to be in [0, 0.5]");
      16              :         }
      17           87 :     }
      18           47 :     void IWSS::fit()
      19              :     {
      20           47 :         initialize();
      21           47 :         computeSuLabels();
      22           47 :         auto featureOrder = argsort(suLabels); // sort descending order
      23           47 :         auto featureOrderCopy = featureOrder;
      24              :         // Add first and second features to result
      25              :         //     First with its own score
      26           47 :         auto first_feature = pop_first(featureOrderCopy);
      27           47 :         selectedFeatures.push_back(first_feature);
      28           47 :         selectedScores.push_back(suLabels.at(first_feature));
      29              :         //     Second with the score of the candidates
      30           47 :         selectedFeatures.push_back(pop_first(featureOrderCopy));
      31           47 :         auto merit = computeMeritCFS();
      32           47 :         selectedScores.push_back(merit);
      33          162 :         for (const auto feature : featureOrderCopy) {
      34          162 :             selectedFeatures.push_back(feature);
      35              :             // Compute merit with selectedFeatures
      36          162 :             auto meritNew = computeMeritCFS();
      37          162 :             double delta = merit != 0.0 ? std::abs(merit - meritNew) / merit : 0.0;
      38          162 :             if (meritNew > merit || delta < threshold) {
      39          117 :                 if (meritNew > merit) {
      40            0 :                     merit = meritNew;
      41              :                 }
      42          117 :                 selectedScores.push_back(meritNew);
      43              :             } else {
      44           45 :                 selectedFeatures.pop_back();
      45           47 :                 break;
      46              :             }
      47          117 :             if (selectedFeatures.size() == maxFeatures) {
      48            2 :                 break;
      49              :             }
      50              :         }
      51           47 :         fitted = true;
      52           47 :     }
      53              : }
        

Generated by: LCOV version 2.0-1