Alexandria  2.14.1
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
UMatrix.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2020 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * @file UMatrix.icpp
21  * @author nikoapos
22  */
23 
24 #include <numeric>
25 #include <algorithm>
26 #include <map>
27 #include "SOM/_impl/ImplTools.h"
28 
29 namespace Euclid {
30 namespace SOM {
31 
32 namespace UMatrix_impl {
33 
36  [](const std::vector<double>& dist_list) {
37  return *std::min_element(dist_list.begin(), dist_list.end());
38  }
39  },
41  [](const std::vector<double>& dist_list) {
42  return *std::max_element(dist_list.begin(), dist_list.end());
43  }
44  },
46  [](const std::vector<double>& dist_list) {
47  return std::accumulate(dist_list.begin(), dist_list.end(), 0.) / dist_list.size();
48  }
49  }
50 };
51 
52 }
53 
54 template <std::size_t ND, typename DistFunc>
56 
57  DistFunc dist_func {};
58 
59  auto size = som.getSize();
60  UMatrix result = UMatrix(ImplTools::indexAxis("X", size.first), ImplTools::indexAxis("Y", size.second));
61 
62  // Chose the method used for computing the u-matrix values
63  auto type_func = UMatrix_impl::type_func_map.at(type);
64 
65  // Go through the SOM cells and compute the u-matrix cells
66  for (std::size_t x = 0; x < size.first; ++x) {
67  for (std::size_t y = 0; y < size.second; ++y) {
68 
69  // Go through the neighbor cells and create the vector with the distances
70  std::vector<double> dist_list {};
71  for (int i = int(x) - 1; i <= (int)x + 1; ++i) {
72  for (int j = int(y) - 1; j <= (int)y + 1; ++ j) {
73 
74  // Check that we are not at the cell itself
75  if (i == (int)x && j == (int)y) {
76  continue;
77  }
78  // Double check that we are not outside of the SOM borders
79  if (i < 0 || i == (int)size.first || j < 0 || j == (int)size.second) {
80  continue;
81  }
82 
83  dist_list.push_back(dist_func.distance(som(x, y), som(i, j)));
84  }
85  }
86 
87  // Populate the u-matrix cell
88  result(x, y) = type_func(dist_list);
89  }
90  }
91 
92  return result;
93 }
94 
95 }
96 }
97 
GridContainer::GridContainer< std::vector< double >, std::size_t, std::size_t > UMatrix
Definition: UMatrix.h:33
T max_element(T...args)
Representation of a multi-dimensional grid which contains axis information.
Definition: GridContainer.h:97
std::map< UMatrixType, std::function< double(const std::vector< double > &)> > type_func_map
Definition: UMatrix.icpp:34
STL class.
T push_back(T...args)
GridContainer::GridAxis< std::size_t > indexAxis(const std::string &name, std::size_t size)
Definition: ImplTools.h:31
UMatrix computeUMatrix(const SOM< ND, DistFunc > &som, UMatrixType type)
Definition: UMatrix.icpp:55
T accumulate(T...args)
T min_element(T...args)