ITK  4.2.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 public:
149 
150  ConstIterator(const Self *sample)
151  {
152  *this = sample->Begin();
153  }
154 
156  {
157  m_Sample = iter.m_Sample;
158  m_MembershipSample = iter.m_MembershipSample;
159  m_InstanceIdentifier = iter.m_InstanceIdentifier;
160  }
161 
162  ConstIterator & operator=(const ConstIterator & iter)
163  {
164  m_Sample = iter.m_Sample;
165  m_MembershipSample = iter.m_MembershipSample;
166  m_InstanceIdentifier = iter.m_InstanceIdentifier;
167  return *this;
168  }
169 
170  bool operator!=(const ConstIterator & it)
171  {
172  return ( m_InstanceIdentifier != it.m_InstanceIdentifier );
173  }
174 
175  bool operator==(const ConstIterator & it)
176  {
177  return ( m_InstanceIdentifier == it.m_InstanceIdentifier );
178  }
179 
180  ConstIterator & operator++()
181  {
182  ++m_InstanceIdentifier;
183  return *this;
184  }
185 
186  AbsoluteFrequencyType GetFrequency() const
187  {
188  return m_Sample->GetFrequency(m_InstanceIdentifier);
189  }
190 
191  const MeasurementVectorType & GetMeasurementVector() const
192  {
193  return m_Sample->GetMeasurementVector(m_InstanceIdentifier);
194  }
195 
196  InstanceIdentifier GetInstanceIdentifier() const
197  {
198  return m_InstanceIdentifier;
199  }
200 
201  unsigned int GetClassLabel() const
202  {
203  return m_MembershipSample->GetClassLabel(m_InstanceIdentifier);
204  }
205 
206 protected:
207  // Purposely not implemented
208  ConstIterator();
209 
210  // Only to be called from the MembershipSample
212  const Self *memberSample, InstanceIdentifier iid):
213  m_Sample( memberSample->GetSample() ), m_MembershipSample(memberSample), m_InstanceIdentifier(iid)
214  {}
215 
216  //typename SampleType::ConstIterator m_Iter;
217  const TSample * m_Sample;
220  };
221 
222  class Iterator:public ConstIterator
223  {
224  friend class MembershipSample;
225 public:
226 
227  Iterator(Self *sample):ConstIterator(sample)
228  {}
229 
230  Iterator(const Iterator & iter):ConstIterator(iter)
231  {}
232 
233  Iterator & operator=(const Iterator & iter)
234  {
235  this->ConstIterator::operator=(iter);
236  return *this;
237  }
238 
239 protected:
240  // To ensure const-correctness these method must not be in the public API.
241  // The are purposly not implemented, since they should never be called.
242  Iterator();
243  Iterator(const Self *sample);
244  Iterator(const ConstIterator & it);
245  ConstIterator & operator=(const ConstIterator & it);
246 
247  // Only to be called from the MembershipSample
248  Iterator(Self *memberSample,
249  InstanceIdentifier iid):
250  ConstIterator(memberSample, iid)
251  {}
252 private:
253  };
254 
257  Iterator Begin()
258  {
259  Iterator iter(this, 0);
260 
261  return iter;
262  }
263 
266  Iterator End()
267  {
268  Iterator iter( this, m_Sample->Size() );
269 
270  return iter;
271  }
272 
273  ConstIterator Begin() const
274  {
275  ConstIterator iter(this, 0);
276 
277  return iter;
278  }
279 
280  ConstIterator End() const
281  {
282  ConstIterator iter( this, m_Sample->Size() );
283 
284  return iter;
285  }
286 
287 protected:
289  virtual ~MembershipSample() {}
290  void PrintSelf(std::ostream & os, Indent indent) const;
291 
292 private:
293  MembershipSample(const Self &); //purposely not implemented
294  void operator=(const Self &); //purposely not implemented
295 
298  int GetInternalClassLabel(const ClassLabelType classLabel) const;
299 
302  std::vector< ClassSamplePointer > m_ClassSamples;
304  unsigned int m_NumberOfClasses;
305 }; // end of class
306 } // end of namespace Statistics
307 } // end of namespace itk
308 
309 #ifndef ITK_MANUAL_INSTANTIATION
310 #include "itkMembershipSample.hxx"
311 #endif
312 
313 #endif
314