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.

test_Misc.hpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file test_Misc.hpp
  4. /// \brief Tests for Misc Functions of module.
  5. /// \author Thibaut Monseigne (Inria).
  6. /// \version 1.0.
  7. /// \date 29/07/2020.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. /// \remarks We use the EEglab Matlab plugin to compare result for validation
  10. ///
  11. ///-------------------------------------------------------------------------------------------------
  12. #pragma once
  13. #include "gtest/gtest.h"
  14. #include "init.hpp"
  15. #include "misc.hpp"
  16. #include <geometry/Misc.hpp>
  17. #include <geometry/Basics.hpp>
  18. //---------------------------------------------------------------------------------------------------
  19. class Tests_Misc : public testing::Test
  20. {
  21. //protected:
  22. // std::vector<Eigen::MatrixXd> m_dataSet;
  23. //
  24. // void SetUp() override { m_dataSet = Vector2DTo1D(InitCovariance::LWF::Reference()); }
  25. };
  26. //---------------------------------------------------------------------------------------------------
  27. //---------------------------------------------------------------------------------------------------
  28. TEST_F(Tests_Misc, Double_Range)
  29. {
  30. const std::vector<double> calc1 = Geometry::doubleRange(0, 10, 2), calc2 = Geometry::doubleRange(0, 10, 2, false),
  31. calc3 = Geometry::doubleRange(0.15, 3.05, 0.5), calc4 = Geometry::doubleRange(0.15, 3.05, 0.5, false),
  32. ref1 = { 0, 2, 4, 6, 8, 10 }, ref2 = { 0, 2, 4, 6, 8 },
  33. ref3 = { 0.15, 0.65, 1.15, 1.65, 2.15, 2.65 }, ref4 = { 0.15, 0.65, 1.15, 1.65, 2.15, 2.65 };
  34. EXPECT_TRUE(isAlmostEqual(ref1, calc1)) << ErrorMsg("Double closed Range with integer value", ref1, calc1);
  35. EXPECT_TRUE(isAlmostEqual(ref2, calc2)) << ErrorMsg("Double opened Range with integer value", ref2, calc2);
  36. EXPECT_TRUE(isAlmostEqual(ref3, calc3)) << ErrorMsg("Double closed Range with double value", ref3, calc3);
  37. EXPECT_TRUE(isAlmostEqual(ref4, calc4)) << ErrorMsg("Double opened Range with double value", ref4, calc4);
  38. }
  39. //---------------------------------------------------------------------------------------------------
  40. //---------------------------------------------------------------------------------------------------
  41. TEST_F(Tests_Misc, Round_Index_Range)
  42. {
  43. const std::vector<size_t> calc1 = Geometry::RoundIndexRange(0, 10, 2), calc2 = Geometry::RoundIndexRange(0, 10, 2, false),
  44. calc3 = Geometry::RoundIndexRange(0.15, 3.15, 0.2), calc4 = Geometry::RoundIndexRange(0.15, 3.05, 0.2, false, false),
  45. ref1 = { 0, 2, 4, 6, 8, 10 }, ref2 = { 0, 2, 4, 6, 8 },
  46. ref3 = { 0, 1, 2, 3 }, ref4 = { 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3 };
  47. EXPECT_TRUE(isAlmostEqual(ref1, calc1)) << ErrorMsg("Round Index closed Range with integer value", ref1, calc1);
  48. EXPECT_TRUE(isAlmostEqual(ref2, calc2)) << ErrorMsg("Round Index opened Range with integer value", ref2, calc2);
  49. EXPECT_TRUE(isAlmostEqual(ref3, calc3)) << ErrorMsg("Round Index closed Range with double value", ref3, calc3);
  50. EXPECT_TRUE(isAlmostEqual(ref4, calc4)) << ErrorMsg("Round Index opened Range with double value", ref4, calc4);
  51. }
  52. //---------------------------------------------------------------------------------------------------
  53. //---------------------------------------------------------------------------------------------------
  54. TEST_F(Tests_Misc, Bin_Histogramm)
  55. {
  56. //========== Create Dataset ==========
  57. const std::vector<Eigen::MatrixXd> matrices = Geometry::Vector2DTo1D(InitDataset::Dataset());
  58. std::vector<std::vector<double>> dataset(NB_CHAN);
  59. // Transform Dataset to vector per channel
  60. for (size_t i = 0; i < NB_CHAN; ++i) { dataset[i].reserve(NB_SAMPLE * matrices.size()); }
  61. for (const auto& m : matrices) { for (size_t i = 0; i < NB_CHAN; ++i) { for (size_t j = 0; j < NB_SAMPLE; ++j) { dataset[i].push_back(m(i, j)); } } }
  62. // Sort and remove first (to begin by 0)
  63. for (auto& d : dataset)
  64. {
  65. std::sort(d.begin(), d.end());
  66. const auto first = d[0];
  67. for (auto& e : d) { e -= first; }
  68. }
  69. //========== Create Ref ==========
  70. const std::vector<std::vector<size_t>> ref =
  71. {
  72. { 12, 10, 0, 15, 15, 0, 6, 12, 0, 11, 11, 0, 11, 14, 3 },
  73. { 17, 0, 26, 0, 0, 18, 0, 28, 0, 0, 15, 0, 7, 0, 9 },
  74. { 36, 0, 0, 34, 0, 0, 0, 0, 0, 15, 0, 0, 20, 0, 15 }
  75. };
  76. //========== Test ==========
  77. for (size_t i = 0; i < NB_CHAN; ++i)
  78. {
  79. const std::vector<size_t> hist = Geometry::BinHist(dataset[i], 15);
  80. EXPECT_TRUE(isAlmostEqual(hist, ref[i])) << ErrorMsg("Bin Histogramm", hist, ref[i]);
  81. }
  82. }
  83. //---------------------------------------------------------------------------------------------------
  84. //---------------------------------------------------------------------------------------------------
  85. TEST_F(Tests_Misc, Fit_Distribution)
  86. {
  87. const std::vector<Eigen::MatrixXd> matrices = Geometry::Vector2DTo1D(InitDataset::Dataset());
  88. std::vector<std::vector<double>> dataset(NB_CHAN);
  89. // Transform Dataset to vector per channel
  90. for (size_t i = 0; i < NB_CHAN; ++i) { dataset[i].reserve(NB_SAMPLE * matrices.size()); }
  91. for (const auto& m : matrices) { for (size_t i = 0; i < NB_CHAN; ++i) { for (size_t j = 0; j < NB_SAMPLE; ++j) { dataset[i].push_back(m(i, j)); } } }
  92. // Begin Fit Distribution
  93. std::vector<double> mu(NB_CHAN), sigma(NB_CHAN);
  94. const std::vector<double> refMu = { -0.840258269642149, - 2.10169835819046, 0.898301641809541 },
  95. refSigma = { 2.76541902273525, 0.435493584265319, 0.435493584265319 };
  96. for (size_t i = 0; i < NB_CHAN; ++i)
  97. {
  98. Geometry::FitDistribution(dataset[i], mu[i], sigma[i]);
  99. EXPECT_TRUE(isAlmostEqual(mu[i], refMu[i])) << ErrorMsg("Fit Distribution Mu", mu[i], refMu[i]);
  100. EXPECT_TRUE(isAlmostEqual(sigma[i], refSigma[i])) << ErrorMsg("Fit Distribution Sigma", sigma[i], refSigma[i]);
  101. }
  102. }
  103. //---------------------------------------------------------------------------------------------------
  104. //---------------------------------------------------------------------------------------------------
  105. TEST_F(Tests_Misc, Sorted_Eigen_Vector_Euclidian)
  106. {
  107. std::vector<Eigen::MatrixXd> matrices = Geometry::Vector2DTo1D(InitCovariance::LWF::Reference());
  108. const size_t n = matrices.size();
  109. std::vector<Eigen::MatrixXd> vectors = InitEigenVector::Euclidian::Vectors();
  110. std::vector<std::vector<double>> values = InitEigenVector::Euclidian::Values();
  111. for (size_t i = 0; i < n; ++i)
  112. {
  113. Eigen::MatrixXd vec;
  114. std::vector<double> val;
  115. Geometry::sortedEigenVector(matrices[i], vec, val, Geometry::EMetric::Euclidian);
  116. EXPECT_TRUE(isAlmostEqual(vectors[i], vec)) << ErrorMsg("Eigen Vector sample " + std::to_string(i) + " : ", vectors[i], vec);
  117. EXPECT_TRUE(isAlmostEqual(values[i], val)) << ErrorMsg("Eigen Value sample " + std::to_string(i) + " : ", values[i], val);
  118. }
  119. }
  120. //---------------------------------------------------------------------------------------------------
  121. /*
  122. //---------------------------------------------------------------------------------------------------
  123. TEST_F(Tests_Misc, Sorted_Eigen_Vector_Riemann)
  124. {
  125. std::cout << "Not implemented" << std::endl;
  126. std::vector<Eigen::MatrixXd> matrices = Geometry::Vector2DTo1D(InitCovariance::LWF::Reference());
  127. const size_t n = matrices.size();
  128. //std::vector<Eigen::MatrixXd> vectors = InitEigenVector::Riemann::Vectors();
  129. //std::vector<std::vector<double>> values = InitEigenVector::Riemann::Values();
  130. for (size_t i = 0; i < n; ++i)
  131. {
  132. Eigen::MatrixXd vec;
  133. std::vector<double> val;
  134. Geometry::sortedEigenVector(matrices[i], vec, val, Geometry::EMetric::Riemann);
  135. //EXPECT_TRUE(isAlmostEqual(vectors[i], vec)) << ErrorMsg("Eigen Vector sample " + std::to_string(i) + " : ", vectors[i], vec);
  136. //EXPECT_TRUE(isAlmostEqual(values[i], val)) << ErrorMsg("Eigen Value sample " + std::to_string(i) + " : ", values[i], val);
  137. }
  138. }
  139. //---------------------------------------------------------------------------------------------------
  140. */