Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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)
 
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 29 of file statistc.h.

Constructor & Destructor Documentation

STATS::STATS ( inT32  min_bucket_value,
inT32  max_bucket_value_plus_1 
)

Definition at line 39 of file statistc.cpp.

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

Definition at line 50 of file statistc.cpp.

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

Definition at line 91 of file statistc.cpp.

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

Member Function Documentation

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

Definition at line 103 of file statistc.cpp.

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

Definition at line 80 of file statistc.cpp.

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

Definition at line 323 of file statistc.cpp.

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

Definition at line 82 of file statistc.h.

82  {
83  return total_count_; // total of all piles
84  }
double STATS::ile ( double  frac) const

Definition at line 176 of file statistc.cpp.

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

Definition at line 265 of file statistc.cpp.

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

Definition at line 224 of file statistc.cpp.

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

Definition at line 137 of file statistc.cpp.

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

Definition at line 242 of file statistc.cpp.

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

Definition at line 208 of file statistc.cpp.

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

Definition at line 117 of file statistc.cpp.

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

Definition at line 74 of file statistc.h.

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

Definition at line 497 of file statistc.cpp.

502  { // colour to draw in
503  if (buckets_ == NULL) {
504  return;
505  }
506  window->Pen(colour);
507 
508  for (int index = 0; index < rangemax_ - rangemin_; index++) {
509  window->Rectangle( xorigin + xscale * index, yorigin,
510  xorigin + xscale * (index + 1),
511  yorigin + yscale * buckets_[index]);
512  }
513 }
void Pen(Color color)
Definition: scrollview.cpp:721
#define NULL
Definition: host.h:144
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:601
void STATS::plotline ( ScrollView window,
float  xorigin,
float  yorigin,
float  xscale,
float  yscale,
ScrollView::Color  colour 
) const

Definition at line 524 of file statistc.cpp.

529  { // colour to draw in
530  if (buckets_ == NULL) {
531  return;
532  }
533  window->Pen(colour);
534  window->SetCursor(xorigin, yorigin + yscale * buckets_[0]);
535  for (int index = 0; index < rangemax_ - rangemin_; index++) {
536  window->DrawTo(xorigin + xscale * index,
537  yorigin + yscale * buckets_[index]);
538  }
539 }
void SetCursor(int x, int y)
Definition: scrollview.cpp:520
void Pen(Color color)
Definition: scrollview.cpp:721
#define NULL
Definition: host.h:144
void DrawTo(int x, int y)
Definition: scrollview.cpp:526
void STATS::print ( ) const

Definition at line 446 of file statistc.cpp.

446  {
447  if (buckets_ == NULL) {
448  return;
449  }
450  inT32 min = min_bucket() - rangemin_;
451  inT32 max = max_bucket() - rangemin_;
452 
453  int num_printed = 0;
454  for (int index = min; index <= max; index++) {
455  if (buckets_[index] != 0) {
456  tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
457  if (++num_printed % 8 == 0)
458  tprintf ("\n");
459  }
460  }
461  tprintf ("\n");
462  print_summary();
463 }
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void print_summary() const
Definition: statistc.cpp:472
inT32 min_bucket() const
Definition: statistc.cpp:208
inT32 max_bucket() const
Definition: statistc.cpp:224
void STATS::print_summary ( ) const

Definition at line 472 of file statistc.cpp.

472  {
473  if (buckets_ == NULL) {
474  return;
475  }
476  inT32 min = min_bucket();
477  inT32 max = max_bucket();
478  tprintf("Total count=%d\n", total_count_);
479  tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
480  tprintf("Lower quartile=%.2f\n", ile(0.25));
481  tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
482  tprintf("Upper quartile=%.2f\n", ile(0.75));
483  tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
484  tprintf("Range=%d\n", max + 1 - min);
485  tprintf("Mean= %.2f\n", mean());
486  tprintf("SD= %.2f\n", sd());
487 }
double mean() const
Definition: statistc.cpp:137
#define NULL
Definition: host.h:144
int inT32
Definition: host.h:102
double ile(double frac) const
Definition: statistc.cpp:176
double sd() const
Definition: statistc.cpp:153
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
double median() const
Definition: statistc.cpp:242
inT32 min_bucket() const
Definition: statistc.cpp:208
inT32 max_bucket() const
Definition: statistc.cpp:224
double STATS::sd ( ) const

Definition at line 153 of file statistc.cpp.

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

Definition at line 61 of file statistc.cpp.

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

Definition at line 292 of file statistc.cpp.

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

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