ITK  5.0.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 "itksys/hash_map.hxx"
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 = itksys::hash_map< InstanceIdentifier, ClassLabelType >;
91 
97 
99  itkSetConstObjectMacro(Sample, SampleType);
100  itkGetConstObjectMacro(Sample, SampleType);
102 
104  void SetNumberOfClasses(unsigned int numberOfClasses);
105 
107  itkGetConstMacro(NumberOfClasses, unsigned int);
108 
113  void AddInstance(const ClassLabelType & classLabel, const InstanceIdentifier & id);
114 
117  unsigned int GetClassLabel(const InstanceIdentifier & id) const;
118 
121  const ClassSampleType * GetClassSample(const ClassLabelType & classLabel) const;
122 
125  const ClassLabelHolderType GetClassLabelHolder() const;
126 
129  const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier & id) const;
130 
133  MeasurementType GetMeasurement(const InstanceIdentifier & id,
134  const unsigned int & dimension);
135 
137  AbsoluteFrequencyType GetFrequency(const InstanceIdentifier & id) const;
138 
140  TotalAbsoluteFrequencyType GetTotalFrequency() const;
141 
143  void Graft(const DataObject *thatObject) override;
144 
145 // void PrintSelf(std::ostream& os, Indent indent) const;
146 
148  {
149  friend class MembershipSample;
150 
151 public:
152 
153  ConstIterator(const Self *sample)
154  {
155  *this = sample->Begin();
156  }
157 
159  {
160  m_Sample = iter.m_Sample;
161  m_MembershipSample = iter.m_MembershipSample;
162  m_InstanceIdentifier = iter.m_InstanceIdentifier;
163  }
164 
166  {
167  m_Sample = iter.m_Sample;
168  m_MembershipSample = iter.m_MembershipSample;
169  m_InstanceIdentifier = iter.m_InstanceIdentifier;
170  return *this;
171  }
172 
173  bool operator!=(const ConstIterator & it)
174  {
175  return ( m_InstanceIdentifier != it.m_InstanceIdentifier );
176  }
177 
178  bool operator==(const ConstIterator & it)
179  {
180  return ( m_InstanceIdentifier == it.m_InstanceIdentifier );
181  }
182 
184  {
185  ++m_InstanceIdentifier;
186  return *this;
187  }
188 
190  {
191  return m_Sample->GetFrequency(m_InstanceIdentifier);
192  }
193 
195  {
196  return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
197  }
198 
200  {
201  return m_InstanceIdentifier;
202  }
203 
204  unsigned int GetClassLabel() const
205  {
206  return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
207  }
208 
209 protected:
210  // Purposely not implemented
211  ConstIterator();
212 
213  // Only to be called from the MembershipSample
215  const Self *memberSample, InstanceIdentifier iid):
216  m_Sample( memberSample->GetSample() ), m_MembershipSample(memberSample), m_InstanceIdentifier(iid)
217  {}
218 
219  //typename SampleType::ConstIterator m_Iter;
220  const TSample * m_Sample;
223  };
224 
225  class Iterator:public ConstIterator
226  {
227  friend class MembershipSample;
228 
229 public:
230 
231  Iterator(Self *sample):ConstIterator(sample)
232  {}
233 
234  Iterator(const Iterator & iter):ConstIterator(iter)
235  {}
236 
237  Iterator & operator=(const Iterator & iter)
238  {
239  this->ConstIterator::operator=(iter);
240  return *this;
241  }
242 
243 protected:
244  // To ensure const-correctness these method must not be in the public API.
245  // The are purposly not implemented, since they should never be called.
246  Iterator();
247  Iterator(const Self *sample);
248  Iterator(const ConstIterator & it);
249  ConstIterator & operator=(const ConstIterator & it);
250 
251  // Only to be called from the MembershipSample
252  Iterator(Self *memberSample,
253  InstanceIdentifier iid):
254  ConstIterator(memberSample, iid)
255  {}
256 
257 private:
258  };
259 
263  {
264  Iterator iter(this, 0);
265 
266  return iter;
267  }
268 
272  {
273  Iterator iter( this, m_Sample->Size() );
274 
275  return iter;
276  }
277 
279  {
280  ConstIterator iter(this, 0);
281 
282  return iter;
283  }
284 
286  {
287  ConstIterator iter( this, m_Sample->Size() );
288 
289  return iter;
290  }
291 
292 protected:
294  ~MembershipSample() override = default;
295  void PrintSelf(std::ostream & os, Indent indent) const override;
296 
297 private:
300  int GetInternalClassLabel(const ClassLabelType classLabel) const;
301 
304  std::vector< ClassSamplePointer > m_ClassSamples;
306  unsigned int m_NumberOfClasses;
307 }; // end of class
308 } // end of namespace Statistics
309 } // end of namespace itk
310 
311 #ifndef ITK_MANUAL_INSTANTIATION
312 #include "itkMembershipSample.hxx"
313 #endif
314 
315 #endif
typename ClassSampleType::ConstPointer ClassSampleConstPointer
typename SampleType::ConstPointer SampleConstPointer
itksys::hash_map< InstanceIdentifier, ClassLabelType > ClassLabelHolderType
typename SampleType::InstanceIdentifier InstanceIdentifier
std::vector< ClassSamplePointer > m_ClassSamples
typename SampleType::AbsoluteFrequencyType AbsoluteFrequencyType
typename SampleType::MeasurementVectorType MeasurementVectorType
This class stores a subset of instance identifiers from another sample object. You can create a subsa...
Definition: itkSubsample.h:41
Container for storing the instance-identifiers of other sample with their associated class labels...
class ITK_FORWARD_EXPORT DataObject
Definition: itkDataObject.h:41
UniqueClassLabelsType m_UniqueClassLabels
Iterator & operator=(const Iterator &iter)
ConstIterator & operator=(const ConstIterator &iter)
typename SampleType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType
std::vector< ClassLabelType > UniqueClassLabelsType
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
Iterator(Self *memberSample, InstanceIdentifier iid)
typename SampleType::MeasurementType MeasurementType
ConstIterator(const Self *memberSample, InstanceIdentifier iid)
typename ClassSampleType::Pointer ClassSamplePointer
A collection of measurements for statistical analysis.
Definition: itkSample.h:61
Control indentation during Print() invocation.
Definition: itkIndent.h:49
const MeasurementVectorType & GetMeasurementVector() const
Base class for most ITK classes.
Definition: itkObject.h:60
Base class for all data objects in ITK.