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);
00071
00075 typedef typename TSample::MeasurementVectorType MeasurementVectorType;
00076 typedef typename TSample::MeasurementType MeasurementType;
00077 typedef typename TSample::InstanceIdentifier InstanceIdentifier;
00078 typedef typename TSample::FrequencyType FrequencyType;
00079 typedef typename TSample::TotalFrequencyType TotalFrequencyType;
00080
00081
00084 typedef std::vector< unsigned int > UniqueClassLabelsType;
00085
00088 typedef itk::hash_map< InstanceIdentifier, unsigned int> ClassLabelHolderType;
00089
00092 typedef Subsample< TSample > ClassSampleType;
00093 typedef typename ClassSampleType::Pointer ClassSamplePointer;
00094 typedef typename ClassSampleType::ConstPointer ClassSampleConstPointer;
00095
00097 void SetSample(const TSample* sample);
00098
00100 const TSample* GetSample() const;
00101
00103 void SetNumberOfClasses(unsigned int numberOfClasses);
00104
00106 unsigned int GetNumberOfClasses() const;
00107
00112 void AddInstance(const unsigned int &classLabel, const InstanceIdentifier &id);
00113
00116 unsigned int GetClassLabel(const InstanceIdentifier &id) const;
00117
00120 int GetInternalClassLabel(const unsigned int classLabel ) const;
00121
00124 unsigned int GetClassSampleSize(const unsigned int &classLabel) const;
00125
00128 const ClassSampleType* GetClassSample(const unsigned int &classLabel) const;
00129
00132 ClassLabelHolderType* GetClassLabels()
00133 { return &m_ClassLabelHolder; }
00134
00136 unsigned int Size(void) const;
00137
00140 const MeasurementVectorType & GetMeasurementVector(const InstanceIdentifier &id) const;
00141
00144 MeasurementType GetMeasurement(const InstanceIdentifier &id,
00145 const unsigned int &dimension);
00146
00148 FrequencyType GetFrequency(const InstanceIdentifier &id) const;
00149
00151 TotalFrequencyType GetTotalFrequency() const;
00152
00153 void Resize(unsigned int n)
00154 {
00155 m_ClassLabelHolder.resize(n);
00156 }
00157
00158 class ConstIterator
00159 {
00160 public:
00161 ConstIterator(InstanceIdentifier id, const Self* membershipSample)
00162 :m_Id(id), m_MembershipSample(membershipSample),
00163 m_Sample(membershipSample->GetSample()) {}
00164
00165 FrequencyType GetFrequency() const
00166 { return m_Sample->GetFrequency(m_Id); }
00167
00168 const MeasurementVectorType & GetMeasurementVector() const
00169 { return m_Sample->GetMeasurementVector(m_Id); }
00170
00171 InstanceIdentifier GetInstanceIdentifier() const
00172 { return m_Id; }
00173
00174 void SetClassLabel(unsigned int classLabel)
00175 { m_MembershipSample->AddInstance(classLabel, m_Id); }
00176
00177 unsigned int GetClassLabel() const
00178 { return m_MembershipSample->GetClassLabel(m_Id); }
00179
00180 ConstIterator& operator++()
00181 {
00182 ++m_Id;
00183 return *this;
00184 }
00185
00186 bool operator!=(const ConstIterator& it)
00187 {
00188 if (m_Id != it.m_Id ||
00189 m_MembershipSample != it.m_MembershipSample ||
00190 m_Sample != it.m_Sample)
00191 {
00192 return true;
00193 }
00194 else
00195 {
00196 return false;
00197 }
00198 }
00199
00200 bool operator==(const ConstIterator& it)
00201 {
00202 if (m_Id == it.m_Id &&
00203 m_MembershipSample == it.m_MembershipSample &&
00204 m_Sample == it.m_Sample)
00205 {
00206 return true;
00207 }
00208 else
00209 {
00210 return false;
00211 }
00212 }
00213
00214 ConstIterator& operator=(const ConstIterator& it)
00215 {
00216 m_Id = it.m_Id;
00217 m_MembershipSample = it.m_MembershipSample;
00218 m_Sample = it.m_Sample;
00219 return *this;
00220 }
00221
00222 ConstIterator(const ConstIterator& it)
00223 {
00224 m_Id = it.m_Id;
00225 m_MembershipSample = it.m_MembershipSample;
00226 m_Sample = it.m_Sample;
00227 }
00228
00229 private:
00230
00231 InstanceIdentifier m_Id;
00232
00233 const Self* m_MembershipSample;
00234 const TSample* m_Sample;
00235 };
00236
00237 ConstIterator Begin() const
00238 {
00239 ConstIterator iter(0, this);
00240 return iter;
00241 }
00242
00243 ConstIterator End() const
00244 {
00245 ConstIterator iter(this->Size(), this);
00246 return iter;
00247 }
00248
00249 protected:
00250 MembershipSample();
00251 virtual ~MembershipSample() {}
00252 void PrintSelf(std::ostream& os, Indent indent) const;
00253
00254 private:
00255 MembershipSample(const Self&);
00256 void operator=(const Self&);
00257
00258 const TSample* m_Sample;
00259 unsigned int m_CurrentClassLabel;
00260 UniqueClassLabelsType m_UniqueClassLabels;
00261 ClassLabelHolderType m_ClassLabelHolder;
00262 unsigned int m_NumberOfClasses;
00263 std::vector< unsigned int > m_ClassSampleSizes;
00264 std::vector< ClassSamplePointer > m_ClassSamples;
00265 };
00266
00267
00268 }
00269 }
00270
00271
00272 #ifndef ITK_MANUAL_INSTANTIATION
00273 #include "itkMembershipSample.txx"
00274 #endif
00275
00276 #endif
00277