ITK  5.2.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  bool
187  operator==(const ConstIterator & it) const
188  {
189  return (m_InstanceIdentifier == it.m_InstanceIdentifier);
190  }
191 
192  ConstIterator &
194  {
195  ++m_InstanceIdentifier;
196  return *this;
197  }
198 
200  GetFrequency() const
201  {
202  return m_Sample->GetFrequency(m_InstanceIdentifier);
203  }
204 
205  const MeasurementVectorType &
207  {
208  return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
209  }
210 
213  {
214  return m_InstanceIdentifier;
215  }
216 
217  unsigned int
219  {
220  return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
221  }
222 
223  protected:
224  // Purposely not implemented
225  ConstIterator();
226 
227  // Only to be called from the MembershipSample
228  ConstIterator(const Self * memberSample, InstanceIdentifier iid)
229  : m_Sample(memberSample->GetSample())
230  , m_MembershipSample(memberSample)
231  , m_InstanceIdentifier(iid)
232  {}
233 
234  // typename SampleType::ConstIterator m_Iter;
235  const TSample * m_Sample;
238  };
239 
240  class Iterator : public ConstIterator
241  {
242  friend class MembershipSample;
243 
244  public:
245  Iterator(Self * sample)
246  : ConstIterator(sample)
247  {}
248 
249  Iterator(const Iterator & iter)
250  : ConstIterator(iter)
251  {}
252 
253  Iterator &
254  operator=(const Iterator & iter)
255  {
256  this->ConstIterator::operator=(iter);
257  return *this;
258  }
259 
260  protected:
261  // To ensure const-correctness these method must not be in the public API.
262  // The are purposly not implemented, since they should never be called.
263  Iterator();
264  Iterator(const Self * sample);
265  Iterator(const ConstIterator & it);
266  ConstIterator &
267  operator=(const ConstIterator & it);
268 
269  // Only to be called from the MembershipSample
270  Iterator(Self * memberSample, InstanceIdentifier iid)
271  : ConstIterator(memberSample, iid)
272  {}
273 
274  private:
275  };
276 
279  Iterator
281  {
282  Iterator iter(this, 0);
283 
284  return iter;
285  }
286 
289  Iterator
290  End()
291  {
292  Iterator iter(this, m_Sample->Size());
293 
294  return iter;
295  }
296 
297  ConstIterator
298  Begin() const
299  {
300  ConstIterator iter(this, 0);
301 
302  return iter;
303  }
304 
305  ConstIterator
306  End() const
307  {
308  ConstIterator iter(this, m_Sample->Size());
309 
310  return iter;
311  }
312 
313 protected:
315  ~MembershipSample() override = default;
316  void
317  PrintSelf(std::ostream & os, Indent indent) const override;
318 
319 private:
322  int
323  GetInternalClassLabel(const ClassLabelType classLabel) const;
324 
327  std::vector<ClassSamplePointer> m_ClassSamples;
329  unsigned int m_NumberOfClasses;
330 }; // end of class
331 } // end of namespace Statistics
332 } // end of namespace itk
333 
334 #ifndef ITK_MANUAL_INSTANTIATION
335 # include "itkMembershipSample.hxx"
336 #endif
337 
338 #endif
itk::Statistics::MembershipSample::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: itkMembershipSample.h:187
itk::Statistics::MembershipSample::SampleConstPointer
typename SampleType::ConstPointer SampleConstPointer
Definition: itkMembershipSample.h:79
itk::Statistics::MembershipSample::ConstIterator::GetMeasurementVector
const MeasurementVectorType & GetMeasurementVector() const
Definition: itkMembershipSample.h:206
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:200
itk::Statistics::MembershipSample::m_NumberOfClasses
unsigned int m_NumberOfClasses
Definition: itkMembershipSample.h:329
itk::Statistics::MembershipSample::MeasurementType
typename SampleType::MeasurementType MeasurementType
Definition: itkMembershipSample.h:77
itk::Statistics::MembershipSample::ConstIterator::m_InstanceIdentifier
InstanceIdentifier m_InstanceIdentifier
Definition: itkMembershipSample.h:237
itk::Statistics::MembershipSample::Iterator
Definition: itkMembershipSample.h:240
itk::Statistics::MembershipSample::m_Sample
SampleConstPointer m_Sample
Definition: itkMembershipSample.h:328
itk::Statistics::MembershipSample::ConstIterator::ConstIterator
ConstIterator(const Self *memberSample, InstanceIdentifier iid)
Definition: itkMembershipSample.h:228
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:326
itk::Statistics::MembershipSample::Begin
ConstIterator Begin() const
Definition: itkMembershipSample.h:298
itk::Statistics::MembershipSample::Iterator::Iterator
Iterator(Self *memberSample, InstanceIdentifier iid)
Definition: itkMembershipSample.h:270
itk::Statistics::MembershipSample::Iterator::Iterator
Iterator(const Iterator &iter)
Definition: itkMembershipSample.h:249
itk::Statistics::MembershipSample::m_ClassSamples
std::vector< ClassSamplePointer > m_ClassSamples
Definition: itkMembershipSample.h:327
itk::Statistics::MembershipSample::ConstIterator::operator++
ConstIterator & operator++()
Definition: itkMembershipSample.h:193
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:236
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:306
itk::Statistics::MembershipSample::End
Iterator End()
Definition: itkMembershipSample.h:290
itk::Statistics::MembershipSample::UniqueClassLabelsType
std::vector< ClassLabelType > UniqueClassLabelsType
Definition: itkMembershipSample.h:87
itk::Statistics::MembershipSample::ConstIterator::GetInstanceIdentifier
InstanceIdentifier GetInstanceIdentifier() const
Definition: itkMembershipSample.h:212
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:325
itk::Statistics::MembershipSample::Begin
Iterator Begin()
Definition: itkMembershipSample.h:280
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:254
itk::Statistics::MembershipSample::ConstIterator::m_Sample
const TSample * m_Sample
Definition: itkMembershipSample.h:235
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Statistics::MembershipSample::ConstIterator::operator!=
bool operator!=(const ConstIterator &it) const
Definition: itkMembershipSample.h:181
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:218
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:245
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