Xmipp  v3.23.11-Nereus
som.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Alberto Pascual Montano (pascual@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 //-----------------------------------------------------------------------------
27 // SOM.hh
28 // Implements Kohonen Self-Organizing Feature Maps
29 //-----------------------------------------------------------------------------
30 
31 #ifndef XMIPPSOM_H
32 #define XMIPPSOM_H
33 
34 #include "base_algorithm.h"
35 #include "map.h"
36 
37 //---------------------------------------------------------------------------
38 // Class Descent
39 //---------------------------------------------------------------------------
48 class Descent
49 {
50 public:
56  Descent(const double _initial = 1, const double _final = 0)
57  : initial(_initial), final(_final)
58  {}
59  ;
60 
62  virtual ~Descent() {}
63 
71  virtual double operator()(const unsigned _step, const unsigned _nSteps)
72  const;
73 
74 
79  virtual void printSelf(std::ostream& _os) const;
80 
85  virtual void readSelf(std::istream& _is);
86 
92  virtual void saveObject(std::ostream& _os) const;
93 
94 
100  virtual void loadObject(std::istream& _is);
101 
102 
108  friend std::ostream& operator << (std::ostream& _os, const Descent& _desc)
109  {
110  _desc.printSelf(_os);
111  return _os;
112  };
113 
120  {
121  _desc.readSelf(_is);
122  return _is;
123  };
124 
125 
126 protected:
127  double initial;
128  double final;
129 };
130 
131 //---------------------------------------------------------------------------
132 
136 class SOM : public ClassificationAlgorithm<ClassificationMap>
137 {
138 public:
140  typedef enum { GAUSSIAN = 0, BUBBLE = 1} neighType;
141 
150  SOM(Descent& _alpha, Descent& _radius, neighType _neighType, unsigned long _nSteps)
151  : ClassificationAlgorithm<ClassificationMap>(), somAlpha(_alpha),
152  somRadius(_radius), somNeigh(_neighType), somNSteps(_nSteps)
153  {}
154  ;
155 
160  SOM(std::istream& _is);
161 
165  virtual ~SOM()
166  {}
167 
168  SOM(const SOM &)=delete; // Remove default copy constructor because Sonarcloud complains
169 
174  void alpha(Descent _alpha);
175 
180  void radius(Descent _radius);
181 
186  void nSteps(const unsigned long& _nSteps);
187 
188 
194  virtual void train(ClassificationMap& _som, ClassicTrainingVectors& _ts) const;
195 
202  virtual double test(const ClassificationMap& _som, const TS& _examples) const;
203 
204 
209  virtual void clear();
210 
215  virtual void printSelf(std::ostream& _os) const;
216 
221  virtual void readSelf(std::istream& _is);
222 
228  virtual void saveObject(std::ostream& _os) const;
229 
230 
236  virtual void loadObject(std::istream& _is);
237 
243  friend std::ostream& operator << (std::ostream& _os, const SOM& _som)
244  {
245  _som.printSelf(_os);
246  return _os;
247  };
248 
255  {
256  _som.readSelf(_is);
257  return _is;
258  };
259 
260 
265  SOM& operator= (const SOM &op1)
266  {
267  std::stringstream _str;
268  op1.printSelf(_str);
269  readSelf(_str);
270  return *this;
271  }
272 
273 
274 protected:
278  unsigned long somNSteps;
279 
280 private:
281  /*
282  * Trains the SOM (never used)
283  * Parameter: _som The som to train
284  * Parameter: _ts The training set
285  */
286  virtual void train(ClassificationMap& _som, const TS& _ts) const
287  {}
288  ;
289 };
291 #endif
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:280
SOM(Descent &_alpha, Descent &_radius, neighType _neighType, unsigned long _nSteps)
Definition: som.h:150
virtual void readSelf(std::istream &_is)
Definition: som.cpp:290
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
virtual void printSelf(std::ostream &_os) const
Definition: som.cpp:197
neighType
Type of neighborhood function.
Definition: som.h:140
virtual ~SOM()
Definition: som.h:165
neighType somNeigh
radius(t)
Definition: som.h:277
Definition: som.h:48
double final
Definition: som.h:128
virtual void loadObject(std::istream &_is)
Definition: som.cpp:325
virtual ~Descent()
Definition: som.h:62
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209
Definition: som.h:136
virtual double operator()(const unsigned _step, const unsigned _nSteps) const
Definition: som.cpp:266
Descent(const double _initial=1, const double _final=0)
Definition: som.h:56
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
Descent somAlpha
Definition: som.h:275
virtual void saveObject(std::ostream &_os) const
Definition: som.cpp:314
friend std::istream & operator>>(std::istream &_is, Descent &_desc)
Definition: som.h:119
double initial
Definition: som.h:123
#define GAUSSIAN
Descent somRadius
alpha(t)
Definition: som.h:276
friend std::ostream & operator<<(std::ostream &_os, const Descent &_desc)
Definition: som.h:108