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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkCumulativeGaussianOptimizer_h
#define itkCumulativeGaussianOptimizer_h
#include "itkMultipleValuedNonLinearOptimizer.h"
#include "itkCumulativeGaussianCostFunction.h"
#include "ITKOptimizersExport.h"
namespace itk
{
/** \class CumulativeGaussianOptimizer
* \brief This is an optimizer specific to estimating
* the parameters of Cumulative Gaussian sampled data.
*
* This optimizer will only work if the data array is
* sampled from a Cumulative Gaussian curve. It's more
* of a curve fitter than an optimizer, with the
* advantage of being fast and specific. It works by
* taking the derivative of the Cumulative Gaussian sample
* then repeatedly extending the tails of the Gaussian
* and recalculating the Gaussian parameters until
* the change in iterations is within tolerance or very small.
* The Gaussian is then integrated to reproduce the
* Cumulative Gaussian and the asymptotes are estimated
* by using least squares fit to estimate the constant
* from integration.
*
* \ingroup Numerics Optimizers
* \ingroup ITKOptimizers
*/
class ITKOptimizers_EXPORT CumulativeGaussianOptimizer : public MultipleValuedNonLinearOptimizer
{
public:
/** Standard type alias. */
using Self = CumulativeGaussianOptimizer;
using Superclass = MultipleValuedNonLinearOptimizer;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
/** Cost function type alias. NOTE: This optimizer is specific to fitting a
Cumulative Gaussian. */
using CostFunctionType = CumulativeGaussianCostFunction;
/** Data array type alias. */
using MeasureType = CostFunctionType::MeasureType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(CumulativeGaussianOptimizer);
/** Set and get macros. */
itkSetMacro(DifferenceTolerance, double);
itkGetMacro(DifferenceTolerance, double);
itkSetMacro(Verbose, bool);
itkGetMacro(Verbose, bool);
itkBooleanMacro(Verbose);
itkGetMacro(ComputedMean, double);
itkGetMacro(ComputedStandardDeviation, double);
itkGetMacro(UpperAsymptote, double);
itkGetMacro(LowerAsymptote, double);
itkGetMacro(FinalSampledArray, MeasureType *);
itkGetMacro(FitError, double);
void
SetDataArray(MeasureType * cumGaussianArray);
/** Start the optimizer. */
void
StartOptimization() override;
/** Print an array. */
void
PrintArray(MeasureType * array);
/** Report the reason for stopping. */
const std::string
GetStopConditionDescription() const override;
protected:
CumulativeGaussianOptimizer();
~CumulativeGaussianOptimizer() override;
void
PrintSelf(std::ostream & os, Indent indent) const override;
private:
/** When to stop the iteration for the Gaussian extension loop. */
double m_DifferenceTolerance{};
/** The final mean of the Cumulative Gaussian. */
double m_ComputedMean{};
/** The final standard deviation of the Cumulative Gaussian. */
double m_ComputedStandardDeviation{};
/** The final amplitude of the Gaussian. */
double m_ComputedAmplitude{};
/** The transition height (distance between upper and lower
* asymptotes) of the Cumulative Gaussian. */
double m_ComputedTransitionHeight{};
/** The final upper asymptote of the Cumulative Gaussian. */
double m_UpperAsymptote{};
/** The final lower asymptote of the Cumulative Gaussian. */
double m_LowerAsymptote{};
/** Offset for the mean calculation. */
double m_OffsetForMean{};
/** Flag to print iteration results. */
bool m_Verbose{};
/** Least squares fit error as a measure of goodness. */
double m_FitError{};
/** Array of values computed from the final parameters of the
* Cumulative Gaussian. */
MeasureType * m_FinalSampledArray{};
/** Original data array. */
MeasureType * m_CumulativeGaussianArray{};
/** Extend the tails of the Gaussian. */
MeasureType *
ExtendGaussian(MeasureType * originalArray, MeasureType * extendedArray, int startingPointForInsertion);
/** Recalculate the parameters of the extended Gaussian array. */
MeasureType *
RecalculateExtendedArrayFromGaussianParameters(MeasureType * originalArray,
MeasureType * extendedArray,
int startingPointForInsertion) const;
/** Calculates the squared difference error between each Gaussian
* iteration loop. */
double
FindAverageSumOfSquaredDifferences(MeasureType * array1, MeasureType * array2);
/** Given an array sampled from a Gaussian, compute the final parameters. */
void
FindParametersOfGaussian(MeasureType * sampledGaussianArray);
/** Measure the parameters of a Gaussian sampled array. */
void
MeasureGaussianParameters(MeasureType * array);
/** Print the header for output table. */
void
PrintComputedParameterHeader();
/** Print the computed parameters. */
void
PrintComputedParameters() const;
/** Find the constant of the integrated sample. */
double
VerticalBestShift(MeasureType * originalArray, MeasureType * newArray);
/** Describe the stop condition */
std::ostringstream m_StopConditionDescription{};
};
} // end namespace itk
#endif
|