Xmipp  v3.23.11-Nereus
batch_som.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  BatchSOM.cc
28  Implements Kohonen Self-Organizing Feature Maps by using Batch SOM
29 -----------------------------------------------------------------------------*/
30 
31 #include "batch_som.h"
32 
38 {
39  readSelf(_is);
40 }
41 
48 {
49 
50 
51  unsigned long t = 0;
52 
53  int verbosity = listener->getVerbosity();
54  if (verbosity)
55  listener->OnReportOperation((std::string) "Batch Training Kohonen SOM....\n");
56  if (verbosity == 1 || verbosity == 3)
58 
59  SomIn aveVector(_som.theItems[0].size());
60  std::vector<unsigned> tmpVector;
61 
62  while (t < somNSteps)
63  {
64  _som.classify(&_ts);
65  // Check for each SOM unit
66  for (unsigned it = 0; it < _som.size(); it++)
67  {
68  for (unsigned a = 0; a < aveVector.size(); a++)
69  aveVector[a] = 0.;
70  long total = 0;
71  // Collects a list of the input vectors assigned to the neighborhood
72  std::vector<unsigned> neig = _som.neighborhood(_som.indexToPos(it), ceil(somRadius(t, somNSteps)));
73  for (std::vector<unsigned>::iterator itt = neig.begin();itt < neig.end();itt++)
74  {
75  tmpVector = _som.classifAt(*itt);
76  for (unsigned j = 0 ; j < tmpVector.size() ; j++)
77  {
78  SomIn v = _ts.theItems[tmpVector[j]];
79  for (unsigned a = 0; a < v.size(); a++)
80  aveVector[a] += v[a];
81  total++;
82  }
83 
84  }
85  if (total != 0)
86  {
87  for (unsigned a = 0; a < aveVector.size(); a++)
88  aveVector[a] /= (floatFeature) total;
89  _som.theItems[it] = aveVector;
90  }
91  }
92 
93  if (verbosity == 1 || verbosity == 3)
94  listener->OnProgress(t);
95  if (verbosity >= 2)
96  {
97  char s[100];
98  sprintf(s, "Iteration %d of %d.\n", (int)(t + 1), (int)somNSteps);
99  listener->OnReportOperation((std::string) s);
100  }
101  t++;
102 
103  } // while t < somSteps
104 
105  if (verbosity == 1 || verbosity == 3)
107 }
108 
109 
SomPos indexToPos(const unsigned &_i) const
Definition: map.cpp:306
virtual void classify(const ClassicTrainingVectors *_ts)
Definition: code_book.cpp:238
virtual void OnReportOperation(const std::string &_rsOp)=0
virtual void OnInitOperation(unsigned long _est_it)=0
virtual const unsigned & getVerbosity() const
Definition: xmipp_funcs.h:1065
unsigned long somNSteps
Neighborhood type for training (Bubble or Gaussian)
Definition: som.h:278
float floatFeature
Definition: data_types.h:72
std::vector< unsigned > neighborhood(const SomPos &_center, double _radius) const
Definition: map.cpp:167
FeatureVector SomIn
Definition: map.h:44
virtual const std::vector< unsigned > & classifAt(const unsigned &_index) const
Definition: code_book.cpp:286
std::vector< Item > theItems
Definition: training_set.h:84
virtual void readSelf(std::istream &_is)
Definition: som.cpp:209
Definition: som.h:136
virtual void train(ClassificationMap &_som, const ClassicTrainingVectors &_ts) const
Definition: batch_som.cpp:47
virtual void OnProgress(unsigned long _it)=0
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
#define j
BatchSOM(Descent &_radius, unsigned long _nSteps)
Definition: batch_som.h:54
Descent somRadius
alpha(t)
Definition: som.h:276
doublereal * a