contrib/brl/bbas/bsta/algo/bsta_sample_set.txx
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/algo/bsta_sample_set.txx
00002 #ifndef bsta_sample_set_txx_
00003 #define bsta_sample_set_txx_
00004 //:
00005 // \file
00006 #include "bsta_sample_set.h"
00007 
00008 #include <vcl_map.h>
00009 
00010 //: Compute the mean in a window around the given pt, the window size is the bandwidth
00011 //  If there are no points within bandwidth of the input \a pt, return false
00012 template <class T, unsigned n>
00013 bool
00014 bsta_sample_set<T,n>::mean(typename bsta_parzen_sphere<T,n>::vector_type const& pt, typename bsta_parzen_sphere<T,n>::vector_type& out)
00015 {
00016   typedef typename bsta_parzen_sphere<T,n>::vector_type vect_t;
00017   typedef typename vcl_vector<vect_t >::const_iterator sit_t;
00018   typedef typename vcl_vector<T >::const_iterator wit_t;
00019 
00020   vect_t sum(T(0));
00021   sit_t sit = bsta_parzen<T,n>::samples_.begin();
00022   wit_t wit = weights_.begin();
00023   T nsamp = 0;
00024   for (; sit != bsta_parzen<T,n>::samples_.end(); ++sit, ++wit) {
00025     vect_t s = *sit;
00026     vect_t dif = s-pt;
00027     vnl_vector_fixed<T,n> dummy(dif);
00028     T d = dummy.magnitude();
00029     if (d < bsta_parzen_sphere<T,n>::bandwidth_) { // this sample is within window of the given point, use it to calculate mean
00030       sum += (*wit)*(*sit);
00031       nsamp += (*wit);
00032     }
00033   }
00034   if (nsamp > 0) {
00035     out = sum / nsamp;
00036     return true;
00037   }
00038 
00039   return false;
00040 }
00041 
00042 //: compute the mean of a particular assignment/mode/cluster
00043 template <class T, unsigned n>
00044 bool
00045 bsta_sample_set<T,n>::mode_mean(int mode, vector_& out) const
00046 {
00047   typedef typename bsta_parzen_sphere<T,n>::vector_type vect_t;
00048   typedef typename vcl_vector<vect_t >::const_iterator sit_t;
00049   typedef typename vcl_vector<T >::const_iterator wit_t;
00050   typedef typename vcl_vector<int >::const_iterator ait_t;
00051 
00052   if (bsta_parzen<T,n>::samples_.size() != weights_.size() || bsta_parzen<T,n>::samples_.size() != assignments_.size()) {
00053     vcl_cout << "Error in - bsta_sample_set<T,n>::mean() : assignments not initialized!\n";
00054     return false;
00055   }
00056 
00057   bool found_one = false;
00058   vect_t sum(T(0));
00059   sit_t sit = bsta_parzen<T,n>::samples_.begin();
00060   wit_t wit = weights_.begin();
00061   ait_t ait = assignments_.begin();
00062   T nsamp = 0;
00063   for (; sit != bsta_parzen<T,n>::samples_.end(); ++sit, ++wit, ++ait) {
00064     if (*ait != mode)
00065       continue;
00066 
00067     found_one = true;
00068     sum += (*wit)*(*sit);
00069     nsamp += (*wit);
00070   }
00071   if (found_one && nsamp > 0) {
00072     out = sum / nsamp;
00073     return true;
00074   }
00075 
00076   return false;
00077 }
00078 
00079 //: return number of assignments to this mode
00080 template <class T, unsigned n>
00081 int
00082 bsta_sample_set<T,n>::mode_size(int mode) const
00083 {
00084   typedef typename vcl_vector<int >::const_iterator ait_t;
00085   ait_t ait = assignments_.begin();
00086   int cnt = 0;
00087   for (; ait != assignments_.end(); ++ait) {
00088     if (*ait == mode)
00089       cnt++;
00090   }
00091 
00092   return cnt;
00093 }
00094 
00095 //: return total weight of assignments to this mode
00096 template <class T, unsigned n>
00097 T
00098 bsta_sample_set<T,n>::mode_weight(int mode) const
00099 {
00100   typedef typename vcl_vector<int >::const_iterator ait_t;
00101   typedef typename vcl_vector<T >::const_iterator wit_t;
00102 
00103   ait_t ait = assignments_.begin();
00104   wit_t wit = weights_.begin();
00105 
00106   T sum = T(0);
00107   for (; ait != assignments_.end(); ++ait, ++wit) {
00108     if (*ait == mode)
00109       sum += *wit;
00110   }
00111 
00112   return sum;
00113 }
00114 
00115 //: return total weight of all assignments
00116 template <class T, unsigned n>
00117 T
00118 bsta_sample_set<T,n>::total_weight() const
00119 {
00120   typedef typename vcl_vector<T >::const_iterator wit_t;
00121 
00122   wit_t wit = weights_.begin();
00123 
00124   T sum = T(0);
00125   for (; wit != weights_.end(); ++wit) {
00126     sum += *wit;
00127   }
00128 
00129   return sum;
00130 }
00131 
00132 //: return number of modes in the current assignment vector
00133 template <class T, unsigned n>
00134 unsigned
00135 bsta_sample_set<T,n>::mode_cnt() const
00136 {
00137   typedef typename vcl_vector<int >::const_iterator ait_t;
00138   ait_t ait = assignments_.begin();
00139   vcl_map<int, bool> modes;
00140   for (; ait != assignments_.end(); ++ait) {
00141     vcl_map<int, bool>::iterator it = modes.find(*ait);
00142     if (it == modes.end()) {
00143       modes[*ait] = true;
00144     }
00145   }
00146   return modes.size();
00147 }
00148 
00149 //: Insert a weighted sample into the distribution
00150 template <class T, unsigned n>
00151 void
00152 bsta_sample_set<T,n>::insert_sample(typename bsta_parzen_sphere<T,n>::vector_type const& sample, T weight)
00153 {
00154   bsta_parzen<T,n>::samples_.push_back(sample);
00155   weights_.push_back(weight);
00156 }
00157 
00158 //: one may need to normalize the weights after the insertion is over
00159 template <class T, unsigned n>
00160 void
00161 bsta_sample_set<T,n>::normalize_weights()
00162 {
00163   T sum(0);
00164   for (unsigned i = 0; i < weights_.size(); i++) {
00165     sum += weights_[i];
00166   }
00167 
00168   if (sum > T(0)) {
00169     for (unsigned i = 0; i < weights_.size(); i++) {
00170       weights_[i] = weights_[i]/sum;
00171     }
00172   }
00173 }
00174 
00175 //: must call this method before using the assignment vector
00176 template <class T, unsigned n>
00177 void
00178 bsta_sample_set<T,n>::initialize_assignments()
00179 {
00180   assignments_.clear();
00181   assignments_ = vcl_vector<int>(bsta_parzen<T,n>::samples_.size(), -1);
00182 }
00183 
00184 #define BSTA_SAMPLE_SET_INSTANTIATE(T,n) \
00185 template class bsta_sample_set<T,n >
00186 
00187 #endif // bsta_sample_set_txx_