47 _significantEV(significantEV), _computed(false), _dataScaleFactor(1.),
48 _probaScaleFactor(1.), _PIj(PIj), _detVarCov(1.),
49 _normFactor(1.), _lnAddFactor(0.)
61 std::list< Point3d >::const_iterator iter( selectedPoints.begin() ), last( selectedPoints.end() ) ;
65 while( iter != last ){
67 for(
int t = 0 ; t < data.
dimT() ; ++t )
68 individuals( i, t ) = T(data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ) ;
71 mean +=
Point3df( (*iter)[0], (*iter)[1], (*iter)[2] ) ;
82 int nbFrame = individuals.
dimY() ;
88 for(
int ind = 0 ; ind < individuals.
dimX() ; ++ind )
89 for(
int t = 0 ; t < individuals.
dimY() ; ++t ){
90 val = individuals( ind, t ) ;
95 double meanMean = 0. ;
98 for(
int t = 0 ; t < nbFrame ; ++t ){
99 _mean[t] = _mean[t] / centeredIndivMatrix.
dimX() ;
100 for(
int ind = 0 ; ind < centeredIndivMatrix.dimX() ; ++ind ){
101 centeredIndivMatrix( ind, t ) = individuals( ind, t ) - _mean[t] ;
102 meanMean += centeredIndivMatrix( ind, t ) ;
111 for(
int k=0; k < centeredIndivMatrix.dimX() ;++k)
112 matVarCov(x1, y1) += centeredIndivMatrix(k, x1) * centeredIndivMatrix(k, y1);
113 matVarCov(x1, y1) /= centeredIndivMatrix.dimX() - 1 ;
139 std::cout <<
"signif EV = " <<
_significantEV <<
" sig2 = " << sig2 << std::endl ;
141 double lnSig2 = log(sig2) ;
142 double sqrtSig2 = sqrt(sig2) ;
144 double lnDetVarCov = 0 ;
148 lnDetVarCov += 0.5 * log( w( i, i ) ) ;
150 invW( i, i ) = 1.0f / w( i, i );
152 for ( i = _significantEV ; i<n; i++ )
154 lnDetVarCov += 0.5 * lnSig2 ;
157 invW( i, i ) = 1.0f / sig2 ;
187 std::cerr <<
"Bad var cov matrix !" << std::endl ;
188 throw std::runtime_error(
"Bad var cov matrix !") ;
197 std::cerr <<
"You must doIt first, parameter not yet computed !" 200 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
208 for(
int i = 0 ; i < x.
dimX() ; ++i ){
210 for(
int j = 0 ; j < x.
dimX() ; ++j )
213 distance += ( x(i) -
_mean(i) ) * sum ;
223 std::cerr <<
"You must doIt first, parameter not yet computed !" 225 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
230 for(
int i = 0 ; i < x.
dimX() ; ++i ){
233 for(
int j = 0 ; j < x.
dimX() ; ++j ){
238 distance += ( x(i) -
_mean(i) ) * sum ;
249 std::cerr <<
"You must doIt first, parameter not yet computed !" 251 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
254 for(
int i = 0 ; i < x.
dimX() ; ++i ){
256 for(
int j = 0 ; j < x.
dimX() ; ++j )
259 distance += ( x(i) -
_mean(i) ) * sum ;
289 std::cerr <<
"You must doIt first, parameter not yet computed !" 291 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
299 const std::vector< std::list< Point3d > >& classes,
300 int significantEV,
const std::vector<double>& PIj )
301 :
_significantEV(significantEV <= 0 ? data.dimT() : significantEV),
302 _classes(classes), _data(data),
_PIj(PIj)
304 if( PIj.size() != 0 )
305 if( PIj.size() == classes.size() )
308 std::cerr <<
"Inconsistant data : number of classes and number of classes prior probabilities are different !" << std::endl ;
310 throw std::runtime_error(
"Inconsistant data : number of classes and number of classes prior probabilities are different !") ;
313 _PIj.reserve( _classes.size() ) ;
314 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
315 _PIj.push_back( 1./_classes.size() ) ;
327 _discrElements.reserve( _classes.size() ) ;
328 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
330 el.
doIt( _classes[c], _data ) ;
333 std::cout <<
"Class " << c <<
" done !" << std::endl ;
335 _discrElements.push_back( el ) ;
337 std::cout <<
"Discriminant analysis initization completed" << std::endl ;
344 std::vector<double> res( _classes.size() ) ;
345 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
346 res[c] = _discrElements[c].posteriorProbability( x, pX ) ;
354 std::vector<double> res( _classes.size() ) ;
356 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
357 res[c] = _discrElements[c].posteriorProbability( x, 1. ) ;
361 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
372 double lnPMax = _discrElements[0].lnPosteriorProbability( x ) ;
374 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
375 lnP = _discrElements[c].lnPosteriorProbability( x ) ;
393 if( dynamicImage.
dimT() != _data.dimT() )
395 if( segmented.
dimX() != _data.dimX() && segmented.
dimY() != _data.dimY()
396 && segmented.
dimZ() != _data.dimZ() )
399 segmented.
setSizeXYZT( _data.sizeX(), _data.sizeY(),
400 _data.sizeZ(), 1.0 ) ;
405 if(
mask(x, y, z) ) {
406 std::cout <<
"x : " << x <<
", y : " << y <<
", z : " << z << std::endl ;
407 for(
int t = 0 ; t < _data.dimT() ; ++t )
408 indiv(t) = dynamicImage(x, y, z, t ) ;
413 std::cout <<
"Discriminant analysis classification completed" << std::endl ;
424 indivPriorProbabilities )
427 = ( indivPriorProbabilities.
dimX( ) == 1
428 && indivPriorProbabilities.
dimY( ) == 1
429 && indivPriorProbabilities.
dimZ( ) == 1 );
437 if(
mask( x, y, z ) )
441 if( dynamicImage.
dimT() != _data.dimT() )
443 if( fuzzySegmented.
dimX() != _data.dimX()
444 && fuzzySegmented.
dimY() != _data.dimY()
445 && fuzzySegmented.
dimZ() != _data.dimZ() &&
446 fuzzySegmented.
dimT() != int(_classes.size() ) )
449 _data.dimZ(), _classes.size() ) ;
450 fuzzySegmented.
setSizeXYZT( _data.sizeX(), _data.sizeY(), _data.sizeZ(),
454 std::vector<double> probabilityRepartition ;
457 if(
mask(x, y, z) ) {
459 for(
int t = 0 ; t < _data.dimT() ; ++t )
460 indiv(t) = dynamicImage(x, y, z, t ) ;
464 for(
int c = 0 ; c < fuzzySegmented.
dimT() ; ++c )
465 fuzzySegmented(x, y, z, c) = probabilityRepartition[c] ;
469 std::cout <<
"Discriminant analysis : fuzzy classification completed" float max(float x, float y)
BucketMap< Void > * mask(const BucketMap< Void > &src, const BucketMap< Void > &m, bool intersect=true)
std::vector< double > andersonScores(const AimsData< double > &x)
void doIt(const AimsData< T > &individuals)
#define ForEach3d(thing, x, y, z)
#define ForEach2d(thing, x, y)
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
std::vector< Point3d > _indivPosition
Point3df cross(const Point3df &A, const Point3df &B)
bool fuzzyClassification(const AimsData< T > &dynamicImage, const AimsData< byte > &mask, AimsData< float > &fuzzySegmented, const AimsData< double > &indivPriorProbabilities=AimsData< double >())
std::vector< double > posteriorProbabilities(const AimsData< double > &x, double px)
AimsData< T > clone() const
double distance(const AimsData< double > &x) const
int affectedTo(const AimsData< double > &x)
DiscriminantAnalysisElement(int significantEV=-1, double PIj=1.)
double lnPosteriorProbability(const AimsData< double > &individual) const
DiscriminantAnalysis(const AimsData< T > &data, const std::vector< std::list< Point3d > > &classes, int significantEV=-1, const std::vector< double > &PIj=std::vector< double >())
bool classification(const AimsData< T > &dynamicImage, const AimsData< byte > &mask, AimsData< short > &segmented)
AimsFastAllocationData< double > _invVarCov
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
double posteriorProbability(const AimsData< double > &individual, double pX) const
const AimsData< double > & mean() const
AimsFastAllocationData< double > _mean