MinMaxScalerTests.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Fixture.h>
  9. #include <FeatureMatrixMinMaxScaler.h>
  10. namespace EMotionFX::MotionMatching
  11. {
  12. class MinMaxScalerFixture
  13. : public Fixture
  14. {
  15. public:
  16. void SetUp() override
  17. {
  18. Fixture::SetUp();
  19. // Construct 3x3 matrix:
  20. // 1 2 3
  21. // 4 5 6
  22. // 7 8 9
  23. m_featureMatrix.resize(3, 3);
  24. float counter = 1.0f;
  25. for (size_t row = 0; row < 3; ++row)
  26. {
  27. for (size_t column = 0; column < 3; ++column)
  28. {
  29. m_featureMatrix(row, column) = counter;
  30. counter++;
  31. }
  32. }
  33. }
  34. FeatureMatrix m_featureMatrix;
  35. static constexpr float s_testEpsilon = 0.000001f;
  36. };
  37. TEST_F(MinMaxScalerFixture, MinMaxValues)
  38. {
  39. FeatureMatrix m;
  40. m.resize(3, 3);
  41. m(0,0) = 0.0f; m(0,1) =-1.0f; m(0,2) = 9.0f;
  42. m(1,0) = 0.5f; m(1,1) = 5.0f; m(1,2) = 6.0f;
  43. m(2,0) = 7.0f; m(2,1) = 0.1f; m(2,2) = 3.0f;
  44. MinMaxScaler minMaxScaler;
  45. EXPECT_TRUE(minMaxScaler.Fit(m));
  46. const AZStd::vector<float>& min = minMaxScaler.GetMin();
  47. const AZStd::vector<float>& max = minMaxScaler.GetMax();
  48. EXPECT_NEAR(min[0], 0.0f, s_testEpsilon); EXPECT_NEAR(max[0], 7.0f, s_testEpsilon);
  49. EXPECT_NEAR(min[1],-1.0f, s_testEpsilon); EXPECT_NEAR(max[1], 5.0f, s_testEpsilon);
  50. EXPECT_NEAR(min[2], 3.0f, s_testEpsilon); EXPECT_NEAR(max[2], 9.0f, s_testEpsilon);
  51. }
  52. TEST_F(MinMaxScalerFixture, Transform)
  53. {
  54. FeatureMatrix m;
  55. m.resize(3, 4);
  56. m(0,0) = 0.0f; m(0,1) =-1.0f; m(0,2) = 10.0f; m(0, 3) = 3.0f;
  57. m(1,0) = 0.5f; m(1,1) = 1.0f; m(1,2) =-10.0f; m(1, 3) = 3.0f;
  58. m(2,0) = 1.0f; m(2,1) = 0.5f; m(2,2) =-5.0f; m(2, 3) = 3.0f;
  59. MinMaxScaler minMaxScaler;
  60. EXPECT_TRUE(minMaxScaler.Fit(m, {/*featureMin=*/0.0f, /*featureMax=*/1.0f, /*clip=*/false }));
  61. FeatureMatrix t = minMaxScaler.Transform(m);
  62. EXPECT_NEAR(t(0,0), 0.0f, s_testEpsilon); EXPECT_NEAR(t(0, 1), 0.0f, s_testEpsilon); EXPECT_NEAR(t(0, 2), 1.0f, s_testEpsilon); EXPECT_NEAR(t(0, 3), 3.0f, s_testEpsilon);
  63. EXPECT_NEAR(t(1,0), 0.5f, s_testEpsilon); EXPECT_NEAR(t(1, 1), 1.0f, s_testEpsilon); EXPECT_NEAR(t(1, 2), 0.0f, s_testEpsilon); EXPECT_NEAR(t(1, 3), 3.0f, s_testEpsilon);
  64. EXPECT_NEAR(t(2,0), 1.0f, s_testEpsilon); EXPECT_NEAR(t(2, 1), 0.75f, s_testEpsilon);EXPECT_NEAR(t(2, 2), 0.25f, s_testEpsilon);EXPECT_NEAR(t(1, 3), 3.0f, s_testEpsilon);
  65. }
  66. TEST_F(MinMaxScalerFixture, TransformValueNoClipping)
  67. {
  68. FeatureMatrix m;
  69. m.resize(2, 1);
  70. m(0, 0) = -2.0f;
  71. m(1, 0) = 2.0f; // range = 4.0
  72. MinMaxScaler minMaxScaler;
  73. EXPECT_TRUE(minMaxScaler.Fit(m, {/*featureMin=*/0.0f, /*featureMax=*/1.0f, /*clip=*/false }));
  74. EXPECT_NEAR(minMaxScaler.Transform(-6.0f, 0), -1.0f, s_testEpsilon);
  75. EXPECT_NEAR(minMaxScaler.Transform(4.0f, 0), 1.5f, s_testEpsilon);
  76. }
  77. TEST_F(MinMaxScalerFixture, TransformValueClip)
  78. {
  79. FeatureMatrix m;
  80. m.resize(2, 1);
  81. m(0, 0) = -2.0f;
  82. m(1, 0) = 2.0f;
  83. MinMaxScaler minMaxScaler;
  84. EXPECT_TRUE(minMaxScaler.Fit(m, {/*featureMin=*/0.0f, /*featureMax=*/1.0f, /*clip=*/true}));
  85. EXPECT_NEAR(minMaxScaler.Transform(-6.0f, 0), 0.0f, s_testEpsilon);
  86. EXPECT_NEAR(minMaxScaler.Transform(8.0f, 0), 1.0f, s_testEpsilon);
  87. }
  88. TEST_F(MinMaxScalerFixture, FeatureRangeTest)
  89. {
  90. FeatureMatrix m;
  91. m.resize(2, 1);
  92. m(0, 0) = -2.0f;
  93. m(1, 0) = 2.0f;
  94. MinMaxScaler minMaxScaler;
  95. EXPECT_TRUE(minMaxScaler.Fit(m, {/*featureMin=*/6.0f, /*featureMax=*/10.0f, /*clip=*/true}));
  96. EXPECT_NEAR(minMaxScaler.Transform(-2.0f, 0), 6.0f, s_testEpsilon);
  97. EXPECT_NEAR(minMaxScaler.Transform(0.0f, 0), 8.0f, s_testEpsilon);
  98. EXPECT_NEAR(minMaxScaler.Transform(2.0f, 0), 10.0f, s_testEpsilon);
  99. //--
  100. m(0, 0) = 10.0f;
  101. m(1, 0) = 20.0f;
  102. FeatureMatrixTransformer::Settings fitSettings;
  103. fitSettings.m_featureMin = -5.0f;
  104. fitSettings.m_featureMax = 5.0f;
  105. EXPECT_TRUE(minMaxScaler.Fit(m, fitSettings));
  106. EXPECT_NEAR(minMaxScaler.Transform(10.0f, 0), -5.0f, s_testEpsilon);
  107. EXPECT_NEAR(minMaxScaler.Transform(15.0f, 0), 0.0f, s_testEpsilon);
  108. EXPECT_NEAR(minMaxScaler.Transform(20.0f, 0), 5.0f, s_testEpsilon);
  109. }
  110. TEST_F(MinMaxScalerFixture, SameValues)
  111. {
  112. FeatureMatrix m;
  113. m.resize(3, 1);
  114. m(0, 0) = 2.0f;
  115. m(1, 0) = 2.0f;
  116. m(2, 0) = 2.0f;
  117. MinMaxScaler minMaxScaler;
  118. EXPECT_TRUE(minMaxScaler.Fit(m));
  119. EXPECT_NEAR(minMaxScaler.GetMin()[0], 2.0f, s_testEpsilon);
  120. EXPECT_NEAR(minMaxScaler.GetMax()[0], 2.0f, s_testEpsilon);
  121. EXPECT_NEAR(minMaxScaler.Transform(2.0f, 0), 2.0f, s_testEpsilon);
  122. EXPECT_NEAR(minMaxScaler.InverseTransform(2.0f, 0), 2.0f, s_testEpsilon);
  123. // Test out of data range.
  124. // In case the feature was constant, it is expected to not transform the value.
  125. EXPECT_NEAR(minMaxScaler.Transform(0.0f, 0), 0.0f, s_testEpsilon);
  126. EXPECT_NEAR(minMaxScaler.Transform(10.0f, 0), 10.0f, s_testEpsilon);
  127. // Test out of feature range.
  128. // As the feature is constant, no matter what the input is, the constant feature should be returned.
  129. EXPECT_NEAR(minMaxScaler.InverseTransform(0.0f, 0), 2.0f, s_testEpsilon);
  130. EXPECT_NEAR(minMaxScaler.InverseTransform(10.0f, 0), 2.0f, s_testEpsilon);
  131. }
  132. TEST_F(MinMaxScalerFixture, CloseEpsilonValues)
  133. {
  134. FeatureMatrix m;
  135. m.resize(3, 1);
  136. m(0, 0) = 2.0f + MinMaxScaler::s_epsilon;
  137. m(1, 0) = 2.0f - MinMaxScaler::s_epsilon;
  138. m(2, 0) = 2.0f;
  139. MinMaxScaler minMaxScaler;
  140. EXPECT_TRUE(minMaxScaler.Fit(m));
  141. EXPECT_NEAR(minMaxScaler.GetMin()[0], 2.0f - MinMaxScaler::s_epsilon, s_testEpsilon);
  142. EXPECT_NEAR(minMaxScaler.GetMax()[0], 2.0f + MinMaxScaler::s_epsilon, s_testEpsilon);
  143. EXPECT_NEAR(minMaxScaler.Transform(2.0f, 0), 2.0f, s_testEpsilon);
  144. EXPECT_NEAR(minMaxScaler.InverseTransform(2.0f, 0), 2.0f, s_testEpsilon);
  145. EXPECT_NEAR(minMaxScaler.Transform(2.0f + MinMaxScaler::s_epsilon, 0), 2.0f + MinMaxScaler::s_epsilon, s_testEpsilon);
  146. EXPECT_NEAR(minMaxScaler.InverseTransform(2.0f + MinMaxScaler::s_epsilon, 0), 2.0f + MinMaxScaler::s_epsilon, s_testEpsilon);
  147. // Test out of data range.
  148. // In case the feature was constant, it is expected to not transform the value.
  149. EXPECT_NEAR(minMaxScaler.Transform(0.0f, 0), 0.0f, s_testEpsilon);
  150. EXPECT_NEAR(minMaxScaler.Transform(10.0f, 0), 10.0f, s_testEpsilon);
  151. // Test out of feature range.
  152. // As the feature is constant, no matter what the input is, the constant feature should be returned.
  153. EXPECT_FLOAT_EQ(minMaxScaler.InverseTransform(0.0f, 0), 2.0f - MinMaxScaler::s_epsilon);
  154. EXPECT_FLOAT_EQ(minMaxScaler.InverseTransform(10.0f, 0), 2.0f + MinMaxScaler::s_epsilon);
  155. }
  156. TEST_F(MinMaxScalerFixture, SimpleRoundTrip)
  157. {
  158. FeatureMatrix m;
  159. m.resize(2, 1);
  160. m(0, 0) = -2.0f;
  161. m(1, 0) = 2.0f; // range = 4.0
  162. MinMaxScaler minMaxScaler;
  163. FeatureMatrixTransformer::Settings fitSettings;
  164. fitSettings.m_featureMin = -10.0f;
  165. fitSettings.m_featureMax = 10.0f;
  166. EXPECT_TRUE(minMaxScaler.Fit(m, fitSettings));
  167. const float tVal = minMaxScaler.Transform(0.0f, 0);
  168. EXPECT_NEAR(tVal, 0.0f, s_testEpsilon);
  169. const float orgVal = minMaxScaler.InverseTransform(tVal, 0);
  170. EXPECT_NEAR(orgVal, 0.0f, s_testEpsilon);
  171. }
  172. TEST_F(MinMaxScalerFixture, RoundTripFeatureRange)
  173. {
  174. FeatureMatrix m;
  175. m.resize(3, 4);
  176. m(0, 0) = 0.0f; m(0, 1) = -1.0f; m(0, 2) = 10.0f; m(0, 3) = 3.0f;
  177. m(1, 0) = 0.5f; m(1, 1) = 1.0f; m(1, 2) = -10.0f; m(1, 3) = 3.0f + MinMaxScaler::s_epsilon;
  178. m(2, 0) = 1.0f; m(2, 1) = 0.5f; m(2, 2) = -5.0f; m(2, 3) = 3.0f;
  179. MinMaxScaler minMaxScaler;
  180. FeatureMatrixTransformer::Settings fitSettings;
  181. fitSettings.m_featureMin = -36.0f;
  182. fitSettings.m_featureMax = 250.0f;
  183. EXPECT_TRUE(minMaxScaler.Fit(m, fitSettings));
  184. const FeatureMatrix t = minMaxScaler.Transform(m);
  185. const FeatureMatrix inv = minMaxScaler.InverseTransform(t);
  186. const FeatureMatrix::Index numRows = m.rows();
  187. const FeatureMatrix::Index numColumns = m.cols();
  188. for (FeatureMatrix::Index row = 0; row < numRows; ++row)
  189. {
  190. for (FeatureMatrix::Index column = 0; column < numColumns; ++column)
  191. {
  192. EXPECT_NEAR(inv(row, column), m(row, column), s_testEpsilon);
  193. }
  194. }
  195. }
  196. } // EMotionFX::MotionMatching