Alexandria  2.25.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SOM.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 SOM.icpp
21  * @author nikoapos
22  */
23 
24 #include "SOM/ImplTools.h"
25 
26 namespace Euclid {
27 namespace SOM {
28 
29 template <typename DistFunc>
31  : m_dimensions(dimensions)
32  , m_cells(std::make_tuple(ImplTools::indexAxis("X", x), ImplTools::indexAxis("Y", y)), m_dimensions)
33  , m_size(x, y) {
34 
35  // Initialize all the grid cells using the given function
36  for (auto array : m_cells) {
37  for (auto& w : array) {
38  w = init_func();
39  }
40  }
41 
42 } // end of namespace SOM_impl
43 
44 template <typename DistFunc>
46  return m_size;
47 }
48 
49 template <typename DistFunc>
51  return m_dimensions;
52 }
53 
54 template <typename DistFunc>
56  return m_cells(x, y);
57 }
58 
59 template <typename DistFunc>
61  return m_cells(x, y);
62 }
63 
64 template <typename DistFunc>
66  return m_cells.begin();
67 }
68 
69 template <typename DistFunc>
71  return m_cells.end();
72 }
73 
74 template <typename DistFunc>
76  return m_cells.begin();
77 }
78 
79 template <typename DistFunc>
81  return m_cells.end();
82 }
83 
84 template <typename DistFunc>
86  return m_cells.cbegin();
87 }
88 
89 template <typename DistFunc>
91  return m_cells.cend();
92 }
93 
94 namespace SOM_impl {
95 
96 template <typename DistFunc>
99  std::function<double(const GridContainer::VectorValueProxy<double>&)> dist_func) {
100  auto result_iter = som.begin();
101  double closest_distance = std::numeric_limits<double>::max();
102  for (auto iter = som.begin(); iter != som.end(); ++iter) {
103  double dist = dist_func(*iter);
104  if (dist < closest_distance) {
105  result_iter = iter;
106  closest_distance = dist;
107  }
108  }
109  return std::make_tuple(result_iter.template axisValue<0>(), result_iter.template axisValue<1>(), closest_distance);
110 }
111 
112 } // end of namespace SOM_impl
113 
114 template <typename DistFunc>
116  assert(input.size() == m_dimensions);
117  DistFunc dist_func{};
118  return SOM_impl::findBMU_impl<DistFunc>(
119  *this, [&dist_func, &input](const GridContainer::VectorValueProxy<double>& cell) -> double {
120  return dist_func.distance(cell.begin(), cell.end(), input.begin());
121  });
122 }
123 
124 template <typename DistFunc>
126  const std::vector<double>& uncertainties) const {
127  assert(input.size() == uncertainties.size() && input.size() == m_dimensions);
128  DistFunc dist_func{};
129  return SOM_impl::findBMU_impl<DistFunc>(
130  *this, [&dist_func, &input, &uncertainties](const GridContainer::VectorValueProxy<double>& cell) -> double {
131  return dist_func.distance(cell.begin(), cell.end(), input.begin(), uncertainties.begin());
132  });
133 }
134 
135 template <typename DistFunc>
136 template <typename InputType, typename WeightFunc>
138  WeightFunc weight_func) const {
139 
140  static_assert(std::is_same<decltype(std::declval<WeightFunc>()(input)), std::vector<double>>::value,
141  "WeightFunc must be callable with input as parameter, returning an std::vector<double>");
142 
143  return findBMU(weight_func(input));
144 }
145 
146 template <typename DistFunc>
147 template <typename InputType, typename WeightFunc, typename UncertaintyFunc>
148 std::tuple<std::size_t, std::size_t, double> SOM<DistFunc>::findBMU(const InputType& input, WeightFunc weight_func,
149  UncertaintyFunc uncertainty_func) const {
150 
151  static_assert(std::is_same<decltype(std::declval<WeightFunc>()(input)), std::vector<double>>::value,
152  "WeightFunc must be callable with input as parameter, returning an std::vector<double>");
153  static_assert(std::is_same<decltype(std::declval<UncertaintyFunc>()(input)), std::vector<double>>::value,
154  "UncertaintyFunc must be callable with input as parameter, returning an std::vector<double>");
155 
156  return findBMU(weight_func(input), uncertainty_func(input));
157 }
158 
159 } // namespace SOM
160 } // namespace Euclid
typename CellGridType::const_iterator const_iterator
Definition: SOM.h:56
const_iterator cend()
Definition: SOM.icpp:90
T make_tuple(T...args)
GridContainer::GridAxis< std::size_t > indexAxis(const std::string &name, std::size_t size)
Definition: ImplTools.cpp:31
const_iterator cbegin()
Definition: SOM.icpp:85
CellGridType m_cells
Definition: SOM.h:103
typename CellGridType::iterator iterator
Definition: SOM.h:55
std::tuple< std::size_t, std::size_t, double > findBMU_impl(const SOM< DistFunc > &som, std::function< double(const GridContainer::VectorValueProxy< double > &)> dist_func)
Definition: SOM.icpp:98
iterator end()
Definition: SOM.icpp:70
typename CellGridType::reference_type reference_type
Definition: SOM.h:57
iterator begin()
Definition: SOM.icpp:65
T size(T...args)
T begin(T...args)