| pktools 2.6.7 Processing Kernel for geospatial data

Public Member Functions | Static Public Member Functions | Friends | List of all members
confusionmatrix::ConfusionMatrix Class Reference

## Public Member Functions
| ConfusionMatrix (short nclass)
| ConfusionMatrix (const std::vector< std::string > &classNames)
| ConfusionMatrix (const ConfusionMatrix &cm)
ConfusionMatrix & | operator= (const ConfusionMatrix &cm)
short | size () const
void | resize (short nclass)
void | setClassNames (const std::vector< std::string > &classNames, bool doSort=false)
void | pushBackClassName (const std::string &className, bool doSort=false)
void | setResults (const Vector2d< double > &theResults)
void | setResult (const std::string &theRef, const std::string &theClass, double theResult)
void | incrementResult (const std::string &theRef, const std::string &theClass, double theIncrement)
void | clearResults ()
double | nReference (const std::string &theRef) const
double | nReference () const
double | nClassified (const std::string &theRef) const
int | nClasses () const
std::string | getClass (int iclass) const
int | getClassIndex (std::string className) const
std::vector< std::string > | getClassNames () const
double | pa (const std::string &theClass, double *se95=NULL) const
double | ua (const std::string &theClass, double *se95=NULL) const
double | oa (double *se95=NULL) const
int | pa_pct (const std::string &theClass, double *se95=NULL) const
int | ua_pct (const std::string &theClass, double *se95=NULL) const
int | oa_pct (double *se95=NULL) const
double | kappa () const
ConfusionMatrix & | operator*= (double weight)
ConfusionMatrix | operator* (double weight)
ConfusionMatrix & | operator+= (const ConfusionMatrix &cm)
ConfusionMatrix | operator+ (const ConfusionMatrix &cm)
void | sortClassNames ()
void | reportSE95 (bool doReport)
void | setFormat (const CM_FORMAT &theFormat)
void | setFormat (const std::string theFormat)
CM_FORMAT | getFormat () const

## Static Public Member Functions
static const CM_FORMAT | getFormat (const std::string theFormat)

## Friends
std::ostream & | operator<< (std::ostream &os, const ConfusionMatrix &cm)

## Detailed Description

Definition at line 32 of file ConfusionMatrix.h.

## Constructor & Destructor Documentation

## ◆ ConfusionMatrix() [1/4]

ConfusionMatrix::ConfusionMatrix | ( |  | ) |

Definition at line 32 of file ConfusionMatrix.cc.

33 : m_classes(),m_results(),m_se95(true),m_format(ASCII)
34{
35}

## ◆ ConfusionMatrix() [2/4]

ConfusionMatrix::ConfusionMatrix | ( | short | nclass | ) |

Definition at line 42 of file ConfusionMatrix.cc.

42 {
43 resize(nclass);
44}

## ◆ ConfusionMatrix() [3/4]

ConfusionMatrix::ConfusionMatrix | ( | const std::vector< std::string > & | classNames | ) |

Definition at line 46 of file ConfusionMatrix.cc.

46 {
47 setClassNames(classNames);
48}

## ◆ ConfusionMatrix() [4/4]

ConfusionMatrix::ConfusionMatrix | ( | const ConfusionMatrix & | cm | ) |

Definition at line 51 of file ConfusionMatrix.cc.

51 {
52 setClassNames(cm.m_classes);
53 setResults(cm.m_results);
54}

## ◆ ~ConfusionMatrix()

ConfusionMatrix::~ConfusionMatrix | ( |  | ) |

Definition at line 37 of file ConfusionMatrix.cc.

38{
39}

## Member Function Documentation

## ◆ clearResults()

void ConfusionMatrix::clearResults | ( |  | ) |

Definition at line 140 of file ConfusionMatrix.cc.

140 {
141 m_results.clear();
142 m_results.resize(m_classes.size(),m_classes.size());
143}

## ◆ getClass()

std::string confusionmatrix::ConfusionMatrix::getClass | ( | int | iclass | ) | const
| inline

Definition at line 51 of file ConfusionMatrix.h.

51{assert(iclass>=0);assert(iclass<m_classes.size());return m_classes[iclass];};

## ◆ getClassIndex()

int confusionmatrix::ConfusionMatrix::getClassIndex | ( | std::string | className | ) | const
| inline

Definition at line 52 of file ConfusionMatrix.h.

52 {
53 int index=0;
54 for(index=0;index<m_classes.size();++index){
55 if(m_classes[index]==className)
56 break;
57 }
58 if(index>=m_classes.size())
59 index=-1;
60 return index;
61 // int index=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),className));
62 // assert(index>=0);
63 // if(index<m_results.size())
64 // return(index);
65 // else
66 // return(-1);
67 }

## ◆ getClassNames()

std::vector< std::string > confusionmatrix::ConfusionMatrix::getClassNames | ( |  | ) | const
| inline

Definition at line 68 of file ConfusionMatrix.h.

68{return m_classes;};

## ◆ getFormat() [1/2]

CM_FORMAT confusionmatrix::ConfusionMatrix::getFormat | ( |  | ) | const
| inline

Definition at line 88 of file ConfusionMatrix.h.

88{return m_format;};

## ◆ getFormat() [2/2]

static const CM_FORMAT confusionmatrix::ConfusionMatrix::getFormat | ( | const std::string | theFormat | ) |
| inline static

Definition at line 90 of file ConfusionMatrix.h.

90 {
91 if(theFormat=="ascii") return(ASCII);
92 else if(theFormat=="latex") return(LATEX);
93 else{
94 std::string errorString="Format not supported: ";
95 errorString+=theFormat;
96 errorString+=" use ascii or latex";
97 throw(errorString);
98 }
99 };

## ◆ incrementResult()

void ConfusionMatrix::incrementResult | ( | const std::string & | theRef,
|  | const std::string & | theClass,
|  | double | theIncrement
| ) |  |

Definition at line 157 of file ConfusionMatrix.cc.

157 {
158 // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef));
159 // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass));
160 int ir=getClassIndex(theRef);
161 int ic=getClassIndex(theClass);
162 assert(ir>=0);
163 if(ir>=m_results.size())
164 std::cerr << "Error: " << theRef << " not found in class ConfusionMatrix when incrementing for class " << theClass << std::endl;
165 assert(ir<m_results.size());
166 assert(ic>=0);
167 assert(ic<m_results[ir].size());
168 m_results[ir][ic]+=theIncrement;
169}

## ◆ kappa()

double ConfusionMatrix::kappa | ( |  | ) | const

Definition at line 272 of file ConfusionMatrix.cc.

272 {
273 double ntotal=m_results.sum();
274 double pChance=0;
275 double pCorrect=0;
276 for(int iclass=0;iclass<m_classes.size();++iclass){
277 pChance+=nClassified(m_classes[iclass])*nReference(m_classes[iclass])/ntotal/ntotal;
278 pCorrect+=static_cast<double>(m_results[iclass][iclass])/ntotal;
279 }
280 if(pChance<1)
281 return((pCorrect-pChance)/(1-pChance));
282 else
283 return(0);
284}

## ◆ nClasses()

int confusionmatrix::ConfusionMatrix::nClasses | ( |  | ) | const
| inline

Definition at line 50 of file ConfusionMatrix.h.

50{return m_classes.size();};

## ◆ nClassified()

double ConfusionMatrix::nClassified | ( | const std::string & | theRef | ) | const

Definition at line 184 of file ConfusionMatrix.cc.

184 {
185 // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass));
186 int ic=getClassIndex(theClass);
187 double nclassified=0;
188 for(int iref=0;iref<m_results.size();++iref){
189 assert(ic<m_results[iref].size());
190 nclassified+=m_results[iref][ic];
191 }
192 return(nclassified);
193}

## ◆ nReference() [1/2]

double ConfusionMatrix::nReference | ( |  | ) | const

Definition at line 177 of file ConfusionMatrix.cc.

177 {
178 double nref=0;
179 for(int ir=0;ir<m_classes.size();++ir)
180 nref+=accumulate(m_results[ir].begin(),m_results[ir].end(),0);
181 return nref;
182}

## ◆ nReference() [2/2]

double ConfusionMatrix::nReference | ( | const std::string & | theRef | ) | const

Definition at line 171 of file ConfusionMatrix.cc.

171 {
172 // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef));
173 int ir=getClassIndex(theRef);
174 return accumulate(m_results[ir].begin(),m_results[ir].end(),0);
175}

## ◆ oa()

double ConfusionMatrix::oa | ( | double * | se95 = `NULL` | ) | const

Definition at line 250 of file ConfusionMatrix.cc.

250 {
251 double ntotal=m_results.sum();
252 double pChance=0;
253 double pCorrect=0;
254 for(int iclass=0;iclass<m_classes.size();++iclass)
255 pCorrect+=static_cast<double>(m_results[iclass][iclass])/ntotal;
256 double qCorrect=1-pCorrect;
257 if(se95!=NULL)
258 *se95=(pCorrect<1&&pCorrect>0)? sqrt(pCorrect*qCorrect/(ntotal-1)) : 0;
259 if(ntotal>0)
260 return(pCorrect);
261 else
262 return(0);
263}

## ◆ oa_pct()

int ConfusionMatrix::oa_pct | ( | double * | se95 = `NULL` | ) | const

Definition at line 265 of file ConfusionMatrix.cc.

265 {
266 double doa=oa(se95);
267 if(se95!=NULL)
268 *se95=static_cast<double>(static_cast<int>(0.5+1000*(*se95)))/10.0;
269 return static_cast<int>(0.5+100.0*doa);
270}

## ◆ operator*()

ConfusionMatrix ConfusionMatrix::operator* | ( | double | weight | ) |

Definition at line 102 of file ConfusionMatrix.cc.

103{
104 ConfusionMatrix result = *this;//make a copy of myself
105 result*=weight;
106 return result;
107}
confusionmatrix::ConfusionMatrix
Definition: ConfusionMatrix.h:32

## ◆ operator*=()

ConfusionMatrix & ConfusionMatrix::operator*= | ( | double | weight | ) |

Definition at line 89 of file ConfusionMatrix.cc.

90{
91 for(int irow=0;irow<m_results.size();++irow){
92 for(int icol=0;icol<m_results[irow].size();++icol)
93 m_results[irow][icol]*=weight;
94 }
95 return *this;
96}

## ◆ operator+()

ConfusionMatrix confusionmatrix::ConfusionMatrix::operator+ | ( | const ConfusionMatrix & | cm | ) |
| inline

Definition at line 80 of file ConfusionMatrix.h.

80 {
81 return ConfusionMatrix(*this)+=cm;
82 }

## ◆ operator+=()

ConfusionMatrix & ConfusionMatrix::operator+= | ( | const ConfusionMatrix & | cm | ) |

Definition at line 68 of file ConfusionMatrix.cc.

69{
70 if(cm.m_classes.size()!=this->m_classes.size()){
71 std::cerr << "error0: "<< cm.m_classes.size() << "!=" << this->m_classes.size() << std::endl;
72 exit(0);
73 }
74 if(cm.m_results.size()!=this->m_results.size()){
75 std::cerr << "error1: "<< cm.m_results.size() << "!=" << this->m_results.size() << std::endl;
76 exit(1);
77 }
78 for(int irow=0;irow<m_results.size();++irow){
79 if(cm.m_results[irow].size()!=this->m_results[irow].size()){
80 std::cerr << "error2: " << cm.m_results[irow].size() << "!=" << this->m_results[irow].size() << std::endl;
81 exit(2);
82 }
83 for(int icol=0;icol<m_results[irow].size();++icol)
84 this->m_results[irow][icol]+=cm.m_results[irow][icol];
85 }
86 return *this;
87}

## ◆ operator=()

ConfusionMatrix & ConfusionMatrix::operator= | ( | const ConfusionMatrix & | cm | ) |

Definition at line 57 of file ConfusionMatrix.cc.

57 {
58 //check for self-assignment by comparing the address of the implicit object and parameter
59 if(this==&cm)
60 return *this;
61 else{
62 setClassNames(cm.m_classes);
63 setResults(cm.m_results);
64 }
65 return *this;
66}

## ◆ pa()

double ConfusionMatrix::pa | ( | const std::string & | theClass,
|  | double * | se95 = `NULL`
| ) |  | const

Definition at line 195 of file ConfusionMatrix.cc.

195 {
196 assert(m_results.size());
197 assert(m_results.size()==m_classes.size());
198 double producer=0;
199 // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass));
200 int ir=getClassIndex(theClass);
201 assert(ir>=0);
202 assert(ir<m_results.size());
203 assert(!theClass.compare(m_classes[ir]));
204 for(int iclass=0;iclass<m_results.size();++iclass){
205 assert(iclass<m_results[ir].size());
206 producer+=m_results[ir][iclass];
207 }
208 double dpa=(producer>0)? static_cast<double>(m_results[ir][ir])/producer : 0;
209 double dqa=1.0-dpa;
210 if(se95!=NULL)
211 *se95=(dpa<1&&dpa>0)? sqrt(dpa*dqa/(producer-1)) : 0;
212 return dpa;
213}

## ◆ pa_pct()

int ConfusionMatrix::pa_pct | ( | const std::string & | theClass,
|  | double * | se95 = `NULL`
| ) |  | const

Definition at line 215 of file ConfusionMatrix.cc.

215 {
216 double dpa=pa(theClass,se95);
217 if(se95!=NULL)
218 *se95=static_cast<double>(static_cast<int>(0.5+1000*(*se95)))/10.0;
219 return static_cast<int>(0.5+100.0*dpa);
220}

## ◆ pushBackClassName()

void ConfusionMatrix::pushBackClassName | ( | const std::string & | className,
|  | bool | doSort = `false`
| ) |  |

Definition at line 127 of file ConfusionMatrix.cc.

127 {
128 m_classes.push_back(className);
129 if(doSort)
130 sortClassNames();
131 if(m_results.size()!=m_classes.size())
132 m_results.resize(m_classes.size(),m_classes.size());
133}

## ◆ reportSE95()

void confusionmatrix::ConfusionMatrix::reportSE95 | ( | bool | doReport | ) |
| inline

Definition at line 85 of file ConfusionMatrix.h.

85{m_se95=doReport;};

## ◆ resize()

void ConfusionMatrix::resize | ( | short | nclass | ) |

Definition at line 109 of file ConfusionMatrix.cc.

109 {
110 m_classes.resize(nclass);
111 for(short iclass=0;iclass<nclass;++iclass){
112 std::ostringstream osclass;
113 osclass << iclass;
114 m_classes[iclass]=osclass.str();
115 }
116 m_results.resize(nclass,nclass);
117}

## ◆ setClassNames()

void ConfusionMatrix::setClassNames | ( | const std::vector< std::string > & | classNames,
|  | bool | doSort = `false`
| ) |  |

Definition at line 119 of file ConfusionMatrix.cc.

119 {
120 m_classes=classNames;
121 if(doSort)
122 sortClassNames();
123 if(m_results.size()!=m_classes.size())
124 m_results.resize(m_classes.size(),m_classes.size());
125}

## ◆ setFormat() [1/2]

void confusionmatrix::ConfusionMatrix::setFormat | ( | const CM_FORMAT & | theFormat | ) |
| inline

Definition at line 86 of file ConfusionMatrix.h.

86{m_format=theFormat;};

## ◆ setFormat() [2/2]

void confusionmatrix::ConfusionMatrix::setFormat | ( | const std::string | theFormat | ) |
| inline

Definition at line 87 of file ConfusionMatrix.h.

87{m_format=getFormat(theFormat);};

## ◆ setResult()

void ConfusionMatrix::setResult | ( | const std::string & | theRef,
|  | const std::string & | theClass,
|  | double | theResult
| ) |  |

Definition at line 145 of file ConfusionMatrix.cc.

145 {
146 // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef));
147 // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass));
148 // assert(ir>=0);
149 // assert(ir<m_results.size());
150 // assert(ic>=0);
151 // assert(ic<m_results[ir].size());
152 int ir=getClassIndex(theRef);
153 int ic=getClassIndex(theClass);
154 m_results[ir][ic]=theResult;
155}

## ◆ setResults()

void ConfusionMatrix::setResults | ( | const Vector2d< double > & | theResults | ) |

Definition at line 136 of file ConfusionMatrix.cc.

136 {
137 m_results=theResults;
138}

## ◆ size()

short confusionmatrix::ConfusionMatrix::size | ( |  | ) | const
| inline

Definition at line 39 of file ConfusionMatrix.h.

39{return m_results.size();};

## ◆ sortClassNames()

void ConfusionMatrix::sortClassNames | ( |  | ) |

Definition at line 98 of file ConfusionMatrix.cc.

98 {
99 sort(m_classes.begin(),m_classes.end(),compareClass);
100}

## ◆ ua()

double ConfusionMatrix::ua | ( | const std::string & | theClass,
|  | double * | se95 = `NULL`
| ) |  | const

Definition at line 223 of file ConfusionMatrix.cc.

223 {
224 assert(m_results.size());
225 assert(m_results.size()==m_classes.size());
226 double user=0;
227 // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass));
228 int ic=getClassIndex(theClass);
229 assert(ic>=0);
230 assert(ic<m_results.size());
231 assert(!theClass.compare(m_classes[ic]));
232 for(int iref=0;iref<m_results.size();++iref){
233 assert(ic<m_results[iref].size());
234 user+=m_results[iref][ic];
235 }
236 double dua=(user>0)? static_cast<double>(m_results[ic][ic])/user : 0;
237 double dva=1.0-dva;
238 if(se95!=NULL)
239 *se95=(dua<1&&dua>0)? sqrt(dua*dva/(user-1)) : 0;
240 return dua;
241}

## ◆ ua_pct()

int ConfusionMatrix::ua_pct | ( | const std::string & | theClass,
|  | double * | se95 = `NULL`
| ) |  | const

Definition at line 243 of file ConfusionMatrix.cc.

243 {
244 double dua=ua(theClass,se95);
245 if(se95!=NULL)
246 *se95=static_cast<double>(static_cast<int>(0.5+1000*(*se95)))/10.0;
247 return static_cast<int>(0.5+100.0*dua);
248}

## Friends And Related Function Documentation

## ◆ operator<<

std::ostream & operator<< | ( | std::ostream & | os,
|  | const ConfusionMatrix & | cm
| ) |  |
| friend

Definition at line 101 of file ConfusionMatrix.h.

101 {
102 std::ostringstream streamLine;
103 /* streamosclass << iclass; */
104 /* m_classes[iclass]=osclass.str(); */
105
106 std::string fieldSeparator=" ";
107 std::string lineSeparator="";
108 std::string mathMode="";
109 switch(cm.getFormat()){
110 case(LATEX):
111 fieldSeparator=" & ";
112 lineSeparator="\\\\";
113 mathMode="$";
114 break;
115 case(ASCII):
116 default:
117 fieldSeparator="\t";
118 lineSeparator="";
119 mathMode="";
120 break;
121 }
122
123 double se95_ua=0;
124 double se95_pa=0;
125 double se95_oa=0;
126 double dua=0;
127 double dpa=0;
128 double doa=0;
129
130 doa = cm.oa(&se95_oa);
131
132 if(cm.getFormat()==LATEX){
133 os << "\\documentclass{article}" << std::endl;
134 os << "\\begin{document}" << std::endl;
135 }
136 os << "Kappa = " << mathMode << cm.kappa() << mathMode ;
137 os << ", Overall Acc. = " << mathMode << 100.0*cm.oa() << mathMode ;
138 if(cm.m_se95)
139 os << " (" << mathMode << se95_oa << mathMode << ")";
140 os << std::endl;
141 os << std::endl;
142 if(cm.getFormat()==LATEX){
143 os << "\\begin{tabular}{@{}l";
144 for(int iclass=0;iclass<cm.nClasses();++iclass)
145 os << "l";
146 os << "}" << std::endl;
147 os << "\\hline" << std::endl;
148 }
149
150 os << "Class";
151 for(int iclass=0;iclass<cm.nClasses();++iclass)
152 os << fieldSeparator << cm.m_classes[iclass];
153 os << lineSeparator << std::endl;
154 if(cm.getFormat()==LATEX)
155 os << "\\hline" << std::endl;
156 assert(cm.m_classes.size()==cm.m_results.size());
157 for(int irow=0;irow<cm.m_results.size();++irow){
158 os << cm.m_classes[irow];
159 for(int icol=0;icol<cm.m_results[irow].size();++icol)
160 os << fieldSeparator << cm.m_results[irow][icol];
161 os << lineSeparator<< std::endl;
162 }
163 if(cm.getFormat()==LATEX){
164 os << "\\hline" << std::endl;
165 }
166 else
167 os << std::endl;
168
169 os << "User' Acc.";
170 for(int iclass=0;iclass<cm.nClasses();++iclass){
171 dua=cm.ua_pct(cm.m_classes[iclass],&se95_ua);
172 os << fieldSeparator << dua;
173 if(cm.m_se95)
174 os << " (" << se95_ua << ")";
175 }
176 os << lineSeparator<< std::endl;
177 os << "Prod. Acc.";
178 for(int iclass=0;iclass<cm.nClasses();++iclass){
179 dpa=cm.pa_pct(cm.m_classes[iclass],&se95_ua);
180 os << fieldSeparator << dpa;
181 if(cm.m_se95)
182 os << " (" << se95_pa << ")";
183 }
184 os << lineSeparator<< std::endl;
185 if(cm.getFormat()==LATEX){
186 os << "\\end{tabular}" << std::endl;
187 os << "\\end{document}" << std::endl;
188 }
189 return os;
190 };

The documentation for this class was generated from the following files:

- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/algorithms/ConfusionMatrix.h
- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/algorithms/ConfusionMatrix.cc