| 1 | /********************************************************************** |
| 2 | ConfusionMatrix.cc: class for (classification accuracy) confusion matrix |
| 3 | Author(s): [email protected] |
| 4 | Copyright (C) 2016-2020 European Union (Joint Research Centre) |
| 5 | |
| 6 | This file is part of jiplib. |
| 7 | |
| 8 | jiplib is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | jiplib is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with jiplib. If not, see <https://www.gnu.org/licenses/>. |
| 20 | ***********************************************************************/ |
| 21 | #include "ConfusionMatrix.h" |
| 22 | #include <iostream> |
| 23 | #include <numeric> |
| 24 | |
| 25 | using namespace confusionmatrix; |
| 26 | |
| 27 | bool compareClass(const std::string& string1, const std::string& string2){ |
| 28 | int int1=string2type<int>(string1); |
| 29 | int int2=string2type<int>(string2); |
| 30 | return(int1<int2); |
| 31 | }; |
| 32 | |
| 33 | ConfusionMatrix::ConfusionMatrix() |
| 34 | : m_classes(),m_results(),m_se95(true),m_format(ASCII) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | ConfusionMatrix::~ConfusionMatrix() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | //constructor where class names are 0,1,...,nclass-1 |
| 43 | ConfusionMatrix::ConfusionMatrix(short nclass){ |
| 44 | resize(nclass); |
| 45 | } |
| 46 | |
| 47 | ConfusionMatrix::ConfusionMatrix(const std::vector<std::string>& classNames){ |
| 48 | setClassNames(classNames); |
| 49 | } |
| 50 | |
| 51 | //copy constructor |
| 52 | ConfusionMatrix::ConfusionMatrix(const ConfusionMatrix& cm){ |
| 53 | setClassNames(cm.m_classes); |
| 54 | setResults(cm.m_results); |
| 55 | } |
| 56 | |
| 57 | //assignment operator |
| 58 | ConfusionMatrix& ConfusionMatrix::operator=(const ConfusionMatrix& cm){ |
| 59 | //check for self-assignment by comparing the address of the implicit object and parameter |
| 60 | if(this==&cm) |
| 61 | return *this; |
| 62 | else{ |
| 63 | setClassNames(cm.m_classes); |
| 64 | setResults(cm.m_results); |
| 65 | } |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | ConfusionMatrix& ConfusionMatrix::operator+=(const ConfusionMatrix &cm) |
| 70 | { |
| 71 | if(cm.m_classes.size()!=this->m_classes.size()){ |
| 72 | std::cerr << "error0: "<< cm.m_classes.size() << "!=" << this->m_classes.size() << std::endl; |
| 73 | exit(0); |
| 74 | } |
| 75 | if(cm.m_results.size()!=this->m_results.size()){ |
| 76 | std::cerr << "error1: "<< cm.m_results.size() << "!=" << this->m_results.size() << std::endl; |
| 77 | exit(1); |
| 78 | } |
| 79 | for(int irow=0;irow<m_results.size();++irow){ |
| 80 | if(cm.m_results[irow].size()!=this->m_results[irow].size()){ |
| 81 | std::cerr << "error2: " << cm.m_results[irow].size() << "!=" << this->m_results[irow].size() << std::endl; |
| 82 | exit(2); |
| 83 | } |
| 84 | for(int icol=0;icol<m_results[irow].size();++icol) |
| 85 | this->m_results[irow][icol]+=cm.m_results[irow][icol]; |
| 86 | } |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | ConfusionMatrix& ConfusionMatrix::operator*=(double weight) |
| 91 | { |
| 92 | for(int irow=0;irow<m_results.size();++irow){ |
| 93 | for(int icol=0;icol<m_results[irow].size();++icol) |
| 94 | m_results[irow][icol]*=weight; |
| 95 | } |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | void ConfusionMatrix::sortClassNames(){ |
| 100 | sort(m_classes.begin(),m_classes.end(),compareClass); |
| 101 | } |
| 102 | |
| 103 | ConfusionMatrix ConfusionMatrix::operator*(double weight) |
| 104 | { |
| 105 | ConfusionMatrix result = *this;//make a copy of myself |
| 106 | result*=weight; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | void ConfusionMatrix::resize(short nclass){ |
| 111 | m_classes.resize(nclass); |
| 112 | for(short iclass=0;iclass<nclass;++iclass){ |
| 113 | std::ostringstream osclass; |
| 114 | osclass << iclass; |
| 115 | m_classes[iclass]=osclass.str(); |
| 116 | } |
| 117 | m_results.resize(nclass,nclass); |
| 118 | } |
| 119 | |
| 120 | void ConfusionMatrix::setClassNames(const std::vector<std::string>& classNames, bool doSort){ |
| 121 | m_classes=classNames; |
| 122 | if(doSort) |
| 123 | sortClassNames(); |
| 124 | if(m_results.size()!=m_classes.size()) |
| 125 | m_results.resize(m_classes.size(),m_classes.size()); |
| 126 | } |
| 127 | |
| 128 | void ConfusionMatrix::pushBackClassName(const std::string& className, bool doSort){ |
| 129 | m_classes.push_back(className); |
| 130 | if(doSort) |
| 131 | sortClassNames(); |
| 132 | if(m_results.size()!=m_classes.size()) |
| 133 | m_results.resize(m_classes.size(),m_classes.size()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | void ConfusionMatrix::setResults(const Vector2d<double>& theResults){ |
| 138 | m_results=theResults; |
| 139 | } |
| 140 | |
| 141 | void ConfusionMatrix::clearResults(){ |
| 142 | m_results.clear(); |
| 143 | m_results.resize(m_classes.size(),m_classes.size()); |
| 144 | } |
| 145 | |
| 146 | void ConfusionMatrix::setResult(const std::string& theRef, const std::string& theClass, double theResult){ |
| 147 | // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef)); |
| 148 | // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass)); |
| 149 | // assert(ir>=0); |
| 150 | // assert(ir<m_results.size()); |
| 151 | // assert(ic>=0); |
| 152 | // assert(ic<m_results[ir].size()); |
| 153 | int ir=getClassIndex(theRef); |
| 154 | int ic=getClassIndex(theClass); |
| 155 | m_results[ir][ic]=theResult; |
| 156 | } |
| 157 | |
| 158 | void ConfusionMatrix::incrementResult(const std::string& theRef, const std::string& theClass, double theIncrement){ |
| 159 | // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef)); |
| 160 | // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass)); |
| 161 | int ir=getClassIndex(theRef); |
| 162 | int ic=getClassIndex(theClass); |
| 163 | assert(ir>=0); |
| 164 | if(ir>=m_results.size()) |
| 165 | std::cerr << "Error: " << theRef << " not found in class ConfusionMatrix when incrementing for class " << theClass << std::endl; |
| 166 | assert(ir<m_results.size()); |
| 167 | assert(ic>=0); |
| 168 | assert(ic<m_results[ir].size()); |
| 169 | m_results[ir][ic]+=theIncrement; |
| 170 | } |
| 171 | |
| 172 | double ConfusionMatrix::nReference(const std::string& theRef) const{ |
| 173 | // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theRef)); |
| 174 | int ir=getClassIndex(theRef); |
| 175 | return accumulate(m_results[ir].begin(),m_results[ir].end(),0); |
| 176 | } |
| 177 | |
| 178 | double ConfusionMatrix::nReference() const{ |
| 179 | double nref=0; |
| 180 | for(int ir=0;ir<m_classes.size();++ir) |
| 181 | nref+=accumulate(m_results[ir].begin(),m_results[ir].end(),0); |
| 182 | return nref; |
| 183 | } |
| 184 | |
| 185 | double ConfusionMatrix::nClassified(const std::string& theClass) const{ |
| 186 | // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass)); |
| 187 | int ic=getClassIndex(theClass); |
| 188 | double nclassified=0; |
| 189 | for(int iref=0;iref<m_results.size();++iref){ |
| 190 | assert(ic<m_results[iref].size()); |
| 191 | nclassified+=m_results[iref][ic]; |
| 192 | } |
| 193 | return(nclassified); |
| 194 | } |
| 195 | |
| 196 | double ConfusionMatrix::pa(const std::string& theClass, double* se95) const{ |
| 197 | assert(m_results.size()); |
| 198 | assert(m_results.size()==m_classes.size()); |
| 199 | double producer=0; |
| 200 | // int ir=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass)); |
| 201 | int ir=getClassIndex(theClass); |
| 202 | assert(ir>=0); |
| 203 | assert(ir<m_results.size()); |
| 204 | assert(!theClass.compare(m_classes[ir])); |
| 205 | for(int iclass=0;iclass<m_results.size();++iclass){ |
| 206 | assert(iclass<m_results[ir].size()); |
| 207 | producer+=m_results[ir][iclass]; |
| 208 | } |
| 209 | double dpa=(producer>0)? static_cast<double>(m_results[ir][ir])/producer : 0; |
| 210 | double dqa=1.0-dpa; |
| 211 | if(se95!=NULL) |
| 212 | *se95=(dpa<1&&dpa>0)? sqrt(dpa*dqa/(producer-1)) : 0; |
| 213 | return dpa; |
| 214 | } |
| 215 | |
| 216 | int ConfusionMatrix::pa_pct(const std::string& theClass, double* se95) const{ |
| 217 | double dpa=pa(theClass,se95); |
| 218 | if(se95!=NULL) |
| 219 | *se95=static_cast<double>(static_cast<int>(0.5+1000*(*se95)))/10.0; |
| 220 | return static_cast<int>(0.5+100.0*dpa); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | double ConfusionMatrix::ua(const std::string& theClass, double* se95) const{ |
| 225 | assert(m_results.size()); |
| 226 | assert(m_results.size()==m_classes.size()); |
| 227 | double user=0; |
| 228 | // int ic=distance(m_classes.begin(),find(m_classes.begin(),m_classes.end(),theClass)); |
| 229 | int ic=getClassIndex(theClass); |
| 230 | assert(ic>=0); |
| 231 | assert(ic<m_results.size()); |
| 232 | assert(!theClass.compare(m_classes[ic])); |
| 233 | for(int iref=0;iref<m_results.size();++iref){ |
| 234 | assert(ic<m_results[iref].size()); |
| 235 | user+=m_results[iref][ic]; |
| 236 | } |
| 237 | double dua=(user>0)? static_cast<double>(m_results[ic][ic])/user : 0; |
| 238 | double dva=1.0-dva; |
| 239 | if(se95!=NULL) |
| 240 | *se95=(dua<1&&dua>0)? sqrt(dua*dva/(user-1)) : 0; |
| 241 | return dua; |
| 242 | } |
| 243 | |
| 244 | int ConfusionMatrix::ua_pct(const std::string& theClass,double* se95) const{ |
| 245 | double dua=ua(theClass,se95); |
| 246 | if(se95!=NULL) |
| 247 | *se95=static_cast<double>(static_cast<int>(0.5+1000*(*se95)))/10.0; |
| 248 | return static_cast<int>(0.5+100.0*dua); |
| 249 | } |
| 250 | |
| 251 | double ConfusionMatrix::oa(double* se95) const{ |
| 252 | double ntotal=m_results.sum(); |
| 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 | } |
| 264 | |
| 265 | int ConfusionMatrix::oa_pct(double* se95) const{ |
| 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 | } |
| 271 | |
| 272 | double ConfusionMatrix::kappa() const{ |
| 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 | } |