ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkKdTreeBasedKmeansEstimator.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkKdTreeBasedKmeansEstimator_h
19#define itkKdTreeBasedKmeansEstimator_h
20
21#include <vector>
22#include <unordered_map>
23
24#include "itkObject.h"
29
30namespace itk::Statistics
31{
70
71template <typename TKdTree>
72class ITK_TEMPLATE_EXPORT KdTreeBasedKmeansEstimator : public Object
73{
74public:
80
82 itkNewMacro(Self);
83
85 itkOverrideGetNameOfClassMacro(KdTreeBasedKmeansEstimator);
86
88 using KdTreeNodeType = typename TKdTree::KdTreeNodeType;
89 using MeasurementType = typename TKdTree::MeasurementType;
90 using MeasurementVectorType = typename TKdTree::MeasurementVectorType;
91 using InstanceIdentifier = typename TKdTree::InstanceIdentifier;
92 using SampleType = typename TKdTree::SampleType;
93 using CentroidType = typename KdTreeNodeType::CentroidType;
94
96 using MeasurementVectorSizeType = unsigned int;
97
101 using InternalParametersType = std::vector<ParameterType>;
103
107
109
112 using MembershipFunctionVectorType = std::vector<MembershipFunctionPointer>;
115
119 GetOutput() const;
120
123 itkSetMacro(Parameters, ParametersType);
124 itkGetConstMacro(Parameters, ParametersType);
126
129 itkSetMacro(MaximumIteration, int);
130 itkGetConstMacro(MaximumIteration, int);
132
135 itkSetMacro(CentroidPositionChangesThreshold, double);
136 itkGetConstMacro(CentroidPositionChangesThreshold, double);
137
139 void
140 SetKdTree(TKdTree * tree);
141
142 const TKdTree *
143 GetKdTree() const;
144
146 itkGetConstMacro(MeasurementVectorSize, MeasurementVectorSizeType);
147
148 itkGetConstMacro(CurrentIteration, int);
149 itkGetConstMacro(CentroidPositionChanges, double);
150
155 void
157
158 using ClusterLabelsType = std::unordered_map<InstanceIdentifier, unsigned int>;
159
160 itkSetMacro(UseClusterLabels, bool);
161 itkGetConstMacro(UseClusterLabels, bool);
162 itkBooleanMacro(UseClusterLabels);
163
164protected:
166 ~KdTreeBasedKmeansEstimator() override = default;
167
168 void
169 PrintSelf(std::ostream & os, Indent indent) const override;
170
171 void
172 FillClusterLabels(KdTreeNodeType * node, int closestIndex);
173
180 {
181 public:
182 CandidateVector() = default;
183
190
191 virtual ~CandidateVector() = default;
192
194 [[nodiscard]] int
195 Size() const
196 {
197 return static_cast<int>(m_Candidates.size());
198 }
199
202 void
204 {
206 m_Candidates.resize(centroids.size());
207 for (unsigned int i = 0; i < centroids.size(); ++i)
208 {
209 Candidate candidate;
210 candidate.Centroid = centroids[i];
212 candidate.WeightedCentroid.Fill(0.0);
213 candidate.Size = 0;
214 m_Candidates[i] = candidate;
215 }
216 }
217
219 void
221 {
222 centroids.resize(this->Size());
223 for (unsigned int i = 0; i < static_cast<unsigned int>(this->Size()); ++i)
224 {
225 centroids[i] = m_Candidates[i].Centroid;
226 }
227 }
228
231 void
233 {
234 for (unsigned int i = 0; i < static_cast<unsigned int>(this->Size()); ++i)
235 {
236 if (m_Candidates[i].Size > 0)
237 {
238 for (unsigned int j = 0; j < m_MeasurementVectorSize; ++j)
239 {
240 m_Candidates[i].Centroid[j] =
241 m_Candidates[i].WeightedCentroid[j] / static_cast<double>(m_Candidates[i].Size);
242 }
243 }
244 }
245 }
246
248 Candidate &
249 operator[](int index)
250 {
251 return m_Candidates[index];
252 }
253
254 private:
256 std::vector<Candidate> m_Candidates;
257
260 }; // end of class
261
267 double
269
272 int
273 GetClosestCandidate(ParameterType & measurements, std::vector<int> & validIndexes);
274
276 bool
278 ParameterType & pointB,
279 MeasurementVectorType & lowerBound,
280 MeasurementVectorType & upperBound);
281
284 void
286 std::vector<int> validIndexes,
287 MeasurementVectorType & lowerBound,
288 MeasurementVectorType & upperBound);
289
291 void
293
295 void
297
299 void
301
303 void
305
306 void
308
309private:
312
315
318
322
324 typename TKdTree::Pointer m_KdTree{};
325
328
331
332 CandidateVector m_CandidateVector{};
333
335
336 bool m_UseClusterLabels{ false };
341}; // end of class
342} // namespace itk::Statistics
343
344#ifndef ITK_MANUAL_INSTANTIATION
345# include "itkKdTreeBasedKmeansEstimator.hxx"
346#endif
347
348#endif
Array class with size defined at construction time.
Definition itkArray.h:48
Control indentation during Print() invocation.
Definition itkIndent.h:50
static void SetLength(T &m, const unsigned int s)
static unsigned int GetLength(const T &)
Decorates any "simple" data type (data types without smart pointers) with a DataObject API.
Implements transparent reference counting.
DistanceToCentroidMembershipFunction models class membership using a distance metric.
typename TKdTree::InstanceIdentifier InstanceIdentifier
void CopyParameters(InternalParametersType &source, ParametersType &target)
EuclideanDistanceMetric< ParameterType >::Pointer m_DistanceMetric
typename MembershipFunctionType::ConstPointer MembershipFunctionPointer
SimpleDataObjectDecorator< MembershipFunctionVectorType > MembershipFunctionVectorObjectType
int GetClosestCandidate(ParameterType &measurements, std::vector< int > &validIndexes)
void CopyParameters(InternalParametersType &source, InternalParametersType &target)
const MembershipFunctionVectorObjectType * GetOutput() const
std::vector< MembershipFunctionPointer > MembershipFunctionVectorType
void PrintPoint(ParameterType &point)
typename MembershipFunctionVectorObjectType::Pointer MembershipFunctionVectorObjectPointer
bool IsFarther(ParameterType &pointA, ParameterType &pointB, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
void FillClusterLabels(KdTreeNodeType *node, int closestIndex)
void CopyParameters(ParametersType &source, InternalParametersType &target)
void Filter(KdTreeNodeType *node, std::vector< int > validIndexes, MeasurementVectorType &lowerBound, MeasurementVectorType &upperBound)
std::unordered_map< InstanceIdentifier, unsigned int > ClusterLabelsType
typename DistanceToCentroidMembershipFunctionType::Pointer DistanceToCentroidMembershipFunctionPointer
void PrintSelf(std::ostream &os, Indent indent) const override
double GetSumOfSquaredPositionChanges(InternalParametersType &previous, InternalParametersType &current)
typename KdTreeNodeType::CentroidType CentroidType
MembershipFunctionBase< MeasurementVectorType > MembershipFunctionType
typename TKdTree::MeasurementVectorType MeasurementVectorType
DistanceToCentroidMembershipFunction< MeasurementVectorType > DistanceToCentroidMembershipFunctionType
void GetPoint(ParameterType &point, MeasurementVectorType measurements)
MembershipFunctionBase defines common interfaces for membership functions.