Xmipp  v3.23.11-Nereus
map.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 // ClassificationMap.hh
28 // Implements a Map of the type used by Kohonen algorithms.
29 //-----------------------------------------------------------------------------
30 
31 #ifndef XMIPPMAP_H
32 #define XMIPPMAP_H
33 
34 // STL includes
35 #include <algorithm>
36 #include <vector>
37 #include <sstream>
38 
39 // xmipp includes
40 #include "code_book.h"
41 #include "fuzzy_code_book.h"
42 #include "vector_ops.h"
43 
45 typedef std::pair<long, long> SomPos;
46 
47 // Forward declarations
48 
49 class Layout;
50 
58 {
59 public:
60 
61 
62  //---------------------------------------------------------------------------
63 
67  explicit ClassificationMap(const ClassificationMap &op1);
68 
76  ClassificationMap(const std::string& _layout, unsigned _width,
77  const unsigned& _height, const unsigned& _size);
78 
79 
89  ClassificationMap(const std::string& _layout, unsigned _width,
90  const unsigned& _height, const unsigned& _size, const double& _lower,
91  const double& _upper);
92 
93 
103  ClassificationMap(const std::string& _layout, unsigned _width,
104  const unsigned& _height, const ClassicTrainingVectors& _ts,
105  const bool _use_rand_cvs = false);
106 
113  ClassificationMap(std::istream& _is, bool _cv = true);
114 
119  {};
120 
126  virtual void add(const FeatureVector& _v, const Label& _l = Label());
127 
131  const std::string& layout() const;
132 
138  std::vector<unsigned> neighborhood(const SomPos& _center, double _radius) const;
139 
145  double neighDist(const SomPos& _center, const SomPos& _v) const;
146 
147 
151  unsigned width() const;
152 
156  unsigned height() const;
157 
158 #ifdef UNUSED // detected as unused 29.6.2018
159 
164  SomIn& itemAtPos(const SomPos& _pos);
165 #endif
166 
172  const SomIn& itemAtPos(const SomPos& _pos) const;
173 
174 #ifdef UNUSED // detected as unused 29.6.2018
175 
179  Label& targetAtPos(const SomPos& _pos);
180 #endif
181 
186  const Label& targetAtPos(const SomPos& _pos) const;
187 
188 
192  void clear();
193 
199  SomPos indexToPos(const unsigned& _i) const;
200 
201 
208  unsigned PosToIndex(const SomPos& _pos) const;
209 
214  SomPos codVecPos(SomIn& _v);
215 
216 #ifdef UNUSED // detected as unused 29.6.2018
217 
222  SomPos applyPos(const SomIn& _in);
223 #endif
224 
225  /*
226  * Standard output for a som
227  * Parameter: _os The output stream
228  * Parameter: _som The som to be printed
229  */
230  friend std::ostream& operator << (std::ostream& _os, const ClassificationMap& _som)
231  {
232  _som.printSelf(_os);
233  return _os;
234  };
235 
236  /*
237  * Standard input for a som
238  * Parameter: _is The input stream
239  * Parameter: _som The code book to be read
240  * @exception runtime_error If there are problems with the stream
241  */
243  {
244  _som.readSelf(_is);
245  return _is;
246  };
247 
253 
258  virtual void printSelf(std::ostream& _os) const;
259 
260 
265  virtual void readSelf(std::istream& _is);
266 
272  virtual void saveObject(std::ostream& _os) const;
273 
274 
280  virtual void loadObject(std::istream& _is);
281 
282 
286  virtual const Layout& getLayout() const
287  {
288  return (Layout&) *somLayout;
289  };
290 
291 
292 protected:
293 
294  const Layout* somLayout; // type of layout
295  unsigned somWidth; // SOM's width
296  unsigned somHeight; // SOM's height
297 
298 };
300 
301 
308 class FuzzyMap : public FuzzyCodeBook
309 {
310 public:
311 
312 
313  //---------------------------------------------------------------------------
314 
324  FuzzyMap(const std::string& _layout, unsigned _width,
325  const unsigned& _height, const unsigned& _size, const double& _lower,
326  const double& _upper);
327 
328 
338  FuzzyMap(const std::string& _layout, unsigned _width,
339  const unsigned& _height, const ClassicTrainingVectors& _ts,
340  const bool _use_rand_cvs = false);
341 
349  FuzzyMap(std::istream& _is, const unsigned _size = 0, bool _cv = true);
350 
351  explicit FuzzyMap(const FuzzyMap& other);
352 
356  virtual ~FuzzyMap()
357  {};
358 
364  virtual void add(const FeatureVector& _v, const Label& _l = Label());
365 
369  const std::string& layout() const;
370 
376  std::vector<unsigned> neighborhood(const SomPos& _center, double _radius) const;
377 
383  void neighborhood(const SomPos& _center, double _radius, std::vector<unsigned>& _neig) const;
384 
385 
392  void localAve(const SomPos& _center, std::vector<double>& _aveVector) const;
393 
394 
400  double neighDist(const SomPos& _center, const SomPos& _v) const;
401 
402 
406  unsigned width() const;
407 
411  unsigned height() const;
412 
418  SomIn& itemAtPos(const SomPos& _pos);
419 
425  const SomIn& itemAtPos(const SomPos& _pos) const;
426 
427 
432  Label& targetAtPos(const SomPos& _pos);
433 
438  const Label& targetAtPos(const SomPos& _pos) const;
439 
440 
444  void clear();
445 
451  SomPos indexToPos(const unsigned& _i) const;
452 
459  unsigned PosToIndex(const SomPos& _pos) const;
460 
465  SomPos codVecPos(SomIn& _v);
466 
472  SomPos applyPos(const unsigned& _in);
473 
474  /*
475  * Standard output for a fuzzy som
476  * Parameter: _os The output stream
477  * Parameter: _som The som to be printed
478  */
479  friend std::ostream& operator << (std::ostream& _os, const FuzzyMap& _fsom)
480  {
481  _fsom.printSelf(_os);
482  return _os;
483  };
484 
485  /*
486  * Standard input for a som
487  * Parameter: _is The input stream
488  * Parameter: _som The code book to be read
489  * @exception runtime_error If there are problems with the stream
490  */
492  {
493  _fsom.readSelf(_is);
494  return _is;
495  };
496 
497 
502  FuzzyMap& operator= (const FuzzyMap &op1);
503 
504 
505 
510  virtual void printSelf(std::ostream& _os) const;
511 
512 
518  virtual void readSelf(std::istream& _is, const unsigned _size = 0);
519 
525  virtual void saveObject(std::ostream& _os) const;
526 
527 
533  virtual void loadObject(std::istream& _is);
534 
535 
539  virtual const Layout& getLayout() const
540  {
541  return (Layout&) *somLayout;
542  };
543 
544 
545 protected:
546 
547  const Layout* somLayout; // type of layout
548  unsigned somWidth; // SOM's width
549  unsigned somHeight; // SOM's height
550 
551 };
553 
554 
555 
556 //-----------------------------------------------------------------------------
557 // Different layout managers
558 //-----------------------------------------------------------------------------
559 
560 
561 
562 //---------------------------------------------------------------------------
563 // Class to manage different Layouts
564 //---------------------------------------------------------------------------
565 
573 class Layout
574 {
575 public:
576 
581  Layout(const std::string& _id = "") : theId(_id)
582  {};
583 
587  virtual ~Layout()
588  {};
589 
590 
597  std::vector<unsigned> neighborhood(const ClassificationMap* _som, const SomPos& _center,
598  double _radius) const;
599 
600 
607  std::vector<unsigned> neighborhood(const FuzzyMap* _som, const SomPos& _center,
608  double _radius) const;
609 
610 
611 
618  virtual void localAve(const FuzzyMap* _som, const SomPos& _center, std::vector<double>& _aveVector) const = 0;
619 
620 
626  virtual double numNeig(const FuzzyMap* _som, const SomPos& _center) const = 0;
627 
628 
636  virtual bool isIn(const SomPos& _center, const SomPos& _v,
637  double _radius) const;
638 
639 
646  virtual double dist(const SomPos& _center, const SomPos& _v) const = 0;
647 
648 
652  const std::string& id() const;
653 
654 private:
655 
656  std::string theId;
657 };
658 
673 class RECTLayout : public Layout
674 {
675 public:
676 
680  RECTLayout() : Layout("rect")
681  {};
682 
683 
689  virtual double dist(const SomPos& _center, const SomPos& _v) const;
690 
697  virtual void localAve(const FuzzyMap* _som, const SomPos& _center, std::vector<double>& _aveVector) const;
698 
703  virtual double numNeig(const FuzzyMap* _som, const SomPos& _center) const;
704 
705 
706 protected:
707 
708  RECTLayout(const std::string& _id) : Layout(_id)
709  {};
710 };
711 
729 class HEXALayout : public Layout
730 {
731 public:
732 
736  HEXALayout() : Layout("hexa")
737  {};
738 
739  virtual double dist(const SomPos& _center, const SomPos& _v) const;
740 
747  virtual void localAve(const FuzzyMap* _som, const SomPos& _center, std::vector<double>& _aveVector) const;
748 
754  virtual double numNeig(const FuzzyMap* _som, const SomPos& _center) const;
755 
756 protected:
757 
758  HEXALayout(const std::string& _id) : Layout(_id)
759  {};
760 };
761 
763 
764 #endif
const SomIn & itemAtPos(const SomPos &_pos) const
Definition: map.cpp:224
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:306
unsigned somHeight
Definition: map.h:549
virtual ~ClassificationMap()
Definition: map.h:118
Definition: map.h:308
unsigned width() const
Definition: map.cpp:186
Layout(const std::string &_id="")
Definition: map.h:581
std::string Label
Definition: data_types.h:79
const Layout * somLayout
Definition: map.h:542
ClassificationMap(const ClassificationMap &op1)
Definition: map.cpp:64
void clear()
Definition: map.cpp:291
virtual const Layout & getLayout() const
Definition: map.h:539
unsigned height() const
Definition: map.cpp:194
HEXALayout(const std::string &_id)
Definition: map.h:758
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:1101
std::vector< unsigned > neighborhood(const SomPos &_center, double _radius) const
Definition: map.cpp:167
FeatureVector SomIn
Definition: map.h:44
HEXALayout()
Definition: map.h:736
virtual void saveObject(std::ostream &_os) const
Definition: map.cpp:438
const std::string & layout() const
Definition: map.cpp:157
RECTLayout()
Definition: map.h:680
virtual void loadObject(std::istream &_is)
Definition: map.cpp:451
ClassificationMap & operator=(const ClassificationMap &op1)
Definition: map.cpp:363
std::pair< long, long > SomPos
Definition: map.h:45
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:375
virtual ~FuzzyMap()
Definition: map.h:356
friend std::ostream & operator<<(std::ostream &_os, const ClassificationMap &_som)
Definition: map.h:230
virtual void add(const FeatureVector &_v, const Label &_l=Label())
Definition: map.cpp:149
virtual const Layout & getLayout() const
Definition: map.h:286
unsigned PosToIndex(const SomPos &_pos) const
Definition: map.cpp:324
const Label & targetAtPos(const SomPos &_pos) const
Definition: map.cpp:266
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
SomPos codVecPos(SomIn &_v)
Definition: map.cpp:341
unsigned somWidth
Definition: map.h:548
virtual void readSelf(std::istream &_is)
Definition: map.cpp:386
Definition: map.h:573
unsigned somHeight
Definition: map.h:296
virtual ~Layout()
Definition: map.h:587
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
double neighDist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:177
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289
RECTLayout(const std::string &_id)
Definition: map.h:708
virtual void readSelf(std::istream &_is, const unsigned _size=0)
Definition: map.cpp:1113
friend std::istream & operator>>(std::istream &_is, ClassificationMap &_som)
Definition: map.h:242