Alexandria  2.27.0
SDC-CH common library for the Euclid project
UMatrix.icpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2022 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 "SOM/ImplTools.h"
25 #include <algorithm>
26 #include <map>
27 #include <numeric>
28 
29 namespace Euclid {
30 namespace SOM {
31 
32 namespace UMatrix_impl {
33 
34 std::map<UMatrixType, std::function<double(const std::vector<double>&)>> type_func_map{
35  {UMatrixType::MIN,
36  [](const std::vector<double>& dist_list) { return *std::min_element(dist_list.begin(), dist_list.end()); }},
37  {UMatrixType::MAX,
38  [](const std::vector<double>& dist_list) { return *std::max_element(dist_list.begin(), dist_list.end()); }},
39  {UMatrixType::MEAN, [](const std::vector<double>& dist_list) {
40  return std::accumulate(dist_list.begin(), dist_list.end(), 0.) / dist_list.size();
41  }}};
42 
43 }
44 
45 template <typename DistFunc>
46 UMatrix computeUMatrix(const SOM<DistFunc>& som, UMatrixType type) {
47 
48  DistFunc dist_func{};
49 
50  auto size = som.getSize();
51  UMatrix result = UMatrix(ImplTools::indexAxis("X", size.first), ImplTools::indexAxis("Y", size.second));
52 
53  // Chose the method used for computing the u-matrix values
54  auto type_func = UMatrix_impl::type_func_map.at(type);
55 
56  // Go through the SOM cells and compute the u-matrix cells
57  for (std::size_t x = 0; x < size.first; ++x) {
58  for (std::size_t y = 0; y < size.second; ++y) {
59 
60  // Go through the neighbor cells and create the vector with the distances
61  std::vector<double> dist_list{};
62  for (int i = int(x) - 1; i <= (int)x + 1; ++i) {
63  for (int j = int(y) - 1; j <= (int)y + 1; ++j) {
64 
65  // Check that we are not at the cell itself
66  if (i == (int)x && j == (int)y) {
67  continue;
68  }
69  // Double check that we are not outside of the SOM borders
70  if (i < 0 || i == (int)size.first || j < 0 || j == (int)size.second) {
71  continue;
72  }
73 
74  auto sxy = som(x, y);
75  auto sij = som(i, j);
76  dist_list.push_back(dist_func.distance(sxy.begin(), sxy.end(), sij.begin()));
77  }
78  }
79 
80  // Populate the u-matrix cell
81  result(x, y) = type_func(dist_list);
82  }
83  }
84 
85  return result;
86 }
87 
88 } // namespace SOM
89 } // namespace Euclid