00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkMembershipSample_h
00018 #define __itkMembershipSample_h
00019
00020 #include "itk_hash_map.h"
00021 #include "itkSample.h"
00022 #include "itkSubsample.h"
00023
00024 #include "itkExceptionObject.h"
00025
00026 namespace itk{
00027 namespace Statistics{
00028
00056 template< class TSample >
00057 class ITK_EXPORT MembershipSample :
00058 public Sample< typename TSample::MeasurementVectorType >
00059 {
00060 public:
00062 typedef MembershipSample Self;
00063 typedef Sample< typename TSample::MeasurementVectorType > Superclass ;
00064 typedef SmartPointer< Self > Pointer ;
00065 typedef SmartPointer< const Self > ConstPointer ;
00066
00068 itkTypeMacro(MembershipSample, Sample);
00069 itkNewMacro(Self) ;
00070
00073 typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00074 typedef typename TSample::MeasurementType MeasurementType;
00075 typedef typename TSample::InstanceIdentifier InstanceIdentifier;
00076 typedef typename TSample::FrequencyType FrequencyType ;
00077
00078
00081 typedef std::vector< unsigned int > UniqueClassLabelsType ;
00082
00085 typedef itk::hash_map< InstanceIdentifier, unsigned int> ClassLabelHolderType ;
00086
00089 typedef Subsample< TSample > ClassSampleType ;
00090 typedef typename ClassSampleType::Pointer ClassSamplePointer;
00091 typedef typename ClassSampleType::ConstPointer ClassSampleConstPointer;
00092
00094 void SetSample(const TSample* sample) ;
00095
00097 const TSample* GetSample() const;
00098
00100 void SetNumberOfClasses(unsigned int numberOfClasses) ;
00101
00103 unsigned int GetNumberOfClasses() const ;
00104
00109 void AddInstance(const unsigned int &classLabel, const InstanceIdentifier &id) ;
00112 unsigned int GetClassLabel(const InstanceIdentifier &id) const ;
00113
00116 int GetInternalClassLabel(const unsigned int classLabel ) const ;
00117
00120 unsigned int GetClassSampleSize(const unsigned int &classLabel) const ;
00121
00124 const ClassSampleType* GetClassSample(const unsigned int &classLabel) const ;
00125
00128 ClassLabelHolderType* GetClassLabels()
00129 { return &m_ClassLabelHolder ; }
00130
00132 unsigned int Size(void) const ;
00133
00136 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00137
00140 MeasurementType GetMeasurement(const InstanceIdentifier &id,
00141 const unsigned int &dimension) ;
00142
00144 FrequencyType GetFrequency(const InstanceIdentifier &id) const ;
00145
00147 FrequencyType GetTotalFrequency() const ;
00148
00149 void Resize(unsigned int n)
00150 {
00151 m_ClassLabelHolder.resize(n) ;
00152 }
00153
00154
00155 class ConstIterator
00156 {
00157 public:
00158 ConstIterator(InstanceIdentifier id, const Self* membershipSample)
00159 :m_Id(id), m_MembershipSample(membershipSample),
00160 m_Sample(membershipSample->GetSample())
00161 {}
00162
00163 FrequencyType GetFrequency() const
00164 { return m_Sample->GetFrequency(m_Id) ; }
00165
00166 const MeasurementVectorType & GetMeasurementVector() const
00167 { return m_Sample->GetMeasurementVector(m_Id) ; }
00168
00169 InstanceIdentifier GetInstanceIdentifier() const
00170 { return m_Id ; }
00171
00172 void SetClassLabel(unsigned int classLabel)
00173 { m_MembershipSample->AddInstance(classLabel, m_Id) ; }
00174
00175 unsigned int GetClassLabel() const
00176 { return m_MembershipSample->GetClassLabel(m_Id) ; }
00177
00178 ConstIterator& operator++()
00179 {
00180 ++m_Id ;
00181 return *this ;
00182 }
00183
00184 bool operator!=(const ConstIterator& it)
00185 {
00186 if (m_Id != it.m_Id ||
00187 m_MembershipSample != it.m_MembershipSample ||
00188 m_Sample != it.m_Sample)
00189 {
00190 return true ;
00191 }
00192 else
00193 {
00194 return false ;
00195 }
00196 }
00197
00198 bool operator==(const ConstIterator& it)
00199 {
00200 if (m_Id == it.m_Id &&
00201 m_MembershipSample == it.m_MembershipSample &&
00202 m_Sample == it.m_Sample)
00203 {
00204 return true ;
00205 }
00206 else
00207 {
00208 return false ;
00209 }
00210 }
00211
00212 ConstIterator& operator=(const ConstIterator& it)
00213 {
00214 m_Id = it.m_Id;
00215 m_MembershipSample = it.m_MembershipSample ;
00216 m_Sample = it.m_Sample ;
00217 return *this ;
00218 }
00219
00220 ConstIterator(const ConstIterator& it)
00221 {
00222 m_Id = it.m_Id;
00223 m_MembershipSample = it.m_MembershipSample ;
00224 m_Sample = it.m_Sample ;
00225 }
00226
00227 private:
00228
00229 InstanceIdentifier m_Id ;
00230
00231 const Self* m_MembershipSample ;
00232 const TSample* m_Sample ;
00233 } ;
00234
00235 ConstIterator Begin() const
00236 {
00237 ConstIterator iter(0, this) ;
00238 return iter;
00239 }
00240
00241 ConstIterator End() const
00242 {
00243 ConstIterator iter(this->Size(), this) ;
00244 return iter;
00245 }
00246
00247 protected:
00248 MembershipSample() ;
00249 virtual ~MembershipSample() {}
00250 void PrintSelf(std::ostream& os, Indent indent) const;
00251
00252 private:
00253 MembershipSample(const Self&) ;
00254 void operator=(const Self&) ;
00255
00256 const TSample* m_Sample ;
00257 unsigned int m_CurrentClassLabel ;
00258 UniqueClassLabelsType m_UniqueClassLabels ;
00259 ClassLabelHolderType m_ClassLabelHolder ;
00260 unsigned int m_NumberOfClasses ;
00261 std::vector< unsigned int > m_ClassSampleSizes ;
00262 std::vector< ClassSamplePointer > m_ClassSamples ;
00263 } ;
00264
00265
00266 }
00267 }
00268
00269
00270 #ifndef ITK_MANUAL_INSTANTIATION
00271 #include "itkMembershipSample.txx"
00272 #endif
00273
00274 #endif
00275
00276
00277
00278
00279
00280
00281