go home Home | Main Page | Topics | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
Loading...
Searching...
No Matches
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
77
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
94
98
100 void
102
104 const ParameterMapType &
106 {
107 return m_ParameterMap;
108 }
109
113 // \todo: we could think of a warning level. (maybe you want warnings, but
114 // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
115 itkSetMacro(PrintErrorMessages, bool);
116 itkGetConstMacro(PrintErrorMessages, bool);
117
119 bool
120 HasParameter(const std::string & parameterName) const
121 {
122 return this->m_ParameterMap.count(parameterName) > 0;
123 }
124
126 std::size_t
127 CountNumberOfParameterEntries(const std::string & parameterName) const;
128
143 template <typename T>
144 bool
145 ReadParameter(T & parameterValue,
146 const std::string & parameterName,
147 const unsigned int entry_nr,
148 const bool produceWarningMessage,
149 std::string & warningMessage) const
150 {
152 warningMessage = "";
153
155 std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
156
158 if (numberOfEntries == 0)
159 {
160 if (produceWarningMessage && this->m_PrintErrorMessages)
161 {
162 std::ostringstream outputStringStream;
163 outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested at entry number "
164 << entry_nr << ", does not exist at all.\n"
165 << " The default value \"" << parameterValue << "\" is used instead.";
166 warningMessage = outputStringStream.str();
167 }
168
169 return false;
170 }
171
173 const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
174
176 if (entry_nr >= numberOfEntries)
177 {
178 if (produceWarningMessage && this->m_PrintErrorMessages)
179 {
180 std::ostringstream outputStringStream;
181 outputStringStream << "WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
182 << entry_nr << ".\n The default value \"" << parameterValue << "\" is used instead.";
183 warningMessage = outputStringStream.str();
184 }
185 return false;
186 }
187
189 bool castSuccesful = elastix::Conversion::StringToValue(vec[entry_nr], parameterValue);
190
192 if (!castSuccesful)
193 {
194 itkExceptionMacro("ERROR: Casting entry number "
195 << entry_nr << " for the parameter \"" << parameterName << "\" failed!\n"
196 << " You tried to cast \"" << vec[entry_nr] << "\" from std::string to "
197 << typeid(parameterValue).name() << '\n');
198 }
199
200 return true;
201
202 } // end ReadParameter()
203
204
206 bool
207 ReadParameter(bool & parameterValue,
208 const std::string & parameterName,
209 const unsigned int entry_nr,
210 const bool produceWarningMessage,
211 std::string & warningMessage) const;
212
216 template <typename T>
217 bool
218 ReadParameter(T & parameterValue,
219 const std::string & parameterName,
220 const unsigned int entry_nr,
221 std::string & warningMessage) const
222 {
223 return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
224 }
225
226
232 template <typename T>
233 bool
234 ReadParameter(T & parameterValue,
235 const std::string & parameterName,
236 const std::string & prefix,
237 const unsigned int entry_nr,
238 const int default_entry_nr,
239 const bool produceWarningMessage,
240 std::string & warningMessage) const
241 {
242 std::string fullname = prefix + parameterName;
243 bool found = false;
244
246 std::string dummyString = "";
247 if (default_entry_nr >= 0)
248 {
250 auto uintdefault = static_cast<unsigned int>(default_entry_nr);
251 found |= this->ReadParameter(parameterValue, parameterName, uintdefault, false, dummyString);
252 found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
253 found |= this->ReadParameter(parameterValue, fullname, uintdefault, false, dummyString);
254 found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
255 }
256 else
257 {
259 found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
260 found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
261 }
262
266 if (!found && produceWarningMessage && this->m_PrintErrorMessages)
267 {
268 return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
269 }
270
271 return found;
272 }
273
274
278 template <typename T>
279 bool
280 ReadParameter(T & parameterValue,
281 const std::string & parameterName,
282 const std::string & prefix,
283 const unsigned int entry_nr,
284 const unsigned int default_entry_nr,
285 std::string & warningMessage) const
286 {
287 return this->ReadParameter(parameterValue, parameterName, prefix, entry_nr, default_entry_nr, true, warningMessage);
288 }
289
290
292 template <typename T>
293 bool
294 ReadParameter(std::vector<T> & parameterValues,
295 const std::string & parameterName,
296 const unsigned int entry_nr_start,
297 const unsigned int entry_nr_end,
298 const bool produceWarningMessage,
299 std::string & warningMessage) const
300 {
302 warningMessage = "";
303
305 std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
306
308 if (numberOfEntries == 0)
309 {
310 if (produceWarningMessage && this->m_PrintErrorMessages)
311 {
312 std::ostringstream outputStringStream;
313 outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested between entry numbers "
314 << entry_nr_start << " and " << entry_nr_end << ", does not exist at all.\n"
315 << " The default values are used instead.";
316 warningMessage = outputStringStream.str();
317 }
318 return false;
319 }
320
322 if (entry_nr_start > entry_nr_end)
323 {
325 itkExceptionMacro("WARNING: The entry number start ("
326 << entry_nr_start << ") should be smaller than entry number end (" << entry_nr_end
327 << "). It was requested for parameter \"" << parameterName << "\".\n");
328 }
329
331 if (entry_nr_end >= numberOfEntries)
332 {
333 itkExceptionMacro("WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
334 << entry_nr_end << ".\nThe default value \"" << T{}
335 << "\" is used instead.");
336 }
337
339 const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
340
346
348 unsigned int j = 0;
349 for (unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i)
350 {
352 bool castSuccesful = elastix::Conversion::StringToValue(vec[i], parameterValues[j]);
353 ++j;
354
356 if (!castSuccesful)
357 {
358 itkExceptionMacro("ERROR: Casting entry number "
359 << i << " for the parameter \"" << parameterName << "\" failed!\n"
360 << " You tried to cast \"" << vec[i] << "\" from std::string to "
361 << typeid(parameterValues[0]).name() << '\n');
362 }
363 }
364
365 return true;
366 }
367
368
370 bool
371 ReadParameter(std::vector<std::string> & parameterValues,
372 const std::string & parameterName,
373 const unsigned int entry_nr_start,
374 const unsigned int entry_nr_end,
375 const bool produceWarningMessage,
376 std::string & warningMessage) const;
377
378
380 std::vector<std::string>
381 GetValues(const std::string & parameterName) const
382 {
383 const auto found = m_ParameterMap.find(parameterName);
384 return (found == m_ParameterMap.cend()) ? std::vector<std::string>{} : found->second;
385 }
386
391 template <typename T>
392 std::unique_ptr<std::vector<T>>
393 RetrieveValues(const std::string & parameterName) const
394 {
395 const auto found = m_ParameterMap.find(parameterName);
396 if (found == m_ParameterMap.end())
397 {
398 return nullptr;
399 }
400 std::vector<T> result;
401 result.reserve(found->second.size());
402
403 for (const std::string & str : found->second)
404 {
405 T value{};
406
408 {
409 result.push_back(value);
410 }
411 else
412 {
413 const auto entry_nr = &str - found->second.data();
414 itkExceptionMacro("Failed to cast parameter \"" << parameterName << "\" entry number " << entry_nr
415 << " value \"" << str << "\" to type \"" << typeid(T).name()
416 << "\"!");
417 }
418 }
419 return std::make_unique<std::vector<T>>(std::move(result));
420 }
421
422
423protected:
426
427private:
430
432};
433
434} // end of namespace itk
435
436#endif // end itkParameterMapInterface_h
static bool StringToValue(const std::string &str, T &value)
std::map< std::string, ParameterValuesType > ParameterMapType
std::vector< std::string > ParameterValuesType
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
const ParameterMapType & GetParameterMap() const
void SetParameterMap(const ParameterMapType &parMap)
itkOverrideGetNameOfClassMacro(ParameterMapInterface)
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 1774142652 for elastix by doxygen 1.15.0 elastix logo