Commit 0adc3c81 authored by Guillaume Pasero's avatar Guillaume Pasero

ENH: support map in StatisticsXMLFileWriter

parent 1fd3fa1a
......@@ -60,9 +60,15 @@ public:
/** Convenient typedef */
typedef std::pair<std::string , MeasurementVectorType> InputDataType;
typedef std::vector< InputDataType > MeasurementVectorContainer;
typedef std::map<std::string , std::string> GenericMapType;
typedef std::map<std::string , GenericMapType> GenericMapContainer;
/** Method to set/get the input list sample */
void AddInput(const char * name, const MeasurementVectorType& inputVector );
template <typename MapType>
void AddInputMap(const char * name, const MapType& map );
/** Trigger the processing */
void Update()
......@@ -88,6 +94,8 @@ private:
std::string m_FileName;
MeasurementVectorContainer m_MeasurementVectorContainer;
GenericMapContainer m_GenericMapContainer;
}; // end of class StatisticsXMLFileWriter
......
......@@ -60,8 +60,8 @@ StatisticsXMLFileWriter<TMeasurementVector>
::GenerateData()
{
// Check if the input are not null
if(m_MeasurementVectorContainer.size() == 0)
itkExceptionMacro(<<"At Least one input is required, please set input using the method AddInput");
if(m_MeasurementVectorContainer.size() == 0 && m_GenericMapContainer.size() == 0)
itkExceptionMacro(<<"At least one input is required, please set input using the methods AddInput or AddInputMap");
// Check if the filename is not empty
if(m_FileName.empty())
......@@ -80,8 +80,12 @@ StatisticsXMLFileWriter<TMeasurementVector>
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "FeatureStatistics");
doc.LinkEndChild( root );
TiXmlElement * root = NULL;
if (m_MeasurementVectorContainer.size())
{
root = new TiXmlElement( "FeatureStatistics");
doc.LinkEndChild( root );
}
// Iterate through the input
for (unsigned int i = 0; i < m_MeasurementVectorContainer.size(); ++i)
......@@ -103,6 +107,39 @@ StatisticsXMLFileWriter<TMeasurementVector>
feature->LinkEndChild(curStatisticVector);
}
}
// Iterate on map containers
TiXmlElement * mapRoot = NULL;
if (m_GenericMapContainer.size())
{
mapRoot = new TiXmlElement( "GeneralStatistics");
doc.LinkEndChild( mapRoot );
}
std::string keyAttr("key");
std::string valAttr("value");
GenericMapContainer::const_iterator containerIt;
for ( containerIt = m_GenericMapContainer.begin() ; containerIt != m_GenericMapContainer.end() ; ++containerIt)
{
std::string mapName = containerIt->first;
GenericMapType::const_iterator mapIter;
// The current statistic
TiXmlElement * feature = new TiXmlElement("Statistic");
feature->SetAttribute("name", mapName.c_str());
mapRoot->LinkEndChild( feature );
// Store the value for this statistic
for( mapIter = containerIt->second.begin() ; mapIter != containerIt->second.end() ; ++mapIter )
{
// For each value in Measurementvector
TiXmlElement * curStatisticMap = new TiXmlElement("StatisticMap");
curStatisticMap->SetAttribute(keyAttr , mapIter->first);
curStatisticMap->SetAttribute(valAttr, mapIter->second);
feature->LinkEndChild(curStatisticMap);
}
}
// Finally, write the file
if (! doc.SaveFile( m_FileName.c_str() ) )
......@@ -114,6 +151,34 @@ StatisticsXMLFileWriter<TMeasurementVector>
}
template < class TMeasurementVector >
template <typename MapType>
void
StatisticsXMLFileWriter<TMeasurementVector>
::AddInputMap(const char * name, const MapType& map )
{
std::string token(name);
if(m_GenericMapContainer.count(token) > 0)
{
itkExceptionMacro(<<"Token selected ("
<<name<<") is already added to the XML file");
}
typename MapType::const_iterator it;
GenericMapType insideMap;
std::ostringstream oss;
std::ostringstream ossKey;
for ( it = map.begin() ; it != map.end() ; ++it)
{
oss.str(std::string(""));
oss << it->second;
ossKey.str(std::string(""));
ossKey << it->first;
insideMap[ossKey.str()] = oss.str();
}
m_GenericMapContainer[token] = insideMap;
}
template < class TMeasurementVector >
void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment