MVAWrapperBase.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 // \version
3 //
4 // \brief Helper functions for MVAReader and MVAWriter wrappers
5 //
6 // \author robert.sulej@cern.ch
7 //
8 //////////////////////////////////////////////////////////////////////////////
9 #ifndef ANAB_MVAWRAPPERBASE_H
10 #define ANAB_MVAWRAPPERBASE_H
11 
13 
15 
16 #include <typeinfo>
17 #include <functional>
18 #include <string>
19 #include <unordered_map>
20 #include <cmath>
21 
22 namespace anab {
23 
24 /// Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
26 public:
27 
28 protected:
29 
30  std::string getProductName(std::type_info const & ti) const;
31  size_t getProductHash(std::type_info const & ti) const { return ti.hash_code(); }
32 
33 };
34 
35 /// Helper functions for MVAReader and MVAWriter wrappers.
37 public:
38 
39 protected:
40 
41  // all mva outputs in the feature vecor sum up to p=1
42 
43  template <class T, size_t N>
44  std::array<float, N> pAccumulate(
45  std::vector< art::Ptr<T> > const & items,
46  std::vector< FeatureVector<N> > const & outs) const;
47 
48  template <class T, size_t N>
49  std::array<float, N> pAccumulate(
50  std::vector< art::Ptr<T> > const & items, std::vector<float> const & weights,
51  std::vector< FeatureVector<N> > const & outs) const;
52 
53  template <class T, size_t N>
54  std::array<float, N> pAccumulate(
55  std::vector< art::Ptr<T> > const & items, std::function<float (T const &)> fweight,
56  std::vector< FeatureVector<N> > const & outs) const;
57 
58  template <class T, size_t N>
59  std::array<float, N> pAccumulate(
60  std::vector< art::Ptr<T> > const & items, std::function<float (art::Ptr<T> const &)> fweight,
61  std::vector< FeatureVector<N> > const & outs) const;
62 
63  // outputs in the feature vecor sum up to p=1 in groups:
64  // - members of a group are tagged in the mask with the same non-negative number
65  // - entries with negative tag in the mask are ignored
66 
67  template <class T, size_t N>
68  std::array<float, N> pAccumulate(
69  std::vector< art::Ptr<T> > const & items,
70  std::vector< FeatureVector<N> > const & outs,
71  std::array<char, N> const & mask) const;
72 };
73 
74 } // namespace anab
75 
76 //----------------------------------------------------------------------------
77 // MVAReader functions.
78 //
79 template <class T, size_t N>
80 std::array<float, N> anab::MVAWrapperBase::pAccumulate(
81  std::vector< art::Ptr<T> > const & items,
82  std::vector< anab::FeatureVector<N> > const & outs) const
83 {
84  std::array<double, N> acc;
85  acc.fill(0);
86 
87  float pmin = 1.0e-6, pmax = 1.0 - pmin;
88  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
89 
90  for (auto const & ptr : items)
91  {
92  auto const & vout = outs[ptr.key()];
93  for (size_t i = 0; i < vout.size(); ++i)
94  {
95  float v;
96  if (vout[i] < pmin) v = log_pmin;
97  else if (vout[i] > pmax) v = log_pmax;
98  else v = std::log(vout[i]);
99 
100  acc[i] += v;
101  }
102  }
103 
104  if (!items.empty())
105  {
106  double totp = 0.0;
107  for (size_t i = 0; i < N; ++i)
108  {
109  acc[i] = exp(acc[i] / items.size());
110  totp += acc[i];
111  }
112  for (size_t i = 0; i < N; ++i)
113  {
114  acc[i] /= totp;
115  }
116  }
117  else std::fill(acc.begin(), acc.end(), 1.0 / N);
118 
119 
120  std::array<float, N> result;
121  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
122  return result;
123 }
124 //----------------------------------------------------------------------------
125 
126 template <class T, size_t N>
128  std::vector< art::Ptr<T> > const & items, std::vector<float> const & weights,
129  std::vector< anab::FeatureVector<N> > const & outs) const
130 {
131  std::array<double, N> acc;
132  acc.fill(0);
133 
134  float pmin = 1.0e-6, pmax = 1.0 - pmin;
135  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
136  double totw = 0.0;
137 
138  for (size_t k = 0; k < items.size(); ++k)
139  {
140  auto const & ptr = items[k];
141  float w = weights[k];
142 
143  if (w == 0) continue;
144 
145  auto const & vout = outs[ptr.key()];
146  for (size_t i = 0; i < vout.size(); ++i)
147  {
148  float v;
149  if (vout[i] < pmin) v = log_pmin;
150  else if (vout[i] > pmax) v = log_pmax;
151  else v = std::log(vout[i]);
152 
153  acc[i] += w * v;
154  }
155  totw += w;
156  }
157 
158  if (!items.empty())
159  {
160  double totp = 0.0;
161  for (size_t i = 0; i < N; ++i)
162  {
163  acc[i] = exp(acc[i] / totw);
164  totp += acc[i];
165  }
166  for (size_t i = 0; i < N; ++i)
167  {
168  acc[i] /= totp;
169  }
170  }
171  else std::fill(acc.begin(), acc.end(), 1.0 / N);
172 
173 
174  std::array<float, N> result;
175  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
176  return result;
177 }
178 //----------------------------------------------------------------------------
179 
180 template <class T, size_t N>
182  std::vector< art::Ptr<T> > const & items, std::function<float (T const &)> fweight,
183  std::vector< anab::FeatureVector<N> > const & outs) const
184 {
185  std::array<double, N> acc;
186  acc.fill(0);
187 
188  float pmin = 1.0e-6, pmax = 1.0 - pmin;
189  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
190  double totw = 0.0;
191 
192  for (size_t k = 0; k < items.size(); ++k)
193  {
194  auto const & ptr = items[k];
195  float w = fweight(*ptr);
196 
197  if (w == 0) continue;
198 
199  auto const & vout = outs[ptr.key()];
200  for (size_t i = 0; i < vout.size(); ++i)
201  {
202  float v;
203  if (vout[i] < pmin) v = log_pmin;
204  else if (vout[i] > pmax) v = log_pmax;
205  else v = std::log(vout[i]);
206 
207  acc[i] += w * v;
208  }
209  totw += w;
210  }
211 
212  if (!items.empty())
213  {
214  double totp = 0.0;
215  for (size_t i = 0; i < N; ++i)
216  {
217  acc[i] = exp(acc[i] / totw);
218  totp += acc[i];
219  }
220  for (size_t i = 0; i < N; ++i)
221  {
222  acc[i] /= totp;
223  }
224  }
225  else std::fill(acc.begin(), acc.end(), 1.0 / N);
226 
227 
228  std::array<float, N> result;
229  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
230  return result;
231 }
232 //----------------------------------------------------------------------------
233 
234 template <class T, size_t N>
236  std::vector< art::Ptr<T> > const & items, std::function<float (art::Ptr<T> const &)> fweight,
237  std::vector< anab::FeatureVector<N> > const & outs) const
238 {
239  std::array<double, N> acc;
240  acc.fill(0);
241 
242  float pmin = 1.0e-6, pmax = 1.0 - pmin;
243  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
244  double totw = 0.0;
245 
246  for (size_t k = 0; k < items.size(); ++k)
247  {
248  auto const & ptr = items[k];
249  float w = fweight(ptr);
250 
251  if (w == 0) continue;
252 
253  auto const & vout = outs[ptr.key()];
254  for (size_t i = 0; i < vout.size(); ++i)
255  {
256  float v;
257  if (vout[i] < pmin) v = log_pmin;
258  else if (vout[i] > pmax) v = log_pmax;
259  else v = std::log(vout[i]);
260 
261  acc[i] += w * v;
262  }
263  totw += w;
264  }
265 
266  if (!items.empty())
267  {
268  double totp = 0.0;
269  for (size_t i = 0; i < N; ++i)
270  {
271  acc[i] = exp(acc[i] / totw);
272  totp += acc[i];
273  }
274  for (size_t i = 0; i < N; ++i)
275  {
276  acc[i] /= totp;
277  }
278  }
279  else std::fill(acc.begin(), acc.end(), 1.0 / N);
280 
281 
282  std::array<float, N> result;
283  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
284  return result;
285 }
286 //----------------------------------------------------------------------------
287 
288 //----------------------------------------------------------------------------
289 // functions with the mask tagging groups og labels
290 //----------------------------------------------------------------------------
291 
292 template <class T, size_t N>
294  std::vector< art::Ptr<T> > const & items,
295  std::vector< anab::FeatureVector<N> > const & outs,
296  std::array<char, N> const & mask) const
297 {
298  size_t n_groups = 0;
299  std::unordered_map<char, size_t> label2group;
300  std::vector<size_t> nb_entries;
301  std::array<int, N> groupidx;
302  for (size_t i = 0; i < N; ++i)
303  {
304  int idx = -1;
305  if (mask[i] >= 0)
306  {
307  auto search = label2group.find(mask[i]);
308  if (search == label2group.end())
309  {
310  idx = n_groups;
311  label2group[mask[i]] = idx;
312  nb_entries.push_back(0);
313  ++n_groups;
314  }
315  else
316  {
317  idx = search->second;
318  nb_entries[idx]++;
319  }
320  }
321  groupidx[i] = idx;
322  }
323 
324  std::array<double, N> acc;
325  acc.fill(0);
326 
327  float pmin = 1.0e-6, pmax = 1.0 - pmin;
328  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
329 
330  for (auto const & ptr : items)
331  {
332  auto const & vout = outs[ptr.key()];
333  for (size_t i = 0; i < vout.size(); ++i)
334  {
335  if (groupidx[i] < 0) continue;
336 
337  float v;
338  if (vout[i] < pmin) v = log_pmin;
339  else if (vout[i] > pmax) v = log_pmax;
340  else v = std::log(vout[i]);
341 
342  acc[i] += v;
343  }
344  }
345 
346  if (!items.empty())
347  {
348  std::vector<double> totp(n_groups, 0.0);
349  for (size_t i = 0; i < N; ++i)
350  {
351  if (groupidx[i] >= 0)
352  {
353  acc[i] = exp(acc[i] / items.size());
354  totp[groupidx[i]] += acc[i];
355  }
356  }
357  for (size_t i = 0; i < N; ++i)
358  {
359  if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
360  }
361  }
362  else
363  {
364  for (size_t i = 0; i < N; ++i)
365  {
366  if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
367  }
368  }
369 
370  std::array<float, N> result;
371  for (size_t i = 0; i < N; ++i) result[i] = acc[i];
372  return result;
373 }
374 //----------------------------------------------------------------------------
375 
376 #endif //ANAB_MVAWRAPPERBASE
static QCString result
std::string string
Definition: nybbler.cc:12
struct vector vector
std::string getProductName(std::type_info const &ti) const
Helper functions for MVAReader and MVAWriter wrappers.
Definition: search.py:1
Helper functions for MVAReader/Writer and FVecReader/Writer wrappers.
def fill(s)
Definition: translator.py:93
std::array< float, N > pAccumulate(std::vector< art::Ptr< T > > const &items, std::vector< FeatureVector< N > > const &outs) const
size_t getProductHash(std::type_info const &ti) const
void function(int client, int *resource, int parblock, int *test, int p)
Definition: fwd.h:31