aimsalgo  5.1.2
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 
35 #include <aims/math/svd.h>
36 #include <aims/math/pca.h>
37 #include <aims/io/writer.h>
38 
39 template <typename T>
40 void
41 AimsPCA::doIt( const std::list< Point3d >& selectedPoints,
42  const carto::rc_ptr<carto::Volume<T> > & data )
43 {
44  carto::VolumeRef<T> individuals(
45  std::max(int(selectedPoints.size()), 1), data->getSizeT(), 1, 1,
46  carto::AllocatorContext::fast() );
47  int i = 0 ;
48  std::list< Point3d >::const_iterator iter( selectedPoints.begin() ), last( selectedPoints.end() ) ;
49  while( iter != last )
50  {
51  for(int t = 0 ; t < data->getSizeT() ; ++t )
52  individuals( i, t ) = data->at( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
53  ++i ; ++iter ;
54  }
55  doIt( individuals ) ;
56 }
57 
58 template <typename T>
59 void
61 {
62  _computed = true ;
63  _validPca = true ;
64  _matricesComputed = false ;
65  int nbFrame = individuals->getSizeY();
66 
67  carto::VolumeRef<float> centeredIndivMatrix(
68  individuals->getSizeX(), individuals->getSizeY(), 1, 1,
69  carto::AllocatorContext::fast() );
70 
71  if( _center || _normalize )
72  {
73  _mean = std::vector<float>(individuals->getSizeY(), 0.) ;
74  _var = std::vector<float>(individuals->getSizeY(), 0.) ;
75  T val ;
76  for( int ind = 0 ; ind < individuals->getSizeX() ; ++ind )
77  for( int t = 0 ; t < individuals->getSizeY() ; ++t )
78  {
79  val = individuals->at( ind, t ) ;
80  _mean[t] += val ;
81  _var[t] += val * val ;
82  }
83 
84  //double meanMean = 0. ;
85 
86  for( int t = 0 ; t < nbFrame ; ++t )
87  {
88  _mean[t] /= centeredIndivMatrix->getSizeX() ;
89  _var[t] = sqrt( _var[t] / ( centeredIndivMatrix->getSizeX() - 1 )
90  - _mean[t] * _mean[t] );
91  for( int ind = 0 ; ind < centeredIndivMatrix->getSizeX() ; ++ind )
92  {
93  centeredIndivMatrix( ind, t ) = individuals->at( ind, t ) ;
94  if( _center )
95  {
96  centeredIndivMatrix( ind, t ) -= _mean[t] ;
97  //meanMean += centeredIndivMatrix( ind, t ) ;
98  }
99  if( _normalize )
100  centeredIndivMatrix( ind, t ) /= _var[t] ;
101  }
102  }
103  //meanMean /= centeredIndivMatrix.getSizeX() * centeredIndivMatrix.getSizeY() ;
104  //std::cout << "Centered data mean (should be 0.) = " << meanMean << std::endl ;
105  }
106 
107 
108  // Matrice des correlations
109  carto::VolumeRef<float>matVarCov(
110  centeredIndivMatrix->getSizeY(), centeredIndivMatrix->getSizeY(), 1, 1,
111  carto::AllocatorContext::fast() );
112 
113  int x1, y1;
114  int dx1 = matVarCov->getSizeX(), dy1 = matVarCov->getSizeY();
115 
116  for( y1=0; y1<dy1; ++y1 )
117  for( x1=0; x1<dx1; ++x1 )
118  {
119  for(int k=0; k < centeredIndivMatrix->getSizeX() ;++k)
120  matVarCov(x1, y1)
121  += centeredIndivMatrix(k, x1) * centeredIndivMatrix(k, y1);
122  matVarCov(x1, y1) /= centeredIndivMatrix->getSizeX() - 1;
123  }
124  std::cerr << "var cov filled" << std::endl ;
125 
126 /* cout << "Centered Indiv Matrix = " << endl */
127 /* << centeredIndivMatrix.transpose() << endl ; */
128 /* cout << "Mat Var Cov = " << endl */
129 /* << matVarCov << endl ; */
130 
131  // Decomposition SVD
132  try
133  {
134  AimsSVD< float > svd;
136  std::cerr << "doing svd !" << std::endl ;
137  carto::VolumeRef< float > eigenVal = svd.doit( matVarCov );
138  std::cerr << "svd done !" << std::endl ;
139 
140  /* cout << "Mat Var Cov Before sort = " << endl */
141  /* << matVarCov << endl ; */
142 
143  svd.sort(matVarCov, eigenVal) ;
144 
145 
146  /* cout << "Mat Var Cov After sort = " << endl */
147  /* << matVarCov << endl ; */
148 
149  _eigenVectors = matVarCov ;
150 
151  // Temporaire
152  _eigenValues.clear() ;
153  _eigenValues.reserve(matVarCov->getSizeX());
154  for( int t = 0 ; t < matVarCov->getSizeX() ; ++t )
155  {
156  _eigenValues.push_back( eigenVal(t) ) ;
157  }
158  } // catch (user_interruption& e){
159 // _validPca = false ;
160 // }
161  catch( std::exception &e )
162  {
163  _validPca = false ;
164  if( individuals->getSizeX() > 0 && individuals->getSizeY() > 0
165  && individuals->getSizeZ() > 0 )
166  {
167  std::cerr << "invalid pca" << std::endl ;
168 
169  }
170  else
171  std::cout << "empty indiv matrix" << std::endl ;
172  }
173 }
174 
bool _matricesComputed
Definition: pca.h:91
carto::VolumeRef< float > _eigenVectors
Definition: pca.h:101
std::vector< float > _mean
Definition: pca.h:94
bool _validPca
Definition: pca.h:89
std::vector< float > _var
Definition: pca.h:95
bool _computed
Definition: pca.h:90
void doIt(const carto::rc_ptr< carto::Volume< T > > &individuals)
Definition: pca_d.h:60
bool _center
Definition: pca.h:92
std::vector< float > _eigenValues
Definition: pca.h:100
bool _normalize
Definition: pca.h:93
Definition: svd.h:59
void sort(carto::VolumeRef< T > &, carto::VolumeRef< T > &, carto::VolumeRef< T > *v=NULL)
sort the U and V matrices and the W vector in decreasing order
carto::VolumeRef< T > doit(carto::VolumeRef< T > &, carto::VolumeRef< T > *v=NULL)
Singular Value Decomposition.
void setReturnType(SVDReturnType rt)
Definition: svd.h:76
const T & at(long x, long y=0, long z=0, long t=0) const
int getSizeY() const
int getSizeX() const
float max(float x, float y)
Definition: thickness.h:98