1 // This is brl/bbas/bsta/algo/bsta_sample_set.hxx
2 #ifndef bsta_sample_set_hxx_
3 #define bsta_sample_set_hxx_
4 //:
5 // \file
6 #include <iostream>
7 #include <map>
8 #include "bsta_sample_set.h"
9
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13
14 //: Compute the mean in a window around the given pt, the window size is the bandwidth
15 // If there are no points within bandwidth of the input \a pt, return false
16 template <class T, unsigned n>
17 bool
mean(typename bsta_parzen_sphere<T,n>::vector_type const & pt,typename bsta_parzen_sphere<T,n>::vector_type & out)18 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)
19 {
20 typedef typename bsta_parzen_sphere<T,n>::vector_type vect_t;
21 typedef typename std::vector<vect_t >::const_iterator sit_t;
22 typedef typename std::vector<T >::const_iterator wit_t;
23
24 vect_t sum(T(0));
25 sit_t sit = bsta_parzen<T,n>::samples_.begin();
26 wit_t wit = weights_.begin();
27 T nsamp = 0;
28 for (; sit != bsta_parzen<T,n>::samples_.end(); ++sit, ++wit) {
29 vect_t s = *sit;
30 vect_t dif = s-pt;
31 vnl_vector_fixed<T,n> dummy(dif);
32 T d = dummy.magnitude();
33 if (d < bsta_parzen_sphere<T,n>::bandwidth_) { // this sample is within window of the given point, use it to calculate mean
34 sum += (*wit)*(*sit);
35 nsamp += (*wit);
36 }
37 }
38 if (nsamp > 0) {
39 out = sum / nsamp;
40 return true;
41 }
42
43 return false;
44 }
45
46 //: compute the mean of a particular assignment/mode/cluster
47 template <class T, unsigned n>
48 bool
mode_mean(int mode,vector_ & out) const49 bsta_sample_set<T,n>::mode_mean(int mode, vector_& out) const
50 {
51 typedef typename bsta_parzen_sphere<T,n>::vector_type vect_t;
52 typedef typename std::vector<vect_t >::const_iterator sit_t;
53 typedef typename std::vector<T >::const_iterator wit_t;
54 typedef typename std::vector<int >::const_iterator ait_t;
55
56 if (bsta_parzen<T,n>::samples_.size() != weights_.size() || bsta_parzen<T,n>::samples_.size() != assignments_.size()) {
57 std::cout << "Error in - bsta_sample_set<T,n>::mean() : assignments not initialized!\n";
58 return false;
59 }
60
61 bool found_one = false;
62 vect_t sum(T(0));
63 sit_t sit = bsta_parzen<T,n>::samples_.begin();
64 wit_t wit = weights_.begin();
65 ait_t ait = assignments_.begin();
66 T nsamp = 0;
67 for (; sit != bsta_parzen<T,n>::samples_.end(); ++sit, ++wit, ++ait) {
68 if (*ait != mode)
69 continue;
70
71 found_one = true;
72 sum += (*wit)*(*sit);
73 nsamp += (*wit);
74 }
75 if (found_one && nsamp > 0) {
76 out = sum / nsamp;
77 return true;
78 }
79
80 return false;
81 }
82
83 //: return number of assignments to this mode
84 template <class T, unsigned n>
85 int
mode_size(int mode) const86 bsta_sample_set<T,n>::mode_size(int mode) const
87 {
88 typedef typename std::vector<int >::const_iterator ait_t;
89 ait_t ait = assignments_.begin();
90 int cnt = 0;
91 for (; ait != assignments_.end(); ++ait) {
92 if (*ait == mode)
93 cnt++;
94 }
95
96 return cnt;
97 }
98
99 //: return total weight of assignments to this mode
100 template <class T, unsigned n>
101 T
102 bsta_sample_set<T,n>::mode_weight(int mode) const
103 {
104 typedef typename std::vector<int >::const_iterator ait_t;
105 typedef typename std::vector<T >::const_iterator wit_t;
106
107 ait_t ait = assignments_.begin();
108 wit_t wit = weights_.begin();
109
110 T sum = T(0);
111 for (; ait != assignments_.end(); ++ait, ++wit) {
112 if (*ait == mode)
113 sum += *wit;
114 }
115
116 return sum;
117 }
118
119 //: return total weight of all assignments
120 template <class T, unsigned n>
121 T
122 bsta_sample_set<T,n>::total_weight() const
123 {
124 typedef typename std::vector<T >::const_iterator wit_t;
125
126 wit_t wit = weights_.begin();
127
128 T sum = T(0);
129 for (; wit != weights_.end(); ++wit) {
130 sum += *wit;
131 }
132
133 return sum;
134 }
135
136 //: return number of modes in the current assignment vector
137 template <class T, unsigned n>
138 unsigned
mode_cnt() const139 bsta_sample_set<T,n>::mode_cnt() const
140 {
141 typedef typename std::vector<int >::const_iterator ait_t;
142 ait_t ait = assignments_.begin();
143 std::map<int, bool> modes;
144 for (; ait != assignments_.end(); ++ait) {
145 std::map<int, bool>::iterator it = modes.find(*ait);
146 if (it == modes.end()) {
147 modes[*ait] = true;
148 }
149 }
150 return modes.size();
151 }
152
153 //: Insert a weighted sample into the distribution
154 template <class T, unsigned n>
155 void
insert_sample(typename bsta_parzen_sphere<T,n>::vector_type const & sample,T weight)156 bsta_sample_set<T,n>::insert_sample(typename bsta_parzen_sphere<T,n>::vector_type const& sample, T weight)
157 {
158 bsta_parzen<T,n>::samples_.push_back(sample);
159 weights_.push_back(weight);
160 }
161
162 //: one may need to normalize the weights after the insertion is over
163 template <class T, unsigned n>
164 void
normalize_weights()165 bsta_sample_set<T,n>::normalize_weights()
166 {
167 T sum(0);
168 for (unsigned i = 0; i < weights_.size(); i++) {
169 sum += weights_[i];
170 }
171
172 if (sum > T(0)) {
173 for (unsigned i = 0; i < weights_.size(); i++) {
174 weights_[i] = weights_[i]/sum;
175 }
176 }
177 }
178
179 //: must call this method before using the assignment vector
180 template <class T, unsigned n>
181 void
initialize_assignments()182 bsta_sample_set<T,n>::initialize_assignments()
183 {
184 assignments_.clear();
185 assignments_ = std::vector<int>(bsta_parzen<T,n>::samples_.size(), -1);
186 }
187
188 #define BSTA_SAMPLE_SET_INSTANTIATE(T,n) \
189 template class bsta_sample_set<T,n >
190
191 #endif // bsta_sample_set_hxx_
192