aimsalgo 6.0.0
Neuroimaging image processing
iterativeclassification_d.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#ifndef ITERATIVECLASSIFICATION_D_H
36#define ITERATIVECLASSIFICATION_D_H
37
39#include <aims/vector/vector.h>
40#include <iostream>
41#include <vector>
42#include <list>
43
44
45template <class T>
47 int nbOfClasses, int maxNbOfRuns,
48 double threshold, bool classified,
49 const ClassifStrategy<T>& strategy ):
50 myClasses(0), myNbOfClasses( nbOfClasses ), myMaxNbOfRuns( maxNbOfRuns ), myThreshold( threshold ),
51 myClassified( classified ), myClassifStrategy(0)
52{
53 myClasses = *classes ;
54 myClassifStrategy = strategy.clone() ;
55}
56
57
58template <class T>
60{
61 delete myClassifStrategy ;
62}
63
64
65template <class T>
67 const ClassifStrategy<T>& strategy, bool /*keepPreviousResult*/ )
68{
69 delete myClassifStrategy ;
70 myClassifStrategy = strategy.clone() ;
71}
72
73
74template <class T>
75const std::vector< std::list< aims::Individuals<T> > >&
77{
78 if( !myClassified )
80 return myClasses ;
81}
82
83
84template<class T>
85void aims::IterativeClassification<T>::initialization( std::vector< std::list< Individuals<T> > >& classes,
86 int nbOfClasses )
87{
88 std::cout << "Initialisation..." ;
89 std::string init_type ;
90
91 // CAS 1 : VECTEURS CODES DONNES
92 if( myClassifStrategy->isCodeVectorsGiven() ){
93 init_type = "CodeVectorsGiven" ;
94 for( int c = 1 ; c <= nbOfClasses ; ++c ){
95 if( classes[c].size() != 0 ){
96 typename std::list< aims::Individuals<T> >::iterator iter( classes[c].begin() ),
97 last( classes[c].end() ) ;
98 while( iter != last ){
99 classes[0].push_back( *iter ) ;
100 ++iter ;
101 }
102 classes[c].clear() ;
103 }
104 }
105 }
106 else{
107 bool classesGiven = true, individualsGiven = true ;
108 for( int i = 1 ; i <= myNbOfClasses ; ++i ){
109 if( classes[i].size() == 0 )
110 classesGiven = false ;
111 else individualsGiven = false ;
112 }
113 // CAS 2 : CLASSES INITIALES (INDIVIDUS DANS LES CLASSES 1 à nbOfClasses)
114 if( classesGiven == true )
115 init_type = "InitialClasses" ;
116 // CAS 3 : PAS DE CLASSES (TOUS LES INDIVIDUS DANS LA CLASSE 0)
117 else if( classes[0].size() != 0 && individualsGiven == true )
118 init_type = "NoInitialClasses" ;
119 // CAS OU IL Y A DES CLASSES INITIALES MAIS 1 OU PLUSIEURS CLASSES SONT VIDES
120 else{
121 init_type = "bad" ;
122 for( int c = 1 ; c <= nbOfClasses ; ++c ){
123 if( classes[c].size() != 0 ){
124 typename std::list< aims::Individuals<T> >::iterator iter( classes[c].begin() ),
125 last( classes[c].end() ) ;
126 while( iter != last ){
127 classes[0].push_back( *iter ) ;
128 ++iter ;
129 }
130 classes[c].clear() ;
131 }
132 }
133 }
134 }
135
136 myClassifStrategy->init( init_type, nbOfClasses, classes ) ;
137}
138
139
140template <class T>
142{
143 bool valid = true, newRun = true ;
144 int nbOfIterations ;
145 double old_inertia = 0., new_inertia = 0., min_inertia = 0. ;
146 int maxNbOfIterations = myClassifStrategy->getMaxNbOfIterations() ;
147 int maxNbOfRuns = myMaxNbOfRuns ;
148 double threshold = myThreshold ;
149
150 // test classification valide
151 valid = myClassifStrategy->isValidStrategy() ;
152 if ( valid == false ){
153 std::cout << "Non valide: impossible de faire la classification." ;
154 // break ;
155 }
156
157 std::vector< std::list< aims::Individuals<T> > > initialClasses( myClasses ) ;
158 std::vector< std::list< aims::Individuals<T> > > bestClasses( myNbOfClasses + 1 ) ;
159
160 // test: si vecteurs codes donnés OU classes initiales données -> imposer 1 SEUL RUN
161 bool classesGiven = true ;
162 for( int i = 1 ; i <= myNbOfClasses ; ++i )
163 if( initialClasses[i].size() == 0 )
164 classesGiven = false ;
165 if( myClassifStrategy->isCodeVectorsGiven() || classesGiven == true )
166 maxNbOfRuns = 1 ;
167
168 // boucle sur les runs, éventuellement, initialisation des variables
169 for( int r = 1 ; r <= maxNbOfRuns ; ++r ){
170 std::cout << "Run numero " << r << std::endl ;
171 newRun = true ;
172 nbOfIterations = 0 ;
173 new_inertia = 0. ;
174 old_inertia = 0. ;
175 initialization( initialClasses, myNbOfClasses ) ;
176
177 // boucle sur les itérations
178 do{
179 if( new_inertia != 0. )
180 old_inertia = new_inertia ;
181 if( newRun ){ // a chaque nouveau run, repartir des classes initiales
182 for( int c = 1 ; c <= myNbOfClasses ; ++c ){
183 if( myClasses[c].size() != 0 ){
184 typename std::list< aims::Individuals<T> >::iterator iter( myClasses[c].begin() ),
185 last( myClasses[c].end() ) ;
186 while( iter != last ){
187 myClasses[0].push_back( *iter ) ;
188 ++iter ;
189 }
190 myClasses[c].clear() ;
191 }
192 }
193 myClassifStrategy->init( "Random", myNbOfClasses, myClasses ) ;
194
195
196 }
197 //initialization( myClasses, myNbOfClasses ) ;myNbOfClasses
198 //myClasses = initialClasses ;
199 new_inertia = myClassifStrategy->iterate( nbOfIterations, myClasses ) ;
200 newRun = false ;
201 std::cout << "Inertie = " << new_inertia << " - Critere = " << (old_inertia - new_inertia ) / new_inertia << std::endl << std::endl ;
202 }
203 while( ((( old_inertia - new_inertia ) / new_inertia ) > threshold ||
204 old_inertia == 0. || new_inertia > old_inertia )
205 && nbOfIterations < maxNbOfIterations ) ;
206
207 if( nbOfIterations == maxNbOfIterations ){
208 valid = false ;
209 std::cout << "Nb max d'iterations atteint!" << std::endl << std::endl ;
210 }
211 if( ( new_inertia < min_inertia || min_inertia == 0. )
212 && nbOfIterations < maxNbOfIterations ){
213 bestClasses = myClasses ; // conserver les classes du meilleur run
214 min_inertia = new_inertia ; // conserver l'inertie minimale
215 std::cout << "Meilleur run n° " << r << std::endl << std::endl ;
216 }
217 }
218 std::cout << "done !" << std::endl ;
219 if( min_inertia == 0. )
220 std::cout << "Classification impossible" << std::endl << std::endl ;
221 myClasses = bestClasses ; // conserver la meilleure classification
222
223 std::cout << "classes nb " << myClasses.size() << std::endl ;
224
225 return valid ;
226}
227
228
229#endif
virtual ClassifStrategy< T > * clone() const =0
void setClassifStrategy(const ClassifStrategy< T > &strategy, bool keepPreviousResult=false)
void initialization(std::vector< std::list< Individuals< T > > > &classes, int nbOfClasses)
IterativeClassification(std::vector< std::list< Individuals< T > > > *classes, int nbOfClasses, int maxNbOfRuns, double threshold, bool classified, const ClassifStrategy< T > &strategy)
const std::vector< std::list< Individuals< T > > > & getClasses()