aimsalgo  5.0.5
Neuroimaging image processing
pca_d.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
36 #include <aims/math/svd.h>
37 #include <aims/math/pca.h>
38 #include <aims/io/writer.h>
39 
40 template <typename T>
41 void
42 AimsPCA::doIt( const std::list< Point3d >& selectedPoints,
43  const AimsData<T>& data )
44 {
45  aims::AimsFastAllocationData<T> individuals( std::max(int(selectedPoints.size()), 1), data.dimT() ) ;
46  int i = 0 ;
47  std::list< Point3d >::const_iterator iter( selectedPoints.begin() ), last( selectedPoints.end() ) ;
48  while( iter != last )
49  {
50  for(int t = 0 ; t < data.dimT() ; ++t )
51  individuals( i, t ) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
52  ++i ; ++iter ;
53  }
54  doIt( individuals ) ;
55 }
56 
57 template <typename T>
58 void
59 AimsPCA::doIt( const AimsData<T>& individuals )
60 {
61  _computed = true ;
62  _validPca = true ;
63  _matricesComputed = false ;
64  int nbFrame = individuals.dimY() ;
65 
66  aims::AimsFastAllocationData<float> centeredIndivMatrix( individuals.dimX(), individuals.dimY() ) ;
67  if( _center || _normalize ){
68  _mean = std::vector<float>(individuals.dimY(), 0.) ;
69  _var = std::vector<float>(individuals.dimY(), 0.) ;
70  T val ;
71  for( int ind = 0 ; ind < individuals.dimX() ; ++ind )
72  for( int t = 0 ; t < individuals.dimY() ; ++t ){
73  val = individuals( ind, t ) ;
74  _mean[t] += val ;
75  _var[t] += val * val ;
76  }
77 
78  //double meanMean = 0. ;
79 
80  for( int t = 0 ; t < nbFrame ; ++t ){
81  _mean[t] /= centeredIndivMatrix.dimX() ;
82  _var[t] = sqrt(_var[t] / ( centeredIndivMatrix.dimX() - 1 ) - _mean[t] * _mean[t]) ;
83  for( int ind = 0 ; ind < centeredIndivMatrix.dimX() ; ++ind ){
84  centeredIndivMatrix( ind, t ) = individuals( ind, t ) ;
85  if( _center ){
86  centeredIndivMatrix( ind, t ) -= _mean[t] ;
87  //meanMean += centeredIndivMatrix( ind, t ) ;
88  }
89  if( _normalize )
90  centeredIndivMatrix( ind, t ) /= _var[t] ;
91  }
92  }
93  //meanMean /= centeredIndivMatrix.dimX() * centeredIndivMatrix.dimY() ;
94  //std::cout << "Centered data mean (should be 0.) = " << meanMean << std::endl ;
95  }
96 
97 
98  // Matrice des correlations
99  aims::AimsFastAllocationData<float>matVarCov(centeredIndivMatrix.dimY(), centeredIndivMatrix.dimY() ) ;
100  int x1, y1;
101  ForEach2d( matVarCov, x1, y1 )
102  {
103  for(int k=0; k < centeredIndivMatrix.dimX() ;++k)
104  matVarCov(x1, y1) += centeredIndivMatrix(k, x1) * centeredIndivMatrix(k, y1);
105  matVarCov(x1, y1) /= centeredIndivMatrix.dimX() - 1 ;
106  }
107  std::cerr << "var cov filled" << std::endl ;
108 
109 /* cout << "Centered Indiv Matrix = " << endl */
110 /* << centeredIndivMatrix.transpose() << endl ; */
111 /* cout << "Mat Var Cov = " << endl */
112 /* << matVarCov << endl ; */
113 
114  // Decomposition SVD
115  try{
116  AimsSVD< float > svd;
118  std::cerr << "doing svd !" << std::endl ;
119  aims::AimsFastAllocationData< float > eigenVal = svd.doit( matVarCov );
120  std::cerr << "svd done !" << std::endl ;
121 
122  /* cout << "Mat Var Cov Before sort = " << endl */
123  /* << matVarCov << endl ; */
124 
125  svd.sort(matVarCov, eigenVal) ;
126 
127 
128  /* cout << "Mat Var Cov After sort = " << endl */
129  /* << matVarCov << endl ; */
130 
131  _eigenVectors = matVarCov ;
132 
133  // Temporaire
134  _eigenValues.clear() ;
135  _eigenValues.reserve(matVarCov.dimX()) ;
136  for( int t = 0 ; t < matVarCov.dimX() ; ++t ){
137  _eigenValues.push_back( eigenVal(t) ) ;
138  }
139  } // catch (user_interruption& e){
140 // _validPca = false ;
141 // }
142  catch( std::exception &e ){
143  _validPca = false ;
144  if( individuals.dimX() > 0 && individuals.dimY() > 0 && individuals.dimZ() > 0 ){
145  std::cerr << "invalid pca" << std::endl ;
146 // aims::Writer< AimsData<T> > wriIM( "indivMatrix.ima") ;
147 // wriIM.write(individuals) ;
148 // aims::Writer< AimsData<float> > wriCIM( "centeredIndivMatrix.ima") ;
149 // wriCIM.write(centeredIndivMatrix) ;
150 // aims::Writer< AimsData<float> > wriVarCov( "matVarCov.ima") ;
151 // wriVarCov.write(matVarCov) ;
152 // char c ;
153 // std::cin >> c ;
154 
155  } else
156  std::cout << "empty indiv matrix" << std::endl ;
157  }
158 }
159 
float max(float x, float y)
Definition: thickness.h:97
void sort(AimsData< T > &, AimsData< T > &, AimsData< T > *v=NULL)
sort the U and V matrices and the W vector in decreasing order
int dimZ() const
AimsData< T > doit(AimsData< T > &, AimsData< T > *v=NULL)
Singular Value Decomposition.
bool _validPca
Definition: pca.h:87
bool _normalize
Definition: pca.h:91
aims::AimsFastAllocationData< float > _eigenVectors
Definition: pca.h:99
#define ForEach2d(thing, x, y)
int dimY() const
std::vector< float > _eigenValues
Definition: pca.h:98
bool _matricesComputed
Definition: pca.h:89
void setReturnType(SVDReturnType rt)
Definition: svd.h:73
std::vector< float > _var
Definition: pca.h:93
bool _computed
Definition: pca.h:88
Definition: svd.h:55
int dimT() const
bool _center
Definition: pca.h:90
void doIt(const AimsData< T > &individuals)
Definition: pca_d.h:59
int dimX() const
std::vector< float > _mean
Definition: pca.h:92