Xmipp  v3.23.11-Nereus
fuzzy_code_book.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 // xmippFuzzyCodeBook.cc
27 // Implements a set of fuzzy code vectors, that is a Fuzzy code book.
28 //-----------------------------------------------------------------------------
29 
30 #include "fuzzy_code_book.h"
31 
41 FuzzyCodeBook::FuzzyCodeBook(unsigned _n, unsigned _size, unsigned _data,
42  bool _cal): CodeBook(_n, _size, _cal)
43 {
44 
45  // Initialize Fuzzy membership Matrix
46 
47  numClusters = _n;
48  numVectors = _data;
49  memb.resize(numVectors);
50  for (unsigned k = 0; k < numVectors; k++)
51  {
52  std::vector <floatFeature> v;
53  v.resize(numClusters, 0);
54  memb[k] = v;
55  } // for k
56 
57 }
58 
59 
72 FuzzyCodeBook::FuzzyCodeBook(unsigned _n, unsigned _size, unsigned _data, double _lower, double _upper,
73  bool _cal): CodeBook(_n, _size, _lower, _upper, _cal)
74 {
75 
76  // Initialize Fuzzy membership Matrix
77 
78  numClusters = _n;
79  numVectors = _data;
80  memb.resize(numVectors);
81  for (unsigned k = 0; k < numVectors; k++)
82  {
83  std::vector <floatFeature> v;
84  v.resize(numClusters, 0);
85  memb[k] = v;
86  } // for k
87 
88 }
89 
100 /* Part of this code were developed by Lorenzo Zampighi and Nelson Tang
101  of the department of Physiology of the David Geffen School of Medistd::cine,
102  University of California, Los Angeles
103 */
104 
106  const bool _use_rand_cvs) : CodeBook(_n, _ts, _use_rand_cvs)
107 {
108  // Initialize Fuzzy membership Matrix
109 
110  numClusters = _n;
111  numVectors = _ts.size();
112  memb.resize(numVectors);
113  std::vector <floatFeature> v;
114  v.resize(numClusters, 0);
115  for (unsigned k = 0; k < numVectors; k++)
116  {
117  memb[k] = v;
118  } // for k
119 }
120 
127 FuzzyCodeBook::FuzzyCodeBook(std::istream& _is, const unsigned _size)
128 {
129  readSelf(_is);
130  // Initialize Fuzzy membership Matrix
131 
132  numClusters = theItems.size();
133  numVectors = _size;
134  memb.clear();
135  memb.resize(numVectors);
136  for (unsigned k = 0; k < numVectors; k++)
137  {
138  std::vector <floatFeature> v;
139  v.resize(numClusters, 0);
140  memb[k] = v;
141  } // for k
142 }
143 
144 
148 #ifdef UNUSED // detected as unused 29.6.2018
149 
155 floatFeature FuzzyCodeBook::membAt(unsigned _di, unsigned _ci) const
156 {
157  std::ostringstream msg;
158  if ((_di >= membVectors()) || (_ci >= membClusters()))
159  {
160  msg << "Out of range. No item at position " << _di << "," << _ci << std::endl;
161  throw std::out_of_range(msg.str());
162  }
163 
164  return memb[_di][_ci];
165 }
166 #endif
167 
174 floatFeature& FuzzyCodeBook::membAt(unsigned _di, unsigned _ci)
175 {
176  std::ostringstream msg;
177  if ((_di >= membVectors()) || (_ci >= membClusters()))
178  {
179  msg << "Out of range. No item at position " << _di << "," << _ci << std::endl;
180  throw std::out_of_range(msg.str());
181  }
182 
183  return memb[_di][_ci];
184 }
185 
186 
192 {
193  return numClusters;
194 }
196 {
197  return numVectors;
198 }
199 
200 
210 {
211  double maxMemb = 0;
212  unsigned best = 0;
213 
214  for (unsigned c = 0; c < membClusters(); c++)
215  {
216  if (maxMemb < memb[_in][c])
217  {
218  maxMemb = (double) memb[_in][c];
219  best = c;
220  } //if
221  } // for i
222 
223  return (FeatureVector&) theItems[best];
224 }
225 
233 unsigned FuzzyCodeBook::fuzzyTestIndex(unsigned _in) const
234 {
235  double maxMemb = 0;
236  unsigned best = 0;
237 
238  for (unsigned c = 0; c < membClusters(); c++)
239  {
240  if (maxMemb < memb[_in][c])
241  {
242  maxMemb = (double) memb[_in][c];
243  best = c;
244  } //if
245  } // for i
246 
247  return best;
248 }
249 
250 #ifdef UNUSED // detected as unused 29.6.2018
251 
255 Label FuzzyCodeBook::fuzzyApply(unsigned _in) const
256 {
257  return theTargets[fuzzyTestIndex(_in)];
258 }
259 
266 void FuzzyCodeBook::fuzzyCalibrate(ClassicTrainingVectors& _ts, Label _def)
267 {
268  // set the default label
269  for (std::vector<FeatureVector>::const_iterator i = itemsBegin();
270  i < itemsEnd() ; i++)
271  theTargets[i - itemsBegin()] = _def;
272 
273  if (_ts.calibrated())
274  {
275  for (unsigned j = 0 ; j < _ts.size() ; j++)
277  calibrated(true);
278  }
279  else
280  calibrated(false);
281 }
282 #endif
283 
289 unsigned FuzzyCodeBook::fuzzyWinner(unsigned _in) const
290 {
291  return fuzzyTestIndex(_in);
292 }
293 
299 unsigned FuzzyCodeBook::fuzzyOutput(unsigned _in) const
300 {
301  return fuzzyTestIndex(_in);
302 }
303 
304 
311 {
312  classifVectors.clear(); // clear previous classification.
313  classifVectors.resize(size());
314  aveDistances.clear(); // clear previous classification.
315  aveDistances.resize(size());
316 
317  for (unsigned j = 0 ; j < _ts->size() ; j++)
318  classifVectors[fuzzyTestIndex(j)].push_back(j);
319 
320  for (unsigned i = 0 ; i < size() ; i++)
321  {
322  double aveDist = 0;
323  for (unsigned j = 0 ; j < classifVectors[i].size() ; j++)
324  aveDist += euclideanDistance(theItems[i], _ts->theItems[classifVectors[i][j]]);
325  if (classifVectors[i].size() != 0)
326  aveDist /= (double) classifVectors[i].size();
327  aveDistances[i] = (double) aveDist;
328  }
329 
330 }
331 
332 #ifdef UNUSED // detected as unused 29.6.2018
333 
338 void FuzzyCodeBook::hardPartition()
339 {
340 
341  for (unsigned k = 1; k < membVectors(); k++)
342  { // Number of input vectors
343  double maxMemb = 0;
344  unsigned maxIndex = membClusters(); // Impossible cluster index
345  for (unsigned i = 0; i < membClusters(); i++)
346  { // Number of clusters
347  if (maxMemb < memb[k][i])
348  { // Always find the maximum
349  if (maxIndex != membClusters())
350  memb[k][maxIndex] = 0.0; // Previous maximum index set to 0
351  maxMemb = memb[k][i];
352  maxIndex = i;
353  memb[k][i] = 1.0; // Assume this is the maximum
354  }
355  else // if maxMemb
356  memb[k][i] = 0.0;
357  } // for i
358  } // for k
359 }
360 #endif
361 
369 FuzzyCodeBook::TS FuzzyCodeBook::alphaCore(TS _ts, double _alpha, unsigned _cluster) const
370 {
371  FuzzyCodeBook::TS _alphaSet(0, _ts.calibrated());
372 
373  _alphaSet.theItems.resize(membVectors());
374  if (_ts.calibrated())
375  _alphaSet.theTargets.resize(membVectors());
376 
377  if ((_alpha < 0) || (_alpha > 1))
378  {
379  std::ostringstream msg;
380  msg << "threshold must be in [0,1]";
381  throw std::runtime_error(msg.str());
382  }
383 
384  if ((_cluster < 0) || (_cluster >= membClusters()))
385  {
386  std::ostringstream msg;
387  msg << "Invalid cluster number";
388  throw std::runtime_error(msg.str());
389  }
390  for (unsigned k = 1; k < membVectors(); k++)
391  { // Number of input vectors
392 
393  double maxMemb = memb[k][_cluster];
394  unsigned maxIndex = _cluster; // Impossible cluster index
395  for (unsigned i = 0; i < membClusters(); i++)
396  { // Number of clusters
397  if (i != _cluster)
398  if (maxMemb < memb[k][i])
399  { // Always find the maximum
400  maxMemb = memb[k][i];
401  maxIndex = i;
402  }
403  } // for i
404 
405  if (maxIndex == _cluster && maxMemb >= _alpha) // If above threshold
406  _alphaSet.theItems[k] = _ts.theItems[k];
407  if (_ts.calibrated())
408  _alphaSet.theTargets[k] = _ts.theTargets[k];
409 
410  } // for k
411 
412  return _alphaSet;
413 }
414 
415 
420 void FuzzyCodeBook::writeMembership(std::ostream& _os) const
421 {
422  _os << membVectors() << std::endl;
423  for (unsigned k = 0; k < numVectors; k++) // Number of input vectors
424  _os << memb[k] << std::endl;
425 }
426 
427 
433 {
434  _is >> numVectors;
435  memb.resize(numVectors);
436  for (unsigned k = 0; k < numVectors; k++) // Number of input vectors
437  _is >> memb[k];
438  numClusters = memb[0].size();
439 }
440 
441 
447 void FuzzyCodeBook::saveObject(std::ostream& _os) const
448 {
449  writeMembership(_os);
451 }
452 
453 
460 void FuzzyCodeBook::readSelf(std::istream& _is, const unsigned _size)
461 {
462  CodeBook::readSelf(_is);
463  // Initialize Fuzzy membership Matrix
464  numClusters = theItems.size();
465  numVectors = _size;
466  memb.clear();
467  memb.resize(numVectors);
468  for (size_t k = 0; k < numVectors; k++)
469  {
470  std::vector <floatFeature> v;
471  v.resize(numClusters, 0);
472  memb[k] = v;
473  } // for k
474 }
475 
476 
477 
484 {
485  clear();
486  readMembership(_is);
488 }
489 
490 #ifdef UNUSED // detected as unused 29.6.2018
491 
495 void FuzzyCodeBook::printDensity(std::ostream& _os) const
496 {
497  _os << "1" << std::endl;
498  for (size_t j = 0; j < numClusters; j++)
499  {
500  double sumDens = 0;
501  for (size_t i = 0; i < numVectors; i++)
502  sumDens += (double) memb[i][j];
503  _os << j << " " << sumDens << std::endl;
504  }
505 }
506 #endif
507 
508 //-----------------------------------------------------------------------------
509 
virtual void loadObject(std::istream &_is)
floatFeature & membAt(unsigned _di, unsigned _ci)
std::vector< FeatureVector >::const_iterator itemsBegin() const
Definition: training_set.h:624
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
doublereal * c
std::string Label
Definition: data_types.h:79
virtual unsigned fuzzyTestIndex(unsigned _in) const
MM memb
Alias for Fuzzy vectors.
virtual void saveObject(std::ostream &_os) const
float floatFeature
Definition: data_types.h:72
unsigned membVectors() const
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
virtual void classify(const ClassicTrainingVectors *_ts)
virtual unsigned fuzzyWinner(unsigned _in) const
virtual unsigned fuzzyOutput(unsigned _in) const
std::vector< double > aveDistances
Definition: code_book.h:62
virtual void readMembership(std::istream &_is)
virtual void readSelf(std::istream &_is, const unsigned _size=0)
virtual void writeMembership(std::ostream &_os) const
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
#define j
unsigned membClusters() const
std::vector< FeatureVector >::const_iterator itemsEnd() const
Definition: training_set.h:633
virtual FeatureVector & fuzzyTest(unsigned _in) const
FuzzyCodeBook(const bool &_calib=false)
std::vector< std::vector< unsigned > > classifVectors
Definition: code_book.h:61
std::vector< floatFeature > FeatureVector
Definition: data_types.h:86
virtual void loadObject(std::istream &_is)
Definition: code_book.cpp:478
virtual TS alphaCore(TS _ts, double _alpha, unsigned _cluster) const
virtual void readSelf(std::istream &_is, long _dim=-1, long _size=-1)
Definition: code_book.cpp:370
virtual void saveObject(std::ostream &_os) const
Definition: code_book.cpp:466