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_Basics.hpp 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file test_Basics.hpp
  4. /// \brief Tests for Basic functions of Module.
  5. /// \author Thibaut Monseigne (Inria).
  6. /// \version 1.0.
  7. /// \date 09/01/2019.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
  9. ///
  10. ///-------------------------------------------------------------------------------------------------
  11. #pragma once
  12. #include "gtest/gtest.h"
  13. #include "init.hpp"
  14. #include "misc.hpp"
  15. #include <geometry/Basics.hpp>
  16. #include <geometry/Metrics.hpp>
  17. //---------------------------------------------------------------------------------------------------
  18. class Tests_Basics : public testing::Test
  19. {
  20. protected:
  21. std::vector<std::vector<Eigen::MatrixXd>> m_dataSet;
  22. void SetUp() override { m_dataSet = InitDataset::Dataset(); }
  23. };
  24. //---------------------------------------------------------------------------------------------------
  25. //---------------------------------------------------------------------------------------------------
  26. TEST_F(Tests_Basics, MatrixStandardization)
  27. {
  28. std::vector<std::vector<Eigen::MatrixXd>> calcC, refC = InitBasics::Center::Reference();
  29. std::vector<std::vector<Eigen::MatrixXd>> calcS, refS = InitBasics::StandardScaler::Reference();
  30. calcC.resize(m_dataSet.size());
  31. calcS.resize(m_dataSet.size());
  32. for (size_t k = 0; k < m_dataSet.size(); ++k)
  33. {
  34. calcC[k].resize(m_dataSet[k].size());
  35. calcS[k].resize(m_dataSet[k].size());
  36. for (size_t i = 0; i < m_dataSet[k].size(); ++i)
  37. {
  38. EXPECT_TRUE(MatrixStandardization(m_dataSet[k][i], calcC[k][i], Geometry::EStandardization::Center)) << "Error During Centerization" << std::endl;
  39. EXPECT_TRUE(MatrixStandardization(m_dataSet[k][i], calcS[k][i], Geometry::EStandardization::StandardScale)) << "Error During Standard Scaler" << std::endl;
  40. const std::string title = "Matrix Center Sample [" + std::to_string(k) + "][" + std::to_string(i) + "]";
  41. EXPECT_TRUE(isAlmostEqual(refC[k][i], calcC[k][i])) << ErrorMsg(title, refC[k][i], calcC[k][i]);
  42. EXPECT_TRUE(isAlmostEqual(refS[k][i], calcS[k][i])) << ErrorMsg(title, refS[k][i], calcS[k][i]);
  43. }
  44. }
  45. }
  46. //---------------------------------------------------------------------------------------------------
  47. //---------------------------------------------------------------------------------------------------
  48. TEST_F(Tests_Basics, GetElements)
  49. {
  50. Eigen::RowVectorXd ref(3);
  51. const std::vector<size_t> idx{ 0, 4, 7 };
  52. ref << -3, -6, -1;
  53. const Eigen::RowVectorXd calc = Geometry::GetElements(m_dataSet[0][0].row(0), idx); // row = -3, -4, -5, -4, -6, -1, -4, -1, -3, -1
  54. EXPECT_TRUE(isAlmostEqual(ref, calc)) << ErrorMsg("GetElements", ref, calc);
  55. }
  56. //---------------------------------------------------------------------------------------------------
  57. //---------------------------------------------------------------------------------------------------
  58. TEST_F(Tests_Basics, ARange)
  59. {
  60. const std::vector<size_t> ref{ 1, 3, 5, 7, 9 },
  61. calc = Geometry::ARange(size_t(1), size_t(10), size_t(2));
  62. EXPECT_TRUE(isAlmostEqual(ref, calc)) << ErrorMsg("ARange", ref, calc);
  63. }
  64. //---------------------------------------------------------------------------------------------------
  65. //---------------------------------------------------------------------------------------------------
  66. TEST_F(Tests_Basics, Vector2DTo1D)
  67. {
  68. std::vector<Eigen::MatrixXd> calc = Geometry::Vector2DTo1D(m_dataSet);
  69. bool equal = true;
  70. size_t idx = 0;
  71. for (auto& set : m_dataSet) { for (const auto& data : set) { if (!isAlmostEqual(data, calc[idx++])) { equal = false; } } }
  72. EXPECT_TRUE(equal) << "Vector2DTo1D fail";
  73. }
  74. //---------------------------------------------------------------------------------------------------
  75. //---------------------------------------------------------------------------------------------------
  76. TEST_F(Tests_Basics, Metrics)
  77. {
  78. EXPECT_TRUE(toString(Geometry::EMetric::Riemann) == "Riemann");
  79. EXPECT_TRUE(toString(Geometry::EMetric::Euclidian) == "Euclidian");
  80. EXPECT_TRUE(toString(Geometry::EMetric::LogEuclidian) == "Log Euclidian");
  81. EXPECT_TRUE(toString(Geometry::EMetric::LogDet) == "Log Determinant");
  82. EXPECT_TRUE(toString(Geometry::EMetric::Kullback) == "Kullback");
  83. EXPECT_TRUE(toString(Geometry::EMetric::ALE) == "AJD-based log-Euclidean");
  84. EXPECT_TRUE(toString(Geometry::EMetric::Harmonic) == "Harmonic");
  85. EXPECT_TRUE(toString(Geometry::EMetric::Wasserstein) == "Wasserstein");
  86. EXPECT_TRUE(toString(Geometry::EMetric::Identity) == "Identity");
  87. EXPECT_TRUE(Geometry::StringToMetric("Riemann") == Geometry::EMetric::Riemann);
  88. EXPECT_TRUE(Geometry::StringToMetric("Euclidian") == Geometry::EMetric::Euclidian);
  89. EXPECT_TRUE(Geometry::StringToMetric("Log Euclidian") == Geometry::EMetric::LogEuclidian);
  90. EXPECT_TRUE(Geometry::StringToMetric("Log Determinant") == Geometry::EMetric::LogDet);
  91. EXPECT_TRUE(Geometry::StringToMetric("Kullback") == Geometry::EMetric::Kullback);
  92. EXPECT_TRUE(Geometry::StringToMetric("AJD-based log-Euclidean") == Geometry::EMetric::ALE);
  93. EXPECT_TRUE(Geometry::StringToMetric("Harmonic") == Geometry::EMetric::Harmonic);
  94. EXPECT_TRUE(Geometry::StringToMetric("Wasserstein") == Geometry::EMetric::Wasserstein);
  95. EXPECT_TRUE(Geometry::StringToMetric("Identity") == Geometry::EMetric::Identity);
  96. EXPECT_TRUE(Geometry::StringToMetric("") == Geometry::EMetric::Identity);
  97. }
  98. //---------------------------------------------------------------------------------------------------
  99. //---------------------------------------------------------------------------------------------------
  100. TEST_F(Tests_Basics, Validation)
  101. {
  102. const Eigen::MatrixXd m1 = Eigen::MatrixXd::Zero(2, 2),
  103. m2 = Eigen::MatrixXd::Zero(1, 2),
  104. m3;
  105. std::vector<Eigen::MatrixXd> v;
  106. EXPECT_TRUE(Geometry::InRange(1, 0, 2)); // 0 <= 1 <= 2 ?
  107. EXPECT_FALSE(Geometry::InRange(2, 0, 1)); // 0 <= 2 <= 1 ?
  108. EXPECT_FALSE(Geometry::AreNotEmpty(v)); // Empty Vector
  109. v.push_back(m3);
  110. EXPECT_FALSE(Geometry::AreNotEmpty(v)); // Vector with one empty matix
  111. v.push_back(m1);
  112. EXPECT_FALSE(Geometry::AreNotEmpty(v)); // Vector With one empty matrix and one non empty matrix
  113. v.clear();
  114. v.push_back(m1);
  115. v.push_back(m2);
  116. EXPECT_TRUE(Geometry::AreNotEmpty(v)); // Vector With two non empty matrix
  117. EXPECT_TRUE(Geometry::HaveSameSize(m1, m1)); // Same matrix
  118. EXPECT_FALSE(Geometry::HaveSameSize(m3, m3)); // Same but empty
  119. EXPECT_FALSE(Geometry::HaveSameSize(m1, m2)); // DIfferents
  120. EXPECT_FALSE(Geometry::HaveSameSize(m1, m3)); // One empty
  121. EXPECT_FALSE(Geometry::HaveSameSize(v)); // Two different
  122. EXPECT_FALSE(Geometry::AreSquare(v)); // One square
  123. v.clear();
  124. v.push_back(m1);
  125. v.push_back(m1);
  126. EXPECT_TRUE(Geometry::HaveSameSize(v) && Geometry::AreSquare(v)); // Same matrix
  127. Geometry::MatrixPrint(m1); // Only to check
  128. Geometry::MatrixPrint(m3); // Only to check
  129. std::vector<std::string> vs = Geometry::Split("0,1,2,3.a\n", ",");
  130. EXPECT_TRUE(vs.size() == 4 && vs[0] == "0" && vs[1] == "1" && vs[2] == "2" && vs[3] == "3.a") << vs.size() << " " << vs[0] << " " << vs[1] << " " << vs[2] << " " << vs[3] << std::endl;
  131. }
  132. //---------------------------------------------------------------------------------------------------