46 _significantEV(significantEV), _computed(false), _dataScaleFactor(1.),
47 _probaScaleFactor(1.), _PIj(PIj), _detVarCov(1.),
48 _normFactor(1.), _lnAddFactor(0.)
56 const std::list< Point3d >& selectedPoints,
60 data->getSizeT(), 1, 1,
61 carto::AllocatorContext::fast() );
63 std::list< Point3d >::const_iterator iter( selectedPoints.begin() ), last( selectedPoints.end() ) ;
67 while( iter != last ){
69 for(
int t = 0 ; t < data->getSizeT() ; ++t )
70 individuals( i, t ) = T(data->at( (*iter)[0], (*iter)[1], (*iter)[2], t ) ) ;
85 int nbFrame = individuals->getSizeY() ;
88 individuals->getSizeX(), individuals->getSizeY(), 1, 1,
89 carto::AllocatorContext::fast() );
91 carto::AllocatorContext::fast() );
94 for(
int ind = 0 ; ind < individuals->getSizeX() ; ++ind )
95 for(
int t = 0 ; t < individuals->getSizeY() ; ++t ){
96 val = individuals->
at( ind, t ) ;
101 double meanMean = 0. ;
104 for(
int t = 0 ; t < nbFrame ; ++t )
107 for(
int ind = 0 ; ind < centeredIndivMatrix->
getSizeX() ; ++ind )
109 centeredIndivMatrix( ind, t ) = individuals->
at( ind, t ) -
_mean(t) ;
110 meanMean += centeredIndivMatrix( ind, t ) ;
118 for( y1=0; y1<dy1; ++y1 )
119 for( x1=0; x1<dx1; ++x1 )
121 for(
int k=0; k < centeredIndivMatrix->
getSizeX() ;++k)
122 matVarCov(x1, y1) += centeredIndivMatrix(k, x1) * centeredIndivMatrix(k, y1);
123 matVarCov(x1, y1) /= centeredIndivMatrix->
getSizeX() - 1 ;
132 carto::AllocatorContext::fast() );
139 carto::AllocatorContext::fast() ) ;
152 std::cout <<
"signif EV = " <<
_significantEV <<
" sig2 = " << sig2 << std::endl ;
154 double lnSig2 = log(sig2) ;
155 double sqrtSig2 = sqrt(sig2) ;
157 double lnDetVarCov = 0 ;
161 lnDetVarCov += 0.5 * log( w( i, i ) ) ;
163 invW( i, i ) = 1.0f / w( i, i );
167 lnDetVarCov += 0.5 * lnSig2 ;
170 invW( i, i ) = 1.0f / sig2 ;
197 for( y1=0; y1<dy1; ++y1 )
198 for( x1=0; x1<dx1; ++x1 )
207 std::cerr <<
"Bad var cov matrix !" << std::endl ;
208 throw std::runtime_error(
"Bad var cov matrix !") ;
217 std::cerr <<
"You must doIt first, parameter not yet computed !"
220 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
228 for(
int i = 0 ; i < x->getSizeX() ; ++i ){
230 for(
int j = 0 ; j < x->getSizeX() ; ++j )
244 std::cerr <<
"You must doIt first, parameter not yet computed !"
246 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
251 for(
int i = 0 ; i < x->getSizeX() ; ++i ){
254 for(
int j = 0 ; j < x->getSizeX() ; ++j ){
271 std::cerr <<
"You must doIt first, parameter not yet computed !"
273 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
276 for(
int i = 0 ; i < x->getSizeX() ; ++i ){
278 for(
int j = 0 ; j < x->getSizeX() ; ++j )
311 std::cerr <<
"You must doIt first, parameter not yet computed !"
313 throw std::runtime_error(
"You must doIt first, parameter not yet computed !") ;
321 const std::vector< std::list< Point3d > >& classes,
322 int significantEV,
const std::vector<double>& PIj )
323 : _significantEV(significantEV <= 0 ? data->getSizeT() : significantEV),
324 _classes(classes), _data(data), _PIj(PIj)
326 if( PIj.size() != 0 )
327 if( PIj.size() == classes.size() )
330 std::cerr <<
"Inconsistant data : number of classes and number of classes prior probabilities are different !" << std::endl ;
332 throw std::runtime_error(
"Inconsistant data : number of classes and number of classes prior probabilities are different !") ;
335 _PIj.reserve( _classes.size() ) ;
336 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
337 _PIj.push_back( 1./_classes.size() ) ;
349 _discrElements.reserve( _classes.size() ) ;
350 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
352 el.
doIt( _classes[c], _data ) ;
355 std::cout <<
"Class " << c <<
" done !" << std::endl ;
357 _discrElements.push_back( el ) ;
359 std::cout <<
"Discriminant analysis initization completed" << std::endl ;
367 std::vector<double> res( _classes.size() ) ;
368 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
369 res[c] = _discrElements[c].posteriorProbability( x, pX ) ;
378 std::vector<double> res( _classes.size() ) ;
380 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
381 res[c] = _discrElements[c].posteriorProbability( x, 1. ) ;
385 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
397 double lnPMax = _discrElements[0].lnPosteriorProbability( x ) ;
399 for(
unsigned int c = 0 ; c < _classes.size() ; ++c )
401 lnP = _discrElements[c].lnPosteriorProbability( x ) ;
420 if( dynamicImage->getSizeT() != _data->getSizeT() )
422 if( segmented->getSizeX() != _data->getSizeX() && segmented->getSizeY() != _data->getSizeY()
423 && segmented->getSizeZ() != _data->getSizeZ() )
426 segmented->setVoxelSize( _data->getVoxelSize() );
430 carto::AllocatorContext::fast() ) ;
432 int dx = dynamicImage->getSizeX(), dy = dynamicImage->getSizeY(),
433 dz = dynamicImage->getSizeZ();
435 for( z=0; z<dz; ++z )
436 for( y=0; y<dy; ++y )
437 for( x=0; x<dx; ++x )
439 if(
mask->at(x, y, z) )
441 std::cout <<
"x : " << x <<
", y : " << y <<
", z : " << z << std::endl ;
442 for(
int t = 0 ; t < _data->getSizeT() ; ++t )
443 indiv(t) = dynamicImage->
at(x, y, z, t ) ;
444 segmented->at(x, y, z) = affectedTo( indiv ) ;
448 std::cout <<
"Discriminant analysis classification completed" << std::endl ;
462 = ( indivPriorProbabilities->getSizeX() == 1
463 && indivPriorProbabilities->getSizeY() == 1
464 && indivPriorProbabilities->getSizeZ() == 1 );
471 int dx =
mask->getSizeX(), dy =
mask->getSizeY(), dz =
mask->getSizeZ();
473 for( z=0; z<dz; ++z )
474 for( y=0; y<dy; ++y )
475 for( x=0; x<dx; ++x )
476 if(
mask->at( x, y, z ) )
480 if( dynamicImage->getSizeT() != _data->getSizeT() )
482 if( fuzzySegmented->getSizeX() != _data->getSizeX()
483 && fuzzySegmented->getSizeY() != _data->getSizeY()
484 && fuzzySegmented->getSizeZ() != _data->getSizeZ() &&
485 fuzzySegmented->getSizeT() != int(_classes.size() ) )
488 _data->getSizeZ(), _classes.size() ) ;
489 fuzzySegmented->setVoxelSize( _data->getVoxelSize() ) ;
492 std::vector<double> probabilityRepartition ;
494 carto::AllocatorContext::fast() ) ;
496 int dx = dynamicImage->getSizeX(), dy = dynamicImage->getSizeY(),
497 dz = dynamicImage->getSizeZ();
499 for( z=0; z<dz; ++z )
500 for( y=0; y<dy; ++y )
501 for( x=0; x<dx; ++x )
503 if(
mask->at(x, y, z) )
506 for(
int t = 0 ; t < _data->getSizeT() ; ++t )
507 indiv(t) = dynamicImage->
at(x, y, z, t ) ;
509 probabilityRepartition = posteriorProbabilities( indiv, 1. ) ;
511 for(
int c = 0 ; c < fuzzySegmented->getSizeT() ; ++c )
512 fuzzySegmented->at(x, y, z, c) = probabilityRepartition[c] ;
516 std::cout <<
"Discriminant analysis : fuzzy classification completed"
carto::VolumeRef< T > doit(carto::VolumeRef< T > &, carto::VolumeRef< T > *v=NULL)
Singular Value Decomposition.
const carto::VolumeRef< double > & mean() const
double posteriorProbability(const carto::rc_ptr< carto::Volume< double > > &individual, double pX) const
double lnPosteriorProbability(const carto::rc_ptr< carto::Volume< double > > &individual) const
carto::VolumeRef< double > _mean
void doIt(const carto::rc_ptr< carto::Volume< T > > &individuals)
DiscriminantAnalysisElement(int significantEV=-1, double PIj=1.)
std::vector< Point3d > _indivPosition
double distance(const carto::rc_ptr< carto::Volume< double > > &x) const
carto::VolumeRef< double > _invVarCov
bool classification(const carto::rc_ptr< carto::Volume< T > > &dynamicImage, const carto::rc_ptr< carto::Volume< byte > > &mask, carto::rc_ptr< carto::Volume< short > > &segmented)
DiscriminantAnalysis(const carto::rc_ptr< carto::Volume< T > > &data, const std::vector< std::list< Point3d > > &classes, int significantEV=-1, const std::vector< double > &PIj=std::vector< double >())
int affectedTo(const carto::rc_ptr< carto::Volume< double > > &x)
std::vector< double > andersonScores(const carto::rc_ptr< carto::Volume< double > > &x)
std::vector< double > posteriorProbabilities(const carto::rc_ptr< carto::Volume< double > > &x, double px)
bool fuzzyClassification(const carto::rc_ptr< carto::Volume< T > > &dynamicImage, const carto::rc_ptr< carto::Volume< byte > > &mask, carto::rc_ptr< carto::Volume< float > > &fuzzySegmented, const carto::rc_ptr< carto::Volume< double > > &indivPriorProbabilities=carto::rc_ptr< carto::Volume< double > >())
VolumeRef< T > deepcopy() const
const T & at(long x, long y=0, long z=0, long t=0) const
float max(float x, float y)
BucketMap< Void > * mask(const BucketMap< Void > &src, const BucketMap< Void > &m, bool intersect=true)
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
VolumeRef< T > matrix_product(const Volume< T > &v1, const Volume< T > &v2)
VolumeRef< T > transpose(const Volume< T > &v)