bioprocessing  5.1.2
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 {
180  _meanReconstructionError = 0. ;
181  _varReconstructionError = 0. ;
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<T>( 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()) -
222  _meanReconstructionError * _meanReconstructionError ;
223  if( this->_constitutedFrom.size() == 1 )
224  _errorMatrix = errorMatrix ;
225 
226  //_unreconstructedInertia = pca.noiseInertia() ;
227  } else {
228  _unreconstructedInertia = 1. ;
229  _meanReconstructionError = 1. ;
230  _varReconstructionError = 1. ;
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<T>(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>
485 Max2By2DistanceElement<T>::Max2By2DistanceElement( const std::set<int>& constitutedFrom, int number ) :
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<T>( 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().getSizeX() ) ;
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().getSizeX() ; ++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().getSizeX() * 0.5 * classesKinDescription[0]->mean().getSizeX() ) ) ;
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().getSizeX() ; ++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>
1089 SCKBDistanceCahElementFactory<T>::SCKBDistanceCahElementFactory( const std::vector<std::list<Point3d> >& classes,
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().getSizeX() ) ;
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<double>(
1118  classesKinDescription[c1]->mean(), classesKinDescription[c2]->mean()) ;
1119 // std::cout << "c1 = " << c1 << " c2 = " << c2 << " == " <<distance2by2( c1, c2 ) << std::endl ;
1120  }
1121  _meanDist2By2 = _varDist2By2 = _meanSurfVolRatio = _varSurfVolRatio = _meanMaxMomentRatio = _varMaxMomentRatio = 0. ;
1122  int nbOfCases = 0 ;
1123  ForEach2d( distance2by2, c1, c2 ){
1124  (*_kinModel2by2Distances)( c1, c2 ) = 0.5 * ( distance2by2( c1, c2 ) + distance2by2( c2, c1 ) ) / nbFrames ;
1125  if( c1 > c2 )
1126  if( !(*_antiCorrelatedMeans)(c1, c2) ){
1127  ++nbOfCases ;
1128  float val = (*_kinModel2by2Distances)( c1, c2 ) ;
1129  _meanDist2By2 += val ;
1130  _varDist2By2 += val * val ;
1131 
1132  if( (*_labelsVolumes)[c1] == 0. || (*_labelsVolumes)[c2] == 0. )
1133  val = 0. ;
1134  else
1135  val = std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1136  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c2] ) ;
1137 
1138  _meanSurfVolRatio += val ;
1139  _varSurfVolRatio += val * val ;
1140 
1141  val = 0.5 * (*_maxMomentValueRatios)(c1, c2) ;
1142  _meanMaxMomentRatio += val ;
1143  _varMaxMomentRatio += val * val ;
1144 
1145  }
1146  }
1147  _meanDist2By2 /= float( nbOfCases ) ;
1148  _meanSurfVolRatio /= float( nbOfCases ) ;
1149  _meanMaxMomentRatio /= float( nbOfCases ) ;
1150 
1151  _varDist2By2 = ( _varDist2By2 / float( nbOfCases ) - _meanDist2By2 * _meanDist2By2 )
1152  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1153  _varSurfVolRatio = ( _varSurfVolRatio / float( nbOfCases ) - _meanSurfVolRatio * _meanSurfVolRatio )
1154  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1155  _varMaxMomentRatio = ( _varMaxMomentRatio / float( nbOfCases ) - _meanMaxMomentRatio * _meanMaxMomentRatio )
1156  / float( nbOfCases - 1 ) * float( nbOfCases ) ;
1157  if( _varMaxMomentRatio == 0. )
1158  _varMaxMomentRatio = 1. ;
1159 
1160  //-------------------------------------
1161  double meanCentered1 = 0., meanCentered2 = 0. ;
1162 
1163  ForEach2d( distance2by2, c1, c2 ){
1164  (*_kinModel2by2Distances)( c1, c2 ) = 0.5 * ( distance2by2( c1, c2 ) + distance2by2( c2, c1 ) ) / nbFrames ;
1165  if( c1 > c2 )
1166  if( !(*_antiCorrelatedMeans)(c1, c2) ){
1167  //std::cout << c1 << " && " << c2 << " : " << (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 << std::endl ;
1168  meanCentered1 += (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 ;
1169  float val ;
1170  if( (*_labelsVolumes)[c1] == 0. || (*_labelsVolumes)[c2] == 0. )
1171  val = 0. ;
1172  else
1173  val = std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1174  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c2] ) ;
1175 
1176  meanCentered2 += val - _meanSurfVolRatio ;
1177  }
1178  }
1179  //-------------------------------------
1180 
1181  AimsData<double> correctedDists( classesKinDescription.size(), classesKinDescription.size() ) ;
1182  AimsData<double> correctedSurfVolRatio( correctedDists.clone() ), correctedMomentOfMax( correctedDists.clone() ) ;
1183  ForEach2d( correctedDists, c1, c2 ){
1184  correctedDists(c1, c2) = ( (*_kinModel2by2Distances)( c1, c2 ) - _meanDist2By2 ) / sqrt( _varDist2By2 ) ;
1185  correctedSurfVolRatio(c1, c2) = -( std::max( (*_interactionSurf)(c1, c2) / (*_labelsVolumes)[c1],
1186  (*_interactionSurf)(c2, c1) / (*_labelsVolumes)[c1] ) - _meanSurfVolRatio ) / sqrt( _varSurfVolRatio ) ;
1187 // correctedMomentOfMax(c1, c2) = ( 1. - std::min( (*_classMaximumMoment)[c1] / (*_classMaximumMoment)[c2],
1188 // (*_classMaximumMoment)[c2] / (*_classMaximumMoment)[c1] ) - _meanMaxMomentRatio )
1189 // / sqrt( _varMaxMomentRatio ) ;
1190  correctedMomentOfMax(c1, c2) = ( (*_maxMomentValueRatios)(c1, c2) - _meanMaxMomentRatio )
1191  / sqrt( _varMaxMomentRatio ) ;
1192  }
1193 
1194 // aims::Writer< AimsData<double> > wri1("correctedDists.ima") ;
1195 // wri1.write(correctedDists) ;
1196 
1197 // aims::Writer< AimsData<double> > wri2("correctedSurfVolRatio.ima") ;
1198 // wri2.write(correctedSurfVolRatio) ;
1199 
1200 
1201 // aims::Writer< AimsData<double> > wri3("correctedMomentOfMax.ima") ;
1202 // wri3.write(correctedMomentOfMax) ;
1203 
1204 // aims::Writer< AimsData<char> > wri4("antiCorr.ima") ;
1205 // wri4.write(*_antiCorrelatedMeans) ;
1206 
1207 // aims::Writer< AimsData<double> > wri("symDistance2by2.ima") ;
1208 // wri.write( *_kinModel2by2Distances ) ;
1209 
1210 // std::cout << "SCKBDistanceCahElementFactory<T>::SCKBDistanceCahElementFactory end" << std::endl ;
1211 }
1212 
1213 
1214 template <typename T>
1215 Element<T>*
1217 {
1218  return new SCKBDistanceElement<T>( _maxMomentValueRatios, _kinModel2by2Distances, _interactionSurf, _labelsVolumes,
1219  _spatialregularizationWeight, _timeOfMaximumWeight,
1220  _meanDist2By2, _varDist2By2, _meanSurfVolRatio, _varSurfVolRatio, _meanMaxMomentRatio, _varMaxMomentRatio,
1221  std::set<int>(), n ) ;
1222 }
1223 
1224 template <typename T>
1226  const std::vector< std::list< Point3d > >& classes,
1227  const AimsData<T>& data ) :
1228  CahElementFactory<T>(classes, data), _nbOfSignificantVps(nbOfSignificantVps) {}
1229 
1230 template <typename T>
1232 
1233 template <typename T>
1234 Element<T> *
1236 {
1237  return new PPcaElement<T>( this->_classes, this->_data, _nbOfSignificantVps, std::set<int>(), n ) ;
1238 }
1239 
1240 template <typename T>
1242  const std::vector< std::list< Point3d > >& classes,
1243  const AimsData<T>& data ) :
1244  CahElementFactory<T>(classes, data), _nbOfSignificantVps(nbOfSignificantVps) {}
1245 
1246 template <typename T>
1248 
1249 template <typename T>
1250 Element<T> *
1252 {
1253  return new PcaReprocReconsErrorElement<T>( this->_classes, this->_data, _nbOfSignificantVps, std::set<int>(), n ) ;
1254 }
1255 
1256 template <typename T>
1258  _classes( method->classes() ), _data(method->data()), _m(0), _canAddElement(true),
1259  _initDistancesAlreadyComputed(false)
1260 {
1261  if( method->classes().size() == 0 && method->methodType() == "Pca" )
1262  throw std::runtime_error("You must specify a method for ascendant hierarcical classification !") ;
1263 
1264  if( method->methodType() == "Max2By2Distance" )
1265  {
1267  dynamic_cast< const Max2By2DistanceCahElementFactory<T>* >( method ) ;
1268 
1269  if( fact->initDistances()->dimX() != fact->initDistances()->dimY() )
1270  throw std::runtime_error("Incompatible pre-computed distances between element matrix size !") ;
1271 
1272  _distanceBetweenInitialElements = fact->initDistances()->clone() ;
1273 
1274  _m = fact->initDistances()->dimX() ;
1275  _listeN = fact->creator() ;
1276  _initDistancesAlreadyComputed = true ;
1277  _canAddElement = false ;
1278  }
1279  else if( method->classes().size() )
1280  {
1281  _m = method->classes().size() ;
1282  _listeN = method->creator() ;
1283  _canAddElement = false ;
1284  }
1285 }
1286 
1288 template <typename T>
1290 {
1291  if( !_canAddElement )
1292  return false ;
1293  if( el->number() == -1 )
1294  el->setNumber( _m + 1 ) ;
1295  _listeN.push_back(el);
1296  _m++;
1297 
1298  return true ;
1299 }
1300 
1301 
1302 template <typename T>
1304 {
1305  if( _initDistancesAlreadyComputed ){
1306  for( int i = 0 ; i < _distanceBetweenInitialElements.dimX() ; ++i )
1307  for( int j = i + 1 ; j < _distanceBetweenInitialElements.dimY() ; ++j )
1308  _mMap.insert( std::pair<float, Point2d>( _distanceBetweenInitialElements(i, j),
1309  Point2d(i, j) ) ) ;
1310 // aims::Writer< AimsData<float> > wt("Distances.ima") ;
1311 // wt.write( _distanceBetweenInitialElements ) ;
1312 
1313  return ;
1314  }
1315 
1316  _distanceBetweenInitialElements = AimsData<float>(_m, _m) ;
1317 
1318  float d = -1.;
1319  typename std::list<Element<T>*>::iterator
1320  iiter(_listeN.begin()), ilast(_listeN.end()) ;
1321 
1322  int i = 0 ;
1323 
1324  while( iiter != ilast )
1325  {
1326  typename std::list<Element<T>*>::iterator
1327  jiter(iiter), jlast(_listeN.end()) ;
1328  ++jiter ;
1329  int j = i + 1 ;
1330  while( jiter != jlast )
1331  {
1332  if( (*iiter) != (*jiter) ){
1333  d = (*iiter)->distanceTo( *jiter, _classes, _data,
1334  _distanceBetweenInitialElements );
1335  _mMap.insert(std::pair<float, Point2d>
1336  (d, Point2d((*iiter)->number(),(*jiter)->number())));
1337  _distanceBetweenInitialElements( (*iiter)->number(),
1338  (*jiter)->number() ) = d ;
1339  _distanceBetweenInitialElements( (*jiter)->number(),
1340  (*iiter)->number() ) = d ;
1341  }
1342  ++jiter ; ++j ;
1343  }
1344  ++iiter ; ++i ;
1345  }
1346 // aims::Writer< AimsData<float> > wt("Distances.ima") ;
1347 // wt.write( _distanceBetweenInitialElements ) ;
1348 }
1349 
1350 template <typename T>
1351 void Cah<T>::oneStep(int etape)
1352 {
1353  std::multimap<float, Point2d>::const_iterator it = _mMap.begin();
1354  int Na = it->second[0], Nb = it->second[1];
1355  Element<T> * elA = 0, *elB = 0 ;
1356 /* int card_a, card_b; */
1357 
1358  // On rajoute la fusion dans _result
1359  _result.push_back(std::pair<Point2d, float> ( Point2d(Na, Nb),it->first));
1360  std::cout <<Na<<" "<<Nb<<" "<<(it->first) << " : " << std::endl;
1361 
1362  // Recherche de Na et Nb dans _listeN, sauvegarde et suppression...
1363  typename std::list<Element<T>*>::iterator vectiter, tmpiter;
1364  vectiter=_listeN.begin();
1365  while(vectiter!=_listeN.end())
1366  {
1367  if( ((*vectiter)->number()) == Na )
1368  {
1369  elA = (*vectiter) ;
1370  tmpiter = vectiter;
1371  ++vectiter ;
1372  _listeN.erase(tmpiter);
1373  }
1374  else
1375  {
1376  if( ((*vectiter)->number()) == Nb )
1377  {
1378  elB = (*vectiter) ;
1379  tmpiter = vectiter;
1380  ++vectiter ;
1381  _listeN.erase(tmpiter);
1382  }
1383  else
1384  {
1385  ++vectiter;
1386  }
1387  }
1388  }
1389 
1390  Element<T> *nouveau = elA->agregateTo( elB, _classes, _data,
1391  _distanceBetweenInitialElements, _m + etape ) ;
1392 
1393  std::set<int>::const_iterator cit(nouveau->constitutedFrom().begin()), clt(nouveau->constitutedFrom().end()) ;
1394  while( cit != clt ){
1395  ++cit ;
1396  }
1397  // Suppression dans _mMap de toutes les distances faisant reference a Na ou Nb
1398  std::multimap<float, Point2d>::iterator mapiter, tempiter;
1399  mapiter= _mMap.begin();
1400  while(mapiter!=_mMap.end())
1401  {
1402  if( (mapiter->second[0]==Na) || (mapiter->second[1]==Na) ||
1403  (mapiter->second[0]==Nb) || (mapiter->second[1]==Nb) )
1404  {
1405  tempiter = mapiter;
1406  ++mapiter;
1407  _mMap.erase(tempiter);
1408  }
1409  else ++mapiter;
1410  }
1411 
1412  // Calcul des distances concernant le nouvel element dans _mMap
1413  float d = -1.;
1414  typename std::list<Element<T>*>::iterator
1415  iiter(_listeN.begin()), ilast(_listeN.end()) ;
1416  while( iiter != ilast )
1417  {
1418  d = nouveau->distanceTo( *iiter, _classes, _data, _distanceBetweenInitialElements ) ;
1419  _mMap.insert(std::pair<float, Point2d>
1420  (d, Point2d((*iiter)->number(), nouveau->number())) );
1421  ++iiter ;
1422  }
1423 
1424 
1425 /* delete elA, elB ; */
1426  // On rajoute a present le nouvel element dans _listeN
1427  _listeN.push_back(nouveau);
1428 }
1429 
1430 
1431 template <typename T>
1432 const std::vector< std::pair<Point2d, float> >& Cah<T>::doit()
1433 {
1434  initialisationDist();
1435  for (unsigned int i=1; i<_m;i++)
1436  {
1437  std::cout<<"Etape n°"<<i<<std::endl;
1438  oneStep(i);
1439  }
1440  return _result;
1441 }
1442 
1443 
1444 
1445 
1446 template <typename T>
1447 std::vector<int> Cah<T>::leaves(int nc)
1448 {
1449 // std::cerr<<"On entre dans leaves classes..."<<std::endl;
1450  std::vector<int> liste;
1451  for(unsigned int i=_m-nc;i<_m-1;i++)
1452  {
1453  if((unsigned int)_result[i].first[0]<(2*_m-nc+1))
1454  {
1455  liste.push_back(_result[i].first[0]);
1456  }
1457 
1458  if((unsigned int)_result[i].first[1]<(2*_m-nc+1))
1459  {
1460  liste.push_back(_result[i].first[1]);
1461  }
1462  }
1463 
1464  return liste;
1465 }
1466 
1467 
1468 template <typename T>
1469 std::vector<int> Cah<T>::leaves(float d)
1470 {
1471 // std::cerr<<"On entre dans leaves distance..."<<std::endl;
1472  std::vector<int> liste;
1473  for(int i=_m-2;(_result[i].second<d)||(i<0);i--)
1474  {
1475  if((unsigned int)_result[i].first[0]<=_m)
1476  {
1477  liste.push_back(_result[i].first[0]);
1478  }
1479  else
1480  {
1481  if(_result[_result[i].first[0]-_m-1].second<d)
1482  {
1483  liste.push_back(_result[i].first[0]);
1484  }
1485  }
1486 
1487  if((unsigned int)_result[i].first[1]<=_m)
1488  {
1489  liste.push_back(_result[i].first[1]);
1490  }
1491  else
1492  {
1493  if(_result[_result[i].first[1]-_m-1].second<d)
1494  {
1495  liste.push_back(_result[i].first[1]);
1496  }
1497  }
1498  }
1499 
1500  return liste;
1501 }
1502 
1503 
1504 // Procédure récursive de parcours d'arbre
1505 template <typename T>
1506 void Cah<T>::Proc(int N, std::vector<int>& aClass )
1507 {
1508  if(N <= int(_m) )
1509  {
1510 // std::cerr<<"On rentre N = "<<N<<" car il est plus petit que _m = "<<_m<<std::endl;
1511  aClass.push_back(N);
1512  }
1513  else
1514  {
1515  Proc(_result[N-_m-1].first[0], aClass );
1516  Proc(_result[N-_m-1].first[1], aClass );
1517  }
1518 }
1519 
1520 
1521 template <typename T>
1522 std::vector<Point2d> Cah<T>::cut( const std::vector< std::pair<Point2d, float> >& res_cah, int nc )
1523 {
1524 
1525 // std::cerr<<"On entre dans cut classes..."<<std::endl;
1526  if( res_cah.size() )
1527  _result = res_cah ;
1528 
1529  _m = _result.size() + 1;
1530  // std::cerr<<"_m vaut : "<<_m<<std::endl;
1531 
1532  std::vector<int> liste = leaves(nc);
1533 
1534  // std::cerr<<"leaves est constituée de : ";
1535 // for(unsigned i=0;i<liste.size();i++) std::cerr<<liste[i]<<" ";
1536 // std::cerr<<std::endl;
1537 
1538 // std::vector<std::vector<int> > res;
1539 
1540  std::vector<Point2d> res;
1541  std::map<int,int> t;
1542  for(unsigned int c=0;c<liste.size();c++)
1543  {
1544  std::vector<int> aClasse ;
1545 // std::cerr<<"On lance Proc avec "<<liste[c]<<std::endl;
1546  Proc(liste[c], aClasse);
1547 // res.push_back(_uneClasse);
1548  for(unsigned int i=0;i<aClasse.size();i++)
1549  {
1550  t[aClasse[i]] = (c+1);
1551  }
1552  }
1553 
1554  std::map<int,int>::iterator mapit;
1555  Point2d point;
1556  for(mapit=t.begin();mapit!=t.end();mapit++)
1557  {
1558  point[0] = mapit->first;
1559  point[1] = mapit->second;
1560  res.push_back(point);
1561  }
1562 
1563  return res;
1564 }
1565 
1566 
1567 template <typename T>
1568 std::vector<Point2d> Cah<T>::distanceCut( const std::vector< std::pair<Point2d, float> >& res_cah, float d )
1569 {
1570 /* if( res_cah.size() ) */
1571 /* _result = res_cah ; */
1572 
1573  int nc= 0 ;
1574  while( (unsigned int)(nc + 1) < res_cah.size() && res_cah[nc + 1].second < d )
1575  ++nc ;
1576 
1577  return cut( res_cah, _m + 1 - nc ) ;
1578 
1579 }
1580 
1581 template <typename T>
1582 std::vector<Point2d> Cah<T>::maxCurvatureCut( const std::vector< std::pair<Point2d, float> >& res_cah )
1583 {
1584 /* if( res_cah.size() ) */
1585 /* _result = res_cah ; */
1586 /* _result = res_cah ; */
1587 
1588  AimsData<float> derivate( res_cah.size() ) ;
1589  AimsData<float> cost( res_cah.size() ) ;
1590  AimsData<float> curvature( res_cah.size() ) ;
1591 
1592  for( unsigned int i = 1 ; i < res_cah.size() - 1 ; ++i ){
1593  cost[i] = res_cah[i].second ;
1594  derivate[i] = ( res_cah[i+1].second - res_cah[i-1].second) * 0.5 ;
1595  }
1596 
1597  int maxCurvInd = 0 ;
1598  float maxCurv = 0. ;
1599  for( unsigned int i = 1 ; i < res_cah.size() - 1 ; ++i ){
1600  curvature[i] = ( res_cah[i+1].second - 2 * res_cah[i].second + res_cah[i-1].second) * 0.25 ;
1601 
1602  if( curvature[i] > maxCurv ){
1603  maxCurv = curvature[i] ;
1604  maxCurvInd = i ;
1605  }
1606  }
1607 
1608 // aims::Writer< AimsData <float> > wri1("cost.ima") ;
1609 // wri1.write(cost) ;
1610 // aims::Writer< AimsData <float> > wri2("derivate.ima") ;
1611 // wri2.write(derivate) ;
1612 // aims::Writer< AimsData <float> > wri3("curv.ima") ;
1613 // wri3.write(curvature) ;
1614 
1615  return cut( res_cah, _m + 1 - maxCurvInd ) ;
1616 }
1617 
1618 template <typename T>
1619 void Cah<T>::save( const std::string& fileout )
1620 {
1621  std::string car = fileout + "_cah" ;
1622 
1623  std::ofstream os( car.c_str() ) ;
1624 
1625  for(unsigned i=0;i<_result.size();i++)
1626  {
1627  os << _result[i].first[0] << " " << _result[i].first[1]
1628  << " " << _result[i].second << std::endl ;
1629  }
1630 
1631  os.close();
1632 
1633  AimsData<float> derivate( _result.size() ) ;
1634  AimsData<float> cost( _result.size() ) ;
1635  AimsData<float> curvature( _result.size() ) ;
1636 
1637  for( unsigned int i = 1 ; i < _result.size() - 3 ; ++i ){
1638  cost[i] = _result[i].second ;
1639  derivate[i] = ( _result[i+1].second - _result[i-1].second) * 0.5 ;
1640  }
1641 
1642  for( unsigned int i = 1 ; i < _result.size() - 1 ; ++i )
1643  curvature[i] = ( _result[i+1].second - 2 * _result[i].second + _result[i-1].second) * 0.25 ;
1644 
1645 // aims::Writer< AimsData <float> > wri1(fileout + "_cost.ima") ;
1646 // wri1.write(cost) ;
1647 // aims::Writer< AimsData <float> > wri2(fileout + "_derivative.ima") ;
1648 // wri2.write(derivate) ;
1649 // aims::Writer< AimsData <float> > wri3(fileout + "_curvature.ima") ;
1650 // wri3.write(curvature) ;
1651 }
1652 
1653 template <typename T>
1654 const std::vector< std::pair<Point2d, float> >& Cah<T>::load( const std::string& filein )
1655 {
1656  // Lecture fichier cah
1657  _result.clear();
1658  unsigned int Na = 0, Nb = 0;
1659  float d = -1.;
1660 
1661  std::ifstream is( filein.c_str() ) ;
1662 
1663  is >> Na ;
1664  is >> Nb ;
1665  is >> d ;
1666  _result.push_back(std::pair<Point2d, float> (Point2d(Na, Nb), d) );
1667 
1668  while( !is.eof() )
1669  {
1670  is >> Na ;
1671  is >> Nb ;
1672  is >> d ;
1673 
1674  _result.push_back(std::pair<Point2d, float> (Point2d(Na, Nb), d) );
1675  }
1676 
1677  is.close();
1678  return _result ;
1679 }
AimsData< double > momentOfMaxValueRatios(const std::vector< aims::DiscriminantAnalysisElement * > &classesKinDescription)
Definition: cah_d.h:1059
AimsData< double > * kineticSampleDistance(const std::vector< aims::DiscriminantAnalysisElement * > &classes)
Definition: cah_d.h:846
AimsData< double > momentOfMaximumDifference(const std::vector< aims::DiscriminantAnalysisElement * > &classesKinDescription)
Definition: cah_d.h:1026
bool correlated(const AimsData< T > &x, const AimsData< T > &y)
Definition: cah_d.h:977
AimsData< double > interactionSurfaces(const AimsData< int > &locMap, const std::vector< short > &unfusionedLabels, AimsData< double > &interactionSurf, std::vector< double > &labelsVolumes)
Definition: cah_d.h:871
virtual ~BaryCahElementFactory()
Definition: cah_d.h:788
BaryCahElementFactory(const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:784
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
const Point3df & baryPosition() const
Definition: cah.h:70
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
const std::vector< float > & baryValue() const
Definition: cah.h:69
BaryElement(int card, const aims::Individuals< float > &bary, const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:21
virtual std::string methodType() const =0
virtual Element< T > * creator(int n) const =0
const std::vector< std::list< Point3d > > & classes() const
Definition: cah.h:276
virtual ~CahElementFactory()
Definition: cah_d.h:770
virtual std::list< Element< T > * > creator() const
Definition: cah_d.h:775
CahElementFactory(const std::vector< std::list< Point3d > > &classes=std::vector< std::list< Point3d > >(), const AimsData< T > &data=AimsData< T >())
Definition: cah_d.h:766
Definition: cah.h:416
void save(const std::string &name)
Definition: cah_d.h:1619
void initialisationDist()
Definition: cah_d.h:1303
std::vector< Point2d > maxCurvatureCut(const std::vector< std::pair< Point2d, float > > &res)
Definition: cah_d.h:1582
const std::vector< std::pair< Point2d, float > > & doit()
Definition: cah_d.h:1432
void oneStep(int)
Definition: cah_d.h:1351
const std::vector< std::pair< Point2d, float > > & load(const std::string &name)
Definition: cah_d.h:1654
std::vector< Point2d > distanceCut(const std::vector< std::pair< Point2d, float > > &res, float)
Definition: cah_d.h:1568
std::vector< Point2d > cut(const std::vector< std::pair< Point2d, float > > &res, int)
Definition: cah_d.h:1522
bool addElement(Element< T > *el)
Returns false if el ca not be added.
Definition: cah_d.h:1289
Cah(const CahElementFactory< T > *method)
Definition: cah_d.h:1257
Definition: cah.h:25
int number() const
Definition: cah.h:36
int _card
Definition: cah.h:54
void setNumber(int number)
Definition: cah.h:37
const std::set< int > & constitutedFrom() const
Definition: cah.h:42
int card() const
Definition: cah.h:39
virtual float distanceTo(Element< T > *other, const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data, const AimsData< float > &initDistances)=0
std::set< int > _constitutedFrom
Definition: cah.h:55
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
Max2By2DistanceCahElementFactory(const AimsData< float > &initDistances)
Definition: cah_d.h:817
virtual ~Max2By2DistanceCahElementFactory()
Definition: cah_d.h:824
virtual std::list< Element< T > * > creator() const
Definition: cah_d.h:836
const AimsData< float > * initDistances() const
Definition: cah.h:344
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
Max2By2DistanceElement(const std::set< int > &constitutedFrom=std::set< int >(), int number=-1)
Definition: cah_d.h:485
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
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:1225
virtual ~PPcaCahElementFactory()
Definition: cah_d.h:1231
aims::ProbabilisticPcaElement * ppca()
Definition: cah.h:185
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
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
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
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 ~PcaCahElementFactory()
Definition: cah_d.h:805
Definition: cah.h:89
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
float unreconstructedInertia()
Definition: cah_d.h:170
float meanReconstructionError()
Definition: cah_d.h:156
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
float varReconstructionError()
Definition: cah_d.h:163
void computeReconstructionError(const std::vector< std::list< Point3d > > &classes, const AimsData< T > &data)
Definition: cah_d.h:177
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:1241
virtual ~PcaReprocReconsErrorCahElementFactory()
Definition: cah_d.h:1247
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
float unreconstructedInertia()
Definition: cah_d.h:658
float varReconstructionError()
Definition: cah_d.h:651
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
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
const AimsData< float > & errorMatrix()
Definition: cah.h:145
float meanReconstructionError()
Definition: cah_d.h:644
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
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 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