Xmipp  v3.23.11-Nereus
Public Member Functions | Protected Attributes | Friends | List of all members
ClassificationMap Class Reference

#include <map.h>

Inheritance diagram for ClassificationMap:
Inheritance graph
[legend]
Collaboration diagram for ClassificationMap:
Collaboration graph
[legend]

Public Member Functions

 ClassificationMap (const ClassificationMap &op1)
 
 ClassificationMap (const std::string &_layout, unsigned _width, const unsigned &_height, const unsigned &_size)
 
 ClassificationMap (const std::string &_layout, unsigned _width, const unsigned &_height, const unsigned &_size, const double &_lower, const double &_upper)
 
 ClassificationMap (const std::string &_layout, unsigned _width, const unsigned &_height, const ClassicTrainingVectors &_ts, const bool _use_rand_cvs=false)
 
 ClassificationMap (std::istream &_is, bool _cv=true)
 
virtual ~ClassificationMap ()
 
virtual void add (const FeatureVector &_v, const Label &_l=Label())
 
const std::string & layout () const
 
std::vector< unsigned > neighborhood (const SomPos &_center, double _radius) const
 
double neighDist (const SomPos &_center, const SomPos &_v) const
 
unsigned width () const
 
unsigned height () const
 
const SomInitemAtPos (const SomPos &_pos) const
 
const LabeltargetAtPos (const SomPos &_pos) const
 
void clear ()
 
SomPos indexToPos (const unsigned &_i) const
 
unsigned PosToIndex (const SomPos &_pos) const
 
SomPos codVecPos (SomIn &_v)
 
ClassificationMapoperator= (const ClassificationMap &op1)
 
virtual void printSelf (std::ostream &_os) const
 
virtual void readSelf (std::istream &_is)
 
virtual void saveObject (std::ostream &_os) const
 
virtual void loadObject (std::istream &_is)
 
virtual const LayoutgetLayout () const
 
- Public Member Functions inherited from CodeBook
 CodeBook (const bool &_calib=false)
 
 CodeBook (unsigned _n, unsigned _size, bool _cal=false)
 
 CodeBook (unsigned _n, unsigned _size, floatFeature _lower=0, floatFeature _upper=1, bool _cal=false)
 
 CodeBook (unsigned _n, const ClassicTrainingVectors &_ts, const bool _use_rand_cvs)
 
 CodeBook (std::istream &_is)
 
virtual ~CodeBook ()
 
virtual FeatureVectortest (const FeatureVector &_in) const
 
virtual unsigned testIndex (const FeatureVector &_in) const
 
virtual void classify (const ClassicTrainingVectors *_ts)
 
virtual const std::vector< unsigned > & classifAt (const unsigned &_index) const
 
virtual unsigned classifSizeAt (const unsigned &_index) const
 
virtual Label apply (const FeatureVector &_in) const
 
virtual void calibrate (ClassicTrainingVectors &_ts, Label _def="")
 
virtual unsigned output (const FeatureVector &_in) const
 
virtual void readSelf (std::istream &_is, long _dim=-1, long _size=-1)
 
virtual void Normalize (const std::vector< ClassicTrainingVectors::statsStruct > &_varStats)
 
virtual void unNormalize (const std::vector< ClassicTrainingVectors::statsStruct > &_varStats)
 
virtual void printHistogram (std::ostream &_os) const
 
virtual void printQuantError (std::ostream &_os) const
 
- Public Member Functions inherited from ClassificationDataSet< FeatureVector, Label >
 ClassificationDataSet ()
 
virtual ~ClassificationDataSet ()
 
- Public Member Functions inherited from ClassificationTrainingSet< FeatureVector, Label >
 ClassificationTrainingSet (const bool &_calib=true, unsigned _n=0)
 
 ClassificationTrainingSet (std::istream &_is)
 
virtual ~ClassificationTrainingSet ()
 
void setSplit (float _tp, float _vp)
 
splitIt beginSubset (unsigned _um)
 
splitIt endSubset (unsigned _um)
 Returns an iterator to the end of the subset. More...
 
virtual void add (const FeatureVector &_i)
 
virtual bool remove (unsigned int _idx)
 
size_t size () const
 
const LabeltargetAt (unsigned _i) const
 
LabeltargetAt (unsigned _i)
 
const FeatureVectoritemAt (unsigned _i) const
 
FeatureVectoritemAt (unsigned _i)
 
bool calibrated () const
 
void calibrated (const bool &_calib)
 
void clear ()
 
unsigned numTargets () const
 
virtual bool swapItems (unsigned _i, unsigned _j)
 

Protected Attributes

const LayoutsomLayout
 
unsigned somWidth
 
unsigned somHeight
 
- Protected Attributes inherited from ClassificationTrainingSet< FeatureVector, Label >
bool isCalibrated
 
splitTS splitTrainingSet
 
unsigned nTargets
 

Friends

std::ostream & operator<< (std::ostream &_os, const ClassificationMap &_som)
 
std::istream & operator>> (std::istream &_is, ClassificationMap &_som)
 

Additional Inherited Members

- Public Types inherited from ClassificationDataSet< FeatureVector, Label >
typedef FeatureVector In
 Class of input vectors. Usually a FeatureVector (vector of Feature) More...
 
typedef Label Out
 Class of the target. Can be a double, string, unsigned, even a vector ... More...
 
typedef ClassificationTrainingSet< In, OutTS
 Training set. Set of vectors (training vectors), probably classified. More...
 
- Public Types inherited from ClassificationTrainingSet< FeatureVector, Label >
enum  splitMode
 Ways the training set can be used. More...
 
enum  useMode
 use of samples More...
 
typedef std::multimap< unsigned, unsigned, std::less< unsigned > > splitTS
 Training sets mode. More...
 
typedef splitTS::iterator splitIt
 iterator More...
 
- Public Attributes inherited from CodeBook
std::vector< std::vector< unsigned > > classifVectors
 
std::vector< double > aveDistances
 
- Public Attributes inherited from ClassificationTrainingSet< FeatureVector, Label >
std::vector< FeatureVectortheItems
 
std::vector< LabeltheTargets
 
- Protected Member Functions inherited from CodeBook
void readClassifVectors (std::istream &_is)
 
void writeClassifVectors (std::ostream &_os) const
 
- Protected Member Functions inherited from ClassificationTrainingSet< FeatureVector, Label >
void computeNumTargets ()
 
void checkCalibrated (std::istream &_is)
 
void readItems (std::istream &_is)
 
void writeCalibrated (std::ostream &_os) const
 
void writeItems (std::ostream &_os, bool _delim=false) const
 
void skipComments (std::istream &_is) const
 
std::vector< FeatureVector >::const_iterator itemsBegin () const
 
std::vector< FeatureVector >::const_iterator itemsEnd () const
 
std::vector< Label >::const_iterator targetsBegin () const
 
std::vector< Label >::const_iterator targetsEnd () const
 

Detailed Description

This class implements a Map of the type used by Kohonen Algorithms

Definition at line 57 of file map.h.

Constructor & Destructor Documentation

◆ ClassificationMap() [1/5]

ClassificationMap::ClassificationMap ( const ClassificationMap other)
explicit

Constructs an empty SOM

Constructs a SOM from other SOM Parameter: other other SOM

Definition at line 64 of file map.cpp.

64  : CodeBook() {
65  *this = other;
66  }
CodeBook(const bool &_calib=false)
Definition: code_book.h:69

◆ ClassificationMap() [2/5]

ClassificationMap::ClassificationMap ( const std::string &  _layout,
unsigned  _width,
const unsigned &  _height,
const unsigned &  _size 
)

Constructs a SOM with initial code vectors filled with zero. Parameter: _layout Type of layout Parameter: _width Width of the output plane Parameter: _height Height of the output plane Parameter: _size Size of code vectors

Definition at line 43 of file map.cpp.

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 }
CodeBook(const bool &_calib=false)
Definition: code_book.h:69
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ ClassificationMap() [3/5]

ClassificationMap::ClassificationMap ( const std::string &  _layout,
unsigned  _width,
const unsigned &  _height,
const unsigned &  _size,
const double &  _lower,
const double &  _upper 
)

Constructs a SOM with random initial code vectors Parameter: _layout Type of layout Parameter: _width Width of the output plane Parameter: _height Height of the output plane Parameter: _size Size of code vectors Parameter: _lower Lower value for random elements Parameter: _upper Upper value for random elements

Definition at line 78 of file map.cpp.

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 }
CodeBook(const bool &_calib=false)
Definition: code_book.h:69
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ ClassificationMap() [4/5]

ClassificationMap::ClassificationMap ( const std::string &  _layout,
unsigned  _width,
const unsigned &  _height,
const ClassicTrainingVectors _ts,
const bool  _use_rand_cvs = false 
)

Constructs a SOM with initial code vectors taken randomly from the training file. Parameter: _layout Type of layout Parameter: _width Width of the output plane Parameter: _height Height of the output plane Parameter: _ts Training set; will be used to get initial values Parameter: _use_rand_cvs Use random code vector values

Constructs a SOM with initial code vectors taken randomly from the training file. Parameter: _layout Type of layout Parameter: _width Width of the output plane Parameter: _height Height of the output plane Parameter: _ts Training set; will be used to get initial values Parameter: _use_rand_cvs Use random code vector pixel values

Definition at line 109 of file map.cpp.

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 }
CodeBook(const bool &_calib=false)
Definition: code_book.h:69
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ ClassificationMap() [5/5]

ClassificationMap::ClassificationMap ( std::istream &  _is,
bool  _cv = true 
)

Construct a SOM from the code vectors in a stream Parameter: _is The stream Parameter: _cv If the stream holds a codevector file or a whole codebook file

Exceptions
runtime_errorIf there are problems with the stream

Construct a SOM from the code vectors in a stream Parameter: _is The stream

Exceptions
runtime_errorIf there are problems with the stream

Definition at line 134 of file map.cpp.

134  : CodeBook(false)
135 {
136  somLayout = NULL;
137  if (_cv)
138  readSelf(_is);
139  else
140  loadObject(_is);
141 }
CodeBook(const bool &_calib=false)
Definition: code_book.h:69
virtual void loadObject(std::istream &_is)
Definition: map.cpp:451
virtual void readSelf(std::istream &_is)
Definition: map.cpp:386
const Layout * somLayout
Definition: map.h:289

◆ ~ClassificationMap()

virtual ClassificationMap::~ClassificationMap ( )
inlinevirtual

Virtual destructor is needed

Definition at line 118 of file map.h.

119  {};

Member Function Documentation

◆ add()

void ClassificationMap::add ( const FeatureVector _v,
const Label _l = Label() 
)
virtual

This method throws an exception if called. There is no sense in adding vectors to a som

Exceptions
range_errorIf this method is called

Reimplemented from ClassificationTrainingSet< FeatureVector, Label >.

Definition at line 149 of file map.cpp.

150 {
151  throw std::runtime_error("You can't add vectors to a S.O.M.");
152 }

◆ clear()

void ClassificationMap::clear ( )

Clears the Som

Definition at line 291 of file map.cpp.

292 {
293  CodeBook::clear();
294  somLayout = NULL;
295  if (somLayout)
296  delete somLayout;
297  somWidth = 0;
298  somHeight = 0;
299 }
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ codVecPos()

SomPos ClassificationMap::codVecPos ( SomIn _v)

Returns the position in the som of a code vector Parameter: _v Reference to the code vector

Definition at line 341 of file map.cpp.

342 {
343  return indexToPos(&_v - &(itemAt(0)));
344 }
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:306
const FeatureVector & itemAt(unsigned _i) const
Definition: training_set.h:264

◆ getLayout()

virtual const Layout& ClassificationMap::getLayout ( ) const
inlinevirtual

Const Reference to the Map Layout

Definition at line 286 of file map.h.

287  {
288  return (Layout&) *somLayout;
289  };
Definition: map.h:573
const Layout * somLayout
Definition: map.h:289

◆ height()

unsigned ClassificationMap::height ( ) const

Returns the height of the SOM

Definition at line 194 of file map.cpp.

195 {
196  return somHeight;
197 }
unsigned somHeight
Definition: map.h:296

◆ indexToPos()

SomPos ClassificationMap::indexToPos ( const unsigned &  _i) const

Return the position associated to an index Parameter: _i Index of the code vector

Exceptions
out_of_range If _i is out of range

Definition at line 306 of file map.cpp.

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 }
std::pair< long, long > SomPos
Definition: map.h:45
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295

◆ itemAtPos()

const SomIn & ClassificationMap::itemAtPos ( const SomPos _pos) const

Returns a const reference to a code vector given its position Parameter: _pos The position of the code vector

Exceptions
out_of_range If _pos is out of range

Definition at line 224 of file map.cpp.

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 }
unsigned somHeight
Definition: map.h:296
const FeatureVector & itemAt(unsigned _i) const
Definition: training_set.h:264
unsigned somWidth
Definition: map.h:295

◆ layout()

const std::string & ClassificationMap::layout ( ) const

Returns the id of layout that som has

Definition at line 157 of file map.cpp.

158 {
159  return somLayout->id();
160 }
const std::string & id() const
Definition: map.cpp:536
const Layout * somLayout
Definition: map.h:289

◆ loadObject()

void ClassificationMap::loadObject ( std::istream &  _is)
virtual

Loads the ClassificationMap class from a stream. this method can be used to load the status of the class. Parameter: _is The output stream

Reimplemented from CodeBook.

Definition at line 451 of file map.cpp.

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 }
void clear()
Definition: map.cpp:291
const std::string & layout() const
Definition: map.cpp:157
void readClassifVectors(std::istream &_is)
Definition: code_book.cpp:439
virtual void loadObject(std::istream &_is)
Definition: training_set.h:360
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ neighborhood()

std::vector< unsigned > ClassificationMap::neighborhood ( const SomPos _center,
double  _radius 
) const

Returns the neighborhood of a neuron Parameter: _center Reference to the center of neighborhood Parameter: _radius Radius of neighbohood

Definition at line 167 of file map.cpp.

168 {
169  return somLayout->neighborhood(this, _center, _radius);
170 }
std::vector< unsigned > neighborhood(const ClassificationMap *_som, const SomPos &_center, double _radius) const
Definition: map.cpp:479
const Layout * somLayout
Definition: map.h:289

◆ neighDist()

double ClassificationMap::neighDist ( const SomPos _center,
const SomPos _v 
) const

Returns the distance between two neurons according to the Layout Parameter: _center Reference to the center of neighborhood Parameter: _v Position of the code vector

Definition at line 177 of file map.cpp.

178 {
179  return somLayout->dist(_center, _v);
180 }
virtual double dist(const SomPos &_center, const SomPos &_v) const =0
const Layout * somLayout
Definition: map.h:289

◆ operator=()

ClassificationMap & ClassificationMap::operator= ( const ClassificationMap op1)

Operator "=" Parameter: op1 ClassificationMap

Definition at line 363 of file map.cpp.

364 {
365  std::stringstream _str;
366  op1.printSelf(_str);
367  readSelf(_str);
368  return *this;
369 }
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:375
virtual void readSelf(std::istream &_is)
Definition: map.cpp:386

◆ PosToIndex()

unsigned ClassificationMap::PosToIndex ( const SomPos _pos) const

Return the index associated to a position Parameter: _pos Position of the code vector

Exceptions
out_of_range If _i is out of range

Definition at line 324 of file map.cpp.

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 }
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295

◆ printSelf()

void ClassificationMap::printSelf ( std::ostream &  _os) const
virtual

Standard output for a map Parameter: _os The output stream

Standard output for a SOM Parameter: _os The output stream

Reimplemented from CodeBook.

Definition at line 375 of file map.cpp.

376 {
377  _os << itemAt(0).size() << " " <<
378  somLayout->id() << " " << somWidth << " " << somHeight << " gaussian" << std::endl;
379  writeItems(_os);
380 }
const std::string & id() const
Definition: map.cpp:536
void writeItems(std::ostream &_os, bool _delim=false) const
Definition: training_set.h:561
unsigned somHeight
Definition: map.h:296
const FeatureVector & itemAt(unsigned _i) const
Definition: training_set.h:264
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ readSelf()

void ClassificationMap::readSelf ( std::istream &  _is)
virtual

Standard input for a map Parameter: _is The input stream

Standard input for a SOM Parameter: _is The input stream

Reimplemented from ClassificationTrainingSet< FeatureVector, Label >.

Definition at line 386 of file map.cpp.

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 }
void clear()
Definition: map.cpp:291
const std::string & layout() const
Definition: map.cpp:157
void toLower(char *_str)
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289
virtual void readSelf(std::istream &_is, long _dim=-1, long _size=-1)
Definition: code_book.cpp:370

◆ saveObject()

void ClassificationMap::saveObject ( std::ostream &  _os) const
virtual

Saves the ClassificationMap class into a stream. this method can be used to save the status of the class. Parameter: _os The output stream

Reimplemented from CodeBook.

Definition at line 438 of file map.cpp.

439 {
440  _os << somLayout->id() << " " << somWidth << " " << somHeight << std::endl;
441  writeClassifVectors(_os);
443 }
void writeClassifVectors(std::ostream &_os) const
Definition: code_book.cpp:453
virtual void saveObject(std::ostream &_os) const
Definition: training_set.h:349
const std::string & id() const
Definition: map.cpp:536
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295
const Layout * somLayout
Definition: map.h:289

◆ targetAtPos()

const Label & ClassificationMap::targetAtPos ( const SomPos _pos) const

Returns a const target of a code vector given its position Parameter: _pos The position of the code vector

Definition at line 266 of file map.cpp.

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 }
const Label & targetAt(unsigned _i) const
Definition: training_set.h:221
unsigned somHeight
Definition: map.h:296
unsigned somWidth
Definition: map.h:295

◆ width()

unsigned ClassificationMap::width ( ) const

Returns the width of the SOM

Definition at line 186 of file map.cpp.

187 {
188  return somWidth;
189 }
unsigned somWidth
Definition: map.h:295

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  _os,
const ClassificationMap _som 
)
friend

Definition at line 230 of file map.h.

231  {
232  _som.printSelf(_os);
233  return _os;
234  };
virtual void printSelf(std::ostream &_os) const
Definition: map.cpp:375

◆ operator>>

std::istream& operator>> ( std::istream &  _is,
ClassificationMap _som 
)
friend

Definition at line 242 of file map.h.

243  {
244  _som.readSelf(_is);
245  return _is;
246  };
virtual void readSelf(std::istream &_is)
Definition: map.cpp:386

Member Data Documentation

◆ somHeight

unsigned ClassificationMap::somHeight
protected

Definition at line 296 of file map.h.

◆ somLayout

const Layout* ClassificationMap::somLayout
protected

Definition at line 289 of file map.h.

◆ somWidth

unsigned ClassificationMap::somWidth
protected

Definition at line 295 of file map.h.


The documentation for this class was generated from the following files: