bioprocessing  5.0.5
cah_d.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #include <iostream>
12 #include <math.h>
13 #include <aims/math/pca_d.h>
14 #include <aims/math/ppca.h>
15 #include <aims/io/writer.h>
16 #include <aims/connectivity/connectivity.h>
17 #include <aims/classification/individuals_d.h>
19 
20 template <typename T>
21 BaryElement<T>::BaryElement( int card, const aims::Individuals<float>& bary,
22  const std::set<int>& constitutedFrom, int number ) :
23  Element<T>( number, card, constitutedFrom ), _bary(bary)
24 {}
25 
26 template <typename T>
27 BaryElement<T>::BaryElement( const std::vector<std::list< Point3d > >& classes,
28  const AimsData<T>& data, const std::set<int>& constitutedFrom,
29  int number ) :
30  Element<T>( number, 0, constitutedFrom )
31 {
32 
33  this->_card = 0 ;
34  std::set<int>::const_iterator aggIt( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
35 
36  while( aggIt != aggLast ) {
37  this->_card += classes[*aggIt].size() ;
38  ++aggIt ;
39  }
40 
41  computeBary( classes, data ) ;
42 }
43 
44 template <typename T>
45 void
46 BaryElement<T>::computeBary( const std::vector<std::list< Point3d > >& classes,
47  const AimsData<T>& data )
48 {
49 
50  int indivSize = data.dimT() ;
51 /* _bary.position() = Point3df(0., 0., 0.) ; */
52 /* _bary.value() = std::vector<float>( indivSize ) ; */
53 
54  Point3df position(0., 0., 0.) ;
55  std::vector<float> value( indivSize ) ;
56 
57  std::set<int>::iterator constFromIter(this->_constitutedFrom.begin()), constLast(this->_constitutedFrom.end()) ;
58 
59  int nbInd = 0 ;
60  while( constFromIter != constLast ){
61  std::list<Point3d>::const_iterator iter(classes[*constFromIter].begin()), last(classes[*constFromIter].end()) ;
62  while( iter != last ){
63  position += Point3df( (*iter)[0], (*iter)[1], (*iter)[2] ) ;
64  for( int j = 0 ; j < indivSize ; ++j )
65  value[j] += data((*iter)[0], (*iter)[1], (*iter)[2], j) ;
66  ++iter ;
67  ++nbInd ;
68  }
69  ++constFromIter ;
70  }
71  if( nbInd){
72  float invNbInd = 1. / nbInd ;
73  for( int j = 0 ; j < indivSize ; ++j )
74  value[j] *= invNbInd ;
75  position = position * invNbInd ;
76  }
77  _bary = aims::Individuals<float>(position, value) ;
78 }
79 
80 template <typename T>
81 float
83  const std::vector< std::list< Point3d > >&,
84  const AimsData<T>&, const AimsData<float>& )
85 {
86  BaryElement<T> * otherB = dynamic_cast<BaryElement<T>*>(other) ;
87  unsigned int size = baryValue().size() ;
88  if( otherB->baryValue().size() != size )
89  throw std::runtime_error("Inconsistant data !") ;
90  float d = 0. ;
91  for( unsigned int i = 0 ; i < size ; ++i )
92  d += (_bary.value()[i] - otherB->baryValue()[i])*(_bary.value()[i] - otherB->baryValue()[i]);
93  d *= this->_card * otherB->card() /( this->_card + otherB->card() ) ;
94 
95  return d ;
96 }
97 
98 template <typename T>
99 Element<T> *
101  const std::vector< std::list< Point3d > >&,
102  const AimsData<T>&, const AimsData<float>&, int newNb )
103 {
104  BaryElement<T> * otherB = dynamic_cast<BaryElement<T>*>(other) ;
105  unsigned int size = baryValue().size() ;
106  if( otherB->baryValue().size() != size )
107  throw std::runtime_error("Inconsistant data !") ;
108 
109  // Agglomeration
110  std::set<int> constFrom( this->constitutedFrom() ) ;
111  std::set<int>::const_iterator cIt(otherB->constitutedFrom().begin() ),
112  cLast(otherB->constitutedFrom().end() ) ;
113  while( cIt != cLast ){
114  constFrom.insert( *cIt ) ;
115  ++cIt ;
116  }
117 
118  // Calcul du nouveau barycentre des 2 classes fusionnees
119  Point3df pos ;
120  std::vector<float> val( size ) ;
121  int newCard = this->_card + otherB->card() ;
122  float invNewCard = 1./newCard ;
123  for(unsigned int i=0;i<_bary.value().size();i++)
124  val[i] = (this->_card*_bary.value()[i] +
125  otherB->card()*otherB->baryValue()[i]) * invNewCard ;
126  pos = (float(this->_card)*_bary.position() +
127  float(otherB->card())*otherB->baryPosition()) * invNewCard ;
128 
129  aims::Individuals<float> newBary( pos, val );
130 
131  // Instanciation du nouvel element
132  Element<T> *nouveau = new BaryElement<T>( newCard, newBary, constFrom, newNb );
133  return nouveau ;
134 }
135 
136 template <typename T>
137 PcaElement<T>::PcaElement( const std::vector<std::list< Point3d > >& classes,
138  const AimsData<T>& data, int numberOfSignificantVps,
139  const std::set<int>& constitutedFrom, int number ) :
140  Element<T>( number, 0, constitutedFrom ), _meanReconstructionError(-1.), _varReconstructionError(-1.),
141 _unreconstructedInertia(-1.), _numberOfSignificantVps(numberOfSignificantVps)
142 {
143  this->_card = 0 ;
144  std::set<int>::const_iterator aggIt( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
145 
146  while( aggIt != aggLast ) {
147  this->_card += classes[*aggIt].size() ;
148  ++aggIt ;
149  }
150 
151  computeReconstructionError( classes, data ) ;
152 }
153 
154 template <typename T>
155 float
157 {
158  return _meanReconstructionError ;
159 }
160 
161 template <typename T>
162 float
164 {
165  return _varReconstructionError ;
166 }
167 
168 template <typename T>
169 float
171 {
172  return _unreconstructedInertia ;
173 }
174 
175 template <typename T>
176 void
177 PcaElement<T>::computeReconstructionError( const std::vector< std::list< Point3d > >& classes,
178  const AimsData<T>& data )
179 {
182  std::set<int>::const_iterator aggIter( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
183 
184  std::list<Point3d> agregatePoints ;
185  while( aggIter != aggLast ){
186  std::list<Point3d>::const_iterator lIter(classes[*aggIter].begin()), lLast(classes[*aggIter].end() ) ;
187  while( lIter != lLast ){
188  agregatePoints.push_back( *lIter ) ;
189  ++lIter ;
190  }
191  ++aggIter ;
192  }
193 
194  if( int(agregatePoints.size()) > data.dimT() ){
195 /* AimsPCA pca( _numberOfSignificantVps ) ; */
196 /* pca.doIt( agregatePoints, data ) ; */
197 /* _unreconstructedInertia = pca.noiseInertia() ; */
198  AimsPCA pca( _numberOfSignificantVps ) ;
199  pca.doIt( agregatePoints, data ) ;
200  AimsData<float> errorMatrix = pca.reconstructionErrorMatrix( ) ;
201 
202  _mean = pca.mean() ;
203  std::list<Point3d>::iterator iter( agregatePoints.begin() ), last( agregatePoints.end() ) ;
204  AimsData<float> indiv( 1, data.dimT() ), indivTr(data.dimT()) ;
205  while( iter != last ){
206  float norm2 = 0. ;
207  for ( int t = 0 ; t < data.dimT() ; ++t ){
208  indiv(0,t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
209  indivTr(t,0) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
210 
211  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
212  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
213  }
214  float val = indiv.cross( errorMatrix.cross(indivTr) )(0,0) / norm2 ;
215  _meanReconstructionError += val ;
216  _varReconstructionError += val * val ;
217  ++iter ;
218  }
219 
220  _meanReconstructionError /= double(agregatePoints.size()) ;
221  _varReconstructionError = _varReconstructionError / double(agregatePoints.size()) -
223  if( this->_constitutedFrom.size() == 1 )
224  _errorMatrix = errorMatrix ;
225 
226  //_unreconstructedInertia = pca.noiseInertia() ;
227  } else {
231  }
232 }
233 
234 template <typename T>
235 float
237  const std::vector< std::list< Point3d > >& classes,
238  const AimsData<T>& data, const AimsData<float>& initDistances )
239 {
240  Element<T> * fusionEl = agregateTo( other, classes, data, initDistances ) ;
241  PcaElement<T> * fusionPcaEl = dynamic_cast< PcaElement<T> * >( fusionEl ) ;
242  PcaElement<T> * otherPcaEl = dynamic_cast< PcaElement<T> * >( other ) ;
243 
244  float distance = fusionPcaEl->card() * fusionPcaEl->meanReconstructionError() /
245  ( this->card() * meanReconstructionError() + otherPcaEl->card() * otherPcaEl->meanReconstructionError() ) ;
246 
247  delete fusionEl ;
248  return distance ;
249 }
250 
251 template <typename T>
252 Element<T> *
254  const std::vector< std::list< Point3d > >& classes,
255  const AimsData<T>& data, const AimsData<float>&, int newNb )
256 {
257 /* BaryElement<T> * otherB = dynamic_cast<BaryElement<T>*>(other) ; */
258  // Agglomeration
259  std::set<int> constFrom( this->constitutedFrom() ) ;
260  std::set<int>::const_iterator cIt(other->constitutedFrom().begin() ),
261  cLast(other->constitutedFrom().end() ) ;
262  while( cIt != cLast ){
263  constFrom.insert( *cIt ) ;
264  ++cIt ;
265  }
266 
267  Element<T> * newEl = new PcaElement<T>( classes, data, _numberOfSignificantVps, constFrom, newNb ) ;
268  return newEl ;
269 }
270 
271 template <typename T>
272 PPcaElement<T>::PPcaElement( const std::vector<std::list< Point3d > >& classes,
273  const AimsData<T>& data, int numberOfSignificantVps,
274  const std::set<int>& constitutedFrom, int number ) :
275  Element<T>( number, 0, constitutedFrom ), _numberOfSignificantVps(numberOfSignificantVps)
276 {
277  this->_card = 0 ;
278  std::set<int>::const_iterator aggIt( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
279 
280  //std::cout << "Card = " ;
281  while( aggIt != aggLast ) {
282  this->_card += classes[*aggIt].size() ;
283  //std::cout << "\t" << this->_card ;
284  ++aggIt ;
285  }
286 
287  _ppcaElement = new aims::ProbabilisticPcaElement( _numberOfSignificantVps ) ;
288 
289  computeReconstructionError( classes, data ) ;
290 }
291 
292 template <typename T>
293 void
294 PPcaElement<T>::computeReconstructionError( const std::vector< std::list< Point3d > >& classes,
295  const AimsData<T>& data )
296 {
297  std::set<int>::const_iterator aggIter( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
298 
299  std::list<Point3d> agregatePoints ;
300  while( aggIter != aggLast ){
301  std::list<Point3d>::const_iterator lIter(classes[*aggIter].begin()), lLast(classes[*aggIter].end() ) ;
302  while( lIter != lLast ){
303  agregatePoints.push_back( *lIter ) ;
304  ++lIter ;
305  }
306  ++aggIter ;
307  }
308 
309  if( int(agregatePoints.size()) > data.dimT() )
310  _ppcaElement->doIt(agregatePoints, data) ;
311 }
312 
313 template <typename T>
314 float
316  const std::vector< std::list< Point3d > >& classes,
317  const AimsData<T>& data, const AimsData<float>& initDistances )
318 {
319  PPcaElement<T> * otherPPca = dynamic_cast< PPcaElement<T> *>(other) ;
320  if( this->constitutedFrom().size() == 1 && other->constitutedFrom().size() == 1 ){
321  AimsData<double> indiv(data.dimT()) ;
322  std::list<Point3d>::const_iterator iter( classes[*(this->constitutedFrom().begin())].begin() ),
323  last( classes[*(this->constitutedFrom().begin())].end() ) ;
324  float oneToTwoMeanError = 0. ;
325  float twoToOneMeanError = 0. ;
326  while( iter != last ){
327  float norm2 = 0. ;
328  for ( int t = 0 ; t < data.dimT() ; ++t ){
329  indiv(t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
330 
331  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
332  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
333  }
334  oneToTwoMeanError -= otherPPca->ppca()->posteriorProbability( indiv, 1. ) ;
335  ++iter ;
336  }
337 
338  oneToTwoMeanError /= float(classes[*(this->constitutedFrom().begin())].size()) ;
339 /* std::cout << "Mean Error = " << oneToTwoMeanError << " against " << otherPcaReproc->meanReconstructionError() << std::endl */;
340 
341  iter = classes[ *(otherPPca->constitutedFrom().begin())].begin() ;
342  last = classes[ *(otherPPca->constitutedFrom().begin())].end() ;
343 
344  while( iter != last ){
345  float norm2 = 0. ;
346  for ( int t = 0 ; t < data.dimT() ; ++t ){
347  indiv(t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
348 
349  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
350  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
351  }
352  twoToOneMeanError -= _ppcaElement->posteriorProbability( indiv, 1. ) ;
353  ++iter ;
354  }
355  twoToOneMeanError /= float(classes[ *(otherPPca->constitutedFrom().begin())].size()) ;
356 /* std::cout << "Mean Error = " << twoToOneMeanError << " against " << _meanReconstructionError << std::endl ; */
357  return std::max( oneToTwoMeanError, twoToOneMeanError ) ;
358  }
359 
360  std::set<int> constFrom = otherPPca->constitutedFrom() ;
361  std::set<int>::const_iterator cIter( this->constitutedFrom().begin() ), cLast( this->constitutedFrom().end() ) ;
362  while( cIter != cLast ){
363  constFrom.insert( *cIter ) ;
364  ++cIter ;
365  }
366 
367 /* std::cout << "Contains" ; */
368 /* std::set<int>::const_iterator c3Iter( constFrom.begin() ), c3Last( constFrom.end() ) ; */
369 /* while( c3Iter != c3Last ) */
370 /* { */
371 /* std::cout << "\t" << *c3Iter ; */
372 /* ++c3Iter ; */
373 /* } */
374 
375  std::set<int>::const_iterator c1Iter( constFrom.begin() ), c1Last( constFrom.end() ) ;
376 
377  float maxDistance = 0. ;
378  while( c1Iter != c1Last )
379  {
380  std::set<int>::const_iterator c2Iter( c1Iter ) ;
381  ++c2Iter ;
382  while( c2Iter != c1Last )
383  {
384 /* std::cout << *c1Iter << " " << *c2Iter << std::endl ; */
385 /* std::cout << "\t( " << *c1Iter << ", " << *c2Iter << " ) = " << initDistances( *c1Iter, *c2Iter ) ; */
386  float initDist = initDistances( *c1Iter, *c2Iter ) ;
387  if( maxDistance < initDist )
388  maxDistance = initDist ;
389  ++c2Iter ;
390  }
391  ++c1Iter ;
392  }
393 
394 /* std::cout << " = " << maxDistance << std::endl ; */
395 
396  return maxDistance ;
397 }
398 
399 template <typename T>
400 Element<T> *
402  const std::vector< std::list< Point3d > >& classes,
403  const AimsData<T>& data, const AimsData<float>&, int newNb )
404 {
405 /* BaryElement<T> * otherB = dynamic_cast<BaryElement<T>*>(other) ; */
406  // Agglomeration
407  std::set<int> constFrom( this->constitutedFrom() ) ;
408  std::set<int>::const_iterator cIt(other->constitutedFrom().begin() ),
409  cLast(other->constitutedFrom().end() ) ;
410  while( cIt != cLast ){
411  constFrom.insert( *cIt ) ;
412  ++cIt ;
413  }
414 
415  Element<T> * newEl = new PPcaElement<T>( classes, data, _numberOfSignificantVps, constFrom, newNb ) ;
416  return newEl ;
417 }
418 
419 template <typename T>
420 float
422  const std::vector< std::list< Point3d > >& ,
423  const AimsData<T>& , const AimsData<float>& )
424 {
425  //std::cout << "Distance To :: begin" << std::endl ;
426  double surf1To2 = 0., surf2To1 = 0., dist1To2 = 0., dist2To1 = 0., maxMomentRatio = 1. ;
427  //double ratio ;
428  for( std::set<int>::iterator itC1 = this->constitutedFrom().begin() ; itC1 != this->constitutedFrom().end() ; ++itC1 )
429  for( std::set<int>::iterator itC2 = other->constitutedFrom().begin() ; itC2 != other->constitutedFrom().end() ; ++itC2 ){
430  surf1To2 += (*_interactionSurf)(*itC1, *itC2) ;
431  surf2To1 += (*_interactionSurf)(*itC2, *itC1) ;
432  dist1To2 = std::max(dist1To2, (*_kinModel2by2Distances)(*itC1, *itC2) ) ;
433  dist2To1 = std::max(dist2To1, (*_kinModel2by2Distances)(*itC2, *itC1) ) ;
434 // if( (*_classMaximumMoment)[*itC1] == (*_classMaximumMoment)[*itC2] )
435 // ratio = 1. ;
436 // else if( (*_classMaximumMoment)[*itC1] == 0. || (*_classMaximumMoment)[*itC2] == 0. )
437 // ratio = 0. ;
438 // else{
439 // ratio = std::min( (*_classMaximumMoment)[*itC1] / (*_classMaximumMoment)[*itC2],
440 // (*_classMaximumMoment)[*itC2] / (*_classMaximumMoment)[*itC1] ) ;
441 // }
442 
443 // maxMomentRatio = std::min( maxMomentRatio, ratio ) ;
444 
445  maxMomentRatio = std::max( maxMomentRatio, (*_maxMomentValueRatios)(*itC1, *itC2) ) ;
446  }
447 
448  double vol1 = 0, vol2 = 0 ;
449  for( std::set<int>::iterator itC1 = this->constitutedFrom().begin() ; itC1 != this->constitutedFrom().end() ; ++itC1 )
450  vol1 += (*_labelsVolumes)[*itC1] ;
451  for( std::set<int>::iterator itC2 = other->constitutedFrom().begin() ; itC2 != other->constitutedFrom().end() ; ++itC2 )
452  vol2 += (*_labelsVolumes)[*itC2] ;
453 
454  double maxSurfVolRatio = std::max( surf1To2 / vol1, surf2To1 / vol2 ) ;
455 
456  return ( 0.5 * ( dist1To2 + dist2To1 ) - _meanDist2By2 ) / sqrt(_varDist2By2)
457  - _spatialregularizationWeight * ( maxSurfVolRatio - _meanSurfVolRatio ) / sqrt(_varSurfVolRatio)
458  + _timeOfMaximumWeight * ( maxMomentRatio - _meanMaxMomentRatio ) / sqrt(_varMaxMomentRatio) ;
459 // return 0.5 * ( dist1To2 + dist2To1 ) - _spatialregularizationWeight * maxSurfVolRatio
460 // - _timeOfMaximumWeight * maxMomentRatio ;
461 }
462 
463 template <typename T>
464 Element<T> *
466  const std::vector< std::list< Point3d > >& ,
467  const AimsData<T>& , const AimsData<float>& , int newNb )
468 {
469  std::set<int> constFrom( this->constitutedFrom() ) ;
470  std::set<int>::const_iterator cIt(other->constitutedFrom().begin() ),
471  cLast(other->constitutedFrom().end() ) ;
472  while( cIt != cLast ){
473  constFrom.insert( *cIt ) ;
474  ++cIt ;
475  }
476 
477  return new SCKBDistanceElement<T>( _maxMomentValueRatios, _kinModel2by2Distances, _interactionSurf, _labelsVolumes,
478  _spatialregularizationWeight, _timeOfMaximumWeight,
479  _meanDist2By2, _varDist2By2, _meanSurfVolRatio, _varSurfVolRatio,
480  _meanMaxMomentRatio, _varMaxMomentRatio,
481  constFrom, newNb ) ;
482 }
483 
484 template <typename T>
486  Element<T>( number, 0, constitutedFrom )
487 {
488  //std::cout << "Max2By2DistanceElement" << std::endl ;
489  this->_card = 1 ;
490 }
491 
492 template <typename T>
493 float
495  const std::vector< std::list< Point3d > >&,
496  const AimsData<T>&, const AimsData<float>& initDistances )
497 {
498 
499  Max2By2DistanceElement<T> * otherPPca = dynamic_cast< Max2By2DistanceElement<T> *>(other) ;
500  if( this->constitutedFrom().size() == 1 && other->constitutedFrom().size() == 1 ){
501  return initDistances( *( this->constitutedFrom().begin() ), *( other->constitutedFrom().begin() ) ) ;
502  }
503 
504  std::set<int> constFrom = otherPPca->constitutedFrom() ;
505  std::set<int>::const_iterator cIter( this->constitutedFrom().begin() ), cLast( this->constitutedFrom().end() ) ;
506  while( cIter != cLast ){
507  constFrom.insert( *cIter ) ;
508  ++cIter ;
509  }
510 
511 /* std::cout << "Contains" ; */
512 /* std::set<int>::const_iterator c3Iter( constFrom.begin() ), c3Last( constFrom.end() ) ; */
513 /* while( c3Iter != c3Last ) */
514 /* { */
515 /* std::cout << "\t" << *c3Iter ; */
516 /* ++c3Iter ; */
517 /* } */
518 
519  std::set<int>::const_iterator c1Iter( constFrom.begin() ), c1Last( constFrom.end() ) ;
520 
521  float maxDistance = initDistances( *(constFrom.begin()), *(constFrom.rbegin()) ) ;
522  while( c1Iter != c1Last ){
523  std::set<int>::const_iterator c2Iter( c1Iter ) ;
524  ++c2Iter ;
525  while( c2Iter != c1Last )
526  {
527  /* std::cout << *c1Iter << " " << *c2Iter << std::endl ; */
528  /* std::cout << "\t( " << *c1Iter << ", " << *c2Iter << " ) = " << initDistances( *c1Iter, *c2Iter ) ; */
529  float initDist = initDistances( *c1Iter, *c2Iter ) ;
530 /* std::cout << *c1Iter << ", " << *c2Iter << " = " << initDist << " -> " ; */
531  if( maxDistance < initDist )
532  maxDistance = initDist ;
533 /* std::cout << maxDistance << std::endl ; */
534  ++c2Iter ;
535  }
536  ++c1Iter ;
537  }
538 
539 /* std::cout << " = " << maxDistance << std::endl ; */
540 
541  return maxDistance ;
542 }
543 
544 template <typename T>
545 Element<T> *
547  const std::vector< std::list< Point3d > >&,
548  const AimsData<T>&, const AimsData<float>&, int newNb )
549 {
550 /* BaryElement<T> * otherB = dynamic_cast<BaryElement<T>*>(other) ; */
551  // Agglomeration
552  std::set<int> constFrom( this->constitutedFrom() ) ;
553  std::set<int>::const_iterator cIt(other->constitutedFrom().begin() ),
554  cLast(other->constitutedFrom().end() ) ;
555  while( cIt != cLast ){
556  constFrom.insert( *cIt ) ;
557  ++cIt ;
558  }
559 
560  //std::cout << "Max2By2DistanceElement<T>::agregateTo" << std::endl ;
561 
562  Element<T> * newEl = new Max2By2DistanceElement<T>( constFrom, newNb ) ;
563  return newEl ;
564 }
565 
566 template <typename T>
567 PcaReprocReconsErrorElement<T>::PcaReprocReconsErrorElement(const std::vector<std::list< Point3d > >& classes,
568  const AimsData<T>& data, int numberOfSignificantVps,
569  const std::set<int>& constitutedFrom, int number) :
570  Element<T>( number, 0, constitutedFrom ), _meanReconstructionError(-1.), _varReconstructionError(-1.),
571  _unreconstructedInertia(-1.), _numberOfSignificantVps(numberOfSignificantVps)
572 {
573  this->_card = 0 ;
574  std::set<int>::const_iterator aggIt( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
575 
576  while( aggIt != aggLast ) {
577  this->_card += classes[*aggIt].size() ;
578  ++aggIt ;
579  }
580  if( this->_constitutedFrom.size() == 1 )
581  computeReconstructionError( classes, data ) ;
582 }
583 
584 template <typename T>
585 void
586 PcaReprocReconsErrorElement<T>::computeReconstructionError( const std::vector< std::list< Point3d > >& classes,
587  const AimsData<T>& data )
588 {
589  _meanReconstructionError = 0. ;
590  _varReconstructionError = 0. ;
591  std::set<int>::const_iterator aggIter( this->_constitutedFrom.begin() ), aggLast( this->_constitutedFrom.end() ) ;
592 
593  std::list<Point3d> agregatePoints ;
594  while( aggIter != aggLast ){
595  std::list<Point3d>::const_iterator lIter(classes[*aggIter].begin()), lLast(classes[*aggIter].end() ) ;
596  while( lIter != lLast ){
597  agregatePoints.push_back( *lIter ) ;
598  ++lIter ;
599  }
600  ++aggIter ;
601  }
602 
603  if( int(agregatePoints.size()) > data.dimT() ){
604 /* AimsPCA pca( _numberOfSignificantVps ) ; */
605 /* pca.doIt( agregatePoints, data ) ; */
606 /* _unreconstructedInertia = pca.noiseInertia() ; */
607  AimsPCA pca( _numberOfSignificantVps ) ;
608  pca.doIt( agregatePoints, data ) ;
609  AimsData<float> errorMatrix = pca.reconstructionErrorMatrix( ) ;
610 
611  _mean = pca.mean() ;
612  std::list<Point3d>::iterator iter( agregatePoints.begin() ), last( agregatePoints.end() ) ;
613  AimsData<float> indiv( 1, data.dimT() ), indivTr(data.dimT()) ;
614  while( iter != last ){
615  float norm2 = 0. ;
616  for ( int t = 0 ; t < data.dimT() ; ++t ){
617  indiv(0,t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
618  indivTr(t,0) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
619 
620  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
621  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
622  }
623  float val = indiv.cross( errorMatrix.cross(indivTr) )(0,0) / norm2 ;
624  _meanReconstructionError += val ;
625  _varReconstructionError += val * val ;
626  ++iter ;
627  }
628 
629  _meanReconstructionError /= double(agregatePoints.size()) ;
630  _varReconstructionError = _varReconstructionError / double(agregatePoints.size()) -
631  _meanReconstructionError * _meanReconstructionError ;
632  _errorMatrix = errorMatrix ;
633 
634  //_unreconstructedInertia = pca.noiseInertia() ;
635  } else {
636  _unreconstructedInertia = 1. ;
637  _meanReconstructionError = 1. ;
638  _varReconstructionError = 1. ;
639  }
640 }
641 
642 template <typename T>
643 float
645 {
646  return _meanReconstructionError ;
647 }
648 
649 template <typename T>
650 float
652 {
653  return _varReconstructionError ;
654 }
655 
656 template <typename T>
657 float
659 {
660  return _unreconstructedInertia ;
661 }
662 
663 template <typename T>
664 float
666  const std::vector< std::list< Point3d > >& classes,
667  const AimsData<T>& data, const AimsData<float>& initDistances )
668 {
669  PcaReprocReconsErrorElement<T> * otherPcaReproc = dynamic_cast< PcaReprocReconsErrorElement<T> *>(other) ;
670  if( this->constitutedFrom().size() == 1 && other->constitutedFrom().size() == 1 ){
671  AimsData<float> indiv( 1, data.dimT() ), indivTr(data.dimT()) ;
672  std::list<Point3d>::const_iterator iter( classes[*(this->constitutedFrom().begin())].begin() ),
673  last( classes[*(this->constitutedFrom().begin())].end() ) ;
674  AimsData<float> secondErrorMatrix = otherPcaReproc->errorMatrix() ;
675  float oneToTwoMeanError = 0. ;
676  float twoToOneMeanError = 0. ;
677  while( iter != last ){
678  float norm2 = 0. ;
679  for ( int t = 0 ; t < data.dimT() ; ++t ){
680  indiv(0,t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
681  indivTr(t,0) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
682 
683  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
684  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
685  }
686  oneToTwoMeanError += indiv.cross( secondErrorMatrix.cross(indivTr) )(0,0) / norm2 ;
687  ++iter ;
688  }
689 
690  oneToTwoMeanError /= float(classes[*(this->constitutedFrom().begin())].size()) ;
691  oneToTwoMeanError = /* 1. - exp( - */( oneToTwoMeanError - otherPcaReproc->meanReconstructionError() ) *
692  ( oneToTwoMeanError - otherPcaReproc->meanReconstructionError() ) /
693  ( 2 * otherPcaReproc->varReconstructionError() /* ) */ ) ;
694 
695  iter = classes[ *(otherPcaReproc->constitutedFrom().begin())].begin() ;
696  last = classes[ *(otherPcaReproc->constitutedFrom().begin())].end() ;
697 
698  while( iter != last ){
699  float norm2 = 0. ;
700  for ( int t = 0 ; t < data.dimT() ; ++t ){
701  indiv(0,t) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
702  indivTr(t,0) = data( (*iter)[0], (*iter)[1], (*iter)[2], t ) - _mean[t] ;
703 
704  norm2 += data( (*iter)[0], (*iter)[1], (*iter)[2], t )*
705  data( (*iter)[0], (*iter)[1], (*iter)[2], t ) ;
706  }
707  twoToOneMeanError += indiv.cross( _errorMatrix.cross(indivTr) )(0,0) / norm2 ;
708  ++iter ;
709  }
710  twoToOneMeanError /= float(classes[ *(otherPcaReproc->constitutedFrom().begin())].size()) ;
711  twoToOneMeanError = /* 1. - exp ( -*/( twoToOneMeanError - _meanReconstructionError ) *
712  ( twoToOneMeanError - _meanReconstructionError ) /
713  ( 2. * _varReconstructionError ) /* ) */ ;
714 
715  return std::max( oneToTwoMeanError, twoToOneMeanError ) ;
716  }
717 
718  std::set<int> constFrom = otherPcaReproc->constitutedFrom() ;
719  std::set<int>::const_iterator cIter( this->constitutedFrom().begin() ), cLast( this->constitutedFrom().end() ) ;
720  while( cIter != cLast ){
721  constFrom.insert( *cIter ) ;
722  ++cIter ;
723  }
724 
725  std::set<int>::const_iterator c1Iter( constFrom.begin() ), c1Last( constFrom.end() ) ;
726 
727  float maxDistance = 0. ;
728  while( c1Iter != c1Last )
729  {
730  std::set<int>::const_iterator c2Iter( c1Iter ) ;
731  ++c2Iter ;
732  while( c2Iter != c1Last )
733  {
734  float initDist = initDistances( *c1Iter, *c2Iter ) ;
735 
736  if( maxDistance < initDist )
737  maxDistance = initDist ;
738  ++c2Iter ;
739  }
740  ++c1Iter ;
741  }
742 
743  return maxDistance ;
744 }
745 
746 template <typename T>
747 Element<T> *
749  const std::vector< std::list< Point3d > >& classes,
750  const AimsData<T>& data, const AimsData<float>&, int newNb )
751 {
752  // Agglomeration
753  std::set<int> constFrom( this->constitutedFrom() ) ;
754  std::set<int>::const_iterator cIt(other->constitutedFrom().begin() ),
755  cLast(other->constitutedFrom().end() ) ;
756  while( cIt != cLast ){
757  constFrom.insert( *cIt ) ;
758  ++cIt ;
759  }
760 
761  Element<T> * newEl = new PcaReprocReconsErrorElement<T>( classes, data, _numberOfSignificantVps, constFrom, newNb ) ;
762  return newEl ;
763 }
764 
765 template <typename T>
766 CahElementFactory<T>::CahElementFactory( const std::vector< std::list< Point3d > >& classes,
767  const AimsData<T>& data ) : _classes( classes ), _data( data ) {}
768 
769 template <typename T>
771 
772 
773 template <typename T>
774 std::list< Element<T>* >
776 {
777  std::list<Element<T>*> listeEl ;
778  for( unsigned int c = 0 ; c < _classes.size() ; ++c )
779  listeEl.push_back( creator(c) ) ;
780  return listeEl ;
781 }
782 
783 template <typename T>
784 BaryCahElementFactory<T>::BaryCahElementFactory( const std::vector< std::list< Point3d > >& classes,
785  const AimsData<T>& data ) : CahElementFactory<T>(classes, data) {}
786 
787 template <typename T>
789 
790 template <typename T>
791 Element<T> *
793 {
794  return new BaryElement<T>( this->_classes, this->_data, std::set<int>(), n ) ;
795 }
796 
797 
798 template <typename T>
800  const std::vector< std::list< Point3d > >& classes,
801  const AimsData<T>& data ) :
802  CahElementFactory<T>(classes, data), _nbOfSignificantVps(nbOfSignificantVps) {}
803 
804 template <typename T>
806 
807 template <typename T>
808 Element<T> *
810 {
811  return new PcaElement<T>( this->_classes, this->_data, _nbOfSignificantVps, std::set<int>(), n ) ;
812 }
813 
814 
815 
816 template <typename T>
818  CahElementFactory<T>( ), _initDistances( initDistances.clone() ), _nbClasses(initDistances.dimX())
819 {
820  std::cout << "Init distances = " << _initDistances.dimX() << ", " << _initDistances.dimY() << std::endl ;
821 }
822 
823 template <typename T>
825 
826 template <typename T>
827 Element<T> *
829 {
830  //std::cout << "Creator " << n << std::endl ;
831  return new Max2By2DistanceElement<T>( std::set<int>(), n ) ;
832 }
833 
834 template <typename T>
835 std::list< Element<T>* >
837 {
838  std::list<Element<T>*> listeEl ;
839  std::cout << "Nb classes = " << _nbClasses << std::endl ;
840  for( int c = 0 ; c < _nbClasses ; ++c )
841  listeEl.push_back( creator(c) ) ;
842  return listeEl ;
843 }
844 
845 AimsData<double> *
846 kineticSampleDistance( const std::vector<aims::DiscriminantAnalysisElement*>& classes )
847 {
848  std::cout << "CAH::kineticSampleDistance" << std::endl ;
849  for( unsigned int c = 0 ; c < classes.size() ; ++c )
850  if( ! classes[c]->computed() )
851  std::cerr << "Not computed for class " << c << std::endl ;
852  int c1, c2 ;
853  double nbFrames = double( classes[0]->mean().dimX() ) ;
854  AimsData<double> distance2by2( classes.size(), classes.size() ) ;
855  //std::cout << "Centroids distance : " << std::endl ;
856  ForEach2d( distance2by2, c1, c2 ){
857  distance2by2( c1, c2 ) = classes[c1]->distance( classes[c2]->mean() ) ;
858  //std::cout << "c1 = " << c1 << " c2 = " << c2 << " == " <<distance2by2( c1, c2 ) << std::endl ;
859  }
860  AimsData<double> * symDistance2by2 = new AimsData<double>( classes.size(), classes.size() ) ;
861  ForEach2d( distance2by2, c1, c2 ){
862  (*symDistance2by2)( c1, c2 ) = 0.5 * ( distance2by2( c1, c2 ) + distance2by2( c2, c1 ) ) / nbFrames ;
863  }
864 // aims::Writer< AimsData<double> > wri("symDistance2by2.ima") ;
865 // wri.write( *symDistance2by2 ) ;
866 
867  return symDistance2by2 ;
868 }
869 
870 AimsData<double>
871 interactionSurfaces( const AimsData<int>& locMap, const std::vector<short>& unfusionedLabels,
872  AimsData<double>& interactionSurf, std::vector<double>& labelsVolumes )
873 {
874  std::cout << "CAH::interactionSurfaces" << std::endl ;
875  int x, y, z ;
876  int maxLabel = unfusionedLabels[0] ;
877  for(unsigned int ind = 0 ; ind < unfusionedLabels.size() ; ++ind ){
878  if( unfusionedLabels[ind] > maxLabel ){
879  maxLabel = unfusionedLabels[ind] ;
880  }
881  //std::cout << " " << unfusionedLabels[ind] ;
882  }
883 
884  std::cout << std::endl << "max label computed : " << maxLabel << " on " << unfusionedLabels.size() << "indivs" << std::endl ;
885 
886  labelsVolumes = std::vector<double>( maxLabel + 1 ) ;
887  interactionSurf = AimsData<double>( maxLabel + 1, maxLabel + 1 ) ;
888 
889  aims::Connectivity * conn = 0 ;
890  if( locMap.dimZ() == 1 )
891  conn = new aims::Connectivity( 0, 0, aims::Connectivity::CONNECTIVITY_4_XY ) ;
892  else
893  conn = new aims::Connectivity( 0, 0, aims::Connectivity::CONNECTIVITY_6_XYZ ) ;
894 
895  Point3df voxelSize( locMap.sizeX(), locMap.sizeY(), locMap.sizeZ() ) ;
896  float voxelVolume = locMap.sizeX() * locMap.sizeY() * locMap.sizeZ() ;
897  std::vector<float> surfaces( conn->nbNeighbors(), voxelVolume ) ;
898 
899  for( int n = 0 ; n < conn->nbNeighbors() ; ++n ){
900  short index = 0 ;
901  if( conn->xyzOffset(n)[1] != 0. )
902  index = 1 ;
903  else if( conn->xyzOffset(n)[2] != 0. )
904  index = 2 ;
905  surfaces[n] /= voxelSize[index] ;
906  std::cout << "xyzOffset : " << conn->xyzOffset(n) << " : " << surfaces[n] << std::endl ;
907  }
908 
909  std::cout << "surface vector computed" << std::endl ;
910 
911  int ind, indLabel, neigh, neighLabel ;
912  Point3d p ;
913  Point3d pNeigh ;
914  Point3d dims( locMap.dimX(), locMap.dimY(), locMap.dimZ() ) ;
915  ForEach3d( locMap, x, y, z ){
916  ind = locMap( x, y, z ) ;
917  indLabel = unfusionedLabels[ind] ;
918  labelsVolumes[ indLabel ] += voxelVolume ;
919  p = Point3d(x, y, z) ;
920  for( int n = 0 ; n < conn->nbNeighbors() ; ++n ){
921  pNeigh = p + conn->xyzOffset(n) ;
922  if( pNeigh[0] >= 0 && pNeigh[1] >= 0 && pNeigh[2] >= 0 &&
923  pNeigh[0] < locMap.dimX() && pNeigh[1] < locMap.dimY() && pNeigh[2] < locMap.dimZ() ) {
924  neigh = locMap( pNeigh ) ;
925  neighLabel = unfusionedLabels[neigh] ;
926  if( indLabel != neighLabel ){
927  interactionSurf( indLabel, neighLabel ) += surfaces[n] ;
928 // std::cout << "p : " << p << " l = " << indLabel << "\tNeigh : " << pNeigh << " l == " << neighLabel
929 // << " IS(p, n) = " << interactionSurf( indLabel, neighLabel )<< std::endl ;
930  }
931  }
932  }
933  }
934  std::cout << "interaction surfaces computed" << std::endl ;
935 
936  int c1, c2 ;
937  AimsData<double> surfaceVolumeRatio( maxLabel + 1, maxLabel + 1 ) ;
938  ForEach2d( interactionSurf, c1, c2 ){
939  if( c1 != c2 )
940  surfaceVolumeRatio( c1, c2 ) = interactionSurf( c1, c2 ) / labelsVolumes[c1] ;
941  }
942 
943  AimsData<double> symetrizedSurfaceVolumeRatio( maxLabel + 1, maxLabel + 1 ) ;
944  double maximum, minimum = std::max( surfaceVolumeRatio( 0, 1 ), surfaceVolumeRatio( 1, 0 ) ) ;
945  maximum = minimum ;
946 
947  ForEach2d( interactionSurf, c1, c2 ){
948  if( c1 != c2 ){
949  symetrizedSurfaceVolumeRatio( c1, c2 ) = std::max( surfaceVolumeRatio( c1, c2 ), surfaceVolumeRatio( c2, c1 ) ) ;
950  if( symetrizedSurfaceVolumeRatio( c1, c2 ) > maximum )
951  maximum = symetrizedSurfaceVolumeRatio( c1, c2 ) ;
952  if( symetrizedSurfaceVolumeRatio( c1, c2 ) < minimum )
953  minimum = symetrizedSurfaceVolumeRatio( c1, c2 ) ;
954  }
955  }
956 
957 // aims::Writer< AimsData<double> > wri1("interactionSurfaces.ima") ;
958 // wri1.write(interactionSurf) ;
959 // aims::Writer< AimsData<double> > wri2("surfaceVolumeRatio.ima") ;
960 // wri2.write(surfaceVolumeRatio) ;
961 // aims::Writer< AimsData<double> > wri3("symetrizedSurfaceVolumeRatio.ima") ;
962 // wri3.write(symetrizedSurfaceVolumeRatio) ;
963 
964  AimsData<double> distanceMatrix(maxLabel + 1, maxLabel + 1) ;
965  ForEach2d( interactionSurf, c1, c2 ){
966  if( c1 == c2 )
967  distanceMatrix(c1, c2) = 0. ;
968  else
969  distanceMatrix(c1, c2) = (symetrizedSurfaceVolumeRatio(c1, c2) - minimum) / ( maximum - minimum ) ;
970  }
971 
972  return distanceMatrix ;
973 }
974 
975 template <class T>
976 bool
977 correlated( const AimsData<T>& x, const AimsData<T>& y )
978 {
979  if( x.dimX() == 1 && y.dimX() == 1 )
980  return true ;
981 
982  if( x.dimX() != y.dimX() ){
983  std::cerr <<"Wrong size for correlation !" << std::endl ;
984  throw std::runtime_error("Wrong size for correlation !") ;
985  }
986 
987  T xMean = 0. ;
988  T yMean = 0. ;
989 
990  for( int t = 0 ; t < x.dimX() ; ++t ){
991  xMean += x(t) ;
992  yMean += y(t) ;
993  }
994  xMean /= T(x.dimX()) ;
995  yMean /= T(y.dimX()) ;
996 
997  T corr = 0. ;
998  for( int t = 0 ; t < x.dimX() ; ++t )
999  corr += ( x(t) - xMean ) * ( y(t) - yMean ) ;
1000  return (corr > 0.) ;
1001 }
1002 
1003 // AimsData<double>
1004 // momentOfMaximumDifference( const std::vector<float>& momentOfMax )
1005 // {
1006 // AimsData<double> res( classesKinDescription.size(), classesKinDescription.size() ) ;
1007 // int c1, c2 ;
1008 // ForEach2d( res, c1, c2){
1009 // if( momentOfMax[c1] == momentOfMax[c2] )
1010 // res( c1, c2) == 0. ;
1011 // else{
1012 // double minMoment = std::min(momentOfMax[c1], momentOfMax[c2]) ;
1013 // res( c1, c2) = 2. * std::max( momentOfMax[c1] - momentOfMax[c2], momentOfMax[c2] - momentOfMax[c1]) *
1014 // exp( - (minMoment * minMoment)
1015 // / ( 2. * 0.333 * classesKinDescription[0]->mean().dimX() * 0.333 * classesKinDescription[0]->mean().dimX() ) ) ;
1016 // }
1017 // }
1018 
1019 // aims::Writer< AimsData<double> > wri( "momentOfMaximumRelDiff.ima" ) ;
1020 // wri.write(res) ;
1021 
1022 // return res ;
1023 // }
1024 
1025 AimsData<double>
1026 momentOfMaximumDifference( const std::vector<aims::DiscriminantAnalysisElement*>& classesKinDescription )
1027 {
1028  std::vector<short> momentOfMax( classesKinDescription.size() ) ;
1029  std::vector<float> valueOfMax( classesKinDescription.size() ) ;
1030  int maxInd = 0 ;
1031  for( unsigned int c = 0 ; c < classesKinDescription.size() ; ++c ){
1032  for( int t = 0 ; t < classesKinDescription[c]->mean().dimX() ; ++t )
1033  if( classesKinDescription[c]->mean()(t) > classesKinDescription[c]->mean()(maxInd) )
1034  maxInd = t ;
1035  momentOfMax[c] = maxInd ;
1036  }
1037 
1038 
1039  AimsData<double> res( classesKinDescription.size(), classesKinDescription.size() ) ;
1040  int c1, c2 ;
1041  ForEach2d( res, c1, c2){
1042  if( momentOfMax[c1] == momentOfMax[c2] )
1043  res( c1, c2) == 0. ;
1044  else{
1045  double minMoment = std::min(momentOfMax[c1], momentOfMax[c2]) ;
1046  res( c1, c2) = 2. * std::max( momentOfMax[c1] - momentOfMax[c2], momentOfMax[c2] - momentOfMax[c1]) *
1047  exp( - (minMoment * minMoment)
1048  / ( 2. * 0.5 * classesKinDescription[0]->mean().dimX() * 0.5 * classesKinDescription[0]->mean().dimX() ) ) ;
1049  }
1050  }
1051 
1052 // aims::Writer< AimsData<double> > wri( "momentOfMaximumRelDiff.ima" ) ;
1053 // wri.write(res) ;
1054 
1055  return res ;
1056 }
1057 
1058 AimsData<double>
1059 momentOfMaxValueRatios( const std::vector<aims::DiscriminantAnalysisElement*>& classesKinDescription )
1060 {
1061  // Getting moment of max
1062  std::vector<short> momentOfMax( classesKinDescription.size() ) ;
1063  std::vector<float> valueOfMax( classesKinDescription.size() ) ;
1064  int maxInd = 0 ;
1065  for( unsigned int c = 0 ; c < classesKinDescription.size() ; ++c ){
1066  for( int t = 0 ; t < classesKinDescription[c]->mean().dimX() ; ++t )
1067  if( classesKinDescription[c]->mean()(t) > classesKinDescription[c]->mean()(maxInd) )
1068  maxInd = t ;
1069  momentOfMax[c] = maxInd ;
1070  }
1071  AimsData<double> valueAtMaxMoment( classesKinDescription.size(), classesKinDescription.size() ) ;
1072  int c1, c2 ;
1073  ForEach2d(valueAtMaxMoment, c1, c2){
1074  //valueAtMaxMoment(c1, c2) = ( classesKinDescription[c1]->mean()( momentOfMax[c2] ) / classesKinDescription[c1]->mean()( momentOfMax[c1] ) ) ;
1075  valueAtMaxMoment(c1, c2) = 1. - ( classesKinDescription[c2]->mean()( momentOfMax[c1] ) / classesKinDescription[c2]->mean()( momentOfMax[c2] ) ) ;
1076  }
1077  AimsData<double> res( valueAtMaxMoment.clone() );
1078  ForEach2d(res, c1, c2){
1079  res(c1, c2) = 0.5 * ( valueAtMaxMoment(c1, c2) + valueAtMaxMoment(c2, c1) ) ;
1080  }
1081 
1082 // aims::Writer< AimsData<double> > wri( "momentOfMaxValueRatios.ima" ) ;
1083 // wri.write(res) ;
1084 
1085  return res ;
1086 }
1087 
1088 template<typename T>
1090  const std::vector<aims::DiscriminantAnalysisElement*>& classesKinDescription,
1091  const AimsData<int>& locMap,
1092  const std::vector<short>& unfusionedLabels,
1093  const std::vector<float>& classMaximaMoment,
1094  float spatialregularizationWeight,
1095  float timeOfMaximumWeight ) :
1096  CahElementFactory<T>( classes ), /*_classMaximumMoment( new std::vector<float>( classMaximaMoment ) ), */
1097  _spatialregularizationWeight(spatialregularizationWeight),
1098  _timeOfMaximumWeight(timeOfMaximumWeight)
1099 {
1100  _interactionSurf = new AimsData<double> ;
1101  _labelsVolumes = new std::vector<double> ;
1102  interactionSurfaces( locMap, unfusionedLabels, *_interactionSurf, *_labelsVolumes ) ;
1103  _maxMomentValueRatios = new AimsData<double>( momentOfMaximumDifference( classesKinDescription /*classMaximaMoment*/ ) ) ;
1104  _antiCorrelatedMeans = new AimsData<char>( classesKinDescription.size(), classesKinDescription.size() ) ;
1105 
1106  int c1, c2 ;
1107  double nbFrames = double( classesKinDescription[0]->mean().dimX() ) ;
1108  _kinModel2by2Distances = new AimsData<double>( classesKinDescription.size(), classesKinDescription.size() ) ;
1109 // std::cout << "Centroids distance : " << std::endl ;
1110  AimsData<double> distance2by2( classesKinDescription.size(), classesKinDescription.size() ) ;
1111  double max2By2Distance = 0 ;
1112 
1113  ForEach2d( distance2by2, c1, c2 ){
1114  distance2by2( c1, c2 ) = classesKinDescription[c1]->distance( classesKinDescription[c2]->mean() ) ;
1115  if( distance2by2( c1, c2 ) > max2By2Distance )
1116  max2By2Distance = distance2by2( c1, c2 ) ;
1117  (*_antiCorrelatedMeans)(c1, c2) = !correlated( classesKinDescription[c1]->mean(), classesKinDescription[c2]->mean()) ;
1118 // std::cout << "c1 = " << c1 << " c2 = " << c2 << " == " <<distance2by2( c1, c2 ) << std::endl ;
1119  }
1120  _meanDist2By2 = _varDist2By2 = _meanSurfVolRatio = _varSurfVolRatio = _meanMaxMomentRatio = _varMaxMomentRatio = 0. ;
1121  int nbOfCases = 0 ;
1122  ForEach2d( distance2by2, c1, c2 ){
1123  (*_kinModel2by2Distances)( c1, c2 ) = 0.5 * ( distance2by2( c1, c2 ) + distance2by2( c2, c1 ) ) / nbFrames ;
1124  if( c1 > c2 )
1125  if( !(*_antiCorrelatedMeans)(c1, c2) ){
1126  ++nbOfCases ;
1127  float val = (*_kinModel2by2Distances)( c1, c2 ) ;
1128  _meanDist2By2 += val ;
1129  _varDist2By2 += val * val ;
1130 
1131  if( (*_labelsVolumes)[c1] == 0. || (*_labelsVolumes)[c2] == 0. )
1132  val = 0. ;
1133  else
1134  val = std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1135  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c2] ) ;
1136 
1137  _meanSurfVolRatio += val ;
1138  _varSurfVolRatio += val * val ;
1139 
1140  val = 0.5 * (*_maxMomentValueRatios)(c1, c2) ;
1141  _meanMaxMomentRatio += val ;
1142  _varMaxMomentRatio += val * val ;
1143 
1144  }
1145  }
1146  _meanDist2By2 /= float( nbOfCases ) ;
1147  _meanSurfVolRatio /= float( nbOfCases ) ;
1148  _meanMaxMomentRatio /= float( nbOfCases ) ;
1149 
1150  _varDist2By2 = ( _varDist2By2 / float( nbOfCases ) - _meanDist2By2 * _meanDist2By2 )
1151  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1152  _varSurfVolRatio = ( _varSurfVolRatio / float( nbOfCases ) - _meanSurfVolRatio * _meanSurfVolRatio )
1153  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1154  _varMaxMomentRatio = ( _varMaxMomentRatio / float( nbOfCases ) - _meanMaxMomentRatio * _meanMaxMomentRatio )
1155  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1156  if( _varMaxMomentRatio == 0. )
1157  _varMaxMomentRatio = 1. ;
1158 
1159  //-------------------------------------
1160  double meanCentered1 = 0., meanCentered2 = 0. ;
1161 
1162  ForEach2d( distance2by2, c1, c2 ){
1163  (*_kinModel2by2Distances)( c1, c2 ) = 0.5 * ( distance2by2( c1, c2 ) + distance2by2( c2, c1 ) ) / nbFrames ;
1164  if( c1 > c2 )
1165  if( !(*_antiCorrelatedMeans)(c1, c2) ){
1166  //std::cout << c1 << " && " << c2 << " : " << (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 << std::endl ;
1167  meanCentered1 += (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 ;
1168  float val ;
1169  if( (*_labelsVolumes)[c1] == 0. || (*_labelsVolumes)[c2] == 0. )
1170  val = 0. ;
1171  else
1172  val = std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1173  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c2] ) ;
1174 
1175  meanCentered2 += val - _meanSurfVolRatio ;
1176  }
1177  }
1178  //-------------------------------------
1179 
1180  AimsData<double> correctedDists( classesKinDescription.size(), classesKinDescription.size() ) ;
1181  AimsData<double> correctedSurfVolRatio( correctedDists.clone() ), correctedMomentOfMax( correctedDists.clone() ) ;
1182  ForEach2d( correctedDists, c1, c2 ){
1183  correctedDists(c1, c2) = ( (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 ) / sqrt( _varDist2By2 ) ;
1184  correctedSurfVolRatio(c1, c2) = -( std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1185  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c1] ) - _meanSurfVolRatio ) / sqrt( _varSurfVolRatio ) ;
1186 // correctedMomentOfMax(c1, c2) = ( 1. - std::min( (*_classMaximumMoment)[c1] / (*_classMaximumMoment)[c2],
1187 // (*_classMaximumMoment)[c2] / (*_classMaximumMoment)[c1] ) - _meanMaxMomentRatio )
1188 // / sqrt( _varMaxMomentRatio ) ;
1189  correctedMomentOfMax(c1, c2) = ( (*_maxMomentValueRatios)(c1, c2) - _meanMaxMomentRatio )
1190  / sqrt( _varMaxMomentRatio ) ;
1191  }
1192 
1193 // aims::Writer< AimsData<double> > wri1("correctedDists.ima") ;
1194 // wri1.write(correctedDists) ;
1195 
1196 // aims::Writer< AimsData<double> > wri2("correctedSurfVolRatio.ima") ;
1197 // wri2.write(correctedSurfVolRatio) ;
1198 
1199 
1200 // aims::Writer< AimsData<double> > wri3("correctedMomentOfMax.ima") ;
1201 // wri3.write(correctedMomentOfMax) ;
1202 
1203 // aims::Writer< AimsData<char> > wri4("antiCorr.ima") ;
1204 // wri4.write(*_antiCorrelatedMeans) ;
1205 
1206 // aims::Writer< AimsData<double> > wri("symDistance2by2.ima") ;
1207 // wri.write( *_kinModel2by2Distances ) ;
1208 
1209 // std::cout << "SCKBDistanceCahElementFactory<T>::SCKBDistanceCahElementFactory end" << std::endl ;
1210 }
1211 
1212 
1213 template <typename T>
1214 Element<T>*
1216 {
1217  return new SCKBDistanceElement<T>( _maxMomentValueRatios, _kinModel2by2Distances, _interactionSurf, _labelsVolumes,
1218  _spatialregularizationWeight, _timeOfMaximumWeight,
1219  _meanDist2By2, _varDist2By2, _meanSurfVolRatio, _varSurfVolRatio, _meanMaxMomentRatio, _varMaxMomentRatio,
1220  std::set<int>(), n ) ;
1221 }
1222 
1223 template <typename T>
1225  const std::vector< std::list< Point3d > >& classes,
1226  const AimsData<T>& data ) :
1227  CahElementFactory<T>(classes, data), _nbOfSignificantVps(nbOfSignificantVps) {}
1228 
1229 template <typename T>
1231 
1232 template <typename T>
1233 Element<T> *
1235 {
1236  return new PPcaElement<T>( this->_classes, this->_data, _nbOfSignificantVps, std::set<int>(), n ) ;
1237 }
1238 
1239 template <typename T>
1241  const std::vector< std::list< Point3d > >& classes,
1242  const AimsData<T>& data ) :
1243  CahElementFactory<T>(classes, data), _nbOfSignificantVps(nbOfSignificantVps) {}
1244 
1245 template <typename T>
1247 
1248 template <typename T>
1249 Element<T> *
1251 {
1252  return new PcaReprocReconsErrorElement<T>( this->_classes, this->_data, _nbOfSignificantVps, std::set<int>(), n ) ;
1253 }
1254 
1255 template <typename T>
1257  _classes( method->classes() ), _data(method->data()), _m(0), _canAddElement(true),
1258  _initDistancesAlreadyComputed(false)
1259 {
1260  if( method->classes().size() == 0 && method->methodType() == "Pca" )
1261  throw std::runtime_error("You must specify a method for ascendant hierarcical classification !") ;
1262 
1263  if( method->methodType() == "Max2By2Distance" )
1264  {
1266  dynamic_cast< const Max2By2DistanceCahElementFactory<T>* >( method ) ;
1267 
1268  if( fact->initDistances()->dimX() != fact->initDistances()->dimY() )
1269  throw std::runtime_error("Incompatible pre-computed distances between element matrix size !") ;
1270 
1271  _distanceBetweenInitialElements = fact->initDistances()->clone() ;
1272 
1273  _m = fact->initDistances()->dimX() ;
1274  _listeN = fact->creator() ;
1275  _initDistancesAlreadyComputed = true ;
1276  _canAddElement = false ;
1277  }
1278  else if( method->classes().size() )
1279  {
1280  _m = method->classes().size() ;
1281  _listeN = method->creator() ;
1282  _canAddElement = false ;
1283  }
1284 }
1285 
1287 template <typename T>
1289 {
1290  if( !_canAddElement )
1291  return false ;
1292  if( el->number() == -1 )
1293  el->setNumber( _m + 1 ) ;
1294  _listeN.push_back(el);
1295  _m++;
1296 
1297  return true ;
1298 }
1299 
1300 
1301 template <typename T>
1303 {
1304  if( _initDistancesAlreadyComputed ){
1305  for( int i = 0 ; i < _distanceBetweenInitialElements.dimX() ; ++i )
1306  for( int j = i + 1 ; j < _distanceBetweenInitialElements.dimY() ; ++j )
1307  _mMap.insert( std::pair<float, Point2d>( _distanceBetweenInitialElements(i, j),
1308  Point2d(i, j) ) ) ;
1309 // aims::Writer< AimsData<float> > wt("Distances.ima") ;
1310 // wt.write( _distanceBetweenInitialElements ) ;
1311 
1312  return ;
1313  }
1314 
1315  _distanceBetweenInitialElements = AimsData<float>(_m, _m) ;
1316 
1317  float d = -1.;
1318  typename std::list<Element<T>*>::iterator
1319  iiter(_listeN.begin()), ilast(_listeN.end()) ;
1320 
1321  int i = 0 ;
1322 
1323  while( iiter != ilast )
1324  {
1325  typename std::list<Element<T>*>::iterator
1326  jiter(iiter), jlast(_listeN.end()) ;
1327  ++jiter ;
1328  int j = i + 1 ;
1329  while( jiter != jlast )
1330  {
1331  if( (*iiter) != (*jiter) ){
1332  d = (*iiter)->distanceTo( *jiter, _classes, _data,
1333  _distanceBetweenInitialElements );
1334  _mMap.insert(std::pair<float, Point2d>
1335  (d, Point2d((*iiter)->number(),(*jiter)->number())));
1336  _distanceBetweenInitialElements( (*iiter)->number(),
1337  (*jiter)->number() ) = d ;
1338  _distanceBetweenInitialElements( (*jiter)->number(),
1339  (*iiter)->number() ) = d ;
1340  }
1341  ++jiter ; ++j ;
1342  }
1343  ++iiter ; ++i ;
1344  }
1345 // aims::Writer< AimsData<float> > wt("Distances.ima") ;
1346 // wt.write( _distanceBetweenInitialElements ) ;
1347 }
1348 
1349 template <typename T>
1350 void Cah<T>::oneStep(int etape)
1351 {
1352  std::multimap<float, Point2d>::const_iterator it = _mMap.begin();
1353  int Na = it->second[0], Nb = it->second[1];
1354  Element<T> * elA = 0, *elB = 0 ;
1355 /* int card_a, card_b; */
1356 
1357  // On rajoute la fusion dans _result
1358  _result.push_back(std::pair<Point2d, float> ( Point2d(Na, Nb),it->first));
1359  std::cout <<Na<<" "<<Nb<<" "<<(it->first) << " : " << std::endl;
1360 
1361  // Recherche de Na et Nb dans _listeN, sauvegarde et suppression...
1362  typename std::list<Element<T>*>::iterator vectiter, tmpiter;
1363  vectiter=_listeN.begin();
1364  while(vectiter!=_listeN.end())
1365  {
1366  if( ((*vectiter)->number()) == Na )
1367  {
1368  elA = (*vectiter) ;
1369  tmpiter = vectiter;
1370  ++vectiter ;
1371  _listeN.erase(tmpiter);
1372  }
1373  else
1374  {
1375  if( ((*vectiter)->number()) == Nb )
1376  {
1377  elB = (*vectiter) ;
1378  tmpiter = vectiter;
1379  ++vectiter ;
1380  _listeN.erase(tmpiter);
1381  }
1382  else
1383  {
1384  ++vectiter;
1385  }
1386  }
1387  }
1388 
1389  Element<T> *nouveau = elA->agregateTo( elB, _classes, _data,
1390  _distanceBetweenInitialElements, _m + etape ) ;
1391 
1392  std::set<int>::const_iterator cit(nouveau->constitutedFrom().begin()), clt(nouveau->constitutedFrom().end()) ;
1393  while( cit != clt ){
1394  ++cit ;
1395  }
1396  // Suppression dans _mMap de toutes les distances faisant reference a Na ou Nb
1397  std::multimap<float, Point2d>::iterator mapiter, tempiter;
1398  mapiter= _mMap.begin();
1399  while(mapiter!=_mMap.end())
1400  {
1401  if( (mapiter->second[0]==Na) || (mapiter->second[1]==Na) ||
1402  (mapiter->second[0]==Nb) || (mapiter->second[1]==Nb) )
1403  {
1404  tempiter = mapiter;
1405  ++mapiter;
1406  _mMap.erase(tempiter);
1407  }
1408  else ++mapiter;
1409  }
1410 
1411  // Calcul des distances concernant le nouvel element dans _mMap
1412  float d = -1.;
1413  typename std::list<Element<T>*>::iterator
1414  iiter(_listeN.begin()), ilast(_listeN.end()) ;
1415  while( iiter != ilast )
1416  {
1417  d = nouveau->distanceTo( *iiter, _classes, _data, _distanceBetweenInitialElements ) ;
1418  _mMap.insert(std::pair<float, Point2d>
1419  (d, Point2d((*iiter)->number(), nouveau->number())) );
1420  ++iiter ;
1421  }
1422 
1423 
1424 /* delete elA, elB ; */
1425  // On rajoute a present le nouvel element dans _listeN
1426  _listeN.push_back(nouveau);
1427 }
1428 
1429 
1430 template <typename T>
1431 const std::vector< std::pair<Point2d, float> >& Cah<T>::doit()
1432 {
1434  for (unsigned int i=1; i<_m;i++)
1435  {
1436  std::cout<<"Etape n°"<<i<<std::endl;
1437  oneStep(i);
1438  }
1439  return _result;
1440 }
1441 
1442 
1443 
1444 
1445 template <typename T>
1446 std::vector<int> Cah<T>::leaves(int nc)
1447 {
1448 // std::cerr<<"On entre dans leaves classes..."<<std::endl;
1449  std::vector<int> liste;
1450  for(unsigned int i=_m-nc;i<_m-1;i++)
1451  {
1452  if((unsigned int)_result[i].first[0]<(2*_m-nc+1))
1453  {
1454  liste.push_back(_result[i].first[0]);
1455  }
1456 
1457  if((unsigned int)_result[i].first[1]<(2*_m-nc+1))
1458  {
1459  liste.push_back(_result[i].first[1]);
1460  }
1461  }
1462 
1463  return liste;
1464 }
1465 
1466 
1467 template <typename T>
1468 std::vector<int> Cah<T>::leaves(float d)
1469 {
1470 // std::cerr<<"On entre dans leaves distance..."<<std::endl;
1471  std::vector<int> liste;
1472  for(int i=_m-2;(_result[i].second<d)||(i<0);i--)
1473  {
1474  if((unsigned int)_result[i].first[0]<=_m)
1475  {
1476  liste.push_back(_result[i].first[0]);
1477  }
1478  else
1479  {
1480  if(_result[_result[i].first[0]-_m-1].second<d)
1481  {
1482  liste.push_back(_result[i].first[0]);
1483  }
1484  }
1485 
1486  if((unsigned int)_result[i].first[1]<=_m)
1487  {
1488  liste.push_back(_result[i].first[1]);
1489  }
1490  else
1491  {
1492  if(_result[_result[i].first[1]-_m-1].second<d)
1493  {
1494  liste.push_back(_result[i].first[1]);
1495  }
1496  }
1497  }
1498 
1499  return liste;
1500 }
1501 
1502 
1503 // Procédure récursive de parcours d'arbre
1504 template <typename T>
1505 void Cah<T>::Proc(int N, std::vector<int>& aClass )
1506 {
1507  if(N <= int(_m) )
1508  {
1509 // std::cerr<<"On rentre N = "<<N<<" car il est plus petit que _m = "<<_m<<std::endl;
1510  aClass.push_back(N);
1511  }
1512  else
1513  {
1514  Proc(_result[N-_m-1].first[0], aClass );
1515  Proc(_result[N-_m-1].first[1], aClass );
1516  }
1517 }
1518 
1519 
1520 template <typename T>
1521 std::vector<Point2d> Cah<T>::cut( const std::vector< std::pair<Point2d, float> >& res_cah, int nc )
1522 {
1523 
1524 // std::cerr<<"On entre dans cut classes..."<<std::endl;
1525  if( res_cah.size() )
1526  _result = res_cah ;
1527 
1528  _m = _result.size() + 1;
1529  // std::cerr<<"_m vaut : "<<_m<<std::endl;
1530 
1531  std::vector<int> liste = leaves(nc);
1532 
1533  // std::cerr<<"leaves est constituée de : ";
1534 // for(unsigned i=0;i<liste.size();i++) std::cerr<<liste[i]<<" ";
1535 // std::cerr<<std::endl;
1536 
1537 // std::vector<std::vector<int> > res;
1538 
1539  std::vector<Point2d> res;
1540  std::map<int,int> t;
1541  for(unsigned int c=0;c<liste.size();c++)
1542  {
1543  std::vector<int> aClasse ;
1544 // std::cerr<<"On lance Proc avec "<<liste[c]<<std::endl;
1545  Proc(liste[c], aClasse);
1546 // res.push_back(_uneClasse);
1547  for(unsigned int i=0;i<aClasse.size();i++)
1548  {
1549  t[aClasse[i]] = (c+1);
1550  }
1551  }
1552 
1553  std::map<int,int>::iterator mapit;
1554  Point2d point;
1555  for(mapit=t.begin();mapit!=t.end();mapit++)
1556  {
1557  point[0] = mapit->first;
1558  point[1] = mapit->second;
1559  res.push_back(point);
1560  }
1561 
1562  return res;
1563 }
1564 
1565 
1566 template <typename T>
1567 std::vector<Point2d> Cah<T>::distanceCut( const std::vector< std::pair<Point2d, float> >& res_cah, float d )
1568 {
1569 /* if( res_cah.size() ) */
1570 /* _result = res_cah ; */
1571 
1572  int nc= 0 ;
1573  while( (unsigned int)(nc + 1) < res_cah.size() && res_cah[nc + 1].second < d )
1574  ++nc ;
1575 
1576  return cut( res_cah, _m + 1 - nc ) ;
1577 
1578 }
1579 
1580 template <typename T>
1581 std::vector<Point2d> Cah<T>::maxCurvatureCut( const std::vector< std::pair<Point2d, float> >& res_cah )
1582 {
1583 /* if( res_cah.size() ) */
1584 /* _result = res_cah ; */
1585 /* _result = res_cah ; */
1586 
1587  AimsData<float> derivate( res_cah.size() ) ;
1588  AimsData<float> cost( res_cah.size() ) ;
1589  AimsData<float> curvature( res_cah.size() ) ;
1590 
1591  for( unsigned int i = 1 ; i < res_cah.size() - 1 ; ++i ){
1592  cost[i] = res_cah[i].second ;
1593  derivate[i] = ( res_cah[i+1].second - res_cah[i-1].second) * 0.5 ;
1594  }
1595 
1596  int maxCurvInd = 0 ;
1597  float maxCurv = 0. ;
1598  for( unsigned int i = 1 ; i < res_cah.size() - 1 ; ++i ){
1599  curvature[i] = ( res_cah[i+1].second - 2 * res_cah[i].second + res_cah[i-1].second) * 0.25 ;
1600 
1601  if( curvature[i] > maxCurv ){
1602  maxCurv = curvature[i] ;
1603  maxCurvInd = i ;
1604  }
1605  }
1606 
1607 // aims::Writer< AimsData <float> > wri1("cost.ima") ;
1608 // wri1.write(cost) ;
1609 // aims::Writer< AimsData <float> > wri2("derivate.ima") ;
1610 // wri2.write(derivate) ;
1611 // aims::Writer< AimsData <float> > wri3("curv.ima") ;
1612 // wri3.write(curvature) ;
1613 
1614  return cut( res_cah, _m + 1 - maxCurvInd ) ;
1615 }
1616 
1617 template <typename T>
1618 void Cah<T>::save( const std::string& fileout )
1619 {
1620  std::string car = fileout + "_cah" ;
1621 
1622  std::ofstream os( car.c_str() ) ;
1623 
1624  for(unsigned i=0;i<_result.size();i++)
1625  {
1626  os << _result[i].first[0] << " " << _result[i].first[1]
1627  << " " << _result[i].second << std::endl ;
1628  }
1629 
1630  os.close();
1631 
1632  AimsData<float> derivate( _result.size() ) ;
1633  AimsData<float> cost( _result.size() ) ;
1634  AimsData<float> curvature( _result.size() ) ;
1635 
1636  for( unsigned int i = 1 ; i < _result.size() - 3 ; ++i ){
1637  cost[i] = _result[i].second ;
1638  derivate[i] = ( _result[i+1].second - _result[i-1].second) * 0.5 ;
1639  }
1640 
1641  for( unsigned int i = 1 ; i < _result.size() - 1 ; ++i )
1642  curvature[i] = ( _result[i+1].second - 2 * _result[i].second + _result[i-1].second) * 0.25 ;
1643 
1644 // aims::Writer< AimsData <float> > wri1(fileout + "_cost.ima") ;
1645 // wri1.write(cost) ;
1646 // aims::Writer< AimsData <float> > wri2(fileout + "_derivative.ima") ;
1647 // wri2.write(derivate) ;
1648 // aims::Writer< AimsData <float> > wri3(fileout + "_curvature.ima") ;
1649 // wri3.write(curvature) ;
1650 }
1651 
1652 template <typename T>
1653 const std::vector< std::pair<Point2d, float> >& Cah<T>::load( const std::string& filein )
1654 {
1655  // Lecture fichier cah
1656  _result.clear();
1657  unsigned int Na = 0, Nb = 0;
1658  float d = -1.;
1659 
1660  std::ifstream is( filein.c_str() ) ;
1661 
1662  is >> Na ;
1663  is >> Nb ;
1664  is >> d ;
1665  _result.push_back(std::pair<Point2d, float> (Point2d(Na, Nb), d) );
1666 
1667  while( !is.eof() )
1668  {
1669  is >> Na ;
1670  is >> Nb ;
1671  is >> d ;
1672 
1673  _result.push_back(std::pair<Point2d, float> (Point2d(Na, Nb), d) );
1674  }
1675 
1676  is.close();
1677  return _result ;
1678 }
bool correlated(const AimsData< T > &x, const AimsData< T > &y)
Definition: cah_d.h:977
int number() const
Definition: cah.h:36
PPcaElement(const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, int numberOfSignificantVps, const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:272
const std::vector< float > & baryValue() const
Definition: cah.h:69
float meanReconstructionError()
Definition: cah_d.h:644
PcaCahElementFactory(int nbOfSignificantVps, const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:799
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:546
const std::vector< std::list< Point3d > > & _classes
Definition: cah.h:283
const Point3df & baryPosition() const
Definition: cah.h:70
Max2By2DistanceElement(const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:485
virtual std::list< Element< T > *> creator() const
Definition: cah_d.h:836
float meanReconstructionError()
Definition: cah_d.h:156
Definition: cah.h:415
float _varReconstructionError
Definition: cah.h:116
float unreconstructedInertia()
Definition: cah_d.h:170
virtual const AimsData< float > * initDistances() const
Definition: cah.h:280
const std::vector< std::list< Point3d > > & classes() const
Definition: cah.h:276
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:494
int card() const
Definition: cah.h:39
CahElementFactory(const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:766
std::vector< float > _mean
Definition: cah.h:119
float varReconstructionError()
Definition: cah_d.h:651
float _unreconstructedInertia
Definition: cah.h:121
void save(const std::string &name)
Definition: cah_d.h:1618
virtual ~PcaCahElementFactory()
Definition: cah_d.h:805
float varReconstructionError()
Definition: cah_d.h:163
BaryElement(int card, const aims::Individuals< float > &bary, const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:21
const std::vector< std::pair< Point2d, float > > & doit()
Definition: cah_d.h:1431
PPcaCahElementFactory(int nbOfSignificantVps, const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:1224
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)=0
virtual std::string methodType() const =0
AimsData< double > * kineticSampleDistance(const std::vector< aims::DiscriminantAnalysisElement *> &classes)
Definition: cah_d.h:846
const std::vector< std::pair< Point2d, float > > & load(const std::string &name)
Definition: cah_d.h:1653
virtual ~Max2By2DistanceCahElementFactory()
Definition: cah_d.h:824
std::vector< Point2d > distanceCut(const std::vector< std::pair< Point2d, float > > &res, float)
Definition: cah_d.h:1567
virtual ~PcaReprocReconsErrorCahElementFactory()
Definition: cah_d.h:1246
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:82
aims::ProbabilisticPcaElement * ppca()
Definition: cah.h:185
BaryCahElementFactory(const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:784
float unreconstructedInertia()
Definition: cah_d.h:658
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:465
virtual ~PPcaCahElementFactory()
Definition: cah_d.h:1230
virtual ~CahElementFactory()
Definition: cah_d.h:770
AimsData< double > momentOfMaxValueRatios(const std::vector< aims::DiscriminantAnalysisElement *> &classesKinDescription)
Definition: cah_d.h:1059
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:421
AimsData< double > momentOfMaximumDifference(const std::vector< aims::DiscriminantAnalysisElement *> &classesKinDescription)
Definition: cah_d.h:1026
Definition: cah.h:25
SCKBDistanceCahElementFactory(const std::vector< std::list< Point3d > > &classes, const std::vector< aims::DiscriminantAnalysisElement *> &classesKinDescription, const AimsData< int > &locMap, const std::vector< short > &unfusionedLabels, const std::vector< float > &classMaximaMoment, float spatialregularizationWeight=1., float timeOfMaximumWeight=0.5)
Definition: cah_d.h:1089
AimsData< double > interactionSurfaces(const AimsData< int > &locMap, const std::vector< short > &unfusionedLabels, AimsData< double > &interactionSurf, std::vector< double > &labelsVolumes)
Definition: cah_d.h:871
int _numberOfSignificantVps
Definition: cah.h:122
const AimsData< float > & errorMatrix()
Definition: cah.h:145
virtual Element< T > * creator(int n) const =0
PcaReprocReconsErrorCahElementFactory(int nbOfSignificantVps, const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:1240
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:401
int _card
Definition: cah.h:54
std::set< int > _constitutedFrom
Definition: cah.h:55
const AimsData< T > & data() const
Definition: cah.h:277
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:236
Max2By2DistanceCahElementFactory(const AimsData< float > &initDistances)
Definition: cah_d.h:817
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:315
std::vector< Point2d > cut(const std::vector< std::pair< Point2d, float > > &res, int)
Definition: cah_d.h:1521
void initialisationDist()
Definition: cah_d.h:1302
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)=0
std::vector< Point2d > maxCurvatureCut(const std::vector< std::pair< Point2d, float > > &res)
Definition: cah_d.h:1581
void setNumber(int number)
Definition: cah.h:37
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)
Definition: cah_d.h:665
const std::set< int > & constitutedFrom() const
Definition: cah.h:42
bool addElement(Element< T > *el)
Returns false if el ca not be added.
Definition: cah_d.h:1288
const AimsData< float > & errorMatrix()
Definition: cah.h:109
AimsData< float > _errorMatrix
Definition: cah.h:118
const AimsData< T > & _data
Definition: cah.h:284
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:748
virtual std::list< Element< T > *> creator() const
Definition: cah_d.h:775
Cah(const CahElementFactory< T > *method)
Definition: cah_d.h:1256
void oneStep(int)
Definition: cah_d.h:1350
virtual ~BaryCahElementFactory()
Definition: cah_d.h:788
PcaElement(const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, int numberOfSignificantVps, const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:137
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:253
const AimsData< float > * initDistances() const
Definition: cah.h:344
PcaReprocReconsErrorElement(const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, int numberOfSignificantVps, const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:567
Definition: cah.h:89
virtual Element< T > * agregateTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances, int newNb=-1)
Definition: cah_d.h:100
float _meanReconstructionError
Definition: cah.h:115
void computeReconstructionError(const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data)
Definition: cah_d.h:177