TestFTransform.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // C++ Implementation: TestFTransform
  3. //
  4. // Description:
  5. //
  6. //
  7. // Author: Michael Koch <Koch.Michael@uni-jena.de>, (C) 2009
  8. //
  9. // Copyright: See COPYING file that comes with this distribution
  10. //
  11. /**
  12. * @file TestFTransform.cpp
  13. * @brief TestFTransform
  14. * @author Michael Koch
  15. * @date Di Aug 4 2009
  16. */
  17. #include "TestFTransform.h"
  18. #include <string>
  19. #include "core/basics/cppunitex.h"
  20. #include "core/basics/numerictools.h"
  21. #include "core/vector/Distance.h"
  22. #include "vislearning/math/ftransform/FTransform.h"
  23. #include "vislearning/math/ftransform/PCA.h"
  24. using namespace std;
  25. using namespace NICE;
  26. using namespace OBJREC;
  27. CPPUNIT_TEST_SUITE_REGISTRATION(TestFTransform);
  28. void TestFTransform::setUp()
  29. {
  30. }
  31. void TestFTransform::tearDown()
  32. {
  33. }
  34. void TestFTransform::TestFTransformComputation()
  35. {
  36. uint samplecount = 16; //samples
  37. uint sampledimension = 8; //sample dimension
  38. bool init_random = true;
  39. NICE::Matrix T(samplecount, sampledimension);
  40. //trivial test
  41. CPPUNIT_ASSERT_EQUAL(samplecount, (uint)T.rows());
  42. CPPUNIT_ASSERT_EQUAL(sampledimension, (uint)T.cols());
  43. T.set(0.0);
  44. if (init_random)
  45. srand48(time(NULL));
  46. // generate random matrix
  47. for (uint i = 0 ; i < T.rows() ; i++)
  48. for (uint j = 0 ; j < T.cols() ; j++)
  49. {
  50. T(i, j) = drand48();
  51. }
  52. //cout<<T<<endl;
  53. PCA *ftransform = new PCA(sampledimension);
  54. #ifdef NICE_USELIB_ICE
  55. #ifdef NICE_USELIB_TRLAN
  56. for (int mode = 0;mode <= 1;mode++)
  57. {
  58. ftransform->calculateBasis(T, sampledimension, mode); //fastes available method
  59. NICE::Matrix basis, featurematrix;
  60. featurematrix = T;
  61. basis = ftransform->getBasis();
  62. //cout<<basis<<endl;
  63. //orthogonality test
  64. for (int k = 0;k < basis.rows() - 1;k++)
  65. {
  66. NICE::Vector b1, b2;
  67. b1 = basis.getRow(k);
  68. b2 = basis.getRow(k + 1);
  69. double sp = b1.scalarProduct(b2);
  70. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0, sp, 1e-4);
  71. }
  72. //transform features and test
  73. for (int k = 0;k < T.rows();k++)
  74. {
  75. NICE::Vector feature, feature_transformed;
  76. feature = T.getRow(k);
  77. feature_transformed = ftransform-> getFeatureVector(feature, true);
  78. for (uint d = 0;d < feature_transformed.size();d++)
  79. {
  80. featurematrix(k, d) = feature_transformed[d];
  81. }
  82. }
  83. //cout<<featurematrix<<endl;
  84. //Covariance Test
  85. NICE::Matrix covariance;
  86. covariance = featurematrix.transpose() * featurematrix;
  87. ////cout<<covariance<<endl;
  88. for (uint i = 0;i < covariance.rows();i++)
  89. {
  90. for (uint j = 0;j < covariance.cols();j++)
  91. {
  92. if (i == j)
  93. {
  94. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN((double)samplecount, covariance(i, j), 1e-4);
  95. }
  96. else
  97. {
  98. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0, covariance(i, j), 1e-4);
  99. }
  100. }
  101. }
  102. }
  103. #endif
  104. #endif
  105. }