Medical Imaging Interaction Toolkit  2018.4.99-08619e4f
Medical Imaging Interaction Toolkit
mitkModelFitStaticParameterMap.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #include <algorithm>
14 
16 #include "mitkModelFitException.h"
17 
18 void mitk::modelFit::StaticParameterMap::Add(const std::string& name, const ValueType& newList)
19 {
20  MapType::const_iterator mapIter = m_map.find(name);
21 
22  // Only add if the name doesn't exist yet
23  if (mapIter == m_map.end())
24  {
25  // If the new list contains more than one values, make sure it contains the same number of
26  // values as other lists that contain more than one value
27  if (newList.size() > 1)
28  {
29  if (m_numValues > 1 && newList.size() != m_numValues)
30  {
31  mitkThrowException(ModelFitException) << "Unable to add variable '" << name
32  << "' to StaticParameterMap: It contains "
33  << newList.size() << "values (should be "
34  << m_numValues;
35  }
36  else if (m_numValues == 1)
37  {
38  m_numValues = newList.size();
39  }
40  }
41 
42  m_map.insert(StaticParameterType(name, newList));
43  }
44 }
45 
47 mitk::modelFit::StaticParameterMap::Get(const std::string& name) const
48 {
49  MapType::const_iterator mapIter = m_map.find(name);
50 
51  if (mapIter != m_map.end())
52  {
53  return mapIter->second;
54  }
55 
56  throw std::range_error("'" + name + "' does not exist in InputStaticParameterMap");
57 }
58 
59 void mitk::modelFit::StaticParameterMap::Sort(const std::string& name /* = "" */)
60 {
61  if (m_map.empty() || m_numValues == 1)
62  {
63  return;
64  }
65 
66  // Copy the actual map and operate on the copy to fulfill exception guarantees.
67  MapType localMap = m_map;
68 
69  // Copy map contents to a m*n matrix where n is the number of values and m is the number of
70  // variables
71  std::vector<ValueType> temp;
72 
73  for (size_t i = 0; i < m_numValues; ++i)
74  {
75  temp.push_back(ValueType());
76  }
77 
78  {
79  MapType::const_iterator foundPair = localMap.find(name);
80 
81  if (!name.empty())
82  {
83  if (foundPair != localMap.end())
84  {
85  // An existing variable name has been given, so make sure its values are in the
86  // first row of the matrix
87  const ValueType& list = foundPair->second;
88 
89  if (list.size() > 1) // only copy lists that have more than one value
90  {
91  for (size_t i = 0; i < list.size(); ++i)
92  {
93  temp[i].push_back(list[i]);
94  }
95  }
96  }
97  }
98 
99  // Copy the rest of the values to the matrix
100  for (MapType::const_iterator mapIter = localMap.begin(); mapIter != localMap.end();
101  ++mapIter)
102  {
103  if (!name.empty() && name == mapIter->first)
104  {
105  // Skip this variable since it was already added (in the first row)
106  continue;
107  }
108 
109  const ValueType& list = mapIter->second;
110 
111  if (list.size() > 1) // only copy lists that have more than one value
112  {
113  for (size_t i = 0; i < list.size(); ++i)
114  {
115  temp[i].push_back(list[i]);
116  }
117  }
118  }
119  }
120 
121  // Sort the matrix
122  std::sort(temp.begin(), temp.end());
123 
124  // Copy the matrix contents back to the map
125  {
126  int i = 0;
127  MapType::iterator foundPair = localMap.find(name);
128 
129  if (foundPair != localMap.end())
130  {
131  // An existing variable name has been given, so make sure the first row of the matrix is
132  // copied back to that variable
133  ValueType& list = foundPair->second;
134 
135  if (list.size() > 1) // only copy lists that have more than one value
136  {
137  list.clear();
138 
139  for (size_t j = 0; j < temp.size(); ++j)
140  {
141  list.push_back(temp[j][i]);
142  }
143 
144  ++i;
145  }
146  }
147 
148  // Copy the rest of the matrix back to the map
149  for (MapType::iterator mapIter = localMap.begin(); mapIter != localMap.end(); ++mapIter)
150  {
151  if (!name.empty() && name == mapIter->first)
152  {
153  // Skip this variable since it was already filled (from the first row)
154  continue;
155  }
156 
157  ValueType& list = mapIter->second;
158 
159  if (list.size() > 1) // only copy lists that have more than one value
160  {
161  list.clear();
162 
163  for (size_t j = 0; j < temp.size(); ++j)
164  {
165  list.push_back(temp[j][i]);
166  }
167 
168  ++i;
169  }
170  }
171  }
172 
173  // Write the now sorted copy back to the actual map
174  m_map = localMap;
175 }
176 
178 {
179  m_map.clear();
180  m_numValues = 1;
181 }
std::pair< KeyType, ValueType > StaticParameterType
Type of a variable, consisting of name and value list.
ModelBase::StaticParameterValuesType ValueType
Type of the map value.
void Add(const std::string &name, const ValueType &newList)
Adds the given value list under the given variable name if the name doesn&#39;t exist already...
void Clear()
Resets the map, so it&#39;s empty.
const ValueType & Get(const std::string &name) const
Returns the values of the given variable name.
#define mitkThrowException(classname)
Class-specific exception for modelfits.
void Sort(const std::string &name="")
Sorts the values of the given variable name in ascending order. The values of all other variables wil...