00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __freAccuracySOMetric_txx
00023 #define __freAccuracySOMetric_txx
00024
00025 #include "freAccuracySOMetric.h"
00026 #include "freStatistics.h"
00027 #include "freGenericSetupToImageAdaptor.h"
00028 #include "freVectorToNormImageFilter.h"
00029 #include "freImageSampleCharacteristicsCalculator.h"
00030 #include "freSessionProcessor.h"
00031
00032 #include "itkNumericTraits.h"
00033 #include "itkSubtractImageFilter.h"
00034 #include "itkImageAdaptor.h"
00035 #include "itkPointSet.h"
00036 #include "itkListSample.h"
00037
00038 namespace FREE
00039 {
00040
00041 template <unsigned int VImageDimension>
00042 void
00043 AccuracySOMetric<VImageDimension>::
00044 SetResultFieldPath(const IDPath& path)
00045 {
00046 itkDebugMacro("setting ResultFieldPath to " << path.ToStr() );
00047 this->m_ResultFieldPath = path;
00048 this->Modified();
00049 };
00050
00051 template <unsigned int VImageDimension>
00052 void
00053 AccuracySOMetric<VImageDimension>::
00054 SetReferenceFieldPath(const IDPath& path)
00055 {
00056 itkDebugMacro("setting ReferenceFieldPath to " << path.ToStr() );
00057 this->m_ReferenceFieldPath = path;
00058 this->Modified();
00059 };
00060
00061 template <unsigned int VImageDimension>
00062 void
00063 AccuracySOMetric<VImageDimension>::
00064 SetReferencePointsPath(const IDPath& path)
00065 {
00066 itkDebugMacro("setting ReferencePointsPath to " << path.ToStr() );
00067 this->m_ReferencePointsPath = path;
00068 this->Modified();
00069 };
00070
00071 template <unsigned int VImageDimension>
00072 void
00073 AccuracySOMetric<VImageDimension>::
00074 SetMovingPointsPath(const IDPath& path)
00075 {
00076 itkDebugMacro("setting MovingPointsPath to " << path.ToStr() );
00077 this->m_MovingPointsPath = path;
00078 this->Modified();
00079 };
00080
00081 template <unsigned int VImageDimension>
00082 AccuracySOMetric<VImageDimension>::
00083 AccuracySOMetric()
00084 {
00085 m_UseField = true;
00086 m_ComputeAdaptationMean = true;
00087 m_lUnevaluatedPoints = 0;
00088 m_ReferenceFieldPath.Reset();
00089 m_ReferencePointsPath.Reset();
00090 m_MovingPointsPath.Reset();
00091 m_ResultFieldPath.Reset();
00092 };
00093
00094 template <unsigned int VImageDimension>
00095 AccuracySOMetric<VImageDimension>::
00096 ~AccuracySOMetric()
00097 {
00098 };
00099
00100
00101 template <unsigned int VImageDimension>
00102 void
00103 AccuracySOMetric<VImageDimension>::
00104 InitializeValueComputation() const
00105 {
00106 m_dMinError = itk::NumericTraits< double >::max();
00107 m_dMaxError = 0.0;
00108 m_lFailedProcessings = 0;
00109 m_lCurAdaptation = 0;
00110 m_lUnevaluatedPoints = 0;
00111 m_dErrorMean = 0.0;
00112 m_dErrorDev = 0.0;
00113
00114 m_dDurMean = 0.0;
00115 m_dDurDev = 0.0;
00116 m_dMinDur = itk::NumericTraits< double >::max();
00117 m_dMaxDur = 0.0;
00118
00119 m_Errors.clear();
00120 m_Vars.clear();
00121 m_MinErrors.clear();
00122 m_MaxErrors.clear();
00123 m_Samplesize.clear();
00124 m_Durations.clear();
00125
00126 m_PointErrors.clear();
00127 };
00128
00129 template <unsigned int VImageDimension>
00130 void
00131 AccuracySOMetric<VImageDimension>::
00132 InitializeMonitor(MonitorType& monitor) const
00133 {
00134
00135 };
00136
00137 template <unsigned int VImageDimension>
00138 void
00139 AccuracySOMetric<VImageDimension>::
00140 InitializeThread(ThreadType& thread) const
00141 {
00142 thread.SetUseField(this->m_UseField);
00143 thread.SetResultFieldPath(this->m_ResultFieldPath);
00144 thread.SetReferenceFieldPath(this->m_ReferenceFieldPath);
00145 thread.SetReferencePointsPath(this->m_ReferencePointsPath);
00146 thread.SetMovingPointsPath(this->m_MovingPointsPath);
00147 };
00148
00149 template <unsigned int VImageDimension>
00150 typename AccuracySOMetric<VImageDimension>::DecomposedMeasureType
00151 AccuracySOMetric<VImageDimension>::
00152 ComputeMeasure(MonitorType& monitor) const
00153 {
00154
00155
00156 this->m_lFailedProcessings = monitor.GetFailureCount();
00157
00158 typename MonitorType::AdaptationIDListType evaluations = monitor.GetListOfEvaluatedAdaptations();
00159
00160 for (typename MonitorType::AdaptationIDListType::const_iterator pos = evaluations.begin(); pos != evaluations.end(); ++pos)
00161 {
00162 EvaluationResultType results;
00163 monitor.GetAdaptationResults(*pos,results);
00164
00165 if (this->m_dMinError > results.GetMinError()) this->m_dMinError = results.GetMinError();
00166 if (this->m_dMaxError < results.GetMaxError()) this->m_dMaxError = results.GetMaxError();
00167
00168 this->m_lUnevaluatedPoints += results.GetUnevaluatedPoints();
00169
00170 this->m_Errors.push_back(results.GetError());
00171 this->m_Vars.push_back(results.GetVariance());
00172 this->m_Samplesize.push_back(results.GetSamplesize());
00173 this->m_Durations.push_back(results.GetDuration());
00174
00175
00176 for (typename EvaluationResultType::PointErrorListType::const_iterator pos2 = results.GetPointErrors().begin(); pos2 != results.GetPointErrors().end(); ++pos2)
00177 {
00178 this->m_PointErrors.push_back(*pos2);
00179 }
00180 }
00181
00182 if (m_UseField)
00183 {
00184 return ComputeFieldMeasures();
00185 }
00186 else
00187 {
00188 return ComputePointsMeasures();
00189 }
00190 };
00191
00192 template <unsigned int VImageDimension>
00193 typename AccuracySOMetric<VImageDimension>::DecomposedMeasureType
00194 AccuracySOMetric<VImageDimension>::
00195 ComputeFieldMeasures() const
00196 {
00197 if (m_ComputeAdaptationMean)
00198 {
00199 unsigned long lUsedVariances = 0;
00200 for (unsigned int iIndex=0; iIndex<m_Errors.size(); iIndex++)
00201 {
00202 m_dErrorMean += m_Errors[iIndex];
00203 if (m_Vars[iIndex]>=0)
00204 {
00205 m_dErrorDev += m_Vars[iIndex];
00206 lUsedVariances++;
00207 }
00208 }
00209
00210 if (m_Errors.size()==0) m_dMinError = 0.0;
00211 else m_dErrorMean /= m_Errors.size();
00212 if (lUsedVariances>0) m_dErrorDev /= lUsedVariances;
00213 m_dErrorDev = sqrt(m_dErrorDev);
00214 }
00215 else
00216 {
00217 unsigned long lTotalSamples = 0;
00218 for (unsigned int iIndex=0; iIndex<m_Errors.size(); iIndex++)
00219 {
00220 m_dErrorMean += m_Errors[iIndex] * m_Samplesize[iIndex];
00221 m_dErrorDev += m_Vars[iIndex] * (m_Samplesize[iIndex]-1);
00222 lTotalSamples += m_Samplesize[iIndex];
00223 }
00224
00225 if (m_Errors.size()==0) m_dMinError = 0.0;
00226
00227 if (lTotalSamples>0) m_dErrorMean /= lTotalSamples;
00228 if (lTotalSamples>1) m_dErrorDev /= lTotalSamples-1;
00229 m_dErrorDev = sqrt(m_dErrorDev);
00230 }
00231
00232 for (unsigned int iIndex=0; iIndex<m_Durations.size(); iIndex++)
00233 {
00234 m_dDurMean += m_Durations[iIndex];
00235 if (m_Durations[iIndex]< m_dMinDur) m_dMinDur = m_Durations[iIndex];
00236 if (m_Durations[iIndex]> m_dMaxDur) m_dMaxDur = m_Durations[iIndex];
00237 }
00238
00239 if (m_Durations.size()==0) m_dMinDur = 0.0;
00240 if (m_Durations.size()>0) m_dDurMean /= m_Durations.size();
00241
00242 if (m_Durations.size()>1)
00243 {
00244 for (unsigned int iIndex=0; iIndex<m_Durations.size(); iIndex++)
00245 {
00246 m_dDurDev += (m_Durations[iIndex] - m_dDurMean)*(m_Durations[iIndex] - m_dDurMean);
00247 }
00248 m_dDurDev = sqrt(m_dDurDev/(m_Durations.size()-1));
00249 }
00250
00251 DecomposedMeasureType results(10);
00252 results.SetElement(0,m_dErrorMean);
00253 results.SetElement(1,m_dErrorDev);
00254 results.SetElement(2,m_dMinError);
00255 results.SetElement(3,m_dMaxError);
00256 results.SetElement(4,m_lFailedProcessings);
00257 results.SetElement(5,m_lUnevaluatedPoints);
00258 results.SetElement(6,m_dDurMean);
00259 results.SetElement(7,m_dDurDev);
00260 results.SetElement(8,m_dMinDur);
00261 results.SetElement(9,m_dMaxDur);
00262
00263 return results;
00264 };
00265
00266 template <unsigned int VImageDimension>
00267 typename AccuracySOMetric<VImageDimension>::DecomposedMeasureType
00268 AccuracySOMetric<VImageDimension>::
00269 ComputePointsMeasures() const
00270 {
00271 const unsigned int iMeasurementVectorSize = 1;
00272 typedef itk::Vector<double,iMeasurementVectorSize> MeasurementVectorType;
00273 typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
00274
00275 SampleType::Pointer smpSample = SampleType::New();
00276 smpSample->SetMeasurementVectorSize(iMeasurementVectorSize);
00277
00278 MeasurementVectorType mv;
00279
00280 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType;
00281 typename CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New();
00282
00283 if (m_ComputeAdaptationMean)
00284 {
00285 unsigned long lUsedVariances = 0;
00286 for (unsigned int iIndex=0; iIndex<m_Errors.size(); iIndex++)
00287 {
00288 m_dErrorMean += m_Errors[iIndex];
00289 if (m_Vars[iIndex]>=0)
00290 {
00291 m_dErrorDev += m_Vars[iIndex];
00292 lUsedVariances++;
00293 }
00294 }
00295
00296 if (m_Errors.size()==0) m_dMinError = 0.0;
00297 else m_dErrorMean /= m_Errors.size();
00298 if (lUsedVariances>0) m_dErrorDev /= lUsedVariances;
00299 m_dErrorDev = sqrt(m_dErrorDev);
00300 }
00301 else
00302 {
00303 if (m_PointErrors.size()>0)
00304 {
00305
00306
00307 for (unsigned int iIndex=0; iIndex<m_PointErrors.size(); iIndex++)
00308 {
00309 mv[0]=m_PointErrors[iIndex];
00310 smpSample->PushBack(mv);
00311 }
00312
00313
00314 covarianceAlgorithm->SetInputSample( smpSample );
00315 covarianceAlgorithm->SetMean( 0 );
00316 covarianceAlgorithm->Update();
00317
00318 m_dErrorMean = covarianceAlgorithm->GetMean()->GetElement(0);
00319
00320 if (m_PointErrors.size()>1)
00321 {
00322 const CovarianceAlgorithmType::OutputType* matrix = covarianceAlgorithm->GetOutput();
00323 m_dErrorDev = sqrt((*matrix)(0,0));
00324 }
00325 else
00326 {
00327 m_dErrorDev = 0.0;
00328 }
00329 }
00330 else
00331 {
00332 m_dMinError = 0.0;
00333
00334 }
00335 }
00336
00337 if (m_Durations.size()>0)
00338 {
00339
00340
00341 smpSample->Clear();
00342 for (unsigned int iIndex=0; iIndex<m_Durations.size(); iIndex++)
00343 {
00344 mv[0]=m_Durations[iIndex];
00345 smpSample->PushBack(mv);
00346
00347 if (mv[0]<m_dMinDur) m_dMinDur = mv[0];
00348 if (mv[0]>m_dMaxDur) m_dMaxDur = mv[0];
00349 }
00350
00351 covarianceAlgorithm->SetInputSample( smpSample );
00352 covarianceAlgorithm->SetMean( 0 );
00353 covarianceAlgorithm->Update();
00354
00355 m_dDurMean = covarianceAlgorithm->GetMean()->GetElement(0);
00356
00357 if (m_Durations.size()>1)
00358 {
00359 const typename CovarianceAlgorithmType::OutputType* matrix = covarianceAlgorithm->GetOutput();
00360 m_dDurDev = sqrt((*matrix)(0,0));
00361 }
00362 else
00363 {
00364 m_dDurDev = 0.0;
00365 }
00366 }
00367 else
00368 {
00369 m_dMinDur = 0.0;
00370
00371 }
00372
00373 DecomposedMeasureType results(10);
00374 results.SetElement(0,m_dErrorMean);
00375 results.SetElement(1,m_dErrorDev);
00376 results.SetElement(2,m_dMinError);
00377 results.SetElement(3,m_dMaxError);
00378 results.SetElement(4,m_lFailedProcessings);
00379 results.SetElement(5,m_lUnevaluatedPoints);
00380 results.SetElement(6,m_dDurMean);
00381 results.SetElement(7,m_dDurDev);
00382 results.SetElement(8,m_dMinDur);
00383 results.SetElement(9,m_dMaxDur);
00384
00385 return results;
00386 };
00387
00388 }
00389
00390 #endif