00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "frePrecisionSOMetric.h"
00024
00025 #include "itkNumericTraits.h"
00026 #include "itkPointSet.h"
00027 #include "itkListSample.h"
00028 #include "itkCovarianceCalculator.h"
00029
00030 namespace FREE
00031 {
00032
00033 void
00034 PrecisionSOMetric::
00035 Initialize()
00036 {
00037 Superclass::Initialize();
00038
00039 if (this->m_EvaluationMetric.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, no evaluation metric set.");
00040 this->m_EvaluationMetric->SetTransform(this->m_Transform);
00041 this->m_EvaluationMetric->SetAdaptationList(this->m_smpAdaptations);
00042
00043 this->m_EvaluationMetric->fnOnEvaluationProgress = fnOnEvaluationProgress;
00044
00045 this->m_EvaluationMetric->Initialize();
00046
00047 this->EnlistDictionaryEntries(this->m_MetricStatistic.GetDictionary());
00048 }
00049
00050 void
00051 PrecisionSOMetric::
00052 EnlistInterimResultsToStatistic(const unsigned int iIndex, const DecomposedMeasureType& results, StatisticEntry& entry) const
00053 {
00054 entry.AddValue(Convert::ToStr(iIndex),1);
00055
00056 for (unsigned int iResultIndex = 0; iResultIndex < results.size(); iResultIndex++)
00057 {
00058 entry.AddValue(Convert::ToStr(results[iResultIndex]),iResultIndex+2);
00059 }
00060 };
00061
00062 void
00063 PrecisionSOMetric::
00064 EnlistDictionaryEntries(StatisticDictionary& dictionary) const
00065 {
00066 dictionary.AddValueDefinition(this->GetNameOfClass(),"Sample #", "Nr of the sample used to estimate the precision");
00067 unsigned int iValueCount = this->m_EvaluationMetric->GetNumberOfValues();
00068
00069 for (unsigned int iValueIndex = 0; iValueIndex < iValueCount; iValueIndex++)
00070 {
00071 dictionary.AddValueDefinition(this->GetNameOfClass(),"evaluation value #"+Convert::ToStr(iValueIndex), "Value #"+Convert::ToStr(iValueIndex)+" of the evaluation metric.");
00072 }
00073
00074 dictionary.AddEntryDefinition(this->GetNameOfClass(),"SampleMetricValues",this->GetNameOfClass(),"Values of a sample of the metric");
00075 };
00076
00077 PrecisionSOMetric::DecomposedMeasureType
00078 PrecisionSOMetric::
00079 ComputeDecomposedValue( const ParametersType & parameters ) const
00080 {
00081 if (this->m_smpAdaptations.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, adaptiation list is null.");
00082 if (this->m_Transform.IsNull()) throwExceptionMacro("Error. Cannot calculate metric value, no setup transform set.");
00083
00084 if (this->m_Transform->GetNumberOfParameters() != parameters.size()) throwExceptionMacro("Error. Cannot calculate metric value, number of passed parameters is not equal to the parameter size of the transform. Passed parameter count: "<< parameters.size());
00085
00086 m_Measures.clear();
00087
00088 for(unsigned long iIndex = 0; iIndex < m_SampleSize; iIndex++)
00089 {
00090 if (this->fnOnNextAdaptation.IsNotNull()) this->fnOnNextAdaptation->Execute(iIndex,"",(void*)this);
00091 this->InvokeEvent( NextAdaptationObserverEvent() );
00092
00093 StatisticEntry* pNewEntry = m_MetricStatistic.GetData().CreateChildEntry(m_MetricStatistic.GetData().GetCurrentEntryID());
00094 pNewEntry->SetRefID(m_MetricStatistic.GetDictionary().GetEntryDefinitionByName(this->GetNameOfClass(),"SampleMetricValues")->GetRefID());
00095
00096 DecomposedMeasureType interimValues = this->m_EvaluationMetric->GetDecomposedValue(parameters);
00097 m_Measures.push_back(interimValues);
00098
00099 this->EnlistInterimResultsToStatistic(iIndex,interimValues,*pNewEntry);
00100
00101
00102 pNewEntry->CloseEntry();
00103
00104 if (fnOnEvaluationDone.IsNotNull()) fnOnEvaluationDone->Execute(iIndex,"",(void*)this);
00105 this->InvokeEvent( EvaluationDoneObserverEvent() );
00106 }
00107
00108 DecomposedMeasureType results(this->GetNumberOfValues());
00109 results.fill(0.0);
00110
00111 if (m_Measures.size())
00112 {
00113 unsigned int iResultIndex = 0;
00114
00115 const unsigned int iMeasureCount = this->m_EvaluationMetric->GetNumberOfValues();
00116 const unsigned int iMeasurementVectorSize = 1;
00117 typedef itk::Vector<double,iMeasurementVectorSize> MeasurementVectorType;
00118 typedef itk::Statistics::ListSample< MeasurementVectorType > SampleType;
00119
00120 typedef itk::Statistics::CovarianceCalculator< SampleType > CovarianceAlgorithmType;
00121 CovarianceAlgorithmType::Pointer covarianceAlgorithm = CovarianceAlgorithmType::New();
00122
00123 for (unsigned int iMeasureIndex = 0; iMeasureIndex < iMeasureCount; iMeasureIndex++)
00124 {
00125 double dMinValue = itk::NumericTraits< double >::max();
00126 double dMaxValue = itk::NumericTraits< double >::NonpositiveMin();
00127
00128 SampleType::Pointer smpSample = SampleType::New();
00129 smpSample->SetMeasurementVectorSize(iMeasurementVectorSize);
00130
00131 for (unsigned int iPos=0; iPos<m_Measures.size(); iPos++)
00132 {
00133 MeasurementVectorType mv;
00134 mv[0]=(m_Measures[iPos])[iMeasureIndex];
00135 smpSample->PushBack(mv);
00136
00137 if (mv[0]<dMinValue) dMinValue = mv[0];
00138 if (mv[0]>dMaxValue) dMaxValue = mv[0];
00139 }
00140
00141
00142 covarianceAlgorithm->SetInputSample( smpSample );
00143 covarianceAlgorithm->SetMean( 0 );
00144 covarianceAlgorithm->Update();
00145
00146 results.SetElement(iResultIndex++,covarianceAlgorithm->GetMean()->GetElement(0));
00147 const CovarianceAlgorithmType::OutputType* matrix = covarianceAlgorithm->GetOutput();
00148 results.SetElement(iResultIndex++,sqrt((*matrix)(0,0)));
00149 results.SetElement(iResultIndex++,dMinValue);
00150 results.SetElement(iResultIndex++,dMaxValue);
00151 }
00152 }
00153
00154 return results;
00155 };
00156
00157 PrecisionSOMetric::
00158 PrecisionSOMetric()
00159 {
00160 m_SampleSize = 100;
00161
00162 m_EvaluationMetric = 0;
00163 };
00164
00165 unsigned int
00166 PrecisionSOMetric::
00167 GetNumberOfValues(void) const
00168 {
00169 if (m_EvaluationMetric.IsNotNull())
00170 {
00171 return m_EvaluationMetric->GetNumberOfValues()*4;
00172 }
00173 return 0;
00174 };
00175
00176 }