Loading...
Searching...
No Matches
itkParameterMapInterface.h
Go to the documentation of this file.
/*=========================================================================
*
* Copyright UMC Utrecht and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkParameterMapInterface_h
#define itkParameterMapInterface_h
#include "elxConversion.h"
#include "itkObject.h"
#include "itkObjectFactory.h"
#include "itkMacro.h"
#include "itkNumericTraits.h"
#include "itkParameterFileParser.h"
#include <algorithm> // For count.
#include <iostream>
#include <memory> // For unique_ptr.
#include <type_traits> // For is_same.
namespace itk
{
class ParameterMapInterface : public Object
{
public:
ITK_DISALLOW_COPY_AND_MOVE(ParameterMapInterface);
using Self = ParameterMapInterface;
using Superclass = Object;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
itkNewMacro(Self);
itkTypeMacro(ParameterMapInterface, Object);
using ParameterValuesType = ParameterFileParser::ParameterValuesType;
using ParameterMapType = ParameterFileParser::ParameterMapType;
void
SetParameterMap(const ParameterMapType & parMap);
// \todo: we could think of a warning level. (maybe you want warnings, but
// not when for example a parameter is not found at entry entry_nr but at entry 0 instead
itkSetMacro(PrintErrorMessages, bool);
itkGetConstMacro(PrintErrorMessages, bool);
bool
HasParameter(const std::string & parameterName) const
{
return this->m_ParameterMap.count(parameterName) > 0;
}
std::size_t
CountNumberOfParameterEntries(const std::string & parameterName) const;
template <class T>
bool
ReadParameter(T & parameterValue,
const std::string & parameterName,
const unsigned int entry_nr,
const bool produceWarningMessage,
std::string & warningMessage) const
{
warningMessage = "";
std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
if (numberOfEntries == 0)
{
if (produceWarningMessage && this->m_PrintErrorMessages)
{
std::ostringstream outputStringStream;
outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested at entry number "
<< entry_nr << ", does not exist at all.\n"
<< " The default value \"" << parameterValue << "\" is used instead.";
warningMessage = outputStringStream.str();
}
return false;
}
const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
if (entry_nr >= numberOfEntries)
{
if (produceWarningMessage && this->m_PrintErrorMessages)
{
std::ostringstream outputStringStream;
outputStringStream << "WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
<< entry_nr << ".\n The default value \"" << parameterValue << "\" is used instead.";
warningMessage = outputStringStream.str();
}
return false;
}
bool castSuccesful = elastix::Conversion::StringToValue(vec[entry_nr], parameterValue);
if (!castSuccesful)
{
itkExceptionMacro("ERROR: Casting entry number "
<< entry_nr << " for the parameter \"" << parameterName << "\" failed!\n"
<< " You tried to cast \"" << vec[entry_nr] << "\" from std::string to "
<< typeid(parameterValue).name() << '\n');
}
return true;
} // end ReadParameter()
bool
ReadParameter(bool & parameterValue,
const std::string & parameterName,
const unsigned int entry_nr,
const bool produceWarningMessage,
std::string & warningMessage) const;
template <class T>
bool
ReadParameter(T & parameterValue,
const std::string & parameterName,
const unsigned int entry_nr,
std::string & warningMessage) const
{
return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
}
template <class T>
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::string fullname = prefix + parameterName;
bool found = false;
std::string dummyString = "";
if (default_entry_nr >= 0)
{
unsigned int uintdefault = static_cast<unsigned int>(default_entry_nr);
found |= this->ReadParameter(parameterValue, parameterName, uintdefault, false, dummyString);
found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
found |= this->ReadParameter(parameterValue, fullname, uintdefault, false, dummyString);
found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
}
else
{
found |= this->ReadParameter(parameterValue, parameterName, entry_nr, false, dummyString);
found |= this->ReadParameter(parameterValue, fullname, entry_nr, false, dummyString);
}
if (!found && produceWarningMessage && this->m_PrintErrorMessages)
{
return this->ReadParameter(parameterValue, parameterName, entry_nr, true, warningMessage);
}
return found;
}
template <class T>
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
{
return this->ReadParameter(parameterValue, parameterName, prefix, entry_nr, default_entry_nr, true, warningMessage);
}
template <class T>
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
{
warningMessage = "";
std::size_t numberOfEntries = this->CountNumberOfParameterEntries(parameterName);
if (numberOfEntries == 0)
{
if (produceWarningMessage && this->m_PrintErrorMessages)
{
std::ostringstream outputStringStream;
outputStringStream << "WARNING: The parameter \"" << parameterName << "\", requested between entry numbers "
<< entry_nr_start << " and " << entry_nr_end << ", does not exist at all.\n"
<< " The default values are used instead.";
warningMessage = outputStringStream.str();
}
return false;
}
if (entry_nr_start > entry_nr_end)
{
itkExceptionMacro("WARNING: The entry number start ("
<< entry_nr_start << ") should be smaller than entry number end (" << entry_nr_end
<< "). It was requested for parameter \"" << parameterName << "\".\n");
}
if (entry_nr_end >= numberOfEntries)
{
itkExceptionMacro("WARNING: The parameter \"" << parameterName << "\" does not exist at entry number "
<< entry_nr_end << ".\nThe default value \"" << T{}
<< "\" is used instead.");
}
const ParameterValuesType & vec = this->m_ParameterMap.find(parameterName)->second;
unsigned int j = 0;
for (unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i)
{
bool castSuccesful = elastix::Conversion::StringToValue(vec[i], parameterValues[j]);
++j;
if (!castSuccesful)
{
itkExceptionMacro("ERROR: Casting entry number "
<< i << " for the parameter \"" << parameterName << "\" failed!\n"
<< " You tried to cast \"" << vec[i] << "\" from std::string to "
<< typeid(parameterValues[0]).name() << '\n');
}
}
return true;
}
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;
std::vector<std::string>
GetValues(const std::string & parameterName) const
{
const auto found = m_ParameterMap.find(parameterName);
return (found == m_ParameterMap.cend()) ? std::vector<std::string>{} : found->second;
}
template <typename T>
std::unique_ptr<std::vector<T>>
RetrieveValues(const std::string & parameterName) const
{
const auto found = m_ParameterMap.find(parameterName);
if (found == m_ParameterMap.end())
{
return nullptr;
}
std::vector<T> result;
result.reserve(found->second.size());
for (const std::string & str : found->second)
{
T value{};
if (elastix::Conversion::StringToValue(str, value))
{
result.push_back(value);
}
else
{
const auto entry_nr = &str - found->second.data();
itkExceptionMacro("Failed to cast parameter \"" << parameterName << "\" entry number " << entry_nr
<< " value \"" << str << "\" to type \"" << typeid(T).name()
<< "\"!");
}
}
return std::make_unique<std::vector<T>>(std::move(result));
}
protected:
ParameterMapInterface();
~ParameterMapInterface() override;
private:
ParameterMapType m_ParameterMap{};
bool m_PrintErrorMessages{ true };
};
} // end of namespace itk
#endif // end itkParameterMapInterface_h
elastix::Conversion::StringToValue
static bool StringToValue(const std::string &str, T &value)
Definition elxConversion.h:174
itk::ParameterFileParser::ParameterMapType
std::map< std::string, ParameterValuesType > ParameterMapType
Definition itkParameterFileParser.h:97
itk::ParameterFileParser::ParameterValuesType
std::vector< std::string > ParameterValuesType
Definition itkParameterFileParser.h:96
itk::ParameterMapInterface::ParameterValuesType
ParameterFileParser::ParameterValuesType ParameterValuesType
Definition itkParameterMapInterface.h:96
itk::ParameterMapInterface::ReadParameter
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
Definition itkParameterMapInterface.h:138
itk::ParameterMapInterface::ReadParameter
bool ReadParameter(bool &parameterValue, const std::string &parameterName, const unsigned int entry_nr, const bool produceWarningMessage, std::string &warningMessage) const
itk::ParameterMapInterface::m_PrintErrorMessages
bool m_PrintErrorMessages
Definition itkParameterMapInterface.h:424
itk::ParameterMapInterface::m_ParameterMap
ParameterMapType m_ParameterMap
Definition itkParameterMapInterface.h:422
itk::ParameterMapInterface::SetParameterMap
void SetParameterMap(const ParameterMapType &parMap)
itk::ParameterMapInterface::ParameterMapInterface
ParameterMapInterface()
itk::ParameterMapInterface::ITK_DISALLOW_COPY_AND_MOVE
ITK_DISALLOW_COPY_AND_MOVE(ParameterMapInterface)
itk::ParameterMapInterface::Self
ParameterMapInterface Self
Definition itkParameterMapInterface.h:84
itk::ParameterMapInterface::~ParameterMapInterface
~ParameterMapInterface() override
itk::ParameterMapInterface::GetValues
std::vector< std::string > GetValues(const std::string &parameterName) const
Definition itkParameterMapInterface.h:374
itk::ParameterMapInterface::HasParameter
bool HasParameter(const std::string &parameterName) const
Definition itkParameterMapInterface.h:113
itk::ParameterMapInterface::ParameterMapType
ParameterFileParser::ParameterMapType ParameterMapType
Definition itkParameterMapInterface.h:97
itk::ParameterMapInterface::ReadParameter
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
Definition itkParameterMapInterface.h:227
itk::ParameterMapInterface::CountNumberOfParameterEntries
std::vcl_size_t CountNumberOfParameterEntries(const std::string &parameterName) const
itk::ParameterMapInterface::ReadParameter
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
itk::ParameterMapInterface::ReadParameter
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
Definition itkParameterMapInterface.h:287
itk::ParameterMapInterface::Pointer
SmartPointer< Self > Pointer
Definition itkParameterMapInterface.h:86
itk::ParameterMapInterface::RetrieveValues
std::unique_ptr< std::vector< T > > RetrieveValues(const std::string &parameterName) const
Definition itkParameterMapInterface.h:386
itk::ParameterMapInterface::ConstPointer
SmartPointer< const Self > ConstPointer
Definition itkParameterMapInterface.h:87
itk::ParameterMapInterface::Superclass
Object Superclass
Definition itkParameterMapInterface.h:85
itk::ParameterMapInterface::ReadParameter
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
Definition itkParameterMapInterface.h:273
itk::ParameterMapInterface::ReadParameter
bool ReadParameter(T &parameterValue, const std::string &parameterName, const unsigned int entry_nr, std::string &warningMessage) const
Definition itkParameterMapInterface.h:211
elxConversion.h
itkParameterFileParser.h
itk
Definition itkAdvancedImageToImageMetric.h:43

Generated on 1768596610 for elastix by 1.15.0 |