35 #ifndef MIXTURE_OF_PPCA_D_H
36 #define MIXTURE_OF_PPCA_D_H
61 double * PpcaAnalyserElement::_exp = 0 ;
64 _useOnlyCorrIndiv( useOnlyCorrIndiv ), _significantNumberOfVp( significantNumberOfVp ), _computed( false ), _energy(0.), _valid(true)
67 _exp =
new double[75010] ;
68 for(
int i = 0 ; i < 7510 ; ++i )
69 _exp[i] = exp( -i/100. ) ;
77 const std::list<int>& selectedIndividuals,
double initialPi,
81 list<int>::const_iterator iter( selectedIndividuals.begin() ),
82 last( selectedIndividuals.end() ) ;
85 while( iter != last ){
86 for(
int t = 0 ; t < individuals->getSizeY() ; ++t )
87 indivMatrix( i, t ) = individuals( *iter, t ) ;
93 doIt( indivMatrix, individuals->
getSizeX(), noiseRef ) ;
100 int totalNbOfIndividuals,
double noiseRef )
103 int nbInd = indivMatrix->getSizeX() ;
104 int nbFrame = indivMatrix->getSizeY() ;
109 for(
int ind = 0 ; ind < nbInd ; ++ind )
110 for(
int t = 0 ; t < nbFrame ; ++t )
111 _mean[t] += indivMatrix( ind, t ) ;
112 for(
int t = 0 ; t < nbFrame ; ++t ){
113 _mean[t] /= centeredIndivMatrix->
getSizeX() ;
114 for(
int ind = 0 ; ind < centeredIndivMatrix->getSizeX() ; ++ind )
115 centeredIndivMatrix( ind, t ) = indivMatrix( ind, t ) - _mean[t] ;
123 for(
int k = 0 ; k < centeredIndivMatrix->getSizeX() ; ++k )
124 matVarCov(x, y) += centeredIndivMatrix(k, x) * centeredIndivMatrix(k, y) ;
125 matVarCov(x, y) /= centeredIndivMatrix->getSizeX() - 1 ;
132 svd.
sort( matVarCov, eigenValues ) ;
137 for(
int i = _significantNumberOfVp ; i < nbFrame ; ++i )
138 _sigma2 += eigenValues[i] ;
139 _sigma2 /= (double)( nbFrame - _significantNumberOfVp ) ;
140 cout << endl <<
"sigma2 = " << setprecision(3) << _sigma2 << endl ;
143 for(
int i = 0 ; i < _significantNumberOfVp ; ++i ){
144 for(
int t = 0 ; t < nbFrame ; ++t ){
145 selectedEigenVectors(t, i) = matVarCov( t, i ) ;
153 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
154 tempWi(i, i) = sqrt( eigenValues[i] - _sigma2 ) ;
155 _Wi = selectedEigenVectors.cross( tempWi ) ;
159 for(
int i = 0 ; i < nbFrame ; ++i )
160 Ci(i, i) += _sigma2 ;
163 double s = 0.000001 ;
171 double ts = s * w.maximum();
174 for ( i = 0 ; i < n; i++ ){
175 if ( w( i, i ) < 0. ){
176 cout << endl <<
"pour i = " << i <<
", valeur w = " << w( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
177 w( i, i ) = - w( i, i ) ;
179 detCi *= w( i, i ) / _sigma2 ;
180 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
181 else w( i, i ) = 0.0f;
185 _invCi = v.cross( w.cross( u.transpose() ) ) ;
189 _normFactor = 1 / ( pow( detCi, 0.5 ) * pow( _sigma2 / noiseRef, nbFrame / 2. ) ) ;
191 cout <<
"norm factor avec noiseRef = " << _normFactor << endl ;
195 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
196 Mi(i, i) += _sigma2 ;
201 w = svd3.
doit( u, &v );
203 ts = s * w.maximum();
205 for ( i = 0 ; i < n; i++ ){
206 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
207 else w( i, i ) = 0.0f;
209 _invMi = v.cross( w.cross( u.transpose() ) ) ;
221 int nbInd = indivMatrix->getSizeX() ;
222 int nbFrame = indivMatrix->getSizeY() ;
233 double sumInd, sumMean, indMean, meanMean, corr = 0. ;
241 for(
int n = 0 ; n < nbInd ; ++n ){
248 for(
int t = 0 ; t < nbFrame ; ++t ){
249 indivn[t] = indivMatrix(n, t) ;
250 centeredIndivn[t] = indivMatrix(n, t) - _mean[t] ;
251 sumInd += indivn[t] ;
252 sumMean += _mean[t] ;
255 if( useOnlyCorrIndiv == 1 ){
256 indMean = sumInd / (double)nbFrame ;
257 meanMean = sumMean / (double)nbFrame ;
258 for(
int t = 0 ; t < nbFrame ; ++t )
259 corr += ( indivn[t] - indMean ) * ( _mean[t] - meanMean ) ;
273 _dist[n] = ( centeredIndivn.clone().transpose() ).
cross( _invCi.cross( centeredIndivn ) )(0, 0) ;
274 if( _dist[n] > 1500 )
277 _pTni[n] = _normFactor * _exp[ int(0.5 * _dist[n] * 100. + 0.000001 ) ] ;
281 _dist[n] = ( centeredIndivn.clone().transpose() ).
cross( _invCi.cross( centeredIndivn ) )(0, 0) ;
282 if( _dist[n] > 1500 )
285 _pTni[n] = _normFactor * _exp[ int(0.5 * _dist[n] * 100. + 0.000001) ] ;
309 if( nbIndTaken < indivMatrix->getSizeY() * 2 )
324 bool explicitComputing = true ;
326 int nbInd = indivMatrix->getSizeX() ;
327 int nbFrame = indivMatrix->getSizeY() ;
341 for(
int n = 0 ; n < nbInd ; ++n ){
342 previousRn = _Rn[n] ;
344 _Rn[n] = _pTni[n] * _Pi / pTn[n] ;
359 cout <<
"critere pour la classe = " << setprecision(9) << _sumDiff2Rni <<
" - " ;
366 for(
int n = 0 ; n < nbInd ; ++n )
369 cout <<
"Pi = " << setprecision(3) << _Pi << endl ;
371 double nbIndClass = _Pi * nbIndTemp ;
372 cout <<
"'nb d'individus appartenant' a la classe = " << nbIndClass << endl ;
380 int nullRniNb = 0, nearNullRni = 0 ;
381 for(
int n = 0 ; n < nbInd ; ++n ){
385 if( _Rn[n] < 10e-30 )
387 for(
int t = 0 ; t < nbFrame ; ++t )
388 sumRnTn[t] += _Rn[n] * indivMatrix(n, t) ;
391 cout <<
"Nombre de RNi nulls : " << nullRniNb <<
"et de Rni presque nulls" << nearNullRni << endl ;
392 for(
int t = 0 ; t < nbFrame ; ++t )
393 _mean[t] = sumRnTn[t] / sumRn ;
408 for(
int n = 0 ; n < nbInd ; ++n )
410 for(
int t = 0 ; t < nbFrame ; ++t )
411 centeredIndiv(t) = indivMatrix(n, t) - _mean[t] ;
413 Si(x, y) += _Rn[n] * centeredIndiv(x) * centeredIndiv(y) ;
416 Si(x, y) /= _Pi * nbIndTemp ;
423 if( explicitComputing ){
430 svd.
sort( Si, eigenValues ) ;
436 for(
int i = _significantNumberOfVp ; i < nbFrame ; ++i )
437 _sigma2 += eigenValues[i] ;
438 _sigma2 /= (double)( nbFrame - _significantNumberOfVp ) ;
439 cout <<
"sigma2 = " << _sigma2 <<
" (explicite)" <<
" - " ;
442 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
443 for(
int t = 0 ; t < nbFrame ; ++t )
444 selectedEigenVectors(t, i) = Si( t, i ) ;
449 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
450 tempWi(i, i) = sqrt( eigenValues[i] - _sigma2 ) ;
451 _Wi = selectedEigenVectors.cross( tempWi ) ;
455 for(
int i = 0 ; i < nbFrame ; ++i )
456 Ci(i, i) += _sigma2 ;
459 double s = 0.000001 ;
467 double ts = s * w.maximum();
469 for ( i = 0 ; i < n; i++ ){
470 if ( w( i, i ) < 0. ){
471 cout << endl <<
"pour i = " << i <<
",valeur w = " << w( i, i ) <<
"! VALEUR NEGATIVE CHANGEE!" << endl ;
472 w( i, i ) = - w( i, i ) ;
474 detCi *= w( i, i ) / _sigma2 ;
475 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
476 else w( i, i ) = 0.0f;
480 _invCi = v.cross( w.cross( u.transpose() ) ) ;
490 for(
int i = 0 ; i < newWi->
getSizeX() ; ++i )
491 newWi(i, i) += _sigma2 ;
493 double s = 0.000001 ;
499 double ts = s * w.maximum();
502 for ( i = 0 ; i < n ; i++ ){
503 if ( w( i, i ) < 0. ){
504 cout << endl <<
"pour i = " << i <<
", w = " << w( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
505 w( i, i ) = - w( i, i ) ;
507 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
508 else w( i, i ) = 0.0f;
510 newWi = v.cross( w.cross( u.transpose() ) ) ;
512 newWi = Si.cross( _Wi.cross( newWi ) ) ;
548 for(
int i = 0 ; i < temp->
getSizeX() ; ++i )
549 _sigma2 += ( Si(i, i) - temp(i, i) ) ;
551 cout <<
"sigma2 = " << _sigma2 <<
" - " ;
553 cout <<
"VALEUR NEGATIVE CHANGEE ! " << endl ;
554 _sigma2 = - _sigma2 ;
565 for(
int i = 0 ; i < Mi->
getSizeX() ; ++i )
566 Mi(i, i) += _sigma2 ;
573 ts = s * w2.maximum();
575 for( i = 0 ; i < n ; i++ ){
576 if ( w2( i, i ) < 0. ){
577 cout << endl <<
"pour i = " << i <<
", w2 = " << w2( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
578 w2( i, i ) = - w2( i, i ) ;
580 if ( w2( i, i ) > ts ) w2( i, i ) = 1.0f / w2( i, i ) ;
581 else w2( i, i ) = 0.0f;
583 _invMi = v2.cross( w2.cross( u2.transpose() ) ) ;
590 for(
int i = 0 ; i < nbFrame ; ++i )
591 Ci(i, i) += _sigma2 ;
595 for( i = 0 ; i < Ci->
getSizeX() ; i++ ){
597 cout <<
"pour i = " << i <<
", Ci = " << Ci( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
598 Ci( i, i ) = - Ci( i, i ) ;
600 for(
int j = 0 ; j < Ci->
getSizeY() ; j++ ){
601 if( Ci(i,j) != Ci(j,i) ){
603 cout <<
"pour i,j = " << i <<
"," << j <<
" Ci(i,j) = " << Ci(i,j) <<
",Ci(j,i) = " << Ci(j,i) << endl ;
607 if( sym ==
false ) cout <<
"--> Ci pas symetrique !" << endl ;
615 ts = s * w3.maximum();
617 bool saveCi = false ;
620 for( i = 0 ; i < n ; i++ ){
622 if ( w3( i, i ) < 0. ){
624 cout << endl <<
"pour i = " << i <<
", valeur w3 = " << w3( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
625 w3( i, i ) = - w3( i, i ) ;
628 detCi *= w3( i, i ) ;
630 detCi *= w3( i, i ) / _sigma2 ;
631 if ( w3( i, i ) > ts ) w3( i, i ) = 1.0f / w3( i, i );
632 else w3( i, i ) = 0.0f;
634 _invCi = v3.cross( w3.cross( u3.transpose() ) ) ;
675 cout <<
"energy = " << _energy ;
678 _normFactor = 1 / pow( detCi, 0.5 ) ;
680 _normFactor = 1 / ( pow( detCi, 0.5 ) * pow( _sigma2 / noiseRef, nbFrame / 2. ) ) ;
681 cout <<
" - detCi = " << detCi
682 <<
" - normFactor = " << _normFactor << endl ;
691 int nbInd = _individuals->getSizeX() ;
693 double sumPtn = 0., prodPtn = 0., meanPtn = 0. , varPtn = 0. ;
697 _nullPtnIndiv.clear() ;
701 for(
int n = 0 ; n < nbInd ; ++n ){
702 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
703 Pi = _elements[c].getPi() ;
704 pTni = _elements[c].getPtni() ;
705 _pTn[n] += pTni[n] * Pi ;
710 _nullPtnIndiv.push_back( n ) ;
713 _nbOfRejected = nbInd - nbIndTemp ;
715 cout <<
"nb d'individus pris en compte = " << nbIndTemp <<
" - nb d'individus rejetes = "
716 << nbInd - nbIndTemp << endl ;
719 double oldSumLnPTn = _logLikelihood ;
720 _logLikelihood = 0. ;
721 for(
int n = 0 ; n < nbInd ; ++n ){
723 _logLikelihood += log( _pTn[n] ) ;
725 _logLikelihood += -100000000. ;
727 prodPtn += _pTn[n] * _pTn[n] ;
729 meanPtn = sumPtn / nbIndTemp ;
730 varPtn = prodPtn / nbIndTemp ;
731 cout << endl <<
"CRITERIUM TO MAXIMIZE : LOG LIKELIHOOD = " << _logLikelihood <<
" and difference = "
732 << (_logLikelihood - oldSumLnPTn)/oldSumLnPTn <<
" for "
733 << nbInd - nbIndTemp <<
" rejections" << endl ;
734 cout <<
"moyenne et variance des p(tn) = " << meanPtn <<
" - " << varPtn << endl ;
736 return (_logLikelihood - oldSumLnPTn)/oldSumLnPTn ;
750 double max = 0., global_dist = 0. ;
752 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
753 Rn = _elements[c].getRn() ;
754 for(
int ind = 0 ; ind < _individuals->getSizeX() ; ++ind )
755 Rni(ind, c) = Rn[ind] ;
758 for(
int ind = 0 ; ind < _individuals->getSizeX() ; ++ind ){
759 max = Rni( ind, 0 ) ;
761 for(
int c = 1 ; c < _nbOfClasses ; ++c ){
762 if( Rni(ind, c) >
max ){
763 max = Rni( ind, c ) ;
764 results[ind] = c + 1 ;
767 mean = _elements[results[ind]-1].getMean() ;
768 invCi = _elements[results[ind]-1].getInvCi() ;
769 for(
int t = 0 ; t < _individuals->getSizeY() ; ++t )
770 centeredIndivn[t] = _individuals(ind, t) - mean[t] ;
771 dist = ( centeredIndivn.clone().transpose() ).
cross( invCi.cross( centeredIndivn ) ) ;
772 global_dist += dist(0,0) ;
789 vector< list< int > > tempList( _nbOfClasses ) ;
802 for(
int ind = 0 ; ind < _individuals->getSizeX() ; ++ind ){
805 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
806 if( Rni(ind, c) >
max ){
807 max = Rni( ind, c ) ;
808 results[ind] = c + 1 ;
811 if( results[ind] > 0 )
812 _finalClasses[results[ind] - 1].push_back( ind ) ;
819 if( nbOfIterations% 30 == 0 || theEnd ){
831 for(
short c = 0 ; c < _nbOfClasses ; ++c ){
832 list<int>::const_iterator iter( _finalClasses[c].begin() ),
833 last( _finalClasses[c].end() ) ;
834 while( iter != last ){
835 theVoxel = _indPosVector[*iter] ;
836 resultImage( theVoxel ) = c + 1 ;
837 rniImage(theVoxel[0], theVoxel[1], theVoxel[2], c) = Rni( *iter, c ) ;
842 list<int>::const_iterator it( _nullPtnIndiv.begin() ),
843 lt( _nullPtnIndiv.end() ) ;
845 theVoxel = _indPosVector[*it] ;
846 rejected( theVoxel ) = 1 ;
852 writer.
write( resultImage ) ;
855 writer3.
write( rejected ) ;
858 writer2.
write( rniImage ) ;
861 cout << endl <<
"Nb d'individus a l'iteration " << nbOfIterations <<
" ds chaque classe = " ;
862 for(
int i = 0 ; i < _nbOfClasses ; ++i )
863 cout << _finalClasses[i].size() <<
" " ;
872 string fformat = f.
format() ;
873 if( !writerMatrix.
write( _individuals,
false, &fformat ) )
880 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
882 list<int>::const_iterator iter( _finalClasses[c].begin() ),
883 last( _finalClasses[c].end() ) ;
884 while( iter != last ){
888 item.
value() = *iter ;
896 writerClasses.
write( bClasses,
true, &fformat ) ;
900 vector<double> classesNorm( _nbOfClasses, 0. ) ;
901 multimap<double, Point2d> fusion ;
902 multimap<double, int>
split ;
904 for(
int i = 0 ; i < _nbOfClasses ; ++i )
905 for(
int n = 0 ; n < _individuals->getSizeX() ; ++n ) {
906 for(
int j = i ; j < _nbOfClasses ; ++j )
907 classCorrelationMatrix(i, j) += Rni( n, i ) * Rni(n, j ) ;
909 classesNorm[i] += Rni( n, i ) * Rni( n, i ) ;
912 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
913 classesNorm[i] = sqrt( classesNorm[i] ) ;
914 double meanNorm = 0. ;
915 for(
int t = 0 ; t < _elements[i].getMean()->getSizeX() ; ++t )
916 meanNorm += _elements[i].getMean()[t] ;
918 split.insert( pair<double, int>( _elements[i].getSigma2() / meanNorm, i) ) ;
920 for(
int i = 0 ; i < _nbOfClasses ; ++i )
921 for(
int j = i ; j < _nbOfClasses ; ++j ){
922 if ( classesNorm[i] * classesNorm[j] > 0 )
923 classCorrelationMatrix(i, j) /= classesNorm[i] * classesNorm[j] ;
925 classCorrelationMatrix(i, j) = 0. ;
928 classCorrelationMatrix(j, i) = classCorrelationMatrix(i, j) ;
930 fusion.insert( pair<double, Point2d> ( classCorrelationMatrix(i, j),
Point2d(i, j) ) ) ;
933 cout <<
"Classes to fusion : " << endl ;
936 for( multimap<double, Point2d>::reverse_iterator rit = fusion.rbegin() ; rit != fusion.rend() && count < 5 ; ++rit, ++count )
937 cout << (rit->second)[0] <<
" avec " << (rit->second)[1] <<
" : " << rit->first << endl ;
939 cout <<
"Classes to separe : " << endl ;
941 for( multimap<double, int>::reverse_iterator rit =
split.rbegin() ; rit !=
split.rend() && count < 5 ; ++rit, ++count )
942 cout << rit->second <<
" : " << rit->first << endl ;
956 multimap< float, Point2d > fScore ;
957 multimap< float, Point2d > meanFScore ;
959 for(
int fromC = 0 ; fromC < _nbOfClasses ; ++fromC ){
960 for(
int toC = 0 ; toC < _nbOfClasses ; ++toC ){
963 list<int>::const_iterator iter( _finalClasses[fromC].begin() ),
964 last( _finalClasses[fromC].end() ) ;
965 while( iter != last ){
966 sumDist += _distToClasses( *iter, toC ) ;
970 meanDistMatrix( fromC, toC ) = sumDist / count ;
977 for(
int i = 0 ; i < _nbOfClasses ; ++i )
978 for(
int j = 0 ; j < _nbOfClasses ; ++j )
979 fScore.insert( pair< float, Point2d >( meanDistMatrix(i, j),
Point2d(i, j) ) ) ;
982 for(
int i = 0 ; i < _nbOfClasses ; ++i )
983 for(
int j = i+1 ; j < _nbOfClasses ; ++j )
984 meanFScore.insert( pair< float, Point2d >( ( meanDistMatrix(i, j) + meanDistMatrix(j, i) )/2.,
987 cout << endl <<
"elements de la map des scores = " << endl;
988 multimap< float, Point2d >::iterator iter( fScore.begin() ), last( fScore.end() ) ;
989 while( iter != last ){
990 if( (*iter).first > 1.5 && (*iter).first < 100. ){
991 cout <<
" [" << (*iter).first <<
", " << (*iter).second <<
"]" ;
992 cout <<
" sigmas = " << _sigma2Matrix[ (*iter).second[0] ] <<
" et "
993 << _sigma2Matrix[ (*iter).second[1] ] << endl;
998 cout << endl <<
"elements de la map des scores moyens 2 a 2 = " << endl;
999 multimap< float, Point2d >::iterator iterM( meanFScore.begin() ), lastM( meanFScore.end() ) ;
1000 while( iterM != lastM ){
1001 if( exp( -0.5 * (*iterM).first ) > 0.01 ){
1002 cout <<
" [" << (*iterM).first <<
", " << (*iterM).second <<
"]" ;
1003 cout <<
" exp(-score/2) = " << exp( -0.5 * (*iterM).first ) << endl ;
1015 int nbOfClasses,
int significantNumberOfVp,
1016 int maxNbOfIterations,
1018 const std::vector< std::list <int> >& initialClasses,
1019 const std::string & fileOut,
int runNb,
int iterationToUseOnlyCorrelatedIndiv ) :
1020 _nbOfClasses( nbOfClasses ), _valid( true ), _significantNumberOfEigenValues( significantNumberOfVp ), _maxNbOfIterations( maxNbOfIterations ), _individuals( individuals ), _indPosVector( indPosVector ), _fileOut( fileOut ), _runNb( runNb ), _itToUseOnlyCorrelatedIndiv(iterationToUseOnlyCorrelatedIndiv)
1022 cout <<
"Entering MixtureOfPPCA Constructor" << endl ;
1024 double Pi_init = 1. / (double)_nbOfClasses ;
1025 _elements.reserve( _nbOfClasses ) ;
1029 _sigma2init = vector<double>( _nbOfClasses ) ;
1032 std::vector< std::list<int> > initClasses( initialClasses ) ;
1034 if( initClasses.size() == 0 ){
1035 initClasses = std::vector< std::list<int> >( _nbOfClasses ) ;
1036 for(
int i = 0 ; i < _individuals->
getSizeX() ; ++i ){
1038 int c = int( aux * _nbOfClasses ) ;
1039 initClasses[c].push_back( i ) ;
1041 std::cout << std::endl ;
1044 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1045 std::cout <<
"Class " << c <<
" : ";
1046 for( std::list<int>::iterator iter = initClasses[c].begin() ; iter < initClasses[c].end() ; ++iter )
1047 std::cout <<
"\t" << (*iter) ;
1048 std::cout << std::endl ;
1051 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1052 if(
int( initClasses[c].size() ) > _individuals->
getSizeY() ) {
1054 cout << endl <<
"Initialization class " << c <<
" ..." ;
1068 el.
init( initClasses[c], Pi_init, individuals, _noiseRef ) ;
1070 cout <<
"Noise Ref = " << setprecision(3) << _noiseRef << endl ;
1072 el.
init( initClasses[c], Pi_init, individuals, _noiseRef ) ;
1073 cout <<
"Class " << c <<
" initialized !" << endl ;
1074 _elements.push_back( el ) ;
1077 cerr <<
"Not enough data for element " << c << endl ;
1088 int nbOfIterations = 0 ;
1092 _finalClasses = vector< list< int > >( _nbOfClasses ) ;
1094 double sumOfEnergies, criteria ;
1105 int oldNbOfRejected = 0 ;
1106 bool notYetTheEnd = true ;
1109 for(
int i = 0 ; i < _nbOfClasses ; ++i )
1110 _finalClasses[i].clear() ;
1111 cout << endl <<
"Iteration n�: " << nbOfIterations ;
1114 cout << endl <<
"Calcul des p(tn|i) pour chaque classe... " << endl ;
1115 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
1116 cerr <<
"Class " << i <<
": " ;
1117 if( _elements[i].isValid() )
1118 _elements[i].newStep1( _individuals, 0 ) ;
1121 oldNbOfRejected = _nbOfRejected ;
1124 cout << endl <<
"Calcul des p(tn)..." ;
1125 imageOfLogLikelihoodProgression(nbOfIterations) = pTnComputation() ;
1126 imageOfLogLikelihood(nbOfIterations) = _logLikelihood ;
1129 cout << endl <<
"Calcul des autres parametres pour chaque classe..." ;
1133 double oldSumOfEnergies = sumOfEnergies ;
1134 sumOfEnergies = 0. ;
1135 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
1136 cout <<
"Class " << i <<
": " ;
1137 if( _elements[i].isValid() ){
1138 _elements[i].newStep2( _pTn, _individuals, 1000000. ) ;
1141 sumOfEnergies += _elements[i].getEnergy() ;
1142 criteria += _elements[i].getSumDiff2Rni() ;
1145 for(
int t = 0 ; t < _individuals->getSizeY() ; ++t )
1146 imageOfMean( i, nbOfIterations, t ) = ( _elements[i].getMean() )[t] ;
1147 imageOfSigma2( i, nbOfIterations ) = _elements[i].getSigma2() ;
1148 imageOfPi( i, nbOfIterations ) = _elements[i].getPi() ;
1149 imageOfRniDiff( i, nbOfIterations ) = _elements[i].getSumDiff2Rni() ;
1153 cout << endl <<
"Critere d'arret = " << criteria <<
" or " << (oldSumOfEnergies - sumOfEnergies)/_elements.size() << endl ;
1154 imageOfEnergy( nbOfIterations ) = sumOfEnergies ;
1171 notYetTheEnd = (nbOfIterations < _maxNbOfIterations) &&
1172 ( (nbOfIterations < 10 ) ||
1173 !( (oldNbOfRejected == _nbOfRejected) && (fabs( imageOfLogLikelihoodProgression(nbOfIterations) ) < 0.0000001) ) ) ;
1176 res = classesVisualisation( nbOfIterations, _fileOut, !notYetTheEnd ) ;
1179 }
while( notYetTheEnd ) ;
1205 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1206 Rn = _elements[c].getRn() ;
1207 for(
int ind = 0 ; ind < _individuals->getSizeX() ; ++ind )
1208 RnAll(ind, c) = Rn[ind] ;
#define ForEach2d(thing, x, y)
const AimsVector< short, 3 > & location() const
void push_back(const AimsBucketItem< T > &item)
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)
std::string format() const
carto::VolumeRef< double > getRni()
bool classesVisualisation(int nbOfIterations, const std::string &fileOut, bool theEnd)
bool distMatrixComputation()
MixtureOfPPCA(int nbOfClasses, int significantNumberOfVp, int maxNbOfIterations, const carto::rc_ptr< carto::Volume< T > > &individuals, const std::vector< Point3d > indPosVector, const std::vector< std::list< int > > &initialClasses, const std::string &fileOut, int runNb, int iterationToUseOnlyCorrelatedIndiv=false)
void init(const std::list< int > &selectedIndividuals, double initialPi, const carto::rc_ptr< carto::Volume< T > > &individuals, double noiseRef=1.)
bool newStep1(const carto::rc_ptr< carto::Volume< T > > &indivMatrix, bool useOnlyCorrIndiv)
double newStep2(carto::rc_ptr< carto::Volume< double > > pTn, const carto::rc_ptr< carto::Volume< T > > &indivMatrix, double noiseRef=1.)
PpcaAnalyserElement(int significantNumberOfVp, bool useOnlyCorrIndiv=false)
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
void setVoxelSize(float vx, float vy=1., float vz=1., float vt=1.)
float max(float x, float y)
Point3df cross(const Point3df &A, const Point3df &B)
std::vector< std::string > split(const std::string &text, const std::string &sep)
std::string toString(const T &object)
double UniformRandom()
Uniform distribution between 0.0 and 1.0.