LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
anab::MVAWrapperBase Class Reference

Helper functions for MVAReader and MVAWriter wrappers. More...

#include "MVAWrapperBase.h"

Inheritance diagram for anab::MVAWrapperBase:
anab::MVAReader< T, N > anab::MVAWriter< N > anab::MVAWriter< 2 > anab::MVAWriter< 3 > anab::MVAWriter< 4 >

Protected Member Functions

template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< float > const &weights, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::function< float(T const &)> fweight, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::function< float(art::Ptr< T > const &)> fweight, std::vector< FeatureVector< N >> const &outs) const
 
template<class T , size_t N>
std::array< float, N > pAccumulate (std::vector< art::Ptr< T >> const &items, std::vector< FeatureVector< N >> const &outs, std::array< char, N > const &mask) const
 

Detailed Description

Helper functions for MVAReader and MVAWriter wrappers.

Definition at line 33 of file MVAWrapperBase.h.

Member Function Documentation

template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< FeatureVector< N >> const &  outs 
) const
protected

Definition at line 73 of file MVAWrapperBase.h.

References trkf::fill().

76 {
77  std::array<double, N> acc;
78  acc.fill(0);
79 
80  float pmin = 1.0e-6, pmax = 1.0 - pmin;
81  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
82 
83  for (auto const& ptr : items) {
84  auto const& vout = outs[ptr.key()];
85  for (size_t i = 0; i < vout.size(); ++i) {
86  float v;
87  if (vout[i] < pmin)
88  v = log_pmin;
89  else if (vout[i] > pmax)
90  v = log_pmax;
91  else
92  v = std::log(vout[i]);
93 
94  acc[i] += v;
95  }
96  }
97 
98  if (!items.empty()) {
99  double totp = 0.0;
100  for (size_t i = 0; i < N; ++i) {
101  acc[i] = exp(acc[i] / items.size());
102  totp += acc[i];
103  }
104  for (size_t i = 0; i < N; ++i) {
105  acc[i] /= totp;
106  }
107  }
108  else
109  std::fill(acc.begin(), acc.end(), 1.0 / N);
110 
111  std::array<float, N> result;
112  for (size_t i = 0; i < N; ++i)
113  result[i] = acc[i];
114  return result;
115 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< float > const &  weights,
std::vector< FeatureVector< N >> const &  outs 
) const
protected

Definition at line 119 of file MVAWrapperBase.h.

References trkf::fill(), and w.

123 {
124  std::array<double, N> acc;
125  acc.fill(0);
126 
127  float pmin = 1.0e-6, pmax = 1.0 - pmin;
128  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
129  double totw = 0.0;
130 
131  for (size_t k = 0; k < items.size(); ++k) {
132  auto const& ptr = items[k];
133  float w = weights[k];
134 
135  if (w == 0) continue;
136 
137  auto const& vout = outs[ptr.key()];
138  for (size_t i = 0; i < vout.size(); ++i) {
139  float v;
140  if (vout[i] < pmin)
141  v = log_pmin;
142  else if (vout[i] > pmax)
143  v = log_pmax;
144  else
145  v = std::log(vout[i]);
146 
147  acc[i] += w * v;
148  }
149  totw += w;
150  }
151 
152  if (!items.empty()) {
153  double totp = 0.0;
154  for (size_t i = 0; i < N; ++i) {
155  acc[i] = exp(acc[i] / totw);
156  totp += acc[i];
157  }
158  for (size_t i = 0; i < N; ++i) {
159  acc[i] /= totp;
160  }
161  }
162  else
163  std::fill(acc.begin(), acc.end(), 1.0 / N);
164 
165  std::array<float, N> result;
166  for (size_t i = 0; i < N; ++i)
167  result[i] = acc[i];
168  return result;
169 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::function< float(T const &)>  fweight,
std::vector< FeatureVector< N >> const &  outs 
) const
protected

Definition at line 173 of file MVAWrapperBase.h.

References trkf::fill(), and w.

177 {
178  std::array<double, N> acc;
179  acc.fill(0);
180 
181  float pmin = 1.0e-6, pmax = 1.0 - pmin;
182  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
183  double totw = 0.0;
184 
185  for (size_t k = 0; k < items.size(); ++k) {
186  auto const& ptr = items[k];
187  float w = fweight(*ptr);
188 
189  if (w == 0) continue;
190 
191  auto const& vout = outs[ptr.key()];
192  for (size_t i = 0; i < vout.size(); ++i) {
193  float v;
194  if (vout[i] < pmin)
195  v = log_pmin;
196  else if (vout[i] > pmax)
197  v = log_pmax;
198  else
199  v = std::log(vout[i]);
200 
201  acc[i] += w * v;
202  }
203  totw += w;
204  }
205 
206  if (!items.empty()) {
207  double totp = 0.0;
208  for (size_t i = 0; i < N; ++i) {
209  acc[i] = exp(acc[i] / totw);
210  totp += acc[i];
211  }
212  for (size_t i = 0; i < N; ++i) {
213  acc[i] /= totp;
214  }
215  }
216  else
217  std::fill(acc.begin(), acc.end(), 1.0 / N);
218 
219  std::array<float, N> result;
220  for (size_t i = 0; i < N; ++i)
221  result[i] = acc[i];
222  return result;
223 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::function< float(art::Ptr< T > const &)>  fweight,
std::vector< FeatureVector< N >> const &  outs 
) const
protected

Definition at line 227 of file MVAWrapperBase.h.

References trkf::fill(), and w.

231 {
232  std::array<double, N> acc;
233  acc.fill(0);
234 
235  float pmin = 1.0e-6, pmax = 1.0 - pmin;
236  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
237  double totw = 0.0;
238 
239  for (size_t k = 0; k < items.size(); ++k) {
240  auto const& ptr = items[k];
241  float w = fweight(ptr);
242 
243  if (w == 0) continue;
244 
245  auto const& vout = outs[ptr.key()];
246  for (size_t i = 0; i < vout.size(); ++i) {
247  float v;
248  if (vout[i] < pmin)
249  v = log_pmin;
250  else if (vout[i] > pmax)
251  v = log_pmax;
252  else
253  v = std::log(vout[i]);
254 
255  acc[i] += w * v;
256  }
257  totw += w;
258  }
259 
260  if (!items.empty()) {
261  double totp = 0.0;
262  for (size_t i = 0; i < N; ++i) {
263  acc[i] = exp(acc[i] / totw);
264  totp += acc[i];
265  }
266  for (size_t i = 0; i < N; ++i) {
267  acc[i] /= totp;
268  }
269  }
270  else
271  std::fill(acc.begin(), acc.end(), 1.0 / N);
272 
273  std::array<float, N> result;
274  for (size_t i = 0; i < N; ++i)
275  result[i] = acc[i];
276  return result;
277 }
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
Float_t w
Definition: plot.C:20
template<class T , size_t N>
std::array< float, N > anab::MVAWrapperBase::pAccumulate ( std::vector< art::Ptr< T >> const &  items,
std::vector< FeatureVector< N >> const &  outs,
std::array< char, N > const &  mask 
) const
protected

Definition at line 285 of file MVAWrapperBase.h.

289 {
290  size_t n_groups = 0;
291  std::unordered_map<char, size_t> label2group;
292  std::vector<size_t> nb_entries;
293  std::array<int, N> groupidx;
294  for (size_t i = 0; i < N; ++i) {
295  int idx = -1;
296  if (mask[i] >= 0) {
297  auto search = label2group.find(mask[i]);
298  if (search == label2group.end()) {
299  idx = n_groups;
300  label2group[mask[i]] = idx;
301  nb_entries.push_back(0);
302  ++n_groups;
303  }
304  else {
305  idx = search->second;
306  nb_entries[idx]++;
307  }
308  }
309  groupidx[i] = idx;
310  }
311 
312  std::array<double, N> acc;
313  acc.fill(0);
314 
315  float pmin = 1.0e-6, pmax = 1.0 - pmin;
316  float log_pmin = std::log(pmin), log_pmax = std::log(pmax);
317 
318  for (auto const& ptr : items) {
319  auto const& vout = outs[ptr.key()];
320  for (size_t i = 0; i < vout.size(); ++i) {
321  if (groupidx[i] < 0) continue;
322 
323  float v;
324  if (vout[i] < pmin)
325  v = log_pmin;
326  else if (vout[i] > pmax)
327  v = log_pmax;
328  else
329  v = std::log(vout[i]);
330 
331  acc[i] += v;
332  }
333  }
334 
335  if (!items.empty()) {
336  std::vector<double> totp(n_groups, 0.0);
337  for (size_t i = 0; i < N; ++i) {
338  if (groupidx[i] >= 0) {
339  acc[i] = exp(acc[i] / items.size());
340  totp[groupidx[i]] += acc[i];
341  }
342  }
343  for (size_t i = 0; i < N; ++i) {
344  if (groupidx[i] >= 0) { acc[i] /= totp[groupidx[i]]; }
345  }
346  }
347  else {
348  for (size_t i = 0; i < N; ++i) {
349  if (groupidx[i] >= 0) { acc[i] = 1 / nb_entries[groupidx[i]]; }
350  }
351  }
352 
353  std::array<float, N> result;
354  for (size_t i = 0; i < N; ++i)
355  result[i] = acc[i];
356  return result;
357 }

The documentation for this class was generated from the following file: