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