go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
itkParameterMapInterface.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright UMC Utrecht and contributors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef itkParameterMapInterface_h
20#define itkParameterMapInterface_h
21
22#include "elxConversion.h"
23
24#include "itkObject.h"
25#include "itkObjectFactory.h"
26#include "itkMacro.h"
27#include "itkNumericTraits.h"
28
30
31#include <algorithm> // For count.
32#include <iostream>
33#include <memory> // For unique_ptr.
34#include <type_traits> // For is_same.
35
36namespace itk
37{
38
78class ParameterMapInterface : public Object
79{
80public:
82
85 using Superclass = Object;
86 using Pointer = SmartPointer<Self>;
87 using ConstPointer = SmartPointer<const Self>;
88
90 itkNewMacro(Self);
91
93 itkTypeMacro(ParameterMapInterface, Object);
94
98
100 void
102
106 // \todo: we could think of a warning level. (maybe you want warnings, but
107 // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
108 itkSetMacro(PrintErrorMessages, bool);
109 itkGetConstMacro(PrintErrorMessages, bool);
110
112 bool
113 HasParameter(const std::string & parameterName) const
114 {
115 return this->m_ParameterMap.count(parameterName) > 0;
116 }
117
119 std::size_t
120 CountNumberOfParameterEntries(const std::string & parameterName) const;
121
136 template <class T>
137 bool
138 ReadParameter(T & parameterValue,
139 const std::string & parameterName,
140 const unsigned int entry_nr,
141 const bool produceWarningMessage,
142 std::string & warningMessage) const
143 {
145 warningMessage = "";
146
148 std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
149
151 if (numberOfEntries == 0)
152 {
153 if (produceWarningMessage && this->m_PrintErrorMessages)
154 {
155 std::ostringstream outputStringStream;
156 outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested at entry number "
157 << entry_nr << ", does not exist at all.\n"
158 << " The default value \"" << parameterValue << "\" is used instead.";
159 warningMessage = outputStringStream.str();
160 }
161
162 return false;
163 }
164
166 const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
167
169 if (entry_nr >= numberOfEntries)
170 {
171 if (produceWarningMessage && this->m_PrintErrorMessages)
172 {
173 std::ostringstream outputStringStream;
174 outputStringStream << "WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
175 << entry_nr << ".\n The default value \"" << parameterValue << "\" is used instead.";
176 warningMessage = outputStringStream.str();
177 }
178 return false;
179 }
180
182 bool castSuccesful = elastix::Conversion::StringToValue(vec[entry_nr], parameterValue);
183
185 if (!castSuccesful)
186 {
187 itkExceptionMacro("ERROR: Casting entry number "
188 << entry_nr << " for the parameter \"" << parameterName << "\" failed!\n"
189 << " You tried to cast \"" << vec[entry_nr] << "\" from std::string to "
190 << typeid(parameterValue).name() << '\n');
191 }
192
193 return true;
194
195 } // end ReadParameter()
196
197
199 bool
200 ReadParameter(bool & parameterValue,
201 const std::string & parameterName,
202 const unsigned int entry_nr,
203 const bool produceWarningMessage,
204 std::string & warningMessage) const;
205
209 template <class T>
210 bool
211 ReadParameter(T & parameterValue,
212 const std::string & parameterName,
213 const unsigned int entry_nr,
214 std::string & warningMessage) const
215 {
216 return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
217 }
218
219
225 template <class T>
226 bool
227 ReadParameter(T & parameterValue,
228 const std::string & parameterName,
229 const std::string & prefix,
230 const unsigned int entry_nr,
231 const int default_entry_nr,
232 const bool produceWarningMessage,
233 std::string & warningMessage) const
234 {
235 std::string fullname = prefix + parameterName;
236 bool found = false;
237
239 std::string dummyString = "";
240 if (default_entry_nr >= 0)
241 {
243 unsigned int uintdefault = static_cast<unsigned int>(default_entry_nr);
244 found |= this->ReadParameter(parameterValue, parameterName, uintdefault, false, dummyString);
245 found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
246 found |= this->ReadParameter(parameterValue, fullname, uintdefault, false, dummyString);
247 found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
248 }
249 else
250 {
252 found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
253 found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
254 }
255
259 if (!found && produceWarningMessage && this->m_PrintErrorMessages)
260 {
261 return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
262 }
263
264 return found;
265 }
266
267
271 template <class T>
272 bool
273 ReadParameter(T & parameterValue,
274 const std::string & parameterName,
275 const std::string & prefix,
276 const unsigned int entry_nr,
277 const unsigned int default_entry_nr,
278 std::string & warningMessage) const
279 {
280 return this->ReadParameter(parameterValue, parameterName, prefix, entry_nr, default_entry_nr, true, warningMessage);
281 }
282
283
285 template <class T>
286 bool
287 ReadParameter(std::vector<T> & parameterValues,
288 const std::string & parameterName,
289 const unsigned int entry_nr_start,
290 const unsigned int entry_nr_end,
291 const bool produceWarningMessage,
292 std::string & warningMessage) const
293 {
295 warningMessage = "";
296
298 std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
299
301 if (numberOfEntries == 0)
302 {
303 if (produceWarningMessage && this->m_PrintErrorMessages)
304 {
305 std::ostringstream outputStringStream;
306 outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested between entry numbers "
307 << entry_nr_start << " and " << entry_nr_end << ", does not exist at all.\n"
308 << " The default values are used instead.";
309 warningMessage = outputStringStream.str();
310 }
311 return false;
312 }
313
315 if (entry_nr_start > entry_nr_end)
316 {
318 itkExceptionMacro("WARNING: The entry number start ("
319 << entry_nr_start << ") should be smaller than entry number end (" << entry_nr_end
320 << "). It was requested for parameter \"" << parameterName << "\".\n");
321 }
322
324 if (entry_nr_end >= numberOfEntries)
325 {
326 itkExceptionMacro("WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
327 << entry_nr_end << ".\nThe default value \"" << T{}
328 << "\" is used instead.");
329 }
330
332 const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
333
341 unsigned int j = 0;
342 for (unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i)
343 {
345 bool castSuccesful = elastix::Conversion::StringToValue(vec[i], parameterValues[j]);
346 ++j;
347
349 if (!castSuccesful)
350 {
351 itkExceptionMacro("ERROR: Casting entry number "
352 << i << " for the parameter \"" << parameterName << "\" failed!\n"
353 << " You tried to cast \"" << vec[i] << "\" from std::string to "
354 << typeid(parameterValues[0]).name() << '\n');
355 }
356 }
357
358 return true;
359 }
360
361
363 bool
364 ReadParameter(std::vector<std::string> & parameterValues,
365 const std::string & parameterName,
366 const unsigned int entry_nr_start,
367 const unsigned int entry_nr_end,
368 const bool produceWarningMessage,
369 std::string & warningMessage) const;
370
371
373 std::vector<std::string>
374 GetValues(const std::string & parameterName) const
375 {
376 const auto found = m_ParameterMap.find(parameterName);
377 return (found == m_ParameterMap.cend()) ? std::vector<std::string>{} : found->second;
378 }
379
384 template <typename T>
385 std::unique_ptr<std::vector<T>>
386 RetrieveValues(const std::string & parameterName) const
387 {
388 const auto found = m_ParameterMap.find(parameterName);
389 if (found == m_ParameterMap.end())
390 {
391 return nullptr;
392 }
393 std::vector<T> result;
394 result.reserve(found->second.size());
395
396 for (const std::string & str : found->second)
397 {
398 T value{};
399
401 {
402 result.push_back(value);
403 }
404 else
405 {
406 const auto entry_nr = &str - found->second.data();
407 itkExceptionMacro("Failed to cast parameter \"" << parameterName << "\" entry number " << entry_nr
408 << " value \"" << str << "\" to type \"" << typeid(T).name()
409 << "\"!");
410 }
411 }
412 return std::make_unique<std::vector<T>>(std::move(result));
413 }
414
415
416protected:
419
420private:
423
425};
426
427} // end of namespace itk
428
429#endif // end itkParameterMapInterface_h
static bool StringToValue(const std::string &str, T &value)
std::map< std::string, ParameterValuesType > ParameterMapType
std::vector< std::string > ParameterValuesType
Implements functionality to get parameters from a parameter map.
ParameterFileParser::ParameterValuesType ParameterValuesType
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
bool ReadParameter(bool &parameterValue, const std::string &parameterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
void SetParameterMap(const ParameterMapType &parMap)
ITK_DISALLOW_COPY_AND_MOVE(ParameterMapInterface)
std::vector< std::string > GetValues(const std::string &parameterName) const
bool HasParameter(const std::string &parameterName) const
ParameterFileParser::ParameterMapType ParameterMapType
bool ReadParameter(T &parameterValue, const std::string &parameterName, const std::string &prefix, const unsigned int entry_nr, const int default_entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
std::vcl_size_t CountNumberOfParameterEntries(const std::string &parameterName) const
bool ReadParameter(std::vector< std::string > &parameterValues, const std::string &parameterName, const unsigned int entry_nr_start, const unsigned int entry_nr_end, const bool produceWarningMessage, std::string &warningMessage) const
bool ReadParameter(std::vector< T > &parameterValues, const std::string &parameterName, const unsigned int entry_nr_start, const unsigned int entry_nr_end, const bool produceWarningMessage, std::string &warningMessage) const
std::unique_ptr< std::vector< T > > RetrieveValues(const std::string &parameterName) const
SmartPointer< const Self > ConstPointer
bool ReadParameter(T &parameterValue, const std::string &parameterName, const std::string &prefix, const unsigned int entry_nr, const unsigned int default_entry_nr, std::string &warningMessage) const
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, std::string &warningMessage) const


Generated on 2024-07-17 for elastix by doxygen 1.11.0 (9b424b03c9833626cd435af22a444888fbbb192d) elastix logo