A.I.M.S algorithms


softDecSimilarityCompAnalysis.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 
35 
36 #ifndef SOFT_DECISION_SIMILAR_COMPONENT_H
37 #define SOFT_DECISION_SIMILAR_COMPONENT_H
38 
39 #include <aims/data/data.h>
40 #include <vector>
41 #include <string.h>
42 
43 
45 {
46 public:
47  SoftDecisionSimilarComponent( int nbClasses, int nbVar ) ;
49 
50  void init( ) ;
51 
52  double doIt( const AimsData<float>& indivMatrix ) ;
53 
54  const AimsData<double>& getRnk() const
55  {
56  return _Rnk ;
57  }
58 
59  std::vector<short> getSegmentationResult() const
60  {
61  if( (!_isInit) || _An.size() == 0 )
62  throw std::runtime_error( "Should doIt before getting result !" ) ;
63 
64  std::vector<short> segRes( _nbInd ) ;
65 
66  for( int ind = 0 ; ind < _nbInd ; ++ind ){
67  if( _valids[ind ] ){
68  short bestClass = 0 ;
69  for( int k = 1 ; k < _nbClasses ; ++k ){
70  if( _Rnk(ind, k) > _Rnk(ind, bestClass) )
71  bestClass = k ;
72  }
73 
74  segRes[ind] = bestClass ;
75  } else
76  segRes[ind] = -1 ;
77  }
78 
79  return segRes ;
80  }
81 
82 private:
83  int _nbClasses ;
84  int _nbVar ;
85  int _nbInd ;
86  bool _isInit ;
87  int _corrNbInd ;
88 
89  std::vector<short> _labels ;
90  std::vector<bool> _valids ;
91 
92  double lnLikelyhood( const AimsData<float>& indivMatrix ) ;
93  void expectationStep( const AimsData<float>& indivMatrix ) ;
94  void maximisationStep( const AimsData<float>& indivMatrix ) ;
95 
96  bool stopCriterion( double threshold ) ;
97 
98  inline double similarity( const AimsData<float>& indivMatrix, int ind, int k ) ;
99  inline double projection( const AimsData<float>& indivMatrix, int ind, int k,
100  const AimsData<double>& newek ) ;
101  std::vector<double> _pk ; // class weight
102  std::vector< AimsData<double> > _ek ; // class mean normalized vect
103  std::vector<double> _An ;
104  std::vector<double> _newpk ; // class weight
105  std::vector< AimsData<double> > _newek ; // class mean normalized vect
106  std::vector<double> _newAn ;
107 
108  AimsData<double> _Rnk ; // posterior proba of classes regarding to data
109 } ;
110 
111 double
112 SoftDecisionSimilarComponent::similarity( const AimsData<float>& indivMatrix, int ind, int k )
113 {
114  double norm = 0., sim = 0. ;
115  for( int t = 0 ; t < _nbVar ; ++t ){
116  norm += indivMatrix( ind, t ) * indivMatrix( ind, t ) ;
117  sim += indivMatrix( ind, t ) * _ek[k](t) ;
118  }
119  if( norm <= 0. )
120  return 0. ;
121  return sim / sqrt(norm) ;
122 }
123 
124 double
125 SoftDecisionSimilarComponent::projection( const AimsData<float>& indivMatrix,
126  int ind, int /* k */,
127  const AimsData<double>& newek )
128 {
129  double proj = 0. ;
130  for( int t = 0 ; t < _nbVar ; ++t ){
131  proj += indivMatrix( ind, t ) * newek(t) ;
132  //std::cout << "proj : " << proj << " with indMat = " << indivMatrix( ind, t ) << " && newek[k](t) = " << newek(t) << std::endl ;
133  }
134 
135  return proj ;
136 }
137 
138 
139 #endif
SoftDecisionSimilarComponent(int nbClasses, int nbVar)
const AimsData< double > & getRnk() const
double doIt(const AimsData< float > &indivMatrix)
std::vector< short > getSegmentationResult() const
AIMSDATA_API float norm(const Tensor &thing)