You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

misc.hpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file misc.hpp
  4. /// \brief Some constants and functions for google tests
  5. /// \author Thibaut Monseigne (Inria).
  6. /// \version 0.1.
  7. /// \date 26/10/2018.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. /// \remarks
  10. /// - For this test I compare the results with the <a href="https://github.com/alexandrebarachant/pyRiemann">pyRiemann</a> library (<a href="https://github.com/alexandrebarachant/pyRiemann/blob/master/LICENSE">License</a>) or <a href="http://scikit-learn.org">sklearn</a> if pyRiemman just redirect the function.
  11. ///
  12. ///-------------------------------------------------------------------------------------------------
  13. #pragma once
  14. #include <cmath>
  15. #include <vector>
  16. #include <type_traits>
  17. #include <geometry/classifier/IMatrixClassifier.hpp>
  18. #include <geometry/artifacts/CASR.hpp>
  19. const std::string SEP = "\n====================\n";
  20. //*********************************************************************************
  21. //********** Comparison of values with epsilon tolerance for google test **********
  22. //*********************************************************************************
  23. /// <summary> Check if two doubles are almost equal. </summary>
  24. /// <param name="x"> The first value. </param>
  25. /// <param name="y"> The second value. </param>
  26. /// <param name="epsilon"> (Optional) The epsilon tolerance. </param>
  27. /// <returns> True if almost equal, false if not. </returns>
  28. inline bool isAlmostEqual(const double x, const double y, const double epsilon = 0.0001) { return std::abs(x - y) < epsilon; }
  29. /// <summary> Check if sum of two vectors are almost equal. </summary>
  30. /// <typeparam name="T"> Generic numeric type parameter. </typeparam>
  31. /// \copydetails isAlmostEqual(const double, const double, const double)
  32. template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
  33. bool isAlmostEqual(const std::vector<T>& x, const std::vector<T>& y, const double epsilon = 0.0001)
  34. {
  35. double xsum = 0.0, ysum = 0.0;
  36. for (const auto& n : x) { xsum += n; }
  37. for (const auto& n : y) { ysum += n; }
  38. return (x.size() == y.size() && isAlmostEqual(xsum, ysum, epsilon));
  39. }
  40. /// <summary> Check if sum of two matrix are almost equal. </summary>
  41. /// \copydetails isAlmostEqual(const double, const double, const double)
  42. inline bool isAlmostEqual(const Eigen::MatrixXd& x, const Eigen::MatrixXd& y, const double epsilon = 0.0001)
  43. {
  44. return x.size() == y.size() && isAlmostEqual(x.cwiseAbs().sum(), y.cwiseAbs().sum(), epsilon);
  45. }
  46. //*****************************************************************
  47. //********** Error Message Standardization for googltest **********
  48. //*****************************************************************
  49. /// <summary> Error message for size_t. </summary>
  50. /// <param name="name"> The name of the test. </param>
  51. /// <param name="ref"> The reference value. </param>
  52. /// <param name="calc"> The calculate value. </param>
  53. /// <returns> Error message. </returns>
  54. inline std::string ErrorMsg(const std::string& name, const size_t ref, const size_t calc)
  55. {
  56. std::stringstream ss;
  57. ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP;
  58. return ss.str();
  59. }
  60. /// <summary> Error message for doubles. </summary>
  61. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t)
  62. inline std::string ErrorMsg(const std::string& name, const double ref, const double calc)
  63. {
  64. std::stringstream ss;
  65. ss << SEP << name << " : Reference : " << ref << ", \tCompute : " << calc << SEP;
  66. return ss.str();
  67. }
  68. /// <summary> Error message for numeric vector. </summary>
  69. /// <typeparam name="T"> Generic numeric type parameter. </typeparam>
  70. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t)
  71. template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
  72. std::string ErrorMsg(const std::string& name, const std::vector<T>& ref, const std::vector<T>& calc)
  73. {
  74. std::stringstream ss;
  75. ss << SEP << name << " : " << std::endl << " Reference : \t[";
  76. for (const T& t : ref) { ss << t << ", "; }
  77. if (!ref.empty()) { ss.seekp(ss.str().length() - 2); }
  78. ss << "] " << std::endl << " Compute : \t[";
  79. for (const T& t : calc) { ss << t << ", "; }
  80. if (!ref.empty()) { ss.seekp(ss.str().length() - 2); }
  81. ss << "] " << SEP;
  82. return ss.str();
  83. }
  84. /// <summary> Error message for matrix. </summary>
  85. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t)
  86. inline std::string ErrorMsg(const std::string& name, const Eigen::MatrixXd& ref, const Eigen::MatrixXd& calc)
  87. {
  88. std::stringstream ss;
  89. ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP;
  90. return ss.str();
  91. }
  92. /// <summary> Error message for matrix Classifier. </summary>
  93. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t)
  94. inline std::string ErrorMsg(const std::string& name, const Geometry::IMatrixClassifier& ref, const Geometry::IMatrixClassifier& calc)
  95. {
  96. std::stringstream ss;
  97. ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP;
  98. return ss.str();
  99. }
  100. /// <summary> Error message for ASR. </summary>
  101. /// \copydetails ErrorMsg(const std::string&, const size_t, const size_t)
  102. inline std::string ErrorMsg(const std::string& name, const Geometry::CASR& ref, const Geometry::CASR& calc)
  103. {
  104. std::stringstream ss;
  105. ss << SEP << name << " : " << std::endl << "********** Reference **********\n" << ref << std::endl << "********** Compute **********\n" << calc << SEP;
  106. return ss.str();
  107. }