1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*=========================================================================
Program: Advanced Normalization Tools
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __antsManifoldParzenWindowsListSampleFunction_hxx
#define __antsManifoldParzenWindowsListSampleFunction_hxx
namespace itk
{
namespace ants
{
namespace Statistics
{
template <typename TListSample, typename TOutput, typename TCoordRep>
ManifoldParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::ManifoldParzenWindowsListSampleFunction()
{
this->m_KdTreeGenerator = nullptr;
this->m_EvaluationKNeighborhood = 50;
this->m_RegularizationSigma = 1.0;
this->m_CovarianceKNeighborhood = 0;
this->m_KernelSigma = 0.0;
}
template <typename TListSample, typename TOutput, typename TCoordRep>
ManifoldParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::~ManifoldParzenWindowsListSampleFunction() =
default;
template <typename TListSample, typename TOutput, typename TCoordRep>
void
ManifoldParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::SetInputListSample(
const InputListSampleType * ptr)
{
Superclass::SetInputListSample(ptr);
if (!this->GetInputListSample())
{
return;
}
if (this->GetInputListSample()->Size() <= 1)
{
itkWarningMacro("The input list sample has <= 1 element."
<< "Function evaluations will be equal to 0.");
return;
}
/**
* Generate KdTree and create set of gaussians from input point set
*/
this->m_KdTreeGenerator = TreeGeneratorType::New();
this->m_KdTreeGenerator->SetSample(const_cast<InputListSampleType *>(this->GetInputListSample()));
this->m_KdTreeGenerator->SetBucketSize(16);
this->m_KdTreeGenerator->Update();
/**
* Calculate covariance matrices
*/
this->m_Gaussians.resize(this->GetInputListSample()->Size());
const unsigned int Dimension = this->GetInputListSample()->GetMeasurementVectorSize();
unsigned long count = 0;
typename InputListSampleType::ConstIterator It = this->GetInputListSample()->Begin();
while (It != this->GetInputListSample()->End())
{
InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
typename GaussianType::MeanVectorType mean(Dimension);
for (unsigned int d = 0; d < Dimension; d++)
{
mean[d] = inputMeasurement[d];
}
this->m_Gaussians[count] = GaussianType::New();
this->m_Gaussians[count]->SetMean(mean);
if (this->m_CovarianceKNeighborhood > 0)
{
/** Temporarily set the covariance */
CovarianceMatrixType Cov(Dimension, Dimension);
Cov.SetIdentity();
Cov *= this->m_KernelSigma;
this->m_Gaussians[count]->SetCovariance(Cov);
Cov.Fill(0);
typename TreeGeneratorType::KdTreeType ::InstanceIdentifierVectorType neighbors;
unsigned int numberOfNeighbors =
std::min(this->m_CovarianceKNeighborhood, static_cast<unsigned int>(this->GetInputListSample()->Size()));
this->m_KdTreeGenerator->GetOutput()->Search(inputMeasurement, numberOfNeighbors, neighbors);
RealType denominator = 0.0;
for (unsigned int j = 0; j < numberOfNeighbors; j++)
{
if (neighbors[j] != count && neighbors[j] < this->GetInputListSample()->Size())
{
InputMeasurementVectorType neighbor =
this->m_KdTreeGenerator->GetOutput()->GetMeasurementVector(neighbors[j]);
RealType kernelValue = this->m_Gaussians[count]->Evaluate(neighbor);
if (this->GetListSampleWeights()->Size() == this->m_Gaussians.size())
{
kernelValue *= static_cast<RealType>((*this->GetListSampleWeights())[count]);
}
denominator += kernelValue;
if (kernelValue > NumericTraits<RealType>::ZeroValue())
{
for (unsigned int m = 0; m < Dimension; m++)
{
for (unsigned int n = m; n < Dimension; n++)
{
RealType covariance =
kernelValue * (neighbor[m] - inputMeasurement[m]) * (neighbor[n] - inputMeasurement[n]);
Cov(m, n) += static_cast<typename CovarianceMatrixType::ComponentType>(covariance);
Cov(n, m) += Cov(m, n);
}
}
}
}
}
if (denominator > NumericTraits<RealType>::ZeroValue())
{
Cov /= denominator;
}
for (unsigned int m = 0; m < Dimension; m++)
{
Cov(m, m) += static_cast<typename CovarianceMatrixType::ComponentType>(this->m_RegularizationSigma *
this->m_RegularizationSigma);
}
this->m_Gaussians[count]->SetCovariance(Cov);
}
else
{
CovarianceMatrixType Cov(Dimension, Dimension);
Cov.SetIdentity();
Cov *= this->m_RegularizationSigma;
this->m_Gaussians[count]->SetCovariance(Cov);
}
++It;
++count;
}
/**
* Calculate normalization factor
*/
this->m_NormalizationFactor = 0.0;
for (unsigned int i = 0; i < this->m_Gaussians.size(); i++)
{
if (this->GetListSampleWeights()->Size() == this->m_Gaussians.size())
{
this->m_NormalizationFactor += static_cast<RealType>((*this->GetListSampleWeights())[i]);
}
else
{
this->m_NormalizationFactor += NumericTraits<RealType>::OneValue();
}
}
}
template <typename TListSample, typename TOutput, typename TCoordRep>
TOutput
ManifoldParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::Evaluate(
const InputMeasurementVectorType & measurement) const
{
try
{
unsigned int numberOfNeighbors =
std::min(this->m_EvaluationKNeighborhood, static_cast<unsigned int>(this->m_Gaussians.size()));
OutputType sum = 0.0;
if (numberOfNeighbors == this->m_Gaussians.size())
{
for (unsigned int j = 0; j < this->m_Gaussians.size(); j++)
{
sum += static_cast<OutputType>(this->m_Gaussians[j]->Evaluate(measurement));
}
}
else
{
typename TreeGeneratorType::KdTreeType::InstanceIdentifierVectorType neighbors;
this->m_KdTreeGenerator->GetOutput()->Search(measurement, numberOfNeighbors, neighbors);
for (unsigned int j = 0; j < numberOfNeighbors; j++)
{
sum += static_cast<OutputType>(this->m_Gaussians[neighbors[j]]->Evaluate(measurement));
}
}
return static_cast<OutputType>(sum / this->m_NormalizationFactor);
}
catch (...)
{
return 0;
}
}
/**
* Standard "PrintSelf" method
*/
template <typename TListSample, typename TOutput, typename TCoordRep>
void
ManifoldParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::PrintSelf(std::ostream & os,
Indent indent) const
{
os << indent << "Regularization sigma: " << this->m_RegularizationSigma << std::endl;
os << indent << "Evaluation K neighborhood: " << this->m_EvaluationKNeighborhood << std::endl;
if (this->m_CovarianceKNeighborhood > 0)
{
os << indent << "Covariance K neighborhood: " << this->m_CovarianceKNeighborhood << std::endl;
os << indent << "Kernel sigma: " << this->m_KernelSigma << std::endl;
}
}
} // end of namespace Statistics
} // end of namespace ants
} // end of namespace itk
#endif
|