ITK  4.3.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< class TSample >
56 class ITK_EXPORT MembershipSample:public DataObject
57 {
58 public:
64 
66  itkTypeMacro(MembershipSample, DataObject);
67  itkNewMacro(Self);
69 
72  typedef TSample SampleType;
73  typedef typename SampleType::MeasurementVectorType MeasurementVectorType;
74  typedef typename SampleType::MeasurementType MeasurementType;
75  typedef typename SampleType::InstanceIdentifier InstanceIdentifier;
76  typedef typename SampleType::ConstPointer SampleConstPointer;
77 
78  typedef typename SampleType::AbsoluteFrequencyType AbsoluteFrequencyType;
79  typedef typename SampleType::TotalAbsoluteFrequencyType TotalAbsoluteFrequencyType;
80 
84  typedef std::vector< ClassLabelType > UniqueClassLabelsType;
85 
88  typedef itksys::hash_map< InstanceIdentifier, ClassLabelType > ClassLabelHolderType;
89 
95 
97  itkSetConstObjectMacro(Sample, SampleType);
98  itkGetConstObjectMacro(Sample, SampleType);
100 
102  void SetNumberOfClasses(unsigned int numberOfClasses);
103 
105  itkGetConstMacro(NumberOfClasses, unsigned int);
106 
111  void AddInstance(const ClassLabelType & classLabel, const InstanceIdentifier & id);
112 
115  unsigned int GetClassLabel(const InstanceIdentifier & id) const;
116 
119  const ClassSampleType * GetClassSample(const ClassLabelType & classLabel) const;
120 
123  const ClassLabelHolderType GetClassLabelHolder() const;
124 
127  const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier & id) const;
128 
131  MeasurementType GetMeasurement(const InstanceIdentifier & id,
132  const unsigned int & dimension);
133 
135  AbsoluteFrequencyType GetFrequency(const InstanceIdentifier & id) const;
136 
138  TotalAbsoluteFrequencyType GetTotalFrequency() const;
139 
141  virtual void Graft(const DataObject *thatObject);
142 
143 // void PrintSelf(std::ostream& os, Indent indent) const;
144 
146  {
147  friend class MembershipSample;
148 
149 public:
150 
151  ConstIterator(const Self *sample)
152  {
153  *this = sample->Begin();
154  }
155 
157  {
158  m_Sample = iter.m_Sample;
159  m_MembershipSample = iter.m_MembershipSample;
160  m_InstanceIdentifier = iter.m_InstanceIdentifier;
161  }
162 
163  ConstIterator & operator=(const ConstIterator & iter)
164  {
165  m_Sample = iter.m_Sample;
166  m_MembershipSample = iter.m_MembershipSample;
167  m_InstanceIdentifier = iter.m_InstanceIdentifier;
168  return *this;
169  }
170 
171  bool operator!=(const ConstIterator & it)
172  {
173  return ( m_InstanceIdentifier != it.m_InstanceIdentifier );
174  }
175 
176  bool operator==(const ConstIterator & it)
177  {
178  return ( m_InstanceIdentifier == it.m_InstanceIdentifier );
179  }
180 
181  ConstIterator & operator++()
182  {
183  ++m_InstanceIdentifier;
184  return *this;
185  }
186 
187  AbsoluteFrequencyType GetFrequency() const
188  {
189  return m_Sample->GetFrequency(m_InstanceIdentifier);
190  }
191 
192  const MeasurementVectorType & GetMeasurementVector() const
193  {
194  return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
195  }
196 
197  InstanceIdentifier GetInstanceIdentifier() const
198  {
199  return m_InstanceIdentifier;
200  }
201 
202  unsigned int GetClassLabel() const
203  {
204  return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
205  }
206 
207 protected:
208  // Purposely not implemented
209  ConstIterator();
210 
211  // Only to be called from the MembershipSample
213  const Self *memberSample, InstanceIdentifier iid):
214  m_Sample( memberSample->GetSample() ), m_MembershipSample(memberSample), m_InstanceIdentifier(iid)
215  {}
216 
217  //typename SampleType::ConstIterator m_Iter;
218  const TSample * m_Sample;
221  };
222 
223  class Iterator:public ConstIterator
224  {
225  friend class MembershipSample;
226 
227 public:
228 
229  Iterator(Self *sample):ConstIterator(sample)
230  {}
231 
232  Iterator(const Iterator & iter):ConstIterator(iter)
233  {}
234 
235  Iterator & operator=(const Iterator & iter)
236  {
237  this->ConstIterator::operator=(iter);
238  return *this;
239  }
240 
241 protected:
242  // To ensure const-correctness these method must not be in the public API.
243  // The are purposly not implemented, since they should never be called.
244  Iterator();
245  Iterator(const Self *sample);
246  Iterator(const ConstIterator & it);
247  ConstIterator & operator=(const ConstIterator & it);
248 
249  // Only to be called from the MembershipSample
250  Iterator(Self *memberSample,
251  InstanceIdentifier iid):
252  ConstIterator(memberSample, iid)
253  {}
254 
255 private:
256  };
257 
260  Iterator Begin()
261  {
262  Iterator iter(this, 0);
263 
264  return iter;
265  }
266 
269  Iterator End()
270  {
271  Iterator iter( this, m_Sample->Size() );
272 
273  return iter;
274  }
275 
276  ConstIterator Begin() const
277  {
278  ConstIterator iter(this, 0);
279 
280  return iter;
281  }
282 
283  ConstIterator End() const
284  {
285  ConstIterator iter( this, m_Sample->Size() );
286 
287  return iter;
288  }
289 
290 protected:
292  virtual ~MembershipSample() {}
293  void PrintSelf(std::ostream & os, Indent indent) const;
294 
295 private:
296  MembershipSample(const Self &); //purposely not implemented
297  void operator=(const Self &); //purposely not implemented
298 
301  int GetInternalClassLabel(const ClassLabelType classLabel) const;
302 
305  std::vector< ClassSamplePointer > m_ClassSamples;
307  unsigned int m_NumberOfClasses;
308 }; // end of class
309 } // end of namespace Statistics
310 } // end of namespace itk
311 
312 #ifndef ITK_MANUAL_INSTANTIATION
313 #include "itkMembershipSample.hxx"
314 #endif
315 
316 #endif
317