LCOV - code coverage report
Current view: top level - src - util.cxx (source / functions) Hit Total Coverage
Test: code_coverage_filter.info Lines: 35 102 34.3 %
Date: 2024-03-28 16:04:17 Functions: 5 10 50.0 %
Branches: 7 100 7.0 %

           Branch data     Line data    Source code
       1                 :            : /**
       2                 :            :  * @author: Abdul-Amir Yassine
       3                 :            :  * @date_created: March 6th, 2018
       4                 :            :  *
       5                 :            :  * \file util.cxx \brief Helper methods implementation
       6                 :            :  * This file implements all the methods inside the namespace Util in util.h
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "util.h"
      10                 :            : #include <cctype>
      11                 :            : #include <algorithm>
      12                 :            : 
      13                 :            : namespace Util
      14                 :            : {
      15                 :            : 
      16                 :            :     chrono::steady_clock::time_point BEGIN_TIME;
      17                 :            :     chrono::steady_clock::time_point END_TIME;
      18                 :            : 
      19                 :            : 
      20                 :            :     //! Print a matrix to a matlab file
      21                 :          0 :     void print_matrix_matlab(FILE* matlab, vector<vector<float>>& mat, string M_name)
      22                 :            :     {   
      23                 :            :     /*     stringstream mfile_name; */
      24                 :            :     /*     mfile_name << "Matlab_tests/" << M_name << ".m"; */
      25                 :            :     /*     FILE* matlab = fopen(mfile_name.str().c_str(), "w"); */
      26                 :            : 
      27 [ #  # ][ #  # ]:          0 :         stringstream Ti, Tj, Tx;
                 [ #  # ]
      28 [ #  # ][ #  # ]:          0 :         Ti << M_name << "Ti";
      29 [ #  # ][ #  # ]:          0 :         Tj << M_name << "Tj";
      30 [ #  # ][ #  # ]:          0 :         Tx << M_name << "Tx";
      31                 :          0 :         string sTi, sTj, sTx;
      32         [ #  # ]:          0 :         sTi = Ti.str();
      33         [ #  # ]:          0 :         sTj = Tj.str();
      34         [ #  # ]:          0 :         sTx = Tx.str();
      35                 :            : 
      36                 :          0 :         vector<unsigned int> Ti_v, Tj_v;
      37                 :          0 :         vector<float> Tx_v;
      38         [ #  # ]:          0 :         for (unsigned int i = 0; i < mat.size(); i++)
      39                 :            :         {
      40         [ #  # ]:          0 :             for (unsigned int j = 0; j < mat[i].size(); j++)
      41                 :            :             {
      42         [ #  # ]:          0 :                 if (abs(mat[i][j]) > 1e-5)
      43                 :            :                 {
      44         [ #  # ]:          0 :                     Ti_v.push_back(i);
      45         [ #  # ]:          0 :                     Tj_v.push_back(j);
      46         [ #  # ]:          0 :                     Tx_v.push_back(mat[i][j]);
      47                 :            :                 }
      48                 :            :             }
      49                 :            :         }
      50                 :            : 
      51 [ #  # ][ #  # ]:          0 :         print_vector_matlab<unsigned int>(matlab, Ti_v, sTi);
      52 [ #  # ][ #  # ]:          0 :         print_vector_matlab<unsigned int>(matlab, Tj_v, sTj);
      53 [ #  # ][ #  # ]:          0 :         print_vector_matlab<float>(matlab, Tx_v, sTx);
      54                 :          0 :         fprintf(matlab, "%s = triplet_format_to_matrix(%s, %s, %s, %ld, %ld);\n", 
      55                 :            :                 M_name.c_str(), sTi.c_str(), sTj.c_str(), sTx.c_str(),
      56         [ #  # ]:          0 :                 mat.size(), mat[0].size());
      57                 :          0 :         fprintf(matlab, "clear %s %s %s \n", sTi.c_str(), sTj.c_str(),
      58         [ #  # ]:          0 :                         sTx.c_str());
      59                 :            : 
      60         [ #  # ]:          0 :         fclose(matlab);
      61                 :          0 :     }
      62                 :            : 
      63                 :            :     // Print a dense matrix to a matlab file
      64                 :          0 :     void print_matrix_matlab_dense(FILE* matlab, vector<vector<float>>& mat, string M_name)
      65                 :            :     {   
      66                 :          0 :         fprintf(matlab, "%s = [", M_name.c_str());
      67                 :            : 
      68         [ #  # ]:          0 :         for (unsigned int i = 0; i < mat.size(); i++)
      69                 :            :         {
      70         [ #  # ]:          0 :             for (unsigned int j = 0; j < mat[0].size(); j++)
      71                 :            :             {
      72                 :          0 :                 fprintf(matlab, "%-15f ", mat[i][j]);
      73                 :            :             }
      74         [ #  # ]:          0 :             if (i < mat.size() - 1)
      75                 :          0 :                 fprintf(matlab, "\n");
      76                 :            :         }
      77                 :          0 :         fprintf(matlab, "];\n\n");
      78                 :          0 :     }
      79                 :            : 
      80                 :            :     //! This function prints the runtime and usage statistics from the profiler class
      81                 :          0 :     void print_profiler_stat()
      82                 :            :     {
      83                 :          0 :         FILE* out = stdout;
      84         [ #  # ]:          0 :         profiler::print_all_task_timer_records( out );
      85         [ #  # ]:          0 :         profiler::clear_all_task_timer_records();
      86                 :            : 
      87                 :            :         // get and print resource usage
      88                 :            :         char buffer[2000];
      89         [ #  # ]:          0 :         profiler::get_resource_usage( buffer, RUSAGE_SELF );
      90         [ #  # ]:          0 :         fprintf(out, "\n%s", buffer);
      91                 :            :         fprintf(out,
      92         [ #  # ]:          0 :         "===========================================================================\n");
      93                 :            :         fprintf(out,
      94         [ #  # ]:          0 :         "===============================END OF RUN==================================\n");
      95                 :            :         fprintf(out,
      96         [ #  # ]:          0 :         "===========================================================================\n\n");
      97         [ #  # ]:          0 :         fclose(out);
      98                 :          0 :     }
      99                 :            : 
     100                 :            :     //! This method returns the current local system time
     101                 :          0 :     bpt::ptime get_local_time()
     102                 :            :     {
     103                 :            :         // Get current system time
     104                 :          0 :         return bpt::second_clock::local_time();
     105                 :            :     }
     106                 :            : 
     107                 :            : 
     108                 :            :     //! This method sorts the given vector in descending order and returns the correspoding permutation
     109                 :          0 :     vector<double> get_sorted_vector_idx(const vector<double>& vec_in, vector<int>& permutation_vec_out) 
     110                 :            :     {
     111         [ #  # ]:          0 :         vector<element_idx> vec_s(vec_in.size());
     112                 :            : 
     113         [ #  # ]:          0 :         for (unsigned int i = 0; i < vec_in.size(); i++)
     114                 :            :         {
     115                 :          0 :             vec_s[i].element = vec_in[i];
     116                 :          0 :             vec_s[i].index = i;
     117                 :            :         }
     118                 :            : 
     119         [ #  # ]:          0 :         sort(vec_s.begin(), vec_s.end(), sort_by_element());
     120                 :            : 
     121                 :            :         // Copying the new permutations and the sorted vector
     122         [ #  # ]:          0 :         vector<double> sorted_elements(vec_in.size()); 
     123                 :          0 :         permutation_vec_out.clear();
     124         [ #  # ]:          0 :         permutation_vec_out.resize(vec_in.size());
     125         [ #  # ]:          0 :         for (unsigned int i = 0; i < vec_in.size(); i++)
     126                 :            :         {
     127                 :          0 :             permutation_vec_out[i] = vec_s[i].index;
     128                 :          0 :             sorted_elements[i] = vec_s[i].element;
     129                 :            :         }
     130                 :            : 
     131                 :          0 :         return sorted_elements;
     132                 :            :     }
     133                 :            : 
     134                 :       1800 :     double compute_slope_regression(const std::vector<double>& x, const std::vector<double> & y) 
     135                 :            :     {
     136                 :       1800 :         auto x_n = x.size(), y_n = y.size(); 
     137         [ -  + ]:       1800 :         if (x_n != y_n) {
     138                 :          0 :             fprintf(stderr, "\033[1;%dmcompute_slope_regression::input vectors sizes do not match! Exiting....\n\033[0m\n", 31);
     139                 :          0 :             std::exit(-1);
     140                 :            :         }
     141                 :            : 
     142         [ -  + ]:       1800 :         if (x_n <= 1) {
     143                 :          0 :             fprintf(stderr, "\033[1;%dmcompute_slope_regression::input vector sizes are less than 2! Exiting....\n\033[0m\n", 31);
     144                 :          0 :             std::exit(-1);
     145                 :            :         }
     146                 :            :         
     147                 :       1800 :         double sum_x = 0.0, sum_xx = 0.0, sum_y = 0.0, sum_xy = 0.0; 
     148         [ +  + ]:      86778 :         for (unsigned i = 0; i < x_n; i++) {
     149                 :      84978 :             double x_i = x[i], y_i = y[i];
     150                 :            : 
     151                 :      84978 :             sum_x += x_i;
     152                 :      84978 :             sum_xx += x_i*x_i;
     153                 :      84978 :             sum_y += y_i;
     154                 :      84978 :             sum_xy += x_i*y_i;
     155                 :            :         }
     156                 :            : 
     157                 :            : 
     158                 :       1800 :         double num = static_cast<double>(x_n)*sum_xy - sum_x*sum_y; 
     159                 :       1800 :         double denom = static_cast<double>(x_n)*sum_xx - sum_x*sum_x; 
     160                 :            : 
     161         [ -  + ]:       1800 :         if (denom < 1e-8) {
     162                 :            :             fprintf(stderr, "\033[1;%dmcompute_slope_regression::[Warning]: denominator is 0 ==> No" 
     163                 :          0 :                     " solution ==> returning 0\n\033[0m\n", 33);
     164                 :          0 :             return 0.0;
     165                 :            :         }
     166                 :            :         
     167                 :       1800 :         return num/denom; 
     168                 :            :     }
     169                 :            : 
     170                 :            :     //! This function disables stdout and stderr by redirecting outputs to /dev/null
     171                 :        874 :     void redirect_standard_streams_to_DEVNULL(int *_piOriginalSTDIN_FILENO, int *_piOriginalSTDOUT_FILENO, int *_piOriginalSTDERR_FILENO) {
     172                 :        874 :         fflush(stdout);
     173                 :        874 :         fflush(stderr);
     174                 :            : 
     175                 :        874 :         *_piOriginalSTDIN_FILENO = dup(STDIN_FILENO);
     176                 :        874 :         *_piOriginalSTDOUT_FILENO = dup(STDOUT_FILENO);
     177                 :        874 :         *_piOriginalSTDERR_FILENO = dup(STDERR_FILENO);
     178                 :            : 
     179                 :        874 :         int devnull = open("/dev/null", O_RDWR);
     180                 :        874 :         dup2(devnull, STDIN_FILENO);
     181                 :        874 :         dup2(devnull, STDOUT_FILENO);
     182                 :        874 :         dup2(devnull, STDERR_FILENO);
     183                 :        874 :         close(devnull);
     184                 :        874 :     }
     185                 :            : 
     186                 :            :     //! This function restores stdout and stderr disabled by redirect_standard_streams_to_DEVNULL()
     187                 :        874 :     void restore_standard_streams(int *_piOriginalSTDIN_FILENO, int *_piOriginalSTDOUT_FILENO, int *_piOriginalSTDERR_FILENO) {
     188                 :        874 :         fflush(stdout);
     189                 :        874 :         fflush(stderr);
     190                 :            : 
     191                 :        874 :         dup2(*_piOriginalSTDIN_FILENO, STDIN_FILENO);
     192                 :        874 :         dup2(*_piOriginalSTDOUT_FILENO, STDOUT_FILENO);
     193                 :        874 :         dup2(*_piOriginalSTDERR_FILENO, STDERR_FILENO);
     194                 :        874 :     }
     195 [ +  - ][ +  - ]:         72 : }

Generated by: LCOV version 1.12