Xmipp  v3.23.11-Nereus
map.cpp
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.cc
28 // Implements Self-Organizing Maps of the type used by Kohonen algorithms.
29 //-----------------------------------------------------------------------------
30 
31 #include "map.h"
32 
33 #include <core/args.h>
34 
35 
43 ClassificationMap::ClassificationMap(const std::string& _layout, unsigned _width,
44  const unsigned& _height, const unsigned& _size)
45  : CodeBook(_width*_height, _size, false), somWidth(_width), somHeight(_height)
46 {
47  if (_layout == "HEXA")
48  {
49  auto *tmpLayout = new HEXALayout();
50  somLayout = tmpLayout;
51  }
52  else
53  {
54  auto *tmpLayout = new RECTLayout();
55  somLayout = tmpLayout;
56  }
57 }
58 
59 
65  *this = other;
66  }
67 
68 
78 ClassificationMap::ClassificationMap(const std::string& _layout, unsigned _width,
79  const unsigned& _height, const unsigned& _size, const double& _lower,
80  const double& _upper)
81  : CodeBook(_width*_height, _size, _lower, _upper, false), somWidth(_width), somHeight(_height)
82 {
83  if (_layout == "HEXA")
84  {
85  auto *tmpLayout = new HEXALayout();
86  somLayout = tmpLayout;
87  }
88  else
89  {
90  auto *tmpLayout = new RECTLayout();
91  somLayout = tmpLayout;
92  }
93 }
94 
95 
105 /* Part of this code were developed by Lorenzo Zampighi and Nelson Tang
106  of the department of Physiology of the David Geffen School of Medistd::cine,
107  University of California, Los Angeles
108 */
109 ClassificationMap::ClassificationMap(const std::string& _layout, unsigned _width,
110  const unsigned& _height, const ClassicTrainingVectors& _ts,
111  const bool _use_rand_cvs)
112  : CodeBook(_width*_height, _ts, _use_rand_cvs),
113  somWidth(_width), somHeight(_height)
114 {
115  if (_layout == "HEXA")
116  {
117  auto *tmpLayout = new HEXALayout();
118  somLayout = tmpLayout;
119  }
120  else
121  {
122  auto *tmpLayout = new RECTLayout();
123  somLayout = tmpLayout;
124  }
125 }
126 
127 
128 
135 {
136  somLayout = NULL;
137  if (_cv)
138  readSelf(_is);
139  else
140  loadObject(_is);
141 }
142 
143 
149 void ClassificationMap::add(const FeatureVector& _v, const Label& _l)
150 {
151  throw std::runtime_error("You can't add vectors to a S.O.M.");
152 }
153 
157 const std::string& ClassificationMap::layout() const
158 {
159  return somLayout->id();
160 }
161 
167 std::vector<unsigned> ClassificationMap::neighborhood(const SomPos& _center, double _radius) const
168 {
169  return somLayout->neighborhood(this, _center, _radius);
170 }
171 
177 double ClassificationMap::neighDist(const SomPos& _center, const SomPos& _v) const
178 {
179  return somLayout->dist(_center, _v);
180 }
181 
182 
186 unsigned ClassificationMap::width() const
187 {
188  return somWidth;
189 }
190 
195 {
196  return somHeight;
197 }
198 
199 #ifdef UNUSED // detected as unused 29.6.2018
200 
206 {
207  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
208  {
209  std::ostringstream msg;
210  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
211  throw std::out_of_range(msg.str());
212  }
213 
214  return itemAt((somWidth * _pos.second) + _pos.first);
215 }
216 #endif
217 
218 
224 const SomIn& ClassificationMap::itemAtPos(const SomPos& _pos) const
225 {
226  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
227  {
228  std::ostringstream msg;
229  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
230  throw std::out_of_range(msg.str());
231  }
232 
233  return itemAt((somWidth * _pos.second) + _pos.first);
234 }
235 
236 #ifdef UNUSED // detected as unused 29.6.2018
237 
242 {
243  if (!calibrated())
244  {
245  std::ostringstream msg;
246  msg << "The S.O.M. is not calibrated. No target at position ("
247  << _pos.first << ", " << _pos.second << ")";
248  throw std::out_of_range(msg.str());
249  }
250  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
251  {
252  std::ostringstream msg;
253  msg << "Out of range. No target at position (" << _pos.first << ", "
254  << _pos.second << ")";
255  throw std::out_of_range(msg.str());
256  }
257 
258  return targetAt((somWidth * _pos.second) + _pos.first);
259 }
260 #endif
261 
266 const Label& ClassificationMap::targetAtPos(const SomPos& _pos) const
267 {
268  if (!calibrated())
269  {
270  std::ostringstream msg;
271  msg << "The S.O.M. is not calibrated. No target at position ("
272  << _pos.first << ", " << _pos.second << ")";
273  throw std::out_of_range(msg.str());
274  }
275  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
276  {
277  std::ostringstream msg;
278  msg << "Out of range. No target at position (" << _pos.first << ", "
279  << _pos.second << ")";
280  throw std::out_of_range(msg.str());
281  }
282 
283  return targetAt((somWidth * _pos.second) + _pos.first);
284 }
285 
286 
287 
292 {
293  CodeBook::clear();
294  somLayout = NULL;
295  if (somLayout)
296  delete somLayout;
297  somWidth = 0;
298  somHeight = 0;
299 }
300 
306 SomPos ClassificationMap::indexToPos(const unsigned& _i) const
307 {
308  if (_i >= somWidth * somHeight)
309  {
310  std::ostringstream msg;
311  msg << "Out of range. No item at position " << _i;
312 
313  throw std::out_of_range(msg.str());
314  }
315 
316  return SomPos(_i % somWidth, _i / somWidth);
317 }
318 
324 unsigned ClassificationMap::PosToIndex(const SomPos& _pos) const
325 {
326  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
327  {
328  std::ostringstream msg;
329  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
330  throw std::out_of_range(msg.str());
331  }
332 
333  return (unsigned)((somWidth * _pos.second) + _pos.first);
334 }
335 
336 
342 {
343  return indexToPos(&_v - &(itemAt(0)));
344 }
345 
346 #ifdef UNUSED // detected as unused 29.6.2018
347 
352 SomPos ClassificationMap::applyPos(const SomIn& _in)
353 {
354  return codVecPos(test(_in));
355 }
356 #endif
357 
358 
364 {
365  std::stringstream _str;
366  op1.printSelf(_str);
367  readSelf(_str);
368  return *this;
369 }
370 
375 void ClassificationMap::printSelf(std::ostream& _os) const
376 {
377  _os << itemAt(0).size() << " " <<
378  somLayout->id() << " " << somWidth << " " << somHeight << " gaussian" << std::endl;
379  writeItems(_os);
380 }
381 
387 {
388  clear();
389  int dim;
390  std::string layout;
391  std::string str;
392  _is >> dim;
393  _is >> layout;
394  toLower(layout);
395 
396  if (layout == "hexa")
397  {
398  auto *tmpLayout = new HEXALayout();
399  somLayout = tmpLayout;
400  }
401  else
402  {
403  auto *tmpLayout = new RECTLayout();
404  somLayout = tmpLayout;
405  }
406  _is >> somWidth;
407  _is >> somHeight;
408  /* IT DOESN'T WORK PROPERLY
409  str = integerToString(dim);
410  str += " ";
411  str += integerToString(somWidth*somHeight);
412  str += " ";
413  for (int i = str.size() - 1; i >= 0; i--)
414  if (_is) _is.putback((char) str[i]);
415  */
416  CodeBook::readSelf(_is, dim, somWidth*somHeight);
417 
418  /* IT DOESN'T WORK PROPERLY
419 
420  _is >> somWidth;
421  _is >> somHeight;
422 
423  char aux[128];
424  strstream ostr(aux,sizeof(aux));
425  ostr << dim << " " << (somWidth*somHeight) << " " <<
426  somWidth << " " << somHeight << std::endl;
427  while (_is.good())
428  ostr.put (_is.get());
429  CodeBook::readSelf(ostr);*/
430 }
431 
432 
438 void ClassificationMap::saveObject(std::ostream& _os) const
439 {
440  _os << somLayout->id() << " " << somWidth << " " << somHeight << std::endl;
441  writeClassifVectors(_os);
443 }
444 
445 
452 {
453  clear();
454  std::string layout;
455  _is >> layout;
456  if (layout == "HEXA")
457  {
458  auto *tmpLayout = new HEXALayout();
459  somLayout = tmpLayout;
460  }
461  else
462  {
463  auto *tmpLayout = new RECTLayout();
464  somLayout = tmpLayout;
465  }
466  _is >> somWidth;
467  _is >> somHeight;
468  readClassifVectors(_is);
470 }
471 
472 
479 std::vector<unsigned> Layout::neighborhood(const ClassificationMap* _som, const SomPos& _center,
480  double _radius) const
481 {
482  std::vector<unsigned> neig;
483 
484  // try to find the neighbors
485  for (unsigned i = 0 ; i < _som->size() ; i++)
486  {
487  if (isIn(_center, _som->indexToPos(i), _radius))
488  neig.push_back(i);
489  }
490 
491  return neig;
492 }
493 
494 
501 std::vector<unsigned> Layout::neighborhood(const FuzzyMap* _som, const SomPos& _center,
502  double _radius) const
503 {
504  std::vector<unsigned> neig;
505 
506  // try to find the neighbors
507  for (unsigned i = 0 ; i < _som->size() ; i++)
508  {
509  if (isIn(_center, _som->indexToPos(i), _radius))
510  {
511  neig.push_back(i);
512  }
513  }
514 
515  return neig;
516 }
517 
518 
526 bool Layout::isIn(const SomPos& _center, const SomPos& _v,
527  double _radius) const
528 {
529  return (dist(_center, _v) <= _radius);
530 }
531 
532 
536 const std::string& Layout::id() const
537 {
538  return theId;
539 }
540 
541 
542 //---------------------------------------------------------------------------
543 
544 
551 double RECTLayout::dist(const SomPos& _center, const SomPos& _v) const
552 {
553  return ((double) sqrt((double)(_center.first - _v.first)*(_center.first - _v.first) +
554  (_center.second - _v.second)*(_center.second - _v.second)));
555 }
556 
563 void RECTLayout::localAve(const FuzzyMap* _som, const SomPos& _center, std::vector<double>& _aveVector) const
564 {
565  int j;
566  int dim = _som->itemAt(0).size();
567  double *ptrAveVector=&(_aveVector[0]);
568  memset(ptrAveVector,0,dim*sizeof(double));
569  int tmpi = _center.first;
570  int tmpj = _center.second;
571  int kk = 0;
572  if ((tmpi - 1) >= 0)
573  {
574  kk++;
575  const floatFeature *codevector=&(_som->itemAt(_som->PosToIndex(SomPos(tmpi - 1, tmpj)))[0]);
576  for (j = 0; j < dim; j++)
577  ptrAveVector[j] += codevector[j];
578  }
579  if ((tmpi + 1) < (int)_som->width())
580  {
581  kk++;
582  const floatFeature *codevector=&(_som->itemAt(_som->PosToIndex(SomPos(tmpi + 1, tmpj)))[0]);
583  for (j = 0; j < dim; j++)
584  ptrAveVector[j] += codevector[j];
585  }
586  if ((tmpj - 1) >= 0)
587  {
588  kk++;
589  const floatFeature *codevector=&(_som->itemAt(_som->PosToIndex(SomPos(tmpi, tmpj-1)))[0]);
590  for (j = 0; j < dim; j++)
591  ptrAveVector[j] += codevector[j];
592  }
593  if ((tmpj + 1) < (int)_som->height())
594  {
595  kk++;
596  const floatFeature *codevector=&(_som->itemAt(_som->PosToIndex(SomPos(tmpi, tmpj+1)))[0]);
597  for (j = 0; j < dim; j++)
598  ptrAveVector[j] += codevector[j];
599  }
600  if (_som->height() == 1 || _som->width() == 1)
601  for (j = 0; j < dim; j++)
602  ptrAveVector[j] *= 1.0/2.0;
603  else
604  for (j = 0; j < dim; j++)
605  ptrAveVector[j] *= 1.0/4.0;
606 }
607 
612 double RECTLayout::numNeig(const FuzzyMap* _som, const SomPos& _center) const
613 {
614  int tmpi = _center.first;
615  int tmpj = _center.second;
616  double kk = 0;
617  if ((tmpi - 1) >= 0)
618  {
619  kk++;
620  }
621  if ((tmpi + 1) < (int)_som->width())
622  {
623  kk++;
624  }
625  if ((tmpj - 1) >= 0)
626  {
627  kk++;
628  }
629  if ((tmpj + 1) < (int)_som->height())
630  {
631  kk++;
632  }
633  if (_som->height() == 1 || _som->width() == 1)
634  return (kk / 2.0);
635  else
636  return (kk / 4.0);
637 }
638 
639 
646 double HEXALayout::dist(const SomPos& _center, const SomPos& _v) const
647 {
648  double ret;
649  double diff;
650  diff = _center.first - _v.first;
651 
652  if (((_center.second - _v.second) % 2) != 0)
653  {
654  if ((_center.second % 2) == 0)
655  {
656  diff -= 0.5;
657  }
658  else
659  {
660  diff += 0.5;
661  }
662  }
663  ret = diff * diff;
664  diff = _center.second - _v.second;
665  ret += 0.75 * diff * diff;
666  ret = (double) sqrt(ret);
667  return ret;
668 }
669 
670 
677 void HEXALayout::localAve(const FuzzyMap* _som, const SomPos& _center, std::vector<double>& _aveVector) const
678 {
679 
680  int j;
681  int dim = _som->itemAt(0).size();
682  for (j = 0; j < dim; j++)
683  _aveVector[j] = 0.0;
684  int tmpi = _center.first;
685  int tmpj = _center.second;
686  int kk = 0;
687  if ((tmpi - 1) >= 0)
688  {
689  kk++;
690  for (j = 0; j < dim; j++)
691  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi - 1, tmpj)))[j]);
692  }
693  if ((tmpi + 1) < (int)_som->width())
694  {
695  kk++;
696  for (j = 0; j < dim; j++)
697  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi + 1, tmpj)))[j]);
698  }
699  if ((tmpj - 1) >= 0)
700  {
701  kk++;
702  for (j = 0; j < dim; j++)
703  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi, tmpj - 1)))[j]);
704  }
705  if ((tmpj + 1) < (int)_som->height())
706  {
707  kk++;
708  for (j = 0; j < dim; j++)
709  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi, tmpj + 1)))[j]);
710  }
711  if (((tmpj - 1) >= 0) && ((tmpi - 1) >= 0))
712  {
713  kk++;
714  for (j = 0; j < dim; j++)
715  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi - 1, tmpj - 1)))[j]);
716  }
717  if (((tmpj + 1) < (int)_som->height()) && ((tmpi - 1) >= 0))
718  {
719  kk++;
720  for (j = 0; j < dim; j++)
721  _aveVector[j] += (double)(_som->itemAt(_som->PosToIndex(SomPos(tmpi - 1, tmpj + 1)))[j]);
722  }
723  /* for (j = 0; j < dim; j++)
724  _aveVector[j] /= kk;*/
725  if (_som->height() == 1 || _som->width() == 1)
726  for (j = 0; j < dim; j++)
727  _aveVector[j] /= 2.0;
728  else
729  for (j = 0; j < dim; j++)
730  _aveVector[j] /= 6.0;
731 }
732 
733 
738 double HEXALayout::numNeig(const FuzzyMap* _som, const SomPos& _center) const
739 {
740  int tmpi = _center.first;
741  int tmpj = _center.second;
742  double kk = 0;
743  if ((tmpi - 1) >= 0)
744  {
745  kk++;
746  }
747  if ((tmpi + 1) < (int)_som->width())
748  {
749  kk++;
750  }
751  if ((tmpj - 1) >= 0)
752  {
753  kk++;
754  }
755  if ((tmpj + 1) < (int)_som->height())
756  {
757  kk++;
758  }
759  if (((tmpj - 1) >= 0) && ((tmpi - 1) >= 0))
760  {
761  kk++;
762  }
763  if (((tmpj + 1) < (int)_som->height()) && ((tmpi - 1) >= 0))
764  {
765  kk++;
766  }
767  if (_som->width() == 1 || _som->height() == 1)
768  return (kk / 2.0);
769  else
770  return (kk / 6.0);
771 }
772 
773 
774 /************** Fuzzy Map ******************************************/
775 
785 FuzzyMap::FuzzyMap(const std::string& _layout, unsigned _width,
786  const unsigned& _height, const unsigned& _size, const double& _lower,
787  const double& _upper)
788  : FuzzyCodeBook(_width*_height, _size, 0, _lower, _upper, false), somWidth(_width), somHeight(_height)
789 {
790  if (_layout == "HEXA")
791  {
792  auto *tmpLayout = new HEXALayout();
793  somLayout = tmpLayout;
794  }
795  else
796  {
797  auto *tmpLayout = new RECTLayout();
798  somLayout = tmpLayout;
799  }
800 }
801 
811 /* Part of this code were developed by Lorenzo Zampighi and Nelson Tang
812  of the department of Physiology of the David Geffen School of Medistd::cine,
813  University of California, Los Angeles
814 */
815 FuzzyMap::FuzzyMap(const std::string& _layout, unsigned _width,
816  const unsigned& _height, const ClassicTrainingVectors& _ts,
817  const bool _use_rand_cvs)
818  : FuzzyCodeBook(_width*_height, _ts, _use_rand_cvs),
819  somWidth(_width), somHeight(_height)
820 {
821  if (_layout == "HEXA")
822  {
823  auto *tmpLayout = new HEXALayout();
824  somLayout = tmpLayout;
825  }
826  else
827  {
828  auto *tmpLayout = new RECTLayout();
829  somLayout = tmpLayout;
830  }
831 }
832 
839 FuzzyMap::FuzzyMap(std::istream& _is, const unsigned _size, bool _cv) : FuzzyCodeBook(false)
840 {
841  somLayout = NULL;
842  if (_cv)
843  readSelf(_is, _size);
844  else
845  loadObject(_is);
846 }
847 
849  *this=other;
850 }
851 
857 void FuzzyMap::add(const FeatureVector& _v, const Label& _l)
858 {
859  throw std::runtime_error("You can't add vectors to a S.O.M.");
860 }
861 
865 const std::string& FuzzyMap::layout() const
866 {
867  return somLayout->id();
868 }
869 
875 std::vector<unsigned> FuzzyMap::neighborhood(const SomPos& _center, double _radius) const
876 {
877  return somLayout->neighborhood(this, _center, _radius);
878 }
879 
885 void FuzzyMap::neighborhood(const SomPos& _center, double _radius, std::vector<unsigned>& _neig) const
886 {
887  _neig = somLayout->neighborhood(this, _center, _radius);
888 }
889 
896 void FuzzyMap::localAve(const SomPos& _center, std::vector<double>& _aveVector) const
897 {
898  somLayout->localAve(this, _center, _aveVector);
899 }
900 
901 
907 double FuzzyMap::neighDist(const SomPos& _center, const SomPos& _v) const
908 {
909  return somLayout->dist(_center, _v);
910 }
911 
912 
916 unsigned FuzzyMap::width() const
917 {
918  return somWidth;
919 }
920 
924 unsigned FuzzyMap::height() const
925 {
926  return somHeight;
927 }
928 
935 {
936  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
937  {
938  std::ostringstream msg;
939  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
940  throw std::out_of_range(msg.str());
941  }
942 
943  return itemAt((somWidth * _pos.second) + _pos.first);
944 }
945 
946 
952 const SomIn& FuzzyMap::itemAtPos(const SomPos& _pos) const
953 {
954  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
955  {
956  std::ostringstream msg;
957  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
958  throw std::out_of_range(msg.str());
959  }
960 
961  return itemAt((somWidth * _pos.second) + _pos.first);
962 }
963 
964 
970 {
971  if (!calibrated())
972  {
973  std::ostringstream msg;
974  msg << "The S.O.M. is not calibrated. No target at position ("
975  << _pos.first << ", " << _pos.second << ")";
976  throw std::out_of_range(msg.str());
977  }
978  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
979  {
980  std::ostringstream msg;
981  msg << "Out of range. No target at position (" << _pos.first << ", "
982  << _pos.second << ")";
983  throw std::out_of_range(msg.str());
984  }
985 
986  return targetAt((somWidth * _pos.second) + _pos.first);
987 }
988 
989 
994 const Label& FuzzyMap::targetAtPos(const SomPos& _pos) const
995 {
996  if (!calibrated())
997  {
998  std::ostringstream msg;
999  msg << "The S.O.M. is not calibrated. No target at position ("
1000  << _pos.first << ", " << _pos.second << ")";
1001  throw std::out_of_range(msg.str());
1002  }
1003  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
1004  {
1005  std::ostringstream msg;
1006  msg << "Out of range. No target at position (" << _pos.first << ", "
1007  << _pos.second << ")";
1008  throw std::out_of_range(msg.str());
1009  }
1010 
1011  return targetAt((somWidth * _pos.second) + _pos.first);
1012 }
1013 
1014 
1015 
1020 {
1022  if (somLayout)
1023  delete somLayout;
1024  somWidth = 0;
1025  somHeight = 0;
1026 }
1027 
1033 SomPos FuzzyMap::indexToPos(const unsigned& _i) const
1034 {
1035  if (_i >= somWidth * somHeight)
1036  {
1037  std::ostringstream msg;
1038  msg << "Out of range. No item at position " << _i;
1039 
1040  throw std::out_of_range(msg.str());
1041  }
1042 
1043  return SomPos(_i % somWidth, _i / somWidth);
1044 }
1045 
1046 
1052 unsigned FuzzyMap::PosToIndex(const SomPos& _pos) const
1053 {
1054  if (_pos.first >= (signed)somWidth || _pos.second >= (signed)somHeight)
1055  {
1056  std::ostringstream msg;
1057  msg << "Out of range. No item at position (" << _pos.first << ", " << _pos.second << ")";
1058  throw std::out_of_range(msg.str());
1059  }
1060 
1061  return (unsigned)((somWidth * _pos.second) + _pos.first);
1062 }
1063 
1064 
1070 {
1071  return indexToPos(&_v - &(itemAt(0)));
1072 }
1073 
1079 SomPos FuzzyMap::applyPos(const unsigned& _in)
1080 {
1081  return codVecPos(fuzzyTest(_in));
1082 }
1083 
1084 
1090 {
1091  std::stringstream _str;
1092  op1.printSelf(_str);
1093  readSelf(_str);
1094  return *this;
1095 }
1096 
1101 void FuzzyMap::printSelf(std::ostream& _os) const
1102 {
1103  _os << itemAt(0).size() << " " <<
1104  somLayout->id() << " " << somWidth << " " << somHeight << " gaussian" << std::endl;
1105  writeItems(_os);
1106 }
1107 
1108 
1113 void FuzzyMap::readSelf(std::istream& _is, const unsigned _size)
1114 {
1115  clear();
1116  int dim;
1117  std::string layout;
1118  std::string str;
1119  _is >> dim;
1120  _is >> layout;
1121  if (layout == "HEXA")
1122  {
1123  auto *tmpLayout = new HEXALayout();
1124  somLayout = tmpLayout;
1125  }
1126  else
1127  {
1128  auto *tmpLayout = new RECTLayout();
1129  somLayout = tmpLayout;
1130  }
1131  _is >> somWidth;
1132  _is >> somHeight;
1133  str = integerToString(dim);
1134  str += " ";
1135  str += integerToString(somWidth * somHeight);
1136  str += " ";
1137  for (int i = str.size() - 1; i >= 0; i--)
1138  if (_is)
1139  _is.putback((char) str[i]);
1140  FuzzyCodeBook::readSelf(_is, _size);
1141 
1142 }
1143 
1144 
1150 void FuzzyMap::saveObject(std::ostream& _os) const
1151 {
1152  _os << somLayout->id() << " " << somWidth << " " << somHeight << std::endl;
1153  writeClassifVectors(_os);
1154  writeMembership(_os);
1156 }
1157 
1158 
1165 {
1166  clear();
1167  std::string layout;
1168  _is >> layout;
1169  if (layout == "HEXA")
1170  {
1171  auto *tmpLayout = new HEXALayout();
1172  somLayout = tmpLayout;
1173  }
1174  else
1175  {
1176  auto *tmpLayout = new RECTLayout();
1177  somLayout = tmpLayout;
1178  }
1179  _is >> somWidth;
1180  _is >> somHeight;
1181  readClassifVectors(_is);
1182  readMembership(_is);
1184 }
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 double dist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:551
Definition: map.h:308
unsigned width() const
Definition: map.cpp:186
SomPos codVecPos(SomIn &_v)
Definition: map.cpp:1069
std::string Label
Definition: data_types.h:79
const Layout * somLayout
Definition: map.h:542
FuzzyMap(const std::string &_layout, unsigned _width, const unsigned &_height, const unsigned &_size, const double &_lower, const double &_upper)
Definition: map.cpp:785
void sqrt(Image< double > &op)
void localAve(const SomPos &_center, std::vector< double > &_aveVector) const
Definition: map.cpp:896
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:1033
FuzzyMap & operator=(const FuzzyMap &op1)
Definition: map.cpp:1089
const std::string & layout() const
Definition: map.cpp:865
virtual void localAve(const FuzzyMap *_som, const SomPos &_center, std::vector< double > &_aveVector) const =0
void writeClassifVectors(std::ostream &_os) const
Definition: code_book.cpp:453
ClassificationMap(const ClassificationMap &op1)
Definition: map.cpp:64
void clear()
Definition: map.cpp:291
virtual double dist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:646
String integerToString(int I, int _width, char fill_with)
float floatFeature
Definition: data_types.h:72
unsigned width() const
Definition: map.cpp:916
const Label & targetAt(unsigned _i) const
Definition: training_set.h:221
unsigned height() const
Definition: map.cpp:194
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:1101
virtual bool isIn(const SomPos &_center, const SomPos &_v, double _radius) const
Definition: map.cpp:526
std::vector< unsigned > neighborhood(const SomPos &_center, double _radius) const
Definition: map.cpp:167
virtual FeatureVector & test(const FeatureVector &_in) const
Definition: code_book.cpp:172
FeatureVector SomIn
Definition: map.h:44
#define i
std::vector< unsigned > neighborhood(const SomPos &_center, double _radius) const
Definition: map.cpp:875
virtual void saveObject(std::ostream &_os) const
Definition: map.cpp:1150
virtual void saveObject(std::ostream &_os) const
Definition: map.cpp:438
const std::string & layout() const
Definition: map.cpp:157
void clear()
Definition: map.cpp:1019
virtual void loadObject(std::istream &_is)
Definition: map.cpp:451
virtual void add(const FeatureVector &_v, const Label &_l=Label())
Definition: map.cpp:857
ClassificationMap & operator=(const ClassificationMap &op1)
Definition: map.cpp:363
std::pair< long, long > SomPos
Definition: map.h:45
unsigned height() const
Definition: map.cpp:924
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:375
virtual void saveObject(std::ostream &_os) const
Definition: training_set.h:349
virtual void add(const FeatureVector &_v, const Label &_l=Label())
Definition: map.cpp:149
virtual void readMembership(std::istream &_is)
virtual void loadObject(std::istream &_is)
Definition: map.cpp:1164
const std::string & id() const
Definition: map.cpp:536
virtual void readSelf(std::istream &_is, const unsigned _size=0)
unsigned PosToIndex(const SomPos &_pos) const
Definition: map.cpp:324
virtual void writeMembership(std::ostream &_os) const
const Label & targetAtPos(const SomPos &_pos) const
Definition: map.cpp:266
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
void writeItems(std::ostream &_os, bool _delim=false) const
Definition: training_set.h:561
virtual double numNeig(const FuzzyMap *_som, const SomPos &_center) const
Definition: map.cpp:738
SomPos codVecPos(SomIn &_v)
Definition: map.cpp:341
#define j
unsigned somWidth
Definition: map.h:548
std::vector< unsigned > neighborhood(const ClassificationMap *_som, const SomPos &_center, double _radius) const
Definition: map.cpp:479
Label & targetAtPos(const SomPos &_pos)
Definition: map.cpp:969
virtual void readSelf(std::istream &_is)
Definition: map.cpp:386
virtual FeatureVector & fuzzyTest(unsigned _in) const
unsigned PosToIndex(const SomPos &_pos) const
Definition: map.cpp:1052
void readClassifVectors(std::istream &_is)
Definition: code_book.cpp:439
double neighDist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:907
SomPos applyPos(const unsigned &_in)
Definition: map.cpp:1079
SomIn & itemAtPos(const SomPos &_pos)
Definition: map.cpp:934
virtual double numNeig(const FuzzyMap *_som, const SomPos &_center) const
Definition: map.cpp:612
void toLower(char *_str)
virtual void loadObject(std::istream &_is)
Definition: training_set.h:360
unsigned somHeight
Definition: map.h:296
virtual double dist(const SomPos &_center, const SomPos &_v) const =0
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
const FeatureVector & itemAt(unsigned _i) const
Definition: training_set.h:264
double neighDist(const SomPos &_center, const SomPos &_v) const
Definition: map.cpp:177
virtual void localAve(const FuzzyMap *_som, const SomPos &_center, std::vector< double > &_aveVector) const
Definition: map.cpp:563
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289
virtual void localAve(const FuzzyMap *_som, const SomPos &_center, std::vector< double > &_aveVector) const
Definition: map.cpp:677
virtual void readSelf(std::istream &_is, long _dim=-1, long _size=-1)
Definition: code_book.cpp:370
virtual void readSelf(std::istream &_is, const unsigned _size=0)
Definition: map.cpp:1113