tesseract 4.1.1
Loading...
Searching...
No Matches
STATS Class Reference

#include <statistc.h>

Public Member Functions

 STATS (int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
 
 STATS ()=default
 
 ~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 31 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 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_t[rangemax_ - rangemin_];
48 clear();
49}
void clear()
Definition: statistc.cpp:75

◆ STATS() [2/2]

STATS::STATS ( )
default

◆ ~STATS()

STATS::~STATS ( )

Definition at line 86 of file statistc.cpp.

86{ delete[] buckets_; }

Member Function Documentation

◆ add()

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

Definition at line 93 of file statistc.cpp.

93 {
94 if (buckets_ == nullptr) {
95 return;
96 }
97 value = ClipToRange(value, rangemin_, rangemax_ - 1);
98 buckets_[value - rangemin_] += count;
99 total_count_ += count; // keep count of total
100}
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108
int count(LIST var_list)
Definition: oldlist.cpp:95

◆ clear()

void STATS::clear ( )

Definition at line 75 of file statistc.cpp.

75 { // clear out buckets
76 total_count_ = 0;
77 if (buckets_ != nullptr)
78 memset(buckets_, 0, (rangemax_ - rangemin_) * sizeof(buckets_[0]));
79}

◆ cluster()

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

Definition at line 312 of file statistc.cpp.

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

◆ get_total()

int32_t STATS::get_total ( ) const
inline

Definition at line 84 of file statistc.h.

84 {
85 return total_count_; // total of all piles
86 }

◆ ile()

double STATS::ile ( double  frac) const

Definition at line 166 of file statistc.cpp.

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

◆ local_min()

bool STATS::local_min ( int32_t  x) const

Definition at line 254 of file statistc.cpp.

254 {
255 if (buckets_ == nullptr) {
256 return false;
257 }
258 x = ClipToRange(x, rangemin_, rangemax_ - 1) - rangemin_;
259 if (buckets_[x] == 0)
260 return true;
261 int32_t index; // table index
262 for (index = x - 1; index >= 0 && buckets_[index] == buckets_[x]; --index);
263 if (index >= 0 && buckets_[index] < buckets_[x])
264 return false;
265 for (index = x + 1; index < rangemax_ - rangemin_ &&
266 buckets_[index] == buckets_[x]; ++index);
267 if (index < rangemax_ - rangemin_ && buckets_[index] < buckets_[x])
268 return false;
269 else
270 return true;
271}

◆ max_bucket()

int32_t STATS::max_bucket ( ) const

Definition at line 213 of file statistc.cpp.

213 { // Find max
214 if (buckets_ == nullptr || total_count_ == 0) {
215 return rangemin_;
216 }
217 int32_t max;
218 for (max = rangemax_ - rangemin_ - 1; max > 0 && buckets_[max] == 0; max--);
219 return rangemin_ + max;
220}

◆ mean()

double STATS::mean ( ) const

Definition at line 127 of file statistc.cpp.

127 { //get mean of samples
128 if (buckets_ == nullptr || total_count_ <= 0) {
129 return static_cast<double>(rangemin_);
130 }
131 int64_t sum = 0;
132 for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
133 sum += static_cast<int64_t>(index) * buckets_[index];
134 }
135 return static_cast<double>(sum) / total_count_ + rangemin_;
136}

◆ median()

double STATS::median ( ) const

Definition at line 231 of file statistc.cpp.

231 { //get median
232 if (buckets_ == nullptr) {
233 return static_cast<double>(rangemin_);
234 }
235 double median = ile(0.5);
236 int median_pile = static_cast<int>(floor(median));
237 if ((total_count_ > 1) && (pile_count(median_pile) == 0)) {
238 int32_t min_pile;
239 int32_t max_pile;
240 /* Find preceding non zero pile */
241 for (min_pile = median_pile; pile_count(min_pile) == 0; min_pile--);
242 /* Find following non zero pile */
243 for (max_pile = median_pile; pile_count(max_pile) == 0; max_pile++);
244 median = (min_pile + max_pile) / 2.0;
245 }
246 return median;
247}
double median() const
Definition: statistc.cpp:231

◆ min_bucket()

int32_t STATS::min_bucket ( ) const

Definition at line 198 of file statistc.cpp.

198 { // Find min
199 if (buckets_ == nullptr || total_count_ == 0) {
200 return rangemin_;
201 }
202 int32_t min = 0;
203 for (min = 0; (min < rangemax_ - rangemin_) && (buckets_[min] == 0); min++);
204 return rangemin_ + min;
205}

◆ mode()

int32_t STATS::mode ( ) const

Definition at line 107 of file statistc.cpp.

107 { // get mode of samples
108 if (buckets_ == nullptr) {
109 return rangemin_;
110 }
111 int32_t max = buckets_[0]; // max cell count
112 int32_t maxindex = 0; // index of max
113 for (int index = rangemax_ - rangemin_ - 1; index > 0; --index) {
114 if (buckets_[index] > max) {
115 max = buckets_[index]; // find biggest
116 maxindex = index;
117 }
118 }
119 return maxindex + rangemin_; // index of biggest
120}

◆ pile_count()

int32_t STATS::pile_count ( int32_t  value) const
inline

Definition at line 76 of file statistc.h.

76 {
77 if (value <= rangemin_)
78 return buckets_[0];
79 if (value >= rangemax_ - 1)
80 return buckets_[rangemax_ - rangemin_ - 1];
81 return buckets_[value - rangemin_];
82 }

◆ plot()

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

Definition at line 577 of file statistc.cpp.

582 { // colour to draw in
583 if (buckets_ == nullptr) {
584 return;
585 }
586 window->Pen(colour);
587
588 for (int index = 0; index < rangemax_ - rangemin_; index++) {
589 window->Rectangle(xorigin + xscale * index, yorigin,
590 xorigin + xscale * (index + 1),
591 yorigin + yscale * buckets_[index]);
592 }
593}
void Pen(Color color)
Definition: scrollview.cpp:719
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:600

◆ plotline()

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

Definition at line 604 of file statistc.cpp.

609 { // colour to draw in
610 if (buckets_ == nullptr) {
611 return;
612 }
613 window->Pen(colour);
614 window->SetCursor(xorigin, yorigin + yscale * buckets_[0]);
615 for (int index = 0; index < rangemax_ - rangemin_; index++) {
616 window->DrawTo(xorigin + xscale * index,
617 yorigin + yscale * buckets_[index]);
618 }
619}
void DrawTo(int x, int y)
Definition: scrollview.cpp:525
void SetCursor(int x, int y)
Definition: scrollview.cpp:519

◆ print()

void STATS::print ( ) const

Definition at line 526 of file statistc.cpp.

526 {
527 if (buckets_ == nullptr) {
528 return;
529 }
530 int32_t min = min_bucket() - rangemin_;
531 int32_t max = max_bucket() - rangemin_;
532
533 int num_printed = 0;
534 for (int index = min; index <= max; index++) {
535 if (buckets_[index] != 0) {
536 tprintf("%4d:%-3d ", rangemin_ + index, buckets_[index]);
537 if (++num_printed % 8 == 0)
538 tprintf ("\n");
539 }
540 }
541 tprintf ("\n");
543}
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
int32_t max_bucket() const
Definition: statistc.cpp:213
int32_t min_bucket() const
Definition: statistc.cpp:198
void print_summary() const
Definition: statistc.cpp:552

◆ print_summary()

void STATS::print_summary ( ) const

Definition at line 552 of file statistc.cpp.

552 {
553 if (buckets_ == nullptr) {
554 return;
555 }
556 int32_t min = min_bucket();
557 int32_t max = max_bucket();
558 tprintf("Total count=%d\n", total_count_);
559 tprintf("Min=%.2f Really=%d\n", ile(0.0), min);
560 tprintf("Lower quartile=%.2f\n", ile(0.25));
561 tprintf("Median=%.2f, ile(0.5)=%.2f\n", median(), ile(0.5));
562 tprintf("Upper quartile=%.2f\n", ile(0.75));
563 tprintf("Max=%.2f Really=%d\n", ile(1.0), max);
564 tprintf("Range=%d\n", max + 1 - min);
565 tprintf("Mean= %.2f\n", mean());
566 tprintf("SD= %.2f\n", sd());
567}
double mean() const
Definition: statistc.cpp:127
double sd() const
Definition: statistc.cpp:143

◆ sd()

double STATS::sd ( ) const

Definition at line 143 of file statistc.cpp.

143 { //standard deviation
144 if (buckets_ == nullptr || total_count_ <= 0) {
145 return 0.0;
146 }
147 int64_t sum = 0;
148 double sqsum = 0.0;
149 for (int index = rangemax_ - rangemin_ - 1; index >= 0; --index) {
150 sum += static_cast<int64_t>(index) * buckets_[index];
151 sqsum += static_cast<double>(index) * index * buckets_[index];
152 }
153 double variance = static_cast<double>(sum) / total_count_;
154 variance = sqsum / total_count_ - variance * variance;
155 if (variance > 0.0)
156 return sqrt(variance);
157 return 0.0;
158}

◆ set_range()

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

Definition at line 56 of file statistc.cpp.

56 {
57 if (max_bucket_value_plus_1 <= min_bucket_value) {
58 return false;
59 }
60 if (rangemax_ - rangemin_ != max_bucket_value_plus_1 - min_bucket_value) {
61 delete [] buckets_;
62 buckets_ = new int32_t[max_bucket_value_plus_1 - min_bucket_value];
63 }
64 rangemin_ = min_bucket_value; // setup
65 rangemax_ = max_bucket_value_plus_1;
66 clear(); // zero it
67 return true;
68}

◆ smooth()

void STATS::smooth ( int32_t  factor)

Definition at line 281 of file statistc.cpp.

281 {
282 if (buckets_ == nullptr || factor < 2) {
283 return;
284 }
285 STATS result(rangemin_, rangemax_);
286 int entrycount = rangemax_ - rangemin_;
287 for (int entry = 0; entry < entrycount; entry++) {
288 //centre weight
289 int count = buckets_[entry] * factor;
290 for (int offset = 1; offset < factor; offset++) {
291 if (entry - offset >= 0)
292 count += buckets_[entry - offset] * (factor - offset);
293 if (entry + offset < entrycount)
294 count += buckets_[entry + offset] * (factor - offset);
295 }
296 result.add(entry + rangemin_, count);
297 }
298 total_count_ = result.total_count_;
299 memcpy(buckets_, result.buckets_, entrycount * sizeof(buckets_[0]));
300}
Definition: statistc.h:31

◆ top_n_modes()

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

Definition at line 461 of file statistc.cpp.

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