File: itkExpectationMaximizationMixtureModelEstimator.h

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (249 lines) | stat: -rw-r--r-- 8,977 bytes parent folder | download
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*=========================================================================
 *
 *  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 itkExpectationMaximizationMixtureModelEstimator_h
#define itkExpectationMaximizationMixtureModelEstimator_h

#include "ITKStatisticsExport.h"
#include "itkMixtureModelComponentBase.h"
#include "itkGaussianMembershipFunction.h"
#include "itkSimpleDataObjectDecorator.h"

namespace itk
{
namespace Statistics
{
/** \class ExpectationMaximizationMixtureModelEstimatorEnums
 * \brief Contains all enum classes used by ExpectationMaximizationMixtureModelEstimator class.
 * \ingroup ITKStatistics
 */
class ExpectationMaximizationMixtureModelEstimatorEnums
{
public:
  /** \class TERMINATION_CODE
   * \ingroup ITKStatistics
   * Termination status after running optimization */
  enum class TERMINATION_CODE : uint8_t
  {
    CONVERGED = 0,
    NOT_CONVERGED = 1
  };
};
// Define how to print enumeration
extern ITKStatistics_EXPORT std::ostream &
                            operator<<(std::ostream & out, const ExpectationMaximizationMixtureModelEstimatorEnums::TERMINATION_CODE value);

/**
 * \class ExpectationMaximizationMixtureModelEstimator
 *  \brief This class generates the parameter estimates for a mixture
 *  model using expectation maximization strategy.
 *
 * The first template argument is the type of the target sample
 * data. This estimator expects one or more mixture model component
 * objects of the classes derived from the
 * MixtureModelComponentBase. The actual component (or module)
 * parameters are updated by each component. Users can think this
 * class as a strategy or a integration point for the EM
 * procedure. The initial proportion (SetInitialProportions), the
 * input sample (SetSample), the mixture model components
 * (AddComponent), and the maximum iteration (SetMaximumIteration) are
 * required. The EM procedure terminates when the current iteration
 * reaches the maximum iteration or the model parameters converge.
 *
 * <b>Recent API changes:</b>
 * The static const macro to get the length of a measurement vector,
 * \c MeasurementVectorSize  has been removed to allow the length of a measurement
 * vector to be specified at run time. It is now obtained at run time from the
 * sample set as input. Please use the function
 * GetMeasurementVectorSize() to get the length.
 *
 * \sa MixtureModelComponentBase, GaussianMixtureModelComponent
 * \ingroup ITKStatistics
 *
 * \sphinx
 * \sphinxexample{Numerics/Statistics/2DGaussianMixtureModelExpectMax,2D Gaussian Mixture Model Expectation Maximum}
 * \sphinxexample{Numerics/Statistics/DistributionOfPixelsUsingGMM,Distribution Of Pixels Using GMM EM}
 * \sphinxexample{Numerics/Statistics/DistributeSamplingUsingGMM,Distribute Sampling Using GMM EM}
 * \endsphinx
 */

template <typename TSample>
class ITK_TEMPLATE_EXPORT ExpectationMaximizationMixtureModelEstimator : public Object
{
public:
  /** Standard class type alias */
  using Self = ExpectationMaximizationMixtureModelEstimator;
  using Superclass = Object;
  using Pointer = SmartPointer<Self>;
  using ConstPointer = SmartPointer<const Self>;

  /** \see LightObject::GetNameOfClass() */
  itkOverrideGetNameOfClassMacro(ExpectationMaximizationMixtureModelEstimator);
  itkNewMacro(Self);

  /** TSample template argument related type alias */
  using SampleType = TSample;
  using MeasurementType = typename TSample::MeasurementType;
  using MeasurementVectorType = typename TSample::MeasurementVectorType;

  /** Typedef required to generate dataobject decorated output that can
   * be plugged into SampleClassifierFilter */
  using GaussianMembershipFunctionType = GaussianMembershipFunction<MeasurementVectorType>;

  using GaussianMembershipFunctionPointer = typename GaussianMembershipFunctionType::Pointer;

  using MembershipFunctionType = MembershipFunctionBase<MeasurementVectorType>;
  using MembershipFunctionPointer = typename MembershipFunctionType::ConstPointer;
  using MembershipFunctionVectorType = std::vector<MembershipFunctionPointer>;
  using MembershipFunctionVectorObjectType = SimpleDataObjectDecorator<MembershipFunctionVectorType>;
  using MembershipFunctionVectorObjectPointer = typename MembershipFunctionVectorObjectType::Pointer;

  /** Type of the mixture model component base class */
  using ComponentType = MixtureModelComponentBase<TSample>;

  /** Type of the component pointer storage */
  using ComponentVectorType = std::vector<ComponentType *>;

  /** Type of the membership function base class */
  using ComponentMembershipFunctionType = MembershipFunctionBase<MeasurementVectorType>;

  /** Type of the array of the proportion values */
  using ProportionVectorType = Array<double>;

  /** Sets the target data that will be classified by this */
  void
  SetSample(const TSample * sample);

  /** Returns the target data */
  const TSample *
  GetSample() const;

  /** Set/Gets the initial proportion values. The size of proportion
   * vector should be same as the number of component (or classes) */
  void
  SetInitialProportions(ProportionVectorType & proportions);

  const ProportionVectorType &
  GetInitialProportions() const;

  /** Gets the result proportion values */
  const ProportionVectorType &
  GetProportions() const;

  /** type alias for decorated array of proportion */
  using MembershipFunctionsWeightsArrayObjectType = SimpleDataObjectDecorator<ProportionVectorType>;
  using MembershipFunctionsWeightsArrayPointer = typename MembershipFunctionsWeightsArrayObjectType::Pointer;

  /** Get method for data decorated Membership functions weights array */
  const MembershipFunctionsWeightsArrayObjectType *
  GetMembershipFunctionsWeightsArray() const;

  /** Set/Gets the maximum number of iterations. When the optimization
   * process reaches the maximum number of iterations, even if the
   * class parameters aren't converged, the optimization process
   * stops. */
  void
  SetMaximumIteration(int numberOfIterations);

  int
  GetMaximumIteration() const;

  /** Gets the current iteration. */
  int
  GetCurrentIteration()
  {
    return m_CurrentIteration;
  }

  /** Adds a new component (or class). */
  int
  AddComponent(ComponentType * component);

  /** Gets the total number of classes currently plugged in. */
  unsigned int
  GetNumberOfComponents() const;

  /** Runs the optimization process. */
  void
  Update();

  using TERMINATION_CODE_ENUM = ExpectationMaximizationMixtureModelEstimatorEnums::TERMINATION_CODE;
#if !defined(ITK_LEGACY_REMOVE)
  /**Exposes enums values for backwards compatibility*/
  static constexpr TERMINATION_CODE_ENUM CONVERGED = TERMINATION_CODE_ENUM::CONVERGED;
  static constexpr TERMINATION_CODE_ENUM NOT_CONVERGED = TERMINATION_CODE_ENUM::NOT_CONVERGED;
#endif

  /** Gets the termination status */
  TERMINATION_CODE_ENUM
  GetTerminationCode() const;

  /** Gets the membership function specified by componentIndex
  argument. */
  ComponentMembershipFunctionType *
  GetComponentMembershipFunction(int componentIndex) const;

  /** Output Membership function vector containing the membership functions with
   * the final optimized parameters */
  const MembershipFunctionVectorObjectType *
  GetOutput() const;

protected:
  ExpectationMaximizationMixtureModelEstimator();
  ~ExpectationMaximizationMixtureModelEstimator() override = default;
  void
  PrintSelf(std::ostream & os, Indent indent) const override;

  bool
  CalculateDensities();

  double
  CalculateExpectation() const;

  bool
  UpdateComponentParameters();

  bool
  UpdateProportions();

  /** Starts the estimation process */
  void
  GenerateData();

private:
  /** Target data sample pointer*/
  const TSample * m_Sample{};

  int m_MaxIteration{ 100 };
  int m_CurrentIteration{ 0 };

  TERMINATION_CODE_ENUM m_TerminationCode{ TERMINATION_CODE_ENUM::NOT_CONVERGED };
  ComponentVectorType   m_ComponentVector{};
  ProportionVectorType  m_InitialProportions{};
  ProportionVectorType  m_Proportions{};

  MembershipFunctionVectorObjectPointer  m_MembershipFunctionsObject{};
  MembershipFunctionsWeightsArrayPointer m_MembershipFunctionsWeightArrayObject{};
}; // end of class
} // end of namespace Statistics
} // end of namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#  include "itkExpectationMaximizationMixtureModelEstimator.hxx"
#endif

#endif