tesseract  4.00.00dev
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() [1/2]

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 }
int32_t inT32
Definition: host.h:38
void clear()
Definition: statistc.cpp:81

◆ STATS() [2/2]

STATS::STATS ( )

Definition at line 51 of file statistc.cpp.

51  {
52  rangemax_ = 0;
53  rangemin_ = 0;
54  buckets_ = NULL;
55 }

◆ ~STATS()

STATS::~STATS ( )

Definition at line 92 of file statistc.cpp.

92  {
93  delete [] buckets_;
94 }

Member Function Documentation

◆ add()

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

Definition at line 101 of file statistc.cpp.

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

◆ clear()

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 }

◆ cluster()

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

Definition at line 320 of file statistc.cpp.

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

◆ get_total()

inT32 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 174 of file statistc.cpp.

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

◆ local_min()

bool STATS::local_min ( inT32  x) const

Definition at line 262 of file statistc.cpp.

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

◆ max_bucket()

inT32 STATS::max_bucket ( ) const

Definition at line 221 of file statistc.cpp.

221  { // Find max
222  if (buckets_ == NULL || total_count_ == 0) {
223  return rangemin_;
224  }
225  inT32 max;
226  for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
227  return rangemin_ + max;
228 }
int32_t inT32
Definition: host.h:38
const int max

◆ mean()

double STATS::mean ( ) const

Definition at line 135 of file statistc.cpp.

135  { //get mean of samples
136  if (buckets_ == NULL || total_count_ <= 0) {
137  return static_cast<double>(rangemin_);
138  }
139  inT64 sum = 0;
140  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
141  sum += static_cast<inT64>(index) * buckets_[index];
142  }
143  return static_cast<double>(sum) / total_count_ + rangemin_;
144 }
int64_t inT64
Definition: host.h:40

◆ median()

double STATS::median ( ) const

Definition at line 239 of file statistc.cpp.

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

◆ min_bucket()

inT32 STATS::min_bucket ( ) const

Definition at line 206 of file statistc.cpp.

206  { // Find min
207  if (buckets_ == NULL || total_count_ == 0) {
208  return rangemin_;
209  }
210  inT32 min = 0;
211  for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
212  return rangemin_ + min;
213 }
int32_t inT32
Definition: host.h:38

◆ mode()

inT32 STATS::mode ( ) const

Definition at line 115 of file statistc.cpp.

115  { // get mode of samples
116  if (buckets_ == NULL) {
117  return rangemin_;
118  }
119  inT32 max = buckets_[0]; // max cell count
120  inT32 maxindex = 0; // index of max
121  for (int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
122  if (buckets_[index] > max) {
123  max = buckets_[index]; // find biggest
124  maxindex = index;
125  }
126  }
127  return maxindex + rangemin_; // index of biggest
128 }
int32_t inT32
Definition: host.h:38
const int max

◆ pile_count()

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  }

◆ plot()

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

Definition at line 585 of file statistc.cpp.

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

◆ plotline()

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

Definition at line 612 of file statistc.cpp.

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

◆ print()

void STATS::print ( ) const

Definition at line 534 of file statistc.cpp.

534  {
535  if (buckets_ == NULL) {
536  return;
537  }
538  inT32 min = min_bucket() - rangemin_;
539  inT32 max = max_bucket() - rangemin_;
540 
541  int num_printed = 0;
542  for (int index = min; index <= max; index++) {
543  if (buckets_[index] != 0) {
544  tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
545  if (++num_printed % 8 == 0)
546  tprintf ("\n");
547  }
548  }
549  tprintf ("\n");
550  print_summary();
551 }
int32_t inT32
Definition: host.h:38
#define tprintf(...)
Definition: tprintf.h:31
void print_summary() const
Definition: statistc.cpp:560
inT32 min_bucket() const
Definition: statistc.cpp:206
const int max
inT32 max_bucket() const
Definition: statistc.cpp:221

◆ print_summary()

void STATS::print_summary ( ) const

Definition at line 560 of file statistc.cpp.

560  {
561  if (buckets_ == NULL) {
562  return;
563  }
564  inT32 min = min_bucket();
565  inT32 max = max_bucket();
566  tprintf("Total count=%d\n", total_count_);
567  tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
568  tprintf("Lower quartile=%.2f\n", ile(0.25));
569  tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
570  tprintf("Upper quartile=%.2f\n", ile(0.75));
571  tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
572  tprintf("Range=%d\n", max + 1 - min);
573  tprintf("Mean= %.2f\n", mean());
574  tprintf("SD= %.2f\n", sd());
575 }
int32_t inT32
Definition: host.h:38
#define tprintf(...)
Definition: tprintf.h:31
double median() const
Definition: statistc.cpp:239
inT32 min_bucket() const
Definition: statistc.cpp:206
const int max
double ile(double frac) const
Definition: statistc.cpp:174
double mean() const
Definition: statistc.cpp:135
double sd() const
Definition: statistc.cpp:151
inT32 max_bucket() const
Definition: statistc.cpp:221

◆ sd()

double STATS::sd ( ) const

Definition at line 151 of file statistc.cpp.

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

◆ set_range()

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 }
int32_t inT32
Definition: host.h:38
void clear()
Definition: statistc.cpp:81

◆ smooth()

void STATS::smooth ( inT32  factor)

Definition at line 289 of file statistc.cpp.

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

◆ top_n_modes()

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

Definition at line 469 of file statistc.cpp.

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

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