TestFTransform.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "vislearning/nice_nonvis.h"
  25. using namespace std;
  26. using namespace NICE;
  27. using namespace OBJREC;
  28. CPPUNIT_TEST_SUITE_REGISTRATION(TestFTransform);
  29. void TestFTransform::setUp()
  30. {
  31. }
  32. void TestFTransform::tearDown()
  33. {
  34. }
  35. void TestFTransform::TestFTransformComputation()
  36. {
  37. uint samplecount = 16; //samples
  38. uint sampledimension = 8; //sample dimension
  39. bool init_random = true;
  40. NICE::Matrix T(samplecount, sampledimension);
  41. //trivial test
  42. CPPUNIT_ASSERT_EQUAL(samplecount, (uint)T.rows());
  43. CPPUNIT_ASSERT_EQUAL(sampledimension, (uint)T.cols());
  44. T.set(0.0);
  45. if (init_random)
  46. srand48(time(NULL));
  47. // generate random matrix
  48. for (uint i = 0 ; i < T.rows() ; i++)
  49. for (uint j = 0 ; j < T.cols() ; j++)
  50. {
  51. T(i, j) = drand48();
  52. }
  53. //cout<<T<<endl;
  54. PCA *ftransform = new PCA(sampledimension);
  55. #ifdef NICE_USELIB_ICE
  56. #ifdef NICE_USELIB_TRLAN
  57. for (int mode = 0;mode <= 1;mode++)
  58. {
  59. ftransform->calculateBasis(T, sampledimension, mode); //fastes available method
  60. NICE::Matrix basis, featurematrix;
  61. featurematrix = T;
  62. basis = ftransform->getBasis();
  63. //cout<<basis<<endl;
  64. //orthogonality test
  65. for (int k = 0;k < basis.rows() - 1;k++)
  66. {
  67. NICE::Vector b1, b2;
  68. b1 = basis.getRow(k);
  69. b2 = basis.getRow(k + 1);
  70. double sp = b1.scalarProduct(b2);
  71. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0, sp, 1e-4);
  72. }
  73. //transform features and test
  74. for (int k = 0;k < T.rows();k++)
  75. {
  76. NICE::Vector feature, feature_transformed;
  77. feature = T.getRow(k);
  78. feature_transformed = ftransform-> getFeatureVector(feature, true);
  79. for (uint d = 0;d < feature_transformed.size();d++)
  80. {
  81. featurematrix(k, d) = feature_transformed[d];
  82. }
  83. }
  84. //cout<<featurematrix<<endl;
  85. //Covariance Test
  86. NICE::Matrix covariance;
  87. covariance = featurematrix.transpose() * featurematrix;
  88. ////cout<<covariance<<endl;
  89. for (uint i = 0;i < covariance.rows();i++)
  90. {
  91. for (uint j = 0;j < covariance.cols();j++)
  92. {
  93. if (i == j)
  94. {
  95. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN((double)samplecount, covariance(i, j), 1e-4);
  96. }
  97. else
  98. {
  99. CPPUNIT_ASSERT_DOUBLES_EQUAL_NOT_NAN(0.0, covariance(i, j), 1e-4);
  100. }
  101. }
  102. }
  103. }
  104. #endif
  105. #endif
  106. }