Commit 8c0fd1bf authored by Charles Peyrega's avatar Charles Peyrega

ENH: Handling the extended headers of input confusion matrices files in...

ENH: Handling the extended headers of input confusion matrices files in otbDSFusionOfClassifiersImageFilterTest
parent d2f0804c
......@@ -30,63 +30,128 @@
typedef unsigned char PixelType;
typedef itk::VariableSizeMatrix<double> ConfusionMatrixType;
typedef unsigned long ConfusionMatrixEltType;
typedef itk::VariableSizeMatrix<ConfusionMatrixEltType> ConfusionMatrixType;
typedef otb::ConfusionMatrixToMassOfBelief<ConfusionMatrixType, PixelType> ConfusionMatrixToMassOfBeliefType;
typedef ConfusionMatrixToMassOfBeliefType::MapOfIndicesType MapOfIndicesType;
typedef ConfusionMatrixToMassOfBeliefType::MapOfClassesType MapOfClassesType;
int CSVConfusionMatrixFileReader(const std::string fileName, MapOfIndicesType &mapOfIndicesClX, ConfusionMatrixType &confusionMatrixClX)
{
std::ifstream inFile;
inFile.open(fileName.c_str());
int CSVConfusionMatrixFileReader(const std::string fileName, MapOfClassesType &mapOfClassesRefClX, ConfusionMatrixType &confusionMatrixClX)
{
std::ifstream inFile;
inFile.open(fileName.c_str());
if (!inFile)
{
std::cerr << "Confusion Matrix File opening problem with file:" << std::endl;
std::cerr << fileName.c_str() << std::endl;
return EXIT_FAILURE;
}
else
{
std::string currentLine, labelsLine, currentValue;
const char commentChar = '#';
const char separatorChar = ',';
const char eolChar = '\n';
std::getline(inFile, labelsLine, commentChar); // Skips the comment char
std::getline(inFile, labelsLine, eolChar); // Gets the first line after the comment char until the End Of Line char
std::istringstream issLabelsLine(labelsLine);
mapOfIndicesClX.clear();
int itLab = 0;
while (issLabelsLine.good())
if (!inFile)
{
std::getline(issLabelsLine, currentValue, separatorChar);
mapOfIndicesClX[itLab] = std::atoi(currentValue.c_str());
++itLab;
std::cerr << "Confusion Matrix File opening problem with file:" << std::endl;
std::cerr << fileName.c_str() << std::endl;
return EXIT_FAILURE;
}
else
{
PixelType labelRef = 0, labelProd = 0;
std::string currentLine, refLabelsLine, prodLabelsLine, currentValue;
const char endCommentChar = ':';
const char separatorChar = ',';
const char eolChar = '\n';
std::getline(inFile, refLabelsLine, endCommentChar); // Skips the comments
std::getline(inFile, refLabelsLine, eolChar); // Gets the first line after the comment char until the End Of Line char
std::getline(inFile, prodLabelsLine, endCommentChar); // Skips the comments
std::getline(inFile, prodLabelsLine, eolChar); // Gets the second line after the comment char until the End Of Line char
std::istringstream issRefLabelsLine(refLabelsLine);
std::istringstream issProdLabelsLine(prodLabelsLine);
MapOfClassesType mapOfClassesProdClX;
mapOfClassesRefClX.clear();
mapOfClassesProdClX.clear();
int itLab = 0;
while (issRefLabelsLine.good())
{
std::getline(issRefLabelsLine, currentValue, separatorChar);
labelRef = static_cast<PixelType> (std::atoi(currentValue.c_str()));
mapOfClassesRefClX[labelRef] = itLab;
++itLab;
}
unsigned int nbLabelsClk = mapOfIndicesClX.size();
confusionMatrixClX = ConfusionMatrixType(nbLabelsClk, nbLabelsClk);
itLab = 0;
while (issProdLabelsLine.good())
{
std::getline(issProdLabelsLine, currentValue, separatorChar);
labelProd = static_cast<PixelType> (std::atoi(currentValue.c_str()));
mapOfClassesProdClX[labelProd] = itLab;
++itLab;
}
for (unsigned int itLin = 0; itLin < nbLabelsClk; ++itLin)
{
//Gets the itLin^th line after the first header line with the labels
std::getline(inFile, currentLine, eolChar);
std::istringstream issCurrentLine(currentLine);
unsigned int itCol = 0;
while (issCurrentLine.good())
unsigned int nbRefLabelsClk = mapOfClassesRefClX.size();
unsigned int nbProdLabelsClk = mapOfClassesProdClX.size();
ConfusionMatrixType confusionMatrixClXTemp;
confusionMatrixClXTemp = ConfusionMatrixType(nbRefLabelsClk, nbProdLabelsClk);
confusionMatrixClXTemp.Fill(0);
// Reading the confusion matrix confusionMatrixClXTemp from the file
for (unsigned int itRow = 0; itRow < nbRefLabelsClk; ++itRow)
{
std::getline(issCurrentLine, currentValue, separatorChar);
confusionMatrixClX(itLin, itCol) = std::atoi(currentValue.c_str());
++itCol;
//Gets the itRow^th line after the header lines with the labels
std::getline(inFile, currentLine, eolChar);
std::istringstream issCurrentLine(currentLine);
unsigned int itCol = 0;
while (issCurrentLine.good())
{
std::getline(issCurrentLine, currentValue, separatorChar);
confusionMatrixClXTemp(itRow, itCol) = static_cast<ConfusionMatrixEltType> (std::atoi(currentValue.c_str()));
++itCol;
}
}
}
}
MapOfClassesType::iterator itMapOfClassesRef, itMapOfClassesProd;
inFile.close();
return EXIT_SUCCESS;
}
/*for (itMapOfClassesRef = mapOfClassesRefClX.begin(); itMapOfClassesRef != mapOfClassesRefClX.end(); ++itMapOfClassesRef)
{
std::cout << "mapOfClassesRefClX[" << itMapOfClassesRef->first << "] = " << itMapOfClassesRef->second << std::endl;
}
std::cout << std::endl;
for (itMapOfClassesProd = mapOfClassesProdClX.begin(); itMapOfClassesProd != mapOfClassesProdClX.end(); ++itMapOfClassesProd)
{
std::cout << "mapOfClassesProdClX[" << itMapOfClassesProd->first << "] = " << itMapOfClassesProd->second << std::endl;
}*/
// Formatting confusionMatrixClX from confusionMatrixClXTemp in order to make confusionMatrixClX a square matrix
// from the reference labels in mapOfClassesRefClX
int indiceLabelRef = 0, indiceLabelProd = 0;
int indiceLabelRefTemp = 0, indiceLabelProdTemp = 0;
// Initialization of confusionMatrixClX
confusionMatrixClX = ConfusionMatrixType(nbRefLabelsClk, nbRefLabelsClk);
confusionMatrixClX.Fill(0);
for (itMapOfClassesRef = mapOfClassesRefClX.begin(); itMapOfClassesRef != mapOfClassesRefClX.end(); ++itMapOfClassesRef)
{
// labels labelRef of mapOfClassesRefClX are already sorted
labelRef = itMapOfClassesRef->first;
indiceLabelRefTemp = itMapOfClassesRef->second;
for (itMapOfClassesProd = mapOfClassesProdClX.begin(); itMapOfClassesProd != mapOfClassesProdClX.end(); ++itMapOfClassesProd)
{
// labels labelProd of mapOfClassesProdClX are already sorted
labelProd = itMapOfClassesProd->first;
indiceLabelProdTemp = itMapOfClassesProd->second;
// If labelProd is present in mapOfClassesRefClX
if (mapOfClassesRefClX.count(labelProd) != 0)
{
indiceLabelProd = mapOfClassesRefClX.find(labelProd)->second; // Indice of labelProd in mapOfClassesRefClX
confusionMatrixClX(indiceLabelRef, indiceLabelProd) = confusionMatrixClXTemp(indiceLabelRefTemp, indiceLabelProdTemp);
}
}
++indiceLabelRef;
}
//std::cout << "confusionMatrixClXTemp:" << std::endl << confusionMatrixClXTemp << std::endl << std::endl;
//std::cout << "confusionMatrixClX:" << std::endl << confusionMatrixClX << std::endl;
}
inFile.close();
return EXIT_SUCCESS;
}
......@@ -193,11 +258,11 @@ int otbDSFusionOfClassifiersImageFilterTest(int argc, char* argv[])
imageList->PushBack(reader->GetOutput());
MapOfIndicesType mapOfIndicesClk;
MapOfClassesType mapOfClassesClk;
ConfusionMatrixType confusionMatrixClk;
CSVConfusionMatrixFileReader(fileNameConfMat, mapOfIndicesClk, confusionMatrixClk);
CSVConfusionMatrixFileReader(fileNameConfMat, mapOfClassesClk, confusionMatrixClk);
confusionMatrixToMassOfBeliefFilter->SetMapOfIndices(mapOfIndicesClk);
confusionMatrixToMassOfBeliefFilter->SetMapOfClasses(mapOfClassesClk);
confusionMatrixToMassOfBeliefFilter->SetConfusionMatrix(confusionMatrixClk);
confusionMatrixToMassOfBeliefFilter->SetDefinitionMethod(massOfBeliefDefMethod);
confusionMatrixToMassOfBeliefFilter->Update();
......
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