LCOV - code coverage report
Current view: top level - routines - pca.hpp (source / functions) Hit Total Coverage
Test: clean.info Lines: 37 37 100.0 %
Date: 2013-05-24 Functions: 5 16 31.2 %
Branches: 66 298 22.1 %

           Branch data     Line data    Source code
       1                 :            : /* This software is distributed under BSD 3-clause license (see LICENSE file).
       2                 :            :  *
       3                 :            :  * Copyright (c) 2012-2013 Sergey Lisitsyn
       4                 :            :  */
       5                 :            : 
       6                 :            : #ifndef TAPKEE_PCA_H_
       7                 :            : #define TAPKEE_PCA_H_
       8                 :            : 
       9                 :            : /* Tapkee includes */
      10                 :            : #include <tapkee/defines.hpp>
      11                 :            : #include <tapkee/utils/time.hpp>
      12                 :            : /* End of Tapkee includes */
      13                 :            : 
      14                 :            : namespace tapkee
      15                 :            : {
      16                 :            : namespace tapkee_internal
      17                 :            : {
      18                 :            : 
      19                 :            : template <class RandomAccessIterator, class FeatureVectorCallback>
      20                 :          5 : DenseMatrix project(const DenseMatrix& projection_matrix, const DenseVector& mean_vector,
      21                 :            :                     RandomAccessIterator begin, RandomAccessIterator end, 
      22                 :            :                     FeatureVectorCallback callback, IndexType dimension)
      23                 :            : {
      24 [ +  - ][ +  - ]:          5 :         timed_context context("Data projection");
         [ +  - ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      25                 :            : 
      26   [ +  -  #  #  :          5 :         DenseVector current_vector(dimension);
                   #  # ]
      27 [ +  - ][ #  # ]:          5 :         DenseVector current_vector_subtracted_mean(dimension);
                 [ #  # ]
      28                 :            : 
      29 [ +  - ][ +  - ]:          5 :         DenseMatrix embedding = DenseMatrix::Zero((end-begin),projection_matrix.cols());
         [ +  - ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      30                 :            : 
      31 [ +  + ][ #  # ]:        255 :         for (RandomAccessIterator iter=begin; iter!=end; ++iter)
                 [ #  # ]
      32                 :            :         {
      33 [ +  - ][ #  # ]:        250 :                 callback.vector(*iter,current_vector);
                 [ #  # ]
      34 [ +  - ][ +  - ]:        250 :                 current_vector_subtracted_mean = current_vector - mean_vector;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      35 [ +  - ][ +  - ]:        250 :                 embedding.row(iter-begin) = projection_matrix.transpose()*current_vector_subtracted_mean;
         [ +  - ][ +  - ]
         [ +  - ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      36                 :            :         }
      37                 :            : 
      38 [ +  - ][ +  - ]:          5 :         return embedding;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      39                 :            : }
      40                 :            : 
      41                 :            : template <class RandomAccessIterator, class FeatureVectorCallback>
      42                 :          6 : DenseVector compute_mean(RandomAccessIterator begin, RandomAccessIterator end,
      43                 :            :                          FeatureVectorCallback callback, IndexType dimension) 
      44                 :            : {
      45 [ +  - ][ #  # ]:          6 :         DenseVector mean = DenseVector::Zero(dimension);
                 [ #  # ]
      46 [ +  - ][ #  # ]:          6 :         DenseVector current_vector(dimension);
                 [ #  # ]
      47 [ +  + ][ #  # ]:        306 :         for (RandomAccessIterator iter=begin; iter!=end; ++iter)
                 [ #  # ]
      48                 :            :         {
      49 [ +  - ][ #  # ]:        300 :                 callback.vector(*iter,current_vector);
                 [ #  # ]
      50 [ +  - ][ #  # ]:        300 :                 mean += current_vector;
                 [ #  # ]
      51                 :            :         }
      52 [ +  - ][ +  - ]:          6 :         mean.array() /= (end-begin);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      53 [ +  - ][ #  # ]:          6 :         return mean;
                 [ #  # ]
      54                 :            : }
      55                 :            : 
      56                 :            : template <class RandomAccessIterator, class FeatureVectorCallback>
      57                 :          1 : DenseSymmetricMatrix compute_covariance_matrix(RandomAccessIterator begin, RandomAccessIterator end, 
      58                 :            :                                                const DenseVector& mean, FeatureVectorCallback callback, IndexType dimension)
      59                 :            : {
      60 [ +  - ][ +  - ]:          1 :         timed_context context("Constructing PCA covariance matrix");
         [ +  - ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      61                 :            : 
      62         [ +  - ]:          1 :         DenseSymmetricMatrix covariance_matrix = DenseSymmetricMatrix::Zero(dimension,dimension);
           [ +  -  #  # ]
           [ #  #  #  # ]
                 [ #  # ]
      63                 :            :         
      64 [ +  - ][ #  # ]:          1 :         DenseVector current_vector(dimension);
                 [ #  # ]
      65 [ +  + ][ #  # ]:         51 :         for (RandomAccessIterator iter=begin; iter!=end; ++iter)
                 [ #  # ]
      66                 :            :         {
      67 [ +  - ][ #  # ]:         50 :                 callback.vector(*iter,current_vector);
                 [ #  # ]
      68 [ +  - ][ +  - ]:         50 :                 covariance_matrix.selfadjointView<Eigen::Upper>().rankUpdate(current_vector,1.0);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      69                 :            :         }
      70 [ +  - ][ +  - ]:          1 :         covariance_matrix.selfadjointView<Eigen::Upper>().rankUpdate(mean,-1.0);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
      71                 :            : 
      72 [ +  - ][ #  # ]:          1 :         return covariance_matrix;
                 [ #  # ]
      73                 :            : }
      74                 :            : 
      75                 :            : template <class RandomAccessIterator, class KernelCallback>
      76                 :          5 : DenseSymmetricMatrix compute_centered_kernel_matrix(RandomAccessIterator begin, RandomAccessIterator end, 
      77                 :            :                                                     KernelCallback callback)
      78                 :            : {
      79 [ +  - ][ +  - ]:          5 :         timed_context context("Constructing kPCA centered kernel matrix");
         [ +  - ][ +  - ]
         [ +  - ][ +  - ]
         [ #  # ][ #  # ]
                 [ #  # ]
      80                 :            : 
      81   [ +  -  +  -  :          5 :         DenseSymmetricMatrix kernel_matrix(end-begin,end-begin);
                   #  # ]
      82                 :            : 
      83 [ +  + ][ +  + ]:        135 :         for (RandomAccessIterator i_iter=begin; i_iter!=end; ++i_iter)
                 [ #  # ]
      84                 :            :         {
      85 [ +  + ][ +  + ]:       2245 :                 for (RandomAccessIterator j_iter=i_iter; j_iter!=end; ++j_iter)
                 [ #  # ]
      86                 :            :                 {
      87 [ +  - ][ #  # ]:       2115 :                         ScalarType k = callback.kernel(*i_iter,*j_iter);
      88   [ +  -  +  - ]:       2115 :                         kernel_matrix(i_iter-begin,j_iter-begin) = k;
                 [ #  # ]
      89 [ +  - ][ +  - ]:       2115 :                         kernel_matrix(j_iter-begin,i_iter-begin) = k;
                 [ #  # ]
      90                 :            :                 }
      91                 :            :         }
      92                 :            : 
      93 [ +  - ][ +  - ]:          5 :         centerMatrix(kernel_matrix);
                 [ #  # ]
      94                 :            : 
      95                 :          5 :         return kernel_matrix;
      96                 :            : }
      97                 :            : 
      98                 :            : } // End of namespace tapkee_internal
      99                 :            : } // End of namespace tapkee
     100                 :            : 
     101                 :            : #endif

Generated by: LCOV version 1.9