tesseract  4.0.0-1-g2a2b
STATS Class Reference

#include <statistc.h>

Public Member Functions

 STATS (int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
 
 STATS ()
 
 ~STATS ()
 
bool set_range (int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
 
void clear ()
 
void add (int32_t value, int32_t count)
 
int32_t mode () const
 
double mean () const
 
double sd () const
 
double ile (double frac) const
 
int32_t min_bucket () const
 
int32_t max_bucket () const
 
double median () const
 
int32_t pile_count (int32_t value) const
 
int32_t get_total () const
 
bool local_min (int32_t x) const
 
void smooth (int32_t factor)
 
int32_t cluster (float lower, float upper, float multiple, int32_t 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() [1/2]

STATS::STATS ( int32_t  min_bucket_value,
int32_t  max_bucket_value_plus_1 
)

Definition at line 41 of file statistc.cpp.

41  {
42  if (max_bucket_value_plus_1 <= min_bucket_value) {
43  min_bucket_value = 0;
44  max_bucket_value_plus_1 = 1;
45  }
46  rangemin_ = min_bucket_value; // setup
47  rangemax_ = max_bucket_value_plus_1;
48  buckets_ = new int32_t[rangemax_ - rangemin_];
49  clear();
50 }
void clear()
Definition: statistc.cpp:82

◆ STATS() [2/2]

STATS::STATS ( )

Definition at line 52 of file statistc.cpp.

52  {
53  rangemax_ = 0;
54  rangemin_ = 0;
55  buckets_ = nullptr;
56 }

◆ ~STATS()

STATS::~STATS ( )

Definition at line 93 of file statistc.cpp.

93 { delete[] buckets_; }

Member Function Documentation

◆ add()

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

Definition at line 100 of file statistc.cpp.

100  {
101  if (buckets_ == nullptr) {
102  return;
103  }
104  value = ClipToRange(value, rangemin_, rangemax_ - 1);
105  buckets_[value - rangemin_] += count;
106  total_count_ += count; // keep count of total
107 }
int count(LIST var_list)
Definition: oldlist.cpp:98
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

◆ clear()

void STATS::clear ( )

Definition at line 82 of file statistc.cpp.

82  { // clear out buckets
83  total_count_ = 0;
84  if (buckets_ != nullptr)
85  memset(buckets_, 0, (rangemax_ - rangemin_) * sizeof(buckets_[0]));
86 }

◆ cluster()

int32_t STATS::cluster ( float  lower,
float  upper,
float  multiple,
int32_t  max_clusters,
STATS clusters 
)

Definition at line 319 of file statistc.cpp.

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

◆ get_total()

int32_t STATS::get_total ( ) const
inline

Definition at line 86 of file statistc.h.

86  {
87  return total_count_; // total of all piles
88  }

◆ ile()

double STATS::ile ( double  frac) const

Definition at line 173 of file statistc.cpp.

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

◆ local_min()

bool STATS::local_min ( int32_t  x) const

Definition at line 261 of file statistc.cpp.

261  {
262  if (buckets_ == nullptr) {
263  return false;
264  }
265  x = ClipToRange(x, rangemin_, rangemax_ - 1) - rangemin_;
266  if (buckets_[x] == 0)
267  return true;
268  int32_t index; // table index
269  for (index = x - 1; index >= 0 && buckets_[index] == buckets_[x]; --index);
270  if (index >= 0 && buckets_[index] < buckets_[x])
271  return false;
272  for (index = x + 1; index < rangemax_ - rangemin_ &&
273  buckets_[index] == buckets_[x]; ++index);
274  if (index < rangemax_ - rangemin_ && buckets_[index] < buckets_[x])
275  return false;
276  else
277  return true;
278 }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

◆ max_bucket()

int32_t STATS::max_bucket ( ) const

Definition at line 220 of file statistc.cpp.

220  { // Find max
221  if (buckets_ == nullptr || total_count_ == 0) {
222  return rangemin_;
223  }
224  int32_t max;
225  for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
226  return rangemin_ + max;
227 }

◆ mean()

double STATS::mean ( ) const

Definition at line 134 of file statistc.cpp.

134  { //get mean of samples
135  if (buckets_ == nullptr || total_count_ <= 0) {
136  return static_cast<double>(rangemin_);
137  }
138  int64_t sum = 0;
139  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
140  sum += static_cast<int64_t>(index) * buckets_[index];
141  }
142  return static_cast<double>(sum) / total_count_ + rangemin_;
143 }

◆ median()

double STATS::median ( ) const

Definition at line 238 of file statistc.cpp.

238  { //get median
239  if (buckets_ == nullptr) {
240  return static_cast<double>(rangemin_);
241  }
242  double median = ile(0.5);
243  int median_pile = static_cast<int>(floor(median));
244  if ((total_count_ > 1) && (pile_count(median_pile) == 0)) {
245  int32_t min_pile;
246  int32_t max_pile;
247  /* Find preceding non zero pile */
248  for (min_pile = median_pile; pile_count(min_pile) == 0; min_pile--);
249  /* Find following non zero pile */
250  for (max_pile = median_pile; pile_count(max_pile) == 0; max_pile++);
251  median = (min_pile + max_pile) / 2.0;
252  }
253  return median;
254 }
int32_t pile_count(int32_t value) const
Definition: statistc.h:78
double median() const
Definition: statistc.cpp:238
double ile(double frac) const
Definition: statistc.cpp:173

◆ min_bucket()

int32_t STATS::min_bucket ( ) const

Definition at line 205 of file statistc.cpp.

205  { // Find min
206  if (buckets_ == nullptr || total_count_ == 0) {
207  return rangemin_;
208  }
209  int32_t min = 0;
210  for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
211  return rangemin_ + min;
212 }

◆ mode()

int32_t STATS::mode ( ) const

Definition at line 114 of file statistc.cpp.

114  { // get mode of samples
115  if (buckets_ == nullptr) {
116  return rangemin_;
117  }
118  int32_t max = buckets_[0]; // max cell count
119  int32_t maxindex = 0; // index of max
120  for (int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
121  if (buckets_[index] > max) {
122  max = buckets_[index]; // find biggest
123  maxindex = index;
124  }
125  }
126  return maxindex + rangemin_; // index of biggest
127 }

◆ pile_count()

int32_t STATS::pile_count ( int32_t  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  }

◆ plot()

void STATS::plot ( ScrollView window,
float  xorigin,
float  yorigin,
float  xscale,
float  yscale,
ScrollView::Color  colour 
) const

Definition at line 584 of file statistc.cpp.

589  { // colour to draw in
590  if (buckets_ == nullptr) {
591  return;
592  }
593  window->Pen(colour);
594 
595  for (int index = 0; index < rangemax_ - rangemin_; index++) {
596  window->Rectangle(xorigin + xscale * index, yorigin,
597  xorigin + xscale * (index + 1),
598  yorigin + yscale * buckets_[index]);
599  }
600 }
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:602
void Pen(Color color)
Definition: scrollview.cpp:722

◆ plotline()

void STATS::plotline ( ScrollView window,
float  xorigin,
float  yorigin,
float  xscale,
float  yscale,
ScrollView::Color  colour 
) const

Definition at line 611 of file statistc.cpp.

616  { // colour to draw in
617  if (buckets_ == nullptr) {
618  return;
619  }
620  window->Pen(colour);
621  window->SetCursor(xorigin, yorigin + yscale * buckets_[0]);
622  for (int index = 0; index < rangemax_ - rangemin_; index++) {
623  window->DrawTo(xorigin + xscale * index,
624  yorigin + yscale * buckets_[index]);
625  }
626 }
void DrawTo(int x, int y)
Definition: scrollview.cpp:527
void SetCursor(int x, int y)
Definition: scrollview.cpp:521
void Pen(Color color)
Definition: scrollview.cpp:722

◆ print()

void STATS::print ( ) const

Definition at line 533 of file statistc.cpp.

533  {
534  if (buckets_ == nullptr) {
535  return;
536  }
537  int32_t min = min_bucket() - rangemin_;
538  int32_t max = max_bucket() - rangemin_;
539 
540  int num_printed = 0;
541  for (int index = min; index <= max; index++) {
542  if (buckets_[index] != 0) {
543  tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
544  if (++num_printed % 8 == 0)
545  tprintf ("\n");
546  }
547  }
548  tprintf ("\n");
549  print_summary();
550 }
int32_t min_bucket() const
Definition: statistc.cpp:205
int32_t max_bucket() const
Definition: statistc.cpp:220
void print_summary() const
Definition: statistc.cpp:559
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37

◆ print_summary()

void STATS::print_summary ( ) const

Definition at line 559 of file statistc.cpp.

559  {
560  if (buckets_ == nullptr) {
561  return;
562  }
563  int32_t min = min_bucket();
564  int32_t max = max_bucket();
565  tprintf("Total count=%d\n", total_count_);
566  tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
567  tprintf("Lower quartile=%.2f\n", ile(0.25));
568  tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
569  tprintf("Upper quartile=%.2f\n", ile(0.75));
570  tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
571  tprintf("Range=%d\n", max + 1 - min);
572  tprintf("Mean= %.2f\n", mean());
573  tprintf("SD= %.2f\n", sd());
574 }
int32_t min_bucket() const
Definition: statistc.cpp:205
int32_t max_bucket() const
Definition: statistc.cpp:220
double median() const
Definition: statistc.cpp:238
double mean() const
Definition: statistc.cpp:134
double ile(double frac) const
Definition: statistc.cpp:173
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
double sd() const
Definition: statistc.cpp:150

◆ sd()

double STATS::sd ( ) const

Definition at line 150 of file statistc.cpp.

150  { //standard deviation
151  if (buckets_ == nullptr || total_count_ <= 0) {
152  return 0.0;
153  }
154  int64_t sum = 0;
155  double sqsum = 0.0;
156  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
157  sum += static_cast<int64_t>(index) * buckets_[index];
158  sqsum += static_cast<double>(index) * index * buckets_[index];
159  }
160  double variance = static_cast<double>(sum) / total_count_;
161  variance = sqsum / total_count_ - variance * variance;
162  if (variance > 0.0)
163  return sqrt(variance);
164  return 0.0;
165 }

◆ set_range()

bool STATS::set_range ( int32_t  min_bucket_value,
int32_t  max_bucket_value_plus_1 
)

Definition at line 63 of file statistc.cpp.

63  {
64  if (max_bucket_value_plus_1 <= min_bucket_value) {
65  return false;
66  }
67  if (rangemax_ - rangemin_ != max_bucket_value_plus_1 - min_bucket_value) {
68  delete [] buckets_;
69  buckets_ = new int32_t[max_bucket_value_plus_1 - min_bucket_value];
70  }
71  rangemin_ = min_bucket_value; // setup
72  rangemax_ = max_bucket_value_plus_1;
73  clear(); // zero it
74  return true;
75 }
void clear()
Definition: statistc.cpp:82

◆ smooth()

void STATS::smooth ( int32_t  factor)

Definition at line 288 of file statistc.cpp.

288  {
289  if (buckets_ == nullptr || factor < 2) {
290  return;
291  }
292  STATS result(rangemin_, rangemax_);
293  int entrycount = rangemax_ - rangemin_;
294  for (int entry = 0; entry < entrycount; entry++) {
295  //centre weight
296  int count = buckets_[entry] * factor;
297  for (int offset = 1; offset < factor; offset++) {
298  if (entry - offset >= 0)
299  count += buckets_[entry - offset] * (factor - offset);
300  if (entry + offset < entrycount)
301  count += buckets_[entry + offset] * (factor - offset);
302  }
303  result.add(entry + rangemin_, count);
304  }
305  total_count_ = result.total_count_;
306  memcpy(buckets_, result.buckets_, entrycount * sizeof(buckets_[0]));
307 }
int count(LIST var_list)
Definition: oldlist.cpp:98
Definition: statistc.h:33

◆ top_n_modes()

int STATS::top_n_modes ( int  max_modes,
GenericVector< tesseract::KDPairInc< float, int > > *  modes 
) const

Definition at line 468 of file statistc.cpp.

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

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