00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __freImageClassificationSOMetric_txx
00023 #define __freImageClassificationSOMetric_txx
00024
00025 #include "freImageClassificationSOMetric.h"
00026 #include "freGenericSetupToImageAdaptor.h"
00027 #include "freImageSampleCharacteristicsCalculator.h"
00028 #include "freSessionProcessor.h"
00029
00030 #include "freImageIO.h"
00031 #include "itkNumericTraits.h"
00032 #include "itkAbsoluteValueDifferenceImageFilter.h"
00033
00034 #include <time.h>
00035
00036 namespace FREE
00037 {
00038
00039 template <unsigned int VImageDimension>
00040 void
00041 ImageClassificationSOMetric<VImageDimension>::
00042 SetReferenceImagePath(const IDPath& path)
00043 {
00044 itkDebugMacro("setting ReferenceImagePath to " << path.ToStr() );
00045 this->m_ReferenceImagePath = path;
00046 this->Modified(); \
00047 };
00048
00049 template <unsigned int VImageDimension>
00050 void
00051 ImageClassificationSOMetric<VImageDimension>::
00052 SetResultImagePath(const IDPath& path)
00053 {
00054 itkDebugMacro("setting ResultImagePath to " << path.ToStr() );
00055 this->m_ResultImagePath = path;
00056 this->Modified(); \
00057 };
00058
00059 template <unsigned int VImageDimension>
00060 ImageClassificationSOMetric<VImageDimension>::
00061 ImageClassificationSOMetric()
00062 {
00063 m_ResultImagePath.Reset();
00064 m_ReferenceImagePath.Reset();
00065 m_FMeasureWeight = 0.0;
00066 };
00067
00068 template <unsigned int VImageDimension>
00069 void
00070 ImageClassificationSOMetric<VImageDimension>::
00071 InitializeValueComputation() const
00072 {
00073 m_dSensitivityMean = 0.0;
00074 m_dSensitivityDev = 0.0;
00075 m_dMinSensitivity = itk::NumericTraits< double >::max();
00076 m_dMaxSensitivity = 0.0;
00077
00078 m_dSpecMean = 0.0;
00079 m_dSpecDev = 0.0;
00080 m_dMinSpec = itk::NumericTraits< double >::max();
00081 m_dMaxSpec = 0.0;
00082
00083 m_dPPVMean = 0.0;
00084 m_dPPVDev = 0.0;
00085 m_dMinPPV = itk::NumericTraits< double >::max();
00086 m_dMaxPPV = 0.0;
00087
00088 m_dNPVMean = 0.0;
00089 m_dNPVDev = 0.0;
00090 m_dMinNPV = itk::NumericTraits< double >::max();
00091 m_dMaxNPV = 0.0;
00092
00093 m_dFMMean = 0.0;
00094 m_dFMDev = 0.0;
00095 m_dMinFM = itk::NumericTraits< double >::max();
00096 m_dMaxFM = 0.0;
00097
00098 m_lFailedProcessings = 0;
00099
00100 m_dDurMean = 0.0;
00101 m_dDurDev = 0.0;
00102 m_dMinDur = itk::NumericTraits< double >::max();
00103 m_dMaxDur = 0.0;
00104
00105 m_TPs.clear();
00106 m_FPs.clear();
00107 m_TNs.clear();
00108 m_FNs.clear();
00109 m_Durations.clear();
00110 };
00111
00112 template <unsigned int VImageDimension>
00113 void
00114 ImageClassificationSOMetric<VImageDimension>::
00115 InitializeMonitor(MonitorType& monitor) const
00116 {
00117
00118 };
00119
00120 template <unsigned int VImageDimension>
00121 void
00122 ImageClassificationSOMetric<VImageDimension>::
00123 InitializeThread(ThreadType& thread) const
00124 {
00125 thread.SetResultImagePath(this->m_ResultImagePath);
00126 thread.SetReferenceImagePath(this->m_ReferenceImagePath);
00127 thread.SetLowerClassThreshold(this->m_LowerClassThreshold);
00128 thread.SetUpperClassThreshold(this->m_UpperClassThreshold);
00129 };
00130
00131 template <unsigned int VImageDimension>
00132 typename ImageClassificationSOMetric<VImageDimension>::DecomposedMeasureType
00133 ImageClassificationSOMetric<VImageDimension>::
00134 ComputeMeasure(MonitorType& monitor) const
00135 {
00136
00137 this->m_lFailedProcessings = monitor.GetFailureCount();
00138
00139 typename MonitorType::AdaptationIDListType evaluations = monitor.GetListOfEvaluatedAdaptations();
00140
00141 for (typename MonitorType::AdaptationIDListType::const_iterator pos = evaluations.begin(); pos != evaluations.end(); ++pos)
00142 {
00143 EvaluationResultType results;
00144 monitor.GetAdaptationResults(*pos,results);
00145
00146 this->m_TPs.push_back(results.GetTP());
00147 this->m_FPs.push_back(results.GetFP());
00148 this->m_TNs.push_back(results.GetTN());
00149 this->m_FNs.push_back(results.GetFN());
00150 this->m_Durations.push_back(results.GetDuration());
00151 }
00152
00153 std::vector<double> sensitivities;
00154 std::vector<double> specitifities;
00155 std::vector<double> ppvs;
00156 std::vector<double> npvs;
00157 std::vector<double> fms;
00158
00159 try
00160 {
00161 for (unsigned int iIndex=0; iIndex<evaluations.size(); iIndex++)
00162 {
00163 double dSensitivity = double(m_TPs[iIndex])/double(m_TPs[iIndex]+m_FNs[iIndex]);
00164 double dSpecifity = double(m_TNs[iIndex])/double(m_FPs[iIndex]+m_TNs[iIndex]);
00165 double dPPV = double(m_TPs[iIndex])/double(m_TPs[iIndex]+m_FPs[iIndex]);
00166 double dNPV = double(m_TNs[iIndex])/double(m_TNs[iIndex]+m_FNs[iIndex]);
00167
00168 if (m_TPs[iIndex]+m_FNs[iIndex]==0) dSensitivity = 1.0;
00169 if (m_FPs[iIndex]+m_TNs[iIndex]==0) dSpecifity = 1.0;
00170 if (m_TPs[iIndex]+m_FPs[iIndex]==0) dPPV = 1.0;
00171 if (m_TNs[iIndex]+m_FNs[iIndex]==0) dNPV = 1.0;
00172
00173 double dFMeasure = ((1+m_FMeasureWeight)*(dPPV*dSensitivity))/((m_FMeasureWeight*dPPV)+dSensitivity);
00174
00175 if (dPPV+dSensitivity==0) dFMeasure = 0.0;
00176
00177 m_dSensitivityMean += dSensitivity;
00178 m_dSpecMean += dSpecifity;
00179 m_dPPVMean += dPPV;
00180 m_dNPVMean += dNPV;
00181 m_dFMMean += dFMeasure;
00182
00183 sensitivities.push_back(dSensitivity);
00184 specitifities.push_back(dSpecifity);
00185 ppvs.push_back(dPPV);
00186 npvs.push_back(dNPV);
00187 fms.push_back(dFMeasure);
00188
00189 if (dSensitivity<m_dMinSensitivity) m_dMinSensitivity = dSensitivity;
00190 else if (dSensitivity>m_dMaxSensitivity) m_dMaxSensitivity = dSensitivity;
00191
00192 if (dSpecifity<m_dMinSpec) m_dMinSpec = dSpecifity;
00193 else if (dSpecifity>m_dMaxSpec) m_dMaxSpec = dSpecifity;
00194
00195 if (dPPV<m_dMinPPV) m_dMinPPV = dPPV;
00196 else if (dPPV>m_dMaxPPV) m_dMaxPPV = dPPV;
00197
00198 if (dNPV<m_dMinNPV) m_dMinNPV = dNPV;
00199 else if (dNPV>m_dMaxNPV) m_dMaxNPV = dNPV;
00200
00201 if (dFMeasure<m_dMinFM) m_dMinFM = dFMeasure;
00202 else if (dFMeasure>m_dMaxFM) m_dMaxFM = dFMeasure;
00203 }
00204
00205 if (evaluations.size()==0)
00206 {
00207 m_dMinSensitivity = 0.0;
00208 m_dMinSpec = 0.0;
00209 m_dMinPPV = 0.0;
00210 m_dMinNPV = 0.0;
00211 m_dMinFM = 0.0;
00212 }
00213 else
00214 {
00215 m_dSensitivityMean /= evaluations.size();
00216 m_dSpecMean /= evaluations.size();
00217 m_dPPVMean /= evaluations.size();
00218 m_dNPVMean /= evaluations.size();
00219 m_dFMMean /= evaluations.size();
00220 }
00221
00222 if (evaluations.size()>1)
00223 {
00224 for (unsigned int iIndex=0; iIndex<evaluations.size(); iIndex++)
00225 {
00226 m_dSensitivityDev += (sensitivities[iIndex] - m_dSensitivityMean)*(sensitivities[iIndex] - m_dSensitivityMean);
00227 m_dSpecDev += (specitifities[iIndex] - m_dSpecMean)*(specitifities[iIndex] - m_dSpecMean);
00228 m_dPPVDev += (ppvs[iIndex] - m_dPPVMean)*(ppvs[iIndex] - m_dPPVMean);
00229 m_dNPVDev += (npvs[iIndex] - m_dNPVMean)*(npvs[iIndex] - m_dNPVMean);
00230 m_dFMDev += (fms[iIndex] - m_dFMMean)*(fms[iIndex] - m_dFMMean);
00231 }
00232
00233 m_dSensitivityDev = sqrt(m_dSensitivityDev / (evaluations.size()-1));
00234 m_dSpecDev = sqrt(m_dSpecDev / (evaluations.size()-1));
00235 m_dPPVDev = sqrt(m_dPPVDev / (evaluations.size()-1));
00236 m_dNPVDev = sqrt(m_dNPVDev / (evaluations.size()-1));
00237 m_dFMDev = sqrt(m_dFMDev / (evaluations.size()-1));
00238 }
00239 }
00240 catchAllNPassMacro("Unkown error while computing the statistical measures.");
00241
00242 try
00243 {
00244
00245 for (unsigned int iIndex=0; iIndex<m_Durations.size(); iIndex++)
00246 {
00247 m_dDurMean += m_Durations[iIndex];
00248 if (m_Durations[iIndex]< m_dMinDur) m_dMinDur = m_Durations[iIndex];
00249 else if (m_Durations[iIndex]> m_dMaxDur) m_dMaxDur = m_Durations[iIndex];
00250 }
00251
00252 if (m_Durations.size()==0) m_dMinDur = 0.0;
00253 if (m_Durations.size()>0) m_dDurMean /= m_Durations.size();
00254
00255 if (m_Durations.size()>1)
00256 {
00257 for (unsigned int iIndex=0; iIndex<m_Durations.size(); iIndex++)
00258 {
00259 m_dDurDev += (m_Durations[iIndex] - m_dDurMean)*(m_Durations[iIndex] - m_dDurMean);
00260 }
00261 m_dDurDev = sqrt(m_dDurDev / (m_Durations.size()-1));
00262 }
00263 }
00264 catchAllNPassMacro("Unkown error while computing the duration.");
00265
00266 DecomposedMeasureType results(25);
00267
00268 results.SetElement(0,m_dSensitivityMean);
00269 results.SetElement(1,m_dSensitivityDev);
00270 results.SetElement(2,m_dMinSensitivity);
00271 results.SetElement(3,m_dMaxSensitivity);
00272 results.SetElement(4,m_dSpecMean);
00273 results.SetElement(5,m_dSpecDev);
00274 results.SetElement(6,m_dMinSpec);
00275 results.SetElement(7,m_dMaxSpec);
00276 results.SetElement(8,m_dPPVMean);
00277 results.SetElement(9,m_dPPVDev);
00278 results.SetElement(10,m_dMinPPV);
00279 results.SetElement(11,m_dMaxPPV);
00280 results.SetElement(12,m_dNPVMean);
00281 results.SetElement(13,m_dNPVDev);
00282 results.SetElement(14,m_dMinNPV);
00283 results.SetElement(15,m_dMaxNPV);
00284 results.SetElement(16,m_dFMMean);
00285 results.SetElement(17,m_dFMDev);
00286 results.SetElement(18,m_dMinFM);
00287 results.SetElement(19,m_dMaxFM);
00288 results.SetElement(20,m_lFailedProcessings);
00289 results.SetElement(21,m_dDurMean);
00290 results.SetElement(22,m_dDurDev);
00291 results.SetElement(23,m_dMinDur);
00292 results.SetElement(24,m_dMaxDur);
00293
00294 return results;
00295 };
00296
00297 }
00298
00299 #endif