All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
STATS Class Reference

#include <statistc.h>

Public Member Functions

 STATS (inT32 min_bucket_value, inT32 max_bucket_value_plus_1)
 
 STATS ()
 
 ~STATS ()
 
bool set_range (inT32 min_bucket_value, inT32 max_bucket_value_plus_1)
 
void clear ()
 
void add (inT32 value, inT32 count)
 
inT32 mode () const
 
double mean () const
 
double sd () const
 
double ile (double frac) const
 
inT32 min_bucket () const
 
inT32 max_bucket () const
 
double median () const
 
inT32 pile_count (inT32 value) const
 
inT32 get_total () const
 
bool local_min (inT32 x) const
 
void smooth (inT32 factor)
 
inT32 cluster (float lower, float upper, float multiple, inT32 max_clusters, STATS *clusters)
 
int top_n_modes (int max_modes, GenericVector< tesseract::KDPairInc< float, int > > *modes) const
 
void print () const
 
void print_summary () const
 
void plot (ScrollView *window, float xorigin, float yorigin, float xscale, float yscale, ScrollView::Color colour) const
 
void plotline (ScrollView *window, float xorigin, float yorigin, float xscale, float yscale, ScrollView::Color colour) const
 

Detailed Description

Definition at line 33 of file statistc.h.

Constructor & Destructor Documentation

STATS::STATS ( inT32  min_bucket_value,
inT32  max_bucket_value_plus_1 
)

Definition at line 40 of file statistc.cpp.

40  {
41  if (max_bucket_value_plus_1 <= min_bucket_value) {
42  min_bucket_value = 0;
43  max_bucket_value_plus_1 = 1;
44  }
45  rangemin_ = min_bucket_value; // setup
46  rangemax_ = max_bucket_value_plus_1;
47  buckets_ = new inT32[rangemax_ - rangemin_];
48  clear();
49 }
void clear()
Definition: statistc.cpp:81
int inT32
Definition: host.h:102
STATS::STATS ( )

Definition at line 51 of file statistc.cpp.

51  {
52  rangemax_ = 0;
53  rangemin_ = 0;
54  buckets_ = NULL;
55 }
#define NULL
Definition: host.h:144
STATS::~STATS ( )

Definition at line 92 of file statistc.cpp.

92  {
93  if (buckets_ != NULL) {
94  delete [] buckets_;
95  buckets_ = NULL;
96  }
97 }
#define NULL
Definition: host.h:144

Member Function Documentation

void STATS::add ( inT32  value,
inT32  count 
)

Definition at line 104 of file statistc.cpp.

104  {
105  if (buckets_ == NULL) {
106  return;
107  }
108  value = ClipToRange(value, rangemin_, rangemax_ - 1);
109  buckets_[value - rangemin_] += count;
110  total_count_ += count; // keep count of total
111 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
int count(LIST var_list)
Definition: oldlist.cpp:108
#define NULL
Definition: host.h:144
void STATS::clear ( )

Definition at line 81 of file statistc.cpp.

81  { // clear out buckets
82  total_count_ = 0;
83  if (buckets_ != NULL)
84  memset(buckets_, 0, (rangemax_ - rangemin_) * sizeof(buckets_[0]));
85 }
#define NULL
Definition: host.h:144
inT32 STATS::cluster ( float  lower,
float  upper,
float  multiple,
inT32  max_clusters,
STATS clusters 
)

Definition at line 324 of file statistc.cpp.

328  { // array of clusters
329  BOOL8 new_cluster; // added one
330  float *centres; // cluster centres
331  inT32 entry; // bucket index
332  inT32 cluster; // cluster index
333  inT32 best_cluster; // one to assign to
334  inT32 new_centre = 0; // residual mode
335  inT32 new_mode; // pile count of new_centre
336  inT32 count; // pile to place
337  float dist; // from cluster
338  float min_dist; // from best_cluster
339  inT32 cluster_count; // no of clusters
340 
341  if (buckets_ == NULL || max_clusters < 1)
342  return 0;
343  centres = new float[max_clusters + 1];
344  for (cluster_count = 1; cluster_count <= max_clusters
345  && clusters[cluster_count].buckets_ != NULL
346  && clusters[cluster_count].total_count_ > 0;
347  cluster_count++) {
348  centres[cluster_count] =
349  static_cast<float>(clusters[cluster_count].ile(0.5));
350  new_centre = clusters[cluster_count].mode();
351  for (entry = new_centre - 1; centres[cluster_count] - entry < lower
352  && entry >= rangemin_
353  && pile_count(entry) <= pile_count(entry + 1);
354  entry--) {
355  count = pile_count(entry) - clusters[0].pile_count(entry);
356  if (count > 0) {
357  clusters[cluster_count].add(entry, count);
358  clusters[0].add (entry, count);
359  }
360  }
361  for (entry = new_centre + 1; entry - centres[cluster_count] < lower
362  && entry < rangemax_
363  && pile_count(entry) <= pile_count(entry - 1);
364  entry++) {
365  count = pile_count(entry) - clusters[0].pile_count(entry);
366  if (count > 0) {
367  clusters[cluster_count].add(entry, count);
368  clusters[0].add(entry, count);
369  }
370  }
371  }
372  cluster_count--;
373 
374  if (cluster_count == 0) {
375  clusters[0].set_range(rangemin_, rangemax_);
376  }
377  do {
378  new_cluster = FALSE;
379  new_mode = 0;
380  for (entry = 0; entry < rangemax_ - rangemin_; entry++) {
381  count = buckets_[entry] - clusters[0].buckets_[entry];
382  //remaining pile
383  if (count > 0) { //any to handle
384  min_dist = static_cast<float>(MAX_INT32);
385  best_cluster = 0;
386  for (cluster = 1; cluster <= cluster_count; cluster++) {
387  dist = entry + rangemin_ - centres[cluster];
388  //find distance
389  if (dist < 0)
390  dist = -dist;
391  if (dist < min_dist) {
392  min_dist = dist; //find least
393  best_cluster = cluster;
394  }
395  }
396  if (min_dist > upper //far enough for new
397  && (best_cluster == 0
398  || entry + rangemin_ > centres[best_cluster] * multiple
399  || entry + rangemin_ < centres[best_cluster] / multiple)) {
400  if (count > new_mode) {
401  new_mode = count;
402  new_centre = entry + rangemin_;
403  }
404  }
405  }
406  }
407  // need new and room
408  if (new_mode > 0 && cluster_count < max_clusters) {
409  cluster_count++;
410  new_cluster = TRUE;
411  if (!clusters[cluster_count].set_range(rangemin_, rangemax_)) {
412  delete [] centres;
413  return 0;
414  }
415  centres[cluster_count] = static_cast<float>(new_centre);
416  clusters[cluster_count].add(new_centre, new_mode);
417  clusters[0].add(new_centre, new_mode);
418  for (entry = new_centre - 1; centres[cluster_count] - entry < lower
419  && entry >= rangemin_
420  && pile_count (entry) <= pile_count(entry + 1); entry--) {
421  count = pile_count(entry) - clusters[0].pile_count(entry);
422  if (count > 0) {
423  clusters[cluster_count].add(entry, count);
424  clusters[0].add(entry, count);
425  }
426  }
427  for (entry = new_centre + 1; entry - centres[cluster_count] < lower
428  && entry < rangemax_
429  && pile_count (entry) <= pile_count(entry - 1); entry++) {
430  count = pile_count(entry) - clusters[0].pile_count(entry);
431  if (count > 0) {
432  clusters[cluster_count].add(entry, count);
433  clusters[0].add (entry, count);
434  }
435  }
436  centres[cluster_count] =
437  static_cast<float>(clusters[cluster_count].ile(0.5));
438  }
439  } while (new_cluster && cluster_count < max_clusters);
440  delete [] centres;
441  return cluster_count;
442 }
inT32 cluster(float lower, float upper, float multiple, inT32 max_clusters, STATS *clusters)
Definition: statistc.cpp:324
inT32 mode() const
Definition: statistc.cpp:118
void add(inT32 value, inT32 count)
Definition: statistc.cpp:104
unsigned char BOOL8
Definition: host.h:113
double ile(double frac) const
Definition: statistc.cpp:177
#define MAX_INT32
Definition: host.h:120
#define FALSE
Definition: capi.h:29
int count(LIST var_list)
Definition: oldlist.cpp:108
#define TRUE
Definition: capi.h:28
bool set_range(inT32 min_bucket_value, inT32 max_bucket_value_plus_1)
Definition: statistc.cpp:62
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 STATS::get_total ( ) const
inline

Definition at line 86 of file statistc.h.

86  {
87  return total_count_; // total of all piles
88  }
double STATS::ile ( double  frac) const

Definition at line 177 of file statistc.cpp.

177  {
178  if (buckets_ == NULL || total_count_ == 0) {
179  return static_cast<double>(rangemin_);
180  }
181 #if 0
182  // TODO(rays) The existing code doesn't seem to be doing the right thing
183  // with target a double but this substitute crashes the code that uses it.
184  // Investigate and fix properly.
185  int target = IntCastRounded(frac * total_count_);
186  target = ClipToRange(target, 1, total_count_);
187 #else
188  double target = frac * total_count_;
189  target = ClipToRange(target, 1.0, static_cast<double>(total_count_));
190 #endif
191  int sum = 0;
192  int index = 0;
193  for (index = 0; index < rangemax_ - rangemin_ && sum < target;
194  sum += buckets_[index++]);
195  if (index > 0) {
196  ASSERT_HOST(buckets_[index - 1] > 0);
197  return rangemin_ + index -
198  static_cast<double>(sum - target) / buckets_[index - 1];
199  } else {
200  return static_cast<double>(rangemin_);
201  }
202 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
int IntCastRounded(double x)
Definition: helpers.h:172
#define NULL
Definition: host.h:144
bool STATS::local_min ( inT32  x) const

Definition at line 266 of file statistc.cpp.

266  {
267  if (buckets_ == NULL) {
268  return false;
269  }
270  x = ClipToRange(x, rangemin_, rangemax_ - 1) - rangemin_;
271  if (buckets_[x] == 0)
272  return true;
273  inT32 index; // table index
274  for (index = x - 1; index >= 0 && buckets_[index] == buckets_[x]; --index);
275  if (index >= 0 && buckets_[index] < buckets_[x])
276  return false;
277  for (index = x + 1; index < rangemax_ - rangemin_ &&
278  buckets_[index] == buckets_[x]; ++index);
279  if (index < rangemax_ - rangemin_ && buckets_[index] < buckets_[x])
280  return false;
281  else
282  return true;
283 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 STATS::max_bucket ( ) const

Definition at line 225 of file statistc.cpp.

225  { // Find max
226  if (buckets_ == NULL || total_count_ == 0) {
227  return rangemin_;
228  }
229  inT32 max;
230  for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
231  return rangemin_ + max;
232 }
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
double STATS::mean ( ) const

Definition at line 138 of file statistc.cpp.

138  { //get mean of samples
139  if (buckets_ == NULL || total_count_ <= 0) {
140  return static_cast<double>(rangemin_);
141  }
142  inT64 sum = 0;
143  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
144  sum += static_cast<inT64>(index) * buckets_[index];
145  }
146  return static_cast<double>(sum) / total_count_ + rangemin_;
147 }
#define NULL
Definition: host.h:144
long long int inT64
Definition: host.h:108
double STATS::median ( ) const

Definition at line 243 of file statistc.cpp.

243  { //get median
244  if (buckets_ == NULL) {
245  return static_cast<double>(rangemin_);
246  }
247  double median = ile(0.5);
248  int median_pile = static_cast<int>(floor(median));
249  if ((total_count_ > 1) && (pile_count(median_pile) == 0)) {
250  inT32 min_pile;
251  inT32 max_pile;
252  /* Find preceeding non zero pile */
253  for (min_pile = median_pile; pile_count(min_pile) == 0; min_pile--);
254  /* Find following non zero pile */
255  for (max_pile = median_pile; pile_count(max_pile) == 0; max_pile++);
256  median = (min_pile + max_pile) / 2.0;
257  }
258  return median;
259 }
double median() const
Definition: statistc.cpp:243
double ile(double frac) const
Definition: statistc.cpp:177
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 STATS::min_bucket ( ) const

Definition at line 209 of file statistc.cpp.

209  { // Find min
210  if (buckets_ == NULL || total_count_ == 0) {
211  return rangemin_;
212  }
213  inT32 min = 0;
214  for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
215  return rangemin_ + min;
216 }
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 STATS::mode ( ) const

Definition at line 118 of file statistc.cpp.

118  { // get mode of samples
119  if (buckets_ == NULL) {
120  return rangemin_;
121  }
122  inT32 max = buckets_[0]; // max cell count
123  inT32 maxindex = 0; // index of max
124  for (int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
125  if (buckets_[index] > max) {
126  max = buckets_[index]; // find biggest
127  maxindex = index;
128  }
129  }
130  return maxindex + rangemin_; // index of biggest
131 }
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
inT32 STATS::pile_count ( inT32  value) const
inline

Definition at line 78 of file statistc.h.

78  {
79  if (value <= rangemin_)
80  return buckets_[0];
81  if (value >= rangemax_ - 1)
82  return buckets_[rangemax_ - rangemin_ - 1];
83  return buckets_[value - rangemin_];
84  }
void STATS::plot ( ScrollView window,
float  xorigin,
float  yorigin,
float  xscale,
float  yscale,
ScrollView::Color  colour 
) const

Definition at line 589 of file statistc.cpp.

594  { // colour to draw in
595  if (buckets_ == NULL) {
596  return;
597  }
598  window->Pen(colour);
599 
600  for (int index = 0; index < rangemax_ - rangemin_; index++) {
601  window->Rectangle( xorigin + xscale * index, yorigin,
602  xorigin + xscale * (index + 1),
603  yorigin + yscale * buckets_[index]);
604  }
605 }
void Pen(Color color)
Definition: scrollview.cpp:726
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
#define NULL
Definition: host.h:144
void STATS::plotline ( ScrollView window,
float  xorigin,
float  yorigin,
float  xscale,
float  yscale,
ScrollView::Color  colour 
) const

Definition at line 616 of file statistc.cpp.

621  { // colour to draw in
622  if (buckets_ == NULL) {
623  return;
624  }
625  window->Pen(colour);
626  window->SetCursor(xorigin, yorigin + yscale * buckets_[0]);
627  for (int index = 0; index < rangemax_ - rangemin_; index++) {
628  window->DrawTo(xorigin + xscale * index,
629  yorigin + yscale * buckets_[index]);
630  }
631 }
void Pen(Color color)
Definition: scrollview.cpp:726
void DrawTo(int x, int y)
Definition: scrollview.cpp:531
void SetCursor(int x, int y)
Definition: scrollview.cpp:525
#define NULL
Definition: host.h:144
void STATS::print ( ) const

Definition at line 538 of file statistc.cpp.

538  {
539  if (buckets_ == NULL) {
540  return;
541  }
542  inT32 min = min_bucket() - rangemin_;
543  inT32 max = max_bucket() - rangemin_;
544 
545  int num_printed = 0;
546  for (int index = min; index <= max; index++) {
547  if (buckets_[index] != 0) {
548  tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
549  if (++num_printed % 8 == 0)
550  tprintf ("\n");
551  }
552  }
553  tprintf ("\n");
554  print_summary();
555 }
inT32 min_bucket() const
Definition: statistc.cpp:209
void print_summary() const
Definition: statistc.cpp:564
#define tprintf(...)
Definition: tprintf.h:31
inT32 max_bucket() const
Definition: statistc.cpp:225
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
void STATS::print_summary ( ) const

Definition at line 564 of file statistc.cpp.

564  {
565  if (buckets_ == NULL) {
566  return;
567  }
568  inT32 min = min_bucket();
569  inT32 max = max_bucket();
570  tprintf("Total count=%d\n", total_count_);
571  tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
572  tprintf("Lower quartile=%.2f\n", ile(0.25));
573  tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
574  tprintf("Upper quartile=%.2f\n", ile(0.75));
575  tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
576  tprintf("Range=%d\n", max + 1 - min);
577  tprintf("Mean= %.2f\n", mean());
578  tprintf("SD= %.2f\n", sd());
579 }
inT32 min_bucket() const
Definition: statistc.cpp:209
double sd() const
Definition: statistc.cpp:154
#define tprintf(...)
Definition: tprintf.h:31
inT32 max_bucket() const
Definition: statistc.cpp:225
double mean() const
Definition: statistc.cpp:138
double median() const
Definition: statistc.cpp:243
double ile(double frac) const
Definition: statistc.cpp:177
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
double STATS::sd ( ) const

Definition at line 154 of file statistc.cpp.

154  { //standard deviation
155  if (buckets_ == NULL || total_count_ <= 0) {
156  return 0.0;
157  }
158  inT64 sum = 0;
159  double sqsum = 0.0;
160  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
161  sum += static_cast<inT64>(index) * buckets_[index];
162  sqsum += static_cast<double>(index) * index * buckets_[index];
163  }
164  double variance = static_cast<double>(sum) / total_count_;
165  variance = sqsum / total_count_ - variance * variance;
166  if (variance > 0.0)
167  return sqrt(variance);
168  return 0.0;
169 }
#define NULL
Definition: host.h:144
long long int inT64
Definition: host.h:108
bool STATS::set_range ( inT32  min_bucket_value,
inT32  max_bucket_value_plus_1 
)

Definition at line 62 of file statistc.cpp.

62  {
63  if (max_bucket_value_plus_1 <= min_bucket_value) {
64  return false;
65  }
66  if (rangemax_ - rangemin_ != max_bucket_value_plus_1 - min_bucket_value) {
67  delete [] buckets_;
68  buckets_ = new inT32[max_bucket_value_plus_1 - min_bucket_value];
69  }
70  rangemin_ = min_bucket_value; // setup
71  rangemax_ = max_bucket_value_plus_1;
72  clear(); // zero it
73  return true;
74 }
void clear()
Definition: statistc.cpp:81
int inT32
Definition: host.h:102
void STATS::smooth ( inT32  factor)

Definition at line 293 of file statistc.cpp.

293  {
294  if (buckets_ == NULL || factor < 2) {
295  return;
296  }
297  STATS result(rangemin_, rangemax_);
298  int entrycount = rangemax_ - rangemin_;
299  for (int entry = 0; entry < entrycount; entry++) {
300  //centre weight
301  int count = buckets_[entry] * factor;
302  for (int offset = 1; offset < factor; offset++) {
303  if (entry - offset >= 0)
304  count += buckets_[entry - offset] * (factor - offset);
305  if (entry + offset < entrycount)
306  count += buckets_[entry + offset] * (factor - offset);
307  }
308  result.add(entry + rangemin_, count);
309  }
310  total_count_ = result.total_count_;
311  memcpy(buckets_, result.buckets_, entrycount * sizeof(buckets_[0]));
312 }
Definition: statistc.h:33
int count(LIST var_list)
Definition: oldlist.cpp:108
#define NULL
Definition: host.h:144
int STATS::top_n_modes ( int  max_modes,
GenericVector< tesseract::KDPairInc< float, int > > *  modes 
) const

Definition at line 473 of file statistc.cpp.

474  {
475  if (max_modes <= 0) return 0;
476  int src_count = rangemax_ - rangemin_;
477  // Used copies the counts in buckets_ as they get used.
478  STATS used(rangemin_, rangemax_);
479  modes->truncate(0);
480  // Total count of the smallest peak found so far.
481  int least_count = 1;
482  // Mode that is used as a seed for each peak
483  int max_count = 0;
484  do {
485  // Find an unused mode.
486  max_count = 0;
487  int max_index = 0;
488  for (int src_index = 0; src_index < src_count; src_index++) {
489  int pile_count = buckets_[src_index] - used.buckets_[src_index];
490  if (pile_count > max_count) {
491  max_count = pile_count;
492  max_index = src_index;
493  }
494  }
495  if (max_count > 0) {
496  // Copy the bucket count to used so it doesn't get found again.
497  used.buckets_[max_index] = max_count;
498  // Get the entire peak.
499  double total_value = max_index * max_count;
500  int total_count = max_count;
501  int prev_pile = max_count;
502  for (int offset = 1; max_index + offset < src_count; ++offset) {
503  if (!GatherPeak(max_index + offset, buckets_, used.buckets_,
504  &prev_pile, &total_count, &total_value))
505  break;
506  }
507  prev_pile = buckets_[max_index];
508  for (int offset = 1; max_index - offset >= 0; ++offset) {
509  if (!GatherPeak(max_index - offset, buckets_, used.buckets_,
510  &prev_pile, &total_count, &total_value))
511  break;
512  }
513  if (total_count > least_count || modes->size() < max_modes) {
514  // We definitely want this mode, so if we have enough discard the least.
515  if (modes->size() == max_modes)
516  modes->truncate(max_modes - 1);
517  int target_index = 0;
518  // Linear search for the target insertion point.
519  while (target_index < modes->size() &&
520  (*modes)[target_index].data >= total_count)
521  ++target_index;
522  float peak_mean =
523  static_cast<float>(total_value / total_count + rangemin_);
524  modes->insert(KDPairInc<float, int>(peak_mean, total_count),
525  target_index);
526  least_count = modes->back().data;
527  }
528  }
529  } while (max_count > 0);
530  return modes->size();
531 }
int size() const
Definition: genericvector.h:72
void truncate(int size)
Definition: statistc.h:33
T & back() const
void insert(T t, int index)
inT32 pile_count(inT32 value) const
Definition: statistc.h:78

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