Skip to content

Commit

Permalink
STYLE: Remove this-> when ComputeDisplacementDistr accesses its data
Browse files Browse the repository at this point in the history
Removed `this->` when ComputeDisplacementDistribution directly accesses its own data.

Also replaced this->m_ScaledCostFunction with Superclass::m_ScaledCostFunction, to make clearer where that data member is from.
  • Loading branch information
N-Dekker committed Dec 4, 2024
1 parent 80a139e commit ce3f534
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Common/itkComputeDisplacementDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ class ITK_TEMPLATE_EXPORT ComputeDisplacementDistribution : public ScaledSingleV
void
SetFixedImageRegion(const FixedImageRegionType & region)
{
if (region != this->m_FixedImageRegion)
if (region != m_FixedImageRegion)
{
this->m_FixedImageRegion = region;
m_FixedImageRegion = region;
}
}

Expand All @@ -124,7 +124,7 @@ class ITK_TEMPLATE_EXPORT ComputeDisplacementDistribution : public ScaledSingleV
void
SetNumberOfWorkUnits(ThreadIdType numberOfThreads)
{
this->m_Threader->SetNumberOfWorkUnits(numberOfThreads);
m_Threader->SetNumberOfWorkUnits(numberOfThreads);
}


Expand Down
82 changes: 41 additions & 41 deletions Common/itkComputeDisplacementDistribution.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ template <typename TFixedImage, typename TTransform>
ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeDisplacementDistribution()
{
/** Initialize the m_ThreaderParameters. */
this->m_ThreaderParameters.st_Self = this;
m_ThreaderParameters.st_Self = this;

} // end Constructor

Expand All @@ -67,7 +67,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::InitializeThreadingPar
* each iteration, in the accumulate functions, in a multi-threaded fashion.
* This has performance benefits for larger vector sizes.
*/
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfWorkUnits();
const ThreadIdType numberOfThreads = m_Threader->GetNumberOfWorkUnits();

// For each thread, assign a struct of zero-initialized values.
m_ComputePerThreadVariables.assign(numberOfThreads, AlignedComputePerThreadStruct());
Expand Down Expand Up @@ -100,24 +100,24 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeSingleThreaded(
const SizeValueType nrofsamples = sampleContainer->Size();

/** Get the number of parameters. */
const auto numberOfParameters = static_cast<unsigned int>(this->m_Transform->GetNumberOfParameters());
const auto numberOfParameters = static_cast<unsigned int>(m_Transform->GetNumberOfParameters());

/** Get scales vector */
const ScalesType & scales = this->GetScales();
this->m_ScaledCostFunction->SetScales(scales);
Superclass::m_ScaledCostFunction->SetScales(scales);

/** Get the exact gradient. */
this->m_ExactGradient.set_size(numberOfParameters);
this->m_ExactGradient.Fill(0.0);
this->GetScaledDerivative(mu, this->m_ExactGradient);
m_ExactGradient.set_size(numberOfParameters);
m_ExactGradient.Fill(0.0);
this->GetScaledDerivative(mu, m_ExactGradient);

/** Get transform and set current position. */
const unsigned int outdim = this->m_Transform->GetOutputSpaceDimension();
const unsigned int outdim = m_Transform->GetOutputSpaceDimension();

unsigned int samplenr = 0;

/** Variables for nonzerojacobian indices and the Jacobian. */
const SizeValueType sizejacind = this->m_Transform->GetNumberOfNonZeroJacobianIndices();
const SizeValueType sizejacind = m_Transform->GetNumberOfNonZeroJacobianIndices();
JacobianType jacj(outdim, sizejacind, 0.0);
NonZeroJacobianIndicesType jacind(sizejacind);
jacind[0] = 0;
Expand All @@ -143,7 +143,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeSingleThreaded(
{
/** Read fixed coordinates and get Jacobian. */
const FixedImagePointType & point = sample.m_ImageCoordinates;
this->m_Transform->GetJacobian(point, jacj, jacind);
m_Transform->GetJacobian(point, jacj, jacind);

/** Apply scales, if necessary. */
if (this->GetUseScales())
Expand Down Expand Up @@ -172,7 +172,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeSingleThreaded(
for (unsigned int j = 0; j < sizejacind; ++j)
{
int pj = jacind[j];
temp += jacj(i, j) * this->m_ExactGradient(pj);
temp += jacj(i, j) * m_ExactGradient(pj);
}
Jgg(i) = temp;
}
Expand Down Expand Up @@ -218,7 +218,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::Compute(const Paramete
std::string methods)
{
/** Option for now to still use the single threaded code. */
if (!this->m_UseMultiThread)
if (!m_UseMultiThread)
{
return this->ComputeSingleThreaded(mu, jacg, maxJJ, methods);
}
Expand Down Expand Up @@ -248,20 +248,20 @@ void
ComputeDisplacementDistribution<TFixedImage, TTransform>::BeforeThreadedCompute(const ParametersType & mu)
{
/** Get the number of parameters. */
this->m_NumberOfParameters =
static_cast<unsigned int>(this->m_Transform->GetNumberOfParameters()); // why is this parameter needed?
m_NumberOfParameters =
static_cast<unsigned int>(m_Transform->GetNumberOfParameters()); // why is this parameter needed?

/** Get scales vector */
const ScalesType & scales = this->GetScales();
this->m_ScaledCostFunction->SetScales(scales);
Superclass::m_ScaledCostFunction->SetScales(scales);

/** Get the exact gradient. */
this->m_ExactGradient.set_size(this->m_NumberOfParameters);
this->m_ExactGradient.Fill(0.0);
this->GetScaledDerivative(mu, this->m_ExactGradient);
m_ExactGradient.set_size(m_NumberOfParameters);
m_ExactGradient.Fill(0.0);
this->GetScaledDerivative(mu, m_ExactGradient);

/** Get samples. */
this->m_SampleContainer = this->SampleFixedImageForJacobianTerms();
m_SampleContainer = this->SampleFixedImageForJacobianTerms();

} // end BeforeThreadedCompute()

Expand All @@ -275,7 +275,7 @@ void
ComputeDisplacementDistribution<TFixedImage, TTransform>::LaunchComputeThreaderCallback() const
{
/** Setup threader and launch. */
this->m_Threader->SetSingleMethodAndExecute(this->ComputeThreaderCallback, &m_ThreaderParameters);
m_Threader->SetSingleMethodAndExecute(this->ComputeThreaderCallback, &m_ThreaderParameters);

} // end LaunchComputeThreaderCallback()

Expand Down Expand Up @@ -313,9 +313,9 @@ void
ComputeDisplacementDistribution<TFixedImage, TTransform>::ThreadedCompute(ThreadIdType threadId)
{
/** Get sample container size, number of threads, and output space dimension. */
const SizeValueType sampleContainerSize = this->m_SampleContainer->Size();
const ThreadIdType numberOfThreads = this->m_Threader->GetNumberOfWorkUnits();
const unsigned int outdim = this->m_Transform->GetOutputSpaceDimension();
const SizeValueType sampleContainerSize = m_SampleContainer->Size();
const ThreadIdType numberOfThreads = m_Threader->GetNumberOfWorkUnits();
const unsigned int outdim = m_Transform->GetOutputSpaceDimension();

/** Get a handle to the scales vector */
const ScalesType & scales = this->GetScales();
Expand All @@ -328,7 +328,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ThreadedCompute(Thread
const auto pos_end = std::min<size_t>(nrOfSamplesPerThreads * (threadId + 1), sampleContainerSize);

/** Variables for nonzerojacobian indices and the Jacobian. */
const SizeValueType sizejacind = this->m_Transform->GetNumberOfNonZeroJacobianIndices();
const SizeValueType sizejacind = m_Transform->GetNumberOfNonZeroJacobianIndices();
JacobianType jacj(outdim, sizejacind, 0.0);
NonZeroJacobianIndicesType jacind(sizejacind);
jacind[0] = 0;
Expand All @@ -349,7 +349,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ThreadedCompute(Thread
unsigned long numberOfPixelsCounted = 0;

/** Create iterator over the sample container. */
const auto beginOfSampleContainer = this->m_SampleContainer->cbegin();
const auto beginOfSampleContainer = m_SampleContainer->cbegin();
const auto threader_fbegin = beginOfSampleContainer + pos_begin;
const auto threader_fend = beginOfSampleContainer + pos_end;

Expand All @@ -358,7 +358,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ThreadedCompute(Thread
{
/** Read fixed coordinates and get Jacobian. */
const FixedImagePointType & point = threader_fiter->m_ImageCoordinates;
this->m_Transform->GetJacobian(point, jacj, jacind);
m_Transform->GetJacobian(point, jacj, jacind);

/** Apply scales, if necessary. */
if (this->GetUseScales())
Expand Down Expand Up @@ -387,7 +387,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ThreadedCompute(Thread
for (unsigned int j = 0; j < sizejacind; ++j)
{
int pj = jacind[j];
temp += jacj(i, j) * this->m_ExactGradient(pj);
temp += jacj(i, j) * m_ExactGradient(pj);
}
Jgg(i) = temp;
}
Expand Down Expand Up @@ -421,22 +421,22 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::AfterThreadedCompute(d
maxJJ = 0.0;
double displacement = 0.0;
double displacementSquared = 0.0;
this->m_NumberOfPixelsCounted = 0.0;
m_NumberOfPixelsCounted = 0.0;

/** Accumulate thread results. */
for (const auto & computePerThreadStruct : m_ComputePerThreadVariables)
{
maxJJ = std::max(maxJJ, computePerThreadStruct.st_MaxJJ);
displacement += computePerThreadStruct.st_Displacement;
displacementSquared += computePerThreadStruct.st_DisplacementSquared;
this->m_NumberOfPixelsCounted += computePerThreadStruct.st_NumberOfPixelsCounted;
m_NumberOfPixelsCounted += computePerThreadStruct.st_NumberOfPixelsCounted;
}
// Reset all variables for the next resolution.
std::fill_n(m_ComputePerThreadVariables.begin(), m_ComputePerThreadVariables.size(), AlignedComputePerThreadStruct());

/** Compute the sigma of the distribution of the displacements. */
const double meanDisplacement = displacement / this->m_NumberOfPixelsCounted;
const double sigma = displacementSquared / this->m_NumberOfPixelsCounted - vnl_math::sqr(meanDisplacement);
const double meanDisplacement = displacement / m_NumberOfPixelsCounted;
const double sigma = displacementSquared / m_NumberOfPixelsCounted - vnl_math::sqr(meanDisplacement);

jacg = meanDisplacement + 2.0 * std::sqrt(sigma);

Expand Down Expand Up @@ -468,24 +468,24 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeUsingSearchDire
const SizeValueType nrofsamples = sampleContainer->Size();

/** Get the number of parameters. */
const auto numberOfParameters = static_cast<unsigned int>(this->m_Transform->GetNumberOfParameters());
const auto numberOfParameters = static_cast<unsigned int>(m_Transform->GetNumberOfParameters());

/** Get scales vector */
const ScalesType & scales = this->GetScales();
this->m_ScaledCostFunction->SetScales(scales);
Superclass::m_ScaledCostFunction->SetScales(scales);

/** Get the exact gradient. */
DerivativeType exactgradient(numberOfParameters);
exactgradient = mu;

/** Get transform and set current position. */
typename TransformType::Pointer transform = this->m_Transform;
const unsigned int outdim = this->m_Transform->GetOutputSpaceDimension();
typename TransformType::Pointer transform = m_Transform;
const unsigned int outdim = m_Transform->GetOutputSpaceDimension();

unsigned int samplenr = 0;

/** Variables for nonzerojacobian indices and the Jacobian. */
const SizeValueType sizejacind = this->m_Transform->GetNumberOfNonZeroJacobianIndices();
const SizeValueType sizejacind = m_Transform->GetNumberOfNonZeroJacobianIndices();
JacobianType jacj(outdim, sizejacind, 0.0);
NonZeroJacobianIndicesType jacind(sizejacind);
jacind[0] = 0;
Expand All @@ -509,7 +509,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::ComputeUsingSearchDire
{
/** Read fixed coordinates and get Jacobian. */
const FixedImagePointType & point = sample.m_ImageCoordinates;
this->m_Transform->GetJacobian(point, jacj, jacind);
m_Transform->GetJacobian(point, jacj, jacind);

/** Apply scales, if necessary. */
if (this->GetUseScales())
Expand Down Expand Up @@ -573,16 +573,16 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::SampleFixedImageForJac
/** Set up grid sampler. */
ImageGridSamplerPointer sampler = ImageGridSamplerType::New();
// ImageFullSamplerPointer sampler = ImageFullSamplerType::New();
sampler->SetInput(this->m_FixedImage);
sampler->SetInput(m_FixedImage);
sampler->SetInputImageRegion(this->GetFixedImageRegion());
sampler->SetMask(this->m_FixedImageMask);
sampler->SetMask(m_FixedImageMask);

/** Determine grid spacing of sampler such that the desired
* NumberOfJacobianMeasurements is achieved approximately.
* Note that the actually obtained number of samples may be lower, due to masks.
* This is taken into account at the end of this function.
*/
SizeValueType nrofsamples = this->m_NumberOfJacobianMeasurements;
SizeValueType nrofsamples = m_NumberOfJacobianMeasurements;
sampler->SetNumberOfSamples(nrofsamples);

/** Get samples and check the actually obtained number of samples. */
Expand All @@ -592,7 +592,7 @@ ComputeDisplacementDistribution<TFixedImage, TTransform>::SampleFixedImageForJac

if (nrofsamples == 0)
{
itkExceptionMacro("No valid voxels (0/" << this->m_NumberOfJacobianMeasurements
itkExceptionMacro("No valid voxels (0/" << m_NumberOfJacobianMeasurements
<< ") found to estimate the AdaptiveStochasticGradientDescent parameters.");
}
return sampleContainer;
Expand Down

0 comments on commit ce3f534

Please # to comment.