Alexandria  2.14.1
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
SOMProjector.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 SOMProjector.icpp
21  * @author nikoapos
22  */
23 
24 #include <tuple>
25 #include "SOM/_impl/ImplTools.h"
26 
27 namespace Euclid {
28 namespace SOM {
29 
30 namespace SOMProjector_impl {
31 
32 template <typename T, std::size_t ND, typename DistFunc, typename InputIter, typename AdderFunc, typename BmuFunc>
33 SOMProjector::ProjectGrid<T> project_impl(const SOM<ND, DistFunc>& som, InputIter begin, InputIter end,
34  AdderFunc adder_func, BmuFunc bmu_func, const T& init_cell) {
35 
36  // Create the grid to return
37  auto size = som.getSize();
38  SOMProjector::ProjectGrid<T> result {ImplTools::indexAxis("X", size.first), ImplTools::indexAxis("Y", size.second)};
39 
40  // Set all the cells to the default value
41  for (auto& cell : result) {
42  cell = init_cell;
43  }
44 
45  // Iterate through all the inputs and project them to the result
46  for (auto it = begin; it != end; ++it) {
47  auto& input = *it;
48 
49  // Get the BMU coordinates
50  std::size_t x;
51  std::size_t y;
52  double dist;
53  std::tie(x, y, dist) = bmu_func(input);
54 
55  // Project the input to the result cell
56  adder_func(result(x, y), input);
57  }
58 
59  return result;
60 }
61 
62 }
63 
64 template <typename T, std::size_t ND, typename DistFunc, typename InputIter, typename WeightFunc, typename AdderFunc>
65 SOMProjector::ProjectGrid<T> SOMProjector::project(const SOM<ND, DistFunc>& som, InputIter begin, InputIter end, WeightFunc weight_func,
66  AdderFunc adder_func, const T& init_cell) {
67 
68  auto bmu_func = [&som, &weight_func](const typename std::iterator_traits<InputIter>::value_type & input) {
69  return som.findBMU(input, weight_func);
70  };
71 
72  return SOMProjector_impl::project_impl(som, begin, end, adder_func, bmu_func, init_cell);
73 }
74 
75 template <typename T, std::size_t ND, typename DistFunc, typename InputIter, typename WeightFunc, typename UncertaintyFunc, typename AdderFunc>
76 SOMProjector::ProjectGrid<T> SOMProjector::project(const SOM<ND, DistFunc>& som, InputIter begin, InputIter end, WeightFunc weight_func,
77  UncertaintyFunc uncertainty_func, AdderFunc adder_func, const T& init_cell) {
78 
79  auto bmu_func = [&som, &weight_func, &uncertainty_func](const typename std::iterator_traits<InputIter>::value_type & input) {
80  return som.findBMU(input, weight_func, uncertainty_func);
81  };
82 
83  return SOMProjector_impl::project_impl(som, begin, end, adder_func, bmu_func, init_cell);
84 }
85 
86 }
87 }
88 
T tie(T...args)
Representation of a multi-dimensional grid which contains axis information.
Definition: GridContainer.h:97
GridContainer::GridAxis< std::size_t > indexAxis(const std::string &name, std::size_t size)
Definition: ImplTools.h:31
static ProjectGrid< T > project(const SOM< ND, DistFunc > &som, InputIter begin, InputIter end, WeightFunc weight_func, AdderFunc adder_func, const T &init_cell=T{})
SOMProjector::ProjectGrid< T > project_impl(const SOM< ND, DistFunc > &som, InputIter begin, InputIter end, AdderFunc adder_func, BmuFunc bmu_func, const T &init_cell)