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

◆ 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 { delete[] buckets_; }

Member Function Documentation

◆ add()

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

Definition at line 99 of file statistc.cpp.

99  {
100  if (buckets_ == NULL) {
101  return;
102  }
103  value = ClipToRange(value, rangemin_, rangemax_ - 1);
104  buckets_[value - rangemin_] += count;
105  total_count_ += count; // keep count of total
106 }
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 318 of file statistc.cpp.

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

◆ 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 172 of file statistc.cpp.

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

◆ local_min()

bool STATS::local_min ( inT32  x) const

Definition at line 260 of file statistc.cpp.

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

◆ max_bucket()

inT32 STATS::max_bucket ( ) const

Definition at line 219 of file statistc.cpp.

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

◆ mean()

double STATS::mean ( ) const

Definition at line 133 of file statistc.cpp.

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

◆ median()

double STATS::median ( ) const

Definition at line 237 of file statistc.cpp.

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

◆ min_bucket()

inT32 STATS::min_bucket ( ) const

Definition at line 204 of file statistc.cpp.

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

◆ mode()

inT32 STATS::mode ( ) const

Definition at line 113 of file statistc.cpp.

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

◆ 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 583 of file statistc.cpp.

588  { // colour to draw in
589  if (buckets_ == NULL) {
590  return;
591  }
592  window->Pen(colour);
593 
594  for (int index = 0; index < rangemax_ - rangemin_; index++) {
595  window->Rectangle( xorigin + xscale * index, yorigin,
596  xorigin + xscale * (index + 1),
597  yorigin + yscale * buckets_[index]);
598  }
599 }
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 610 of file statistc.cpp.

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

◆ print()

void STATS::print ( ) const

Definition at line 532 of file statistc.cpp.

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

◆ print_summary()

void STATS::print_summary ( ) const

Definition at line 558 of file statistc.cpp.

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

◆ sd()

double STATS::sd ( ) const

Definition at line 149 of file statistc.cpp.

149  { //standard deviation
150  if (buckets_ == NULL || total_count_ <= 0) {
151  return 0.0;
152  }
153  inT64 sum = 0;
154  double sqsum = 0.0;
155  for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
156  sum += static_cast<inT64>(index) * buckets_[index];
157  sqsum += static_cast<double>(index) * index * buckets_[index];
158  }
159  double variance = static_cast<double>(sum) / total_count_;
160  variance = sqsum / total_count_ - variance * variance;
161  if (variance > 0.0)
162  return sqrt(variance);
163  return 0.0;
164 }
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 }
void clear()
Definition: statistc.cpp:81
int32_t inT32
Definition: host.h:38

◆ smooth()

void STATS::smooth ( inT32  factor)

Definition at line 287 of file statistc.cpp.

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

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

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