OTB  9.0.0
Orfeo Toolbox
otbContingencyTable.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbContingencyTable_h
22 #define otbContingencyTable_h
23 
24 #include <vector>
25 #include <iomanip>
26 #include <itkObject.h>
27 #include <itkObjectFactory.h>
28 #include <itkVariableSizeMatrix.h>
29 #include <string>
30 
31 namespace otb
32 {
33 template <class TClassLabel>
34 class ContingencyTable : public itk::Object
35 {
36 public:
39  typedef itk::Object Superclass;
40  typedef itk::SmartPointer<Self> Pointer;
41  typedef itk::SmartPointer<const Self> ConstPointer;
42 
44  itkTypeMacro(ContingencyTableCalculator, itk::Object);
45 
47  itkNewMacro(Self);
48 
49  typedef itk::VariableSizeMatrix<unsigned long> MatrixType;
50  typedef std::vector<TClassLabel> LabelList;
51 
53 
54  void SetLabels(const LabelList& referenceLabels, const LabelList& producedLabels)
55  {
56  m_RefLabels = referenceLabels;
57  m_ProdLabels = producedLabels;
58  unsigned int rows = static_cast<unsigned int>(m_RefLabels.size());
59  unsigned int cols = static_cast<unsigned int>(m_ProdLabels.size());
60  matrix.SetSize(rows, cols);
61  matrix.Fill(0);
62  }
63 
64  friend std::ostream& operator<<(std::ostream& o, const ContingencyTable<TClassLabel>& contingencyTable)
65  {
66 
67  // Retrieve the maximal width from the matrix and the labels
68  size_t maxWidth = 6;
69  maxWidth = GetLabelsMaximumLength(contingencyTable.m_ProdLabels, maxWidth);
70  maxWidth = GetLabelsMaximumLength(contingencyTable.m_RefLabels, maxWidth);
71 
72  for (unsigned int i = 0; i < contingencyTable.matrix.Rows(); ++i)
73  {
74  for (unsigned int j = 0; j < contingencyTable.matrix.Cols(); ++j)
75  {
76  std::ostringstream oss;
77  oss << contingencyTable.matrix(i, j);
78  size_t length = oss.str().length();
79  if (length > maxWidth)
80  maxWidth = length;
81  }
82  }
83 
84  int width = static_cast<int>(maxWidth) + 1;
85 
86  // Write the first line of the matrix (produced labels)
87  o << std::setfill(' ') << std::setw(width) << "labels";
88  for (size_t i = 0; i < contingencyTable.m_ProdLabels.size(); ++i)
89  {
90  o << std::setfill(' ') << std::setw(width) << contingencyTable.m_ProdLabels[i];
91  }
92  o << std::endl;
93 
94  // For each line write the reference label, then the count value
95  for (unsigned int i = 0; i < contingencyTable.matrix.Rows(); ++i)
96  {
97  o << std::setfill(' ') << std::setw(width) << contingencyTable.m_RefLabels[i];
98  for (unsigned int j = 0; j < contingencyTable.matrix.Cols(); ++j)
99  {
100  o << std::setfill(' ') << std::setw(width) << contingencyTable.matrix(i, j);
101  }
102  o << std::endl;
103  }
104 
105  return o;
106  }
107 
108  std::string ToCSV() const
109  {
110  const char separator = ',';
111 
112  std::ostringstream oss;
113  oss << "labels";
114  for (size_t i = 0; i < m_ProdLabels.size(); ++i)
115  {
116  oss << separator << m_ProdLabels[i];
117  }
118  oss << std::endl;
119 
120  // For each line write the reference label, then the count value
121  for (unsigned int i = 0; i < matrix.Rows(); ++i)
122  {
123  oss << m_RefLabels[i];
124  for (unsigned int j = 0; j < matrix.Cols(); ++j)
125  {
126  oss << separator << matrix(i, j);
127  }
128  oss << std::endl;
129  }
130  oss << std::endl;
131 
132  return oss.str();
133  }
134 
135 protected:
137  {
139  }
140  ~ContingencyTable() override
141  {
142  }
143  void PrintSelf(std::ostream& os, itk::Indent itkNotUsed(indent)) const override
144  {
145  os << *this;
146  }
147 
148 private:
149  ContingencyTable(const Self&) = delete;
150  void operator=(const Self&) = delete;
151 
152  static size_t GetLabelsMaximumLength(const LabelList& labels, size_t maxWidth)
153  {
154  size_t tmpMaxWidth = maxWidth;
155  for (size_t i = 0; i < labels.size(); ++i)
156  {
157  std::ostringstream oss;
158  oss << labels[i];
159  size_t length = oss.str().length();
160  if (length > tmpMaxWidth)
161  tmpMaxWidth = length;
162  }
163  return tmpMaxWidth;
164  }
165 
168 };
169 }
170 
171 #endif // otbContingencyTable_h
otb::ContingencyTable::SetLabels
void SetLabels(const LabelList &referenceLabels, const LabelList &producedLabels)
Definition: otbContingencyTable.h:54
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ContingencyTable::GetLabelsMaximumLength
static vcl_size_t GetLabelsMaximumLength(const LabelList &labels, vcl_size_t maxWidth)
Definition: otbContingencyTable.h:152
otb::ContingencyTable::Self
ContingencyTable Self
Definition: otbContingencyTable.h:38
otb::ContingencyTable::Superclass
itk::Object Superclass
Definition: otbContingencyTable.h:39
otb::ContingencyTable::m_RefLabels
LabelList m_RefLabels
Definition: otbContingencyTable.h:166
otb::ContingencyTable::ContingencyTable
ContingencyTable()
Definition: otbContingencyTable.h:136
otb::ContingencyTable::operator=
void operator=(const Self &)=delete
otb::ContingencyTable::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbContingencyTable.h:41
otb::ContingencyTable::operator<<
friend std::ostream & operator<<(std::ostream &o, const ContingencyTable< TClassLabel > &contingencyTable)
Definition: otbContingencyTable.h:64
otb::ContingencyTable::ToCSV
std::string ToCSV() const
Definition: otbContingencyTable.h:108
otb::ContingencyTable::~ContingencyTable
~ContingencyTable() override
Definition: otbContingencyTable.h:140
otb::ContingencyTable::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent) const override
Definition: otbContingencyTable.h:143
otb::ContingencyTable::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbContingencyTable.h:40
otb::ContingencyTableCalculator
ContingencyTableCalculator provide facilities to compute ContingencyTable.
Definition: otbContingencyTableCalculator.h:41
otb::ContingencyTable::m_ProdLabels
LabelList m_ProdLabels
Definition: otbContingencyTable.h:167
otb::ContingencyTable::LabelList
std::vector< TClassLabel > LabelList
Definition: otbContingencyTable.h:50
otb::ContingencyTable
Definition: otbContingencyTable.h:34
otb::ContingencyTable::MatrixType
itk::VariableSizeMatrix< unsigned long > MatrixType
Definition: otbContingencyTable.h:47
otb::ContingencyTable::matrix
MatrixType matrix
Definition: otbContingencyTable.h:52