ITK  5.3.0
Insight Toolkit
itkMembershipSample.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  * http://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 itkMembershipSample_h
19 #define itkMembershipSample_h
20 
21 #include <unordered_map>
22 #include "itkSubsample.h"
23 
24 namespace itk
25 {
26 namespace Statistics
27 {
56 template <typename TSample>
57 class ITK_TEMPLATE_EXPORT MembershipSample : public DataObject
58 {
59 public:
60  ITK_DISALLOW_COPY_AND_MOVE(MembershipSample);
61 
67 
69  itkTypeMacro(MembershipSample, DataObject);
70  itkNewMacro(Self);
72 
75  using SampleType = TSample;
76  using MeasurementVectorType = typename SampleType::MeasurementVectorType;
77  using MeasurementType = typename SampleType::MeasurementType;
78  using InstanceIdentifier = typename SampleType::InstanceIdentifier;
79  using SampleConstPointer = typename SampleType::ConstPointer;
80 
81  using AbsoluteFrequencyType = typename SampleType::AbsoluteFrequencyType;
82  using TotalAbsoluteFrequencyType = typename SampleType::TotalAbsoluteFrequencyType;
83 
87  using UniqueClassLabelsType = std::vector<ClassLabelType>;
88 
91  using ClassLabelHolderType = std::unordered_map<InstanceIdentifier, ClassLabelType>;
92 
98 
100  itkSetConstObjectMacro(Sample, SampleType);
101  itkGetConstObjectMacro(Sample, SampleType);
103 
105  void
106  SetNumberOfClasses(unsigned int numberOfClasses);
107 
109  itkGetConstMacro(NumberOfClasses, unsigned int);
110 
115  void
116  AddInstance(const ClassLabelType & classLabel, const InstanceIdentifier & id);
117 
120  unsigned int
121  GetClassLabel(const InstanceIdentifier & id) const;
122 
125  const ClassSampleType *
126  GetClassSample(const ClassLabelType & classLabel) const;
127 
131  GetClassLabelHolder() const;
132 
135  const MeasurementVectorType &
136  GetMeasurementVector(const InstanceIdentifier & id) const;
137 
141  GetMeasurement(const InstanceIdentifier & id, const unsigned int & dimension);
142 
145  GetFrequency(const InstanceIdentifier & id) const;
146 
149  GetTotalFrequency() const;
150 
152  void
153  Graft(const DataObject * thatObject) override;
154 
155  // void PrintSelf(std::ostream& os, Indent indent) const;
156 
158  {
159  friend class MembershipSample;
160 
161  public:
162  ConstIterator(const Self * sample) { *this = sample->Begin(); }
163 
165  {
166  m_Sample = iter.m_Sample;
167  m_MembershipSample = iter.m_MembershipSample;
168  m_InstanceIdentifier = iter.m_InstanceIdentifier;
169  }
170 
171  ConstIterator &
172  operator=(const ConstIterator & iter)
173  {
174  m_Sample = iter.m_Sample;
175  m_MembershipSample = iter.m_MembershipSample;
176  m_InstanceIdentifier = iter.m_InstanceIdentifier;
177  return *this;
178  }
179 
180  bool
181  operator==(const ConstIterator & it) const
182  {
183  return (m_InstanceIdentifier == it.m_InstanceIdentifier);
184  }
185 
186  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(ConstIterator);
187 
188  ConstIterator &
190  {
191  ++m_InstanceIdentifier;
192  return *this;
193  }
194 
196  GetFrequency() const
197  {
198  return m_Sample->GetFrequency(m_InstanceIdentifier);
199  }
200 
201  const MeasurementVectorType &
203  {
204  return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
205  }
206 
209  {
210  return m_InstanceIdentifier;
211  }
212 
213  unsigned int
215  {
216  return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
217  }
218 
219  protected:
220  // Purposely not implemented
221  ConstIterator();
222 
223  // Only to be called from the MembershipSample
224  ConstIterator(const Self * memberSample, InstanceIdentifier iid)
225  : m_Sample(memberSample->GetSample())
226  , m_MembershipSample(memberSample)
227  , m_InstanceIdentifier(iid)
228  {}
229 
230  // typename SampleType::ConstIterator m_Iter;
231  const TSample * m_Sample;
234  };
235 
236  class Iterator : public ConstIterator
237  {
238  friend class MembershipSample;
239 
240  public:
241  Iterator(Self * sample)
242  : ConstIterator(sample)
243  {}
244 
245  Iterator(const Iterator & iter)
246  : ConstIterator(iter)
247  {}
248 
249  Iterator &
250  operator=(const Iterator & iter)
251  {
252  this->ConstIterator::operator=(iter);
253  return *this;
254  }
255 
256  protected:
257  // To ensure const-correctness these method must not be in the public API.
258  // The are purposly not implemented, since they should never be called.
259  Iterator();
260  Iterator(const Self * sample);
261  Iterator(const ConstIterator & it);
262  ConstIterator &
263  operator=(const ConstIterator & it);
264 
265  // Only to be called from the MembershipSample
266  Iterator(Self * memberSample, InstanceIdentifier iid)
267  : ConstIterator(memberSample, iid)
268  {}
269 
270  private:
271  };
272 
275  Iterator
277  {
278  Iterator iter(this, 0);
279 
280  return iter;
281  }
282 
285  Iterator
286  End()
287  {
288  Iterator iter(this, m_Sample->Size());
289 
290  return iter;
291  }
292 
293  ConstIterator
294  Begin() const
295  {
296  ConstIterator iter(this, 0);
297 
298  return iter;
299  }
300 
301  ConstIterator
302  End() const
303  {
304  ConstIterator iter(this, m_Sample->Size());
305 
306  return iter;
307  }
308 
309 protected:
311  ~MembershipSample() override = default;
312  void
313  PrintSelf(std::ostream & os, Indent indent) const override;
314 
315 private:
318  int
319  GetInternalClassLabel(const ClassLabelType classLabel) const;
320 
323  std::vector<ClassSamplePointer> m_ClassSamples;
325  unsigned int m_NumberOfClasses;
326 }; // end of class
327 } // end of namespace Statistics
328 } // end of namespace itk
329 
330 #ifndef ITK_MANUAL_INSTANTIATION
331 # include "itkMembershipSample.hxx"
332 #endif
333 
334 #endif
itk::Statistics::MembershipSample::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkMembershipSample.h:181
itk::Statistics::MembershipSample::SampleConstPointer
typename SampleType::ConstPointer SampleConstPointer
Definition: itkMembershipSample.h:79
itk::Statistics::MembershipSample::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkMembershipSample.h:202
itk::Statistics::MembershipSample
Container for storing the instance-identifiers of other sample with their associated class labels.
Definition: itkMembershipSample.h:57
itk::Statistics::MembershipSample::MeasurementVectorType
typename SampleType::MeasurementVectorType MeasurementVectorType
Definition: itkMembershipSample.h:76
itkSubsample.h
itk::Statistics::MembershipSample::ConstIterator::GetFrequency
AbsoluteFrequencyType GetFrequency() const
Definition: itkMembershipSample.h:196
itk::Statistics::MembershipSample::m_NumberOfClasses
unsigned int m_NumberOfClasses
Definition: itkMembershipSample.h:325
itk::Statistics::MembershipSample::MeasurementType
typename SampleType::MeasurementType MeasurementType
Definition: itkMembershipSample.h:77
itk::Statistics::MembershipSample::ConstIterator::m_InstanceIdentifier
InstanceIdentifier m_InstanceIdentifier
Definition: itkMembershipSample.h:233
itk::Statistics::MembershipSample::Iterator
Definition: itkMembershipSample.h:236
itk::Statistics::MembershipSample::m_Sample
SampleConstPointer m_Sample
Definition: itkMembershipSample.h:324
itk::Statistics::MembershipSample::ConstIterator::ConstIterator
ConstIterator(const Self *memberSample, InstanceIdentifier iid)
Definition: itkMembershipSample.h:224
itk::Statistics::MembershipSample::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &iter)
Definition: itkMembershipSample.h:164
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::Statistics::MembershipSample::m_ClassLabelHolder
ClassLabelHolderType m_ClassLabelHolder
Definition: itkMembershipSample.h:322
itk::Statistics::MembershipSample::Begin
ConstIterator Begin() const
Definition: itkMembershipSample.h:294
itk::Statistics::MembershipSample::Iterator::Iterator
Iterator(Self *memberSample, InstanceIdentifier iid)
Definition: itkMembershipSample.h:266
itk::Statistics::MembershipSample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkMembershipSample.h:245
itk::Statistics::MembershipSample::m_ClassSamples
std::vector< ClassSamplePointer > m_ClassSamples
Definition: itkMembershipSample.h:323
itk::Statistics::MembershipSample::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkMembershipSample.h:189
itk::Statistics::MembershipSample::ClassSamplePointer
typename ClassSampleType::Pointer ClassSamplePointer
Definition: itkMembershipSample.h:96
itk::Statistics::MembershipSample::SampleType
TSample SampleType
Definition: itkMembershipSample.h:75
itk::Statistics::MembershipSample::ConstIterator
Definition: itkMembershipSample.h:157
itk::Statistics::MembershipSample::TotalAbsoluteFrequencyType
typename SampleType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
Definition: itkMembershipSample.h:82
itk::Statistics::MembershipSample::InstanceIdentifier
typename SampleType::InstanceIdentifier InstanceIdentifier
Definition: itkMembershipSample.h:78
itk::Statistics::MembershipSample::ConstIterator::m_MembershipSample
const MembershipSample * m_MembershipSample
Definition: itkMembershipSample.h:232
itk::Statistics::Subsample
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:42
itk::Statistics::MembershipSample::End
ConstIterator End() const
Definition: itkMembershipSample.h:302
itk::Statistics::MembershipSample::End
Iterator End()
Definition: itkMembershipSample.h:286
itk::Statistics::MembershipSample::UniqueClassLabelsType
std::vector< ClassLabelType > UniqueClassLabelsType
Definition: itkMembershipSample.h:87
itk::Statistics::MembershipSample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkMembershipSample.h:208
itk::DataObject
class ITK_FORWARD_EXPORT DataObject
Definition: itkDataObject.h:42
itk::Statistics::MembershipSample::ConstIterator::ConstIterator
ConstIterator(const Self *sample)
Definition: itkMembershipSample.h:162
itk::Statistics::MembershipSample::m_UniqueClassLabels
UniqueClassLabelsType m_UniqueClassLabels
Definition: itkMembershipSample.h:321
itk::Statistics::MembershipSample::Begin
Iterator Begin()
Definition: itkMembershipSample.h:276
itk::Statistics::MembershipSample::AbsoluteFrequencyType
typename SampleType::AbsoluteFrequencyType AbsoluteFrequencyType
Definition: itkMembershipSample.h:81
itk::Statistics::MembershipSample::Iterator::operator=
Iterator & operator=(const Iterator &iter)
Definition: itkMembershipSample.h:250
itk::Statistics::MembershipSample::ConstIterator::m_Sample
const TSample * m_Sample
Definition: itkMembershipSample.h:231
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Statistics::MembershipSample::ClassSampleConstPointer
typename ClassSampleType::ConstPointer ClassSampleConstPointer
Definition: itkMembershipSample.h:97
itk::Statistics::Sample
A collection of measurements for statistical analysis.
Definition: itkSample.h:62
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::Statistics::MembershipSample::ConstIterator::GetClassLabel
unsigned int GetClassLabel() const
Definition: itkMembershipSample.h:214
itk::Statistics::MembershipSample::ConstIterator::operator=
ConstIterator & operator=(const ConstIterator &iter)
Definition: itkMembershipSample.h:172
itk::Statistics::MembershipSample::Iterator::Iterator
Iterator(Self *sample)
Definition: itkMembershipSample.h:241
itk::IdentifierType
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
itk::Statistics::MembershipSample::ClassLabelType
IdentifierType ClassLabelType
Definition: itkMembershipSample.h:84
itk::DataObject
Base class for all data objects in ITK.
Definition: itkDataObject.h:293
itk::Statistics::MembershipSample::ClassLabelHolderType
std::unordered_map< InstanceIdentifier, ClassLabelType > ClassLabelHolderType
Definition: itkMembershipSample.h:91