Histograms.Mesa
Copyright © 1985 by Xerox Corporation. All rights reserved.
Mike Spreitzer September 27, 1986 4:28:18 pm PDT
A Histogram is a simple 1D or 2D data collector.
A 1D Histogram:
is a sequence of counters addressed by an integer I in the range [0..IMax). IMax is automatically maintained to be about as big as needed. IMax cells are actually allocated, so keep IMax within reason.
A 2D Histogram:
is a 2D array of counters addressed by two integers <I, J>, where their bounds are specified at creation time. Again, all counters are allocated, so keep bounds reasonable.
DIRECTORY IO, Rope, ViewerClasses;
Histograms: CEDAR DEFINITIONS = {
ROPE: TYPE = Rope.ROPE;
Histogram: TYPE = REF HistogramRep;
HistogramRep: TYPE;
Create1D: PROC--makes a 1D histogram
[
factor: REAL ← 1.0, --center x = (I+0.5)*factor + offset
offset: REAL ← -0.5]
RETURNS [Histogram];
Make a new one. All counters start at zero.
Create2D: PROC
[
iMin, iMax, jMin, jMax: INT, --bounds are inclusive on both ends
iFactor, jFactor: REAL ← 1.0, --center x = (I+0.5)*iFactor + iOffset
iOffset, jOffset: REAL ← 0.0] --center y = (J+0.5)*jFactor + jOffset
RETURNS [Histogram];
Increment: PROC [h: Histogram, i: NAT] =
INLINE {Change[h, i, 1]};
increment counter number i.
Decrement: PROC [h: Histogram, i: NAT] =
INLINE {Change[h, i, -1]};
decrement counter number i:
Change: PROC [h: Histogram, who: NAT, howMuch: INTEGER];
Change counter number who. Works only for 1D histograms.
IncrementTransformed: PROC [h: Histogram, xmin, xmax, x: REAL];
Force x inside [xmin .. xmax], inverse transform by factor & offset, round, and Increment.
ChangeTransformed: PROC [h: Histogram, x: REAL, y: REAL ← 0.0, delta: INTEGER ← 1];
Works for 1D or 2D Histograms.
ClearAll: PROC [h: Histogram];
Sets all counters to 0.
Stats1D: PROC [h: Histogram] RETURNS [sum0: INT, sum1, sum2, avg, stdDev, xmin, xmax: REAL];
Consider the histogram a set of <x, n> pairs, each saying that there are n counts in the bin centered on x.
sum0 = S n
sum1 = S n*x
sum2 = S n*x2
avg = sum1/sum0
stdDev = SqRt[( S n*(x - avg)2 ) / ((Sn) - 1)]
xmin = least x with non-zero n
xmax = greatest x with non-zero n
xmin>xmax { sum0=0
Stats2D: PROC [h: Histogram] RETURNS [sum0: INT, sumx, sumy: REAL];
WriteTo: PROC [h: Histogram, to: IO.STREAM];
list non-zero counters
Show: PROC [
h: Histogram,
viewerInit: ViewerClasses.ViewerRec ← [],
format: ROPENIL, --NIL means "%d"
width: NAT ← 0, --max number of chars produced when formatting
updatePeriod: REAL ← -1.0, --If > 0, every updatePeriod seconds the viewer is repainted if it's out of date. If updatePeriod=0, the viewer is painted every time something changes. If updatePeriod<0, the viewer is never automatically repainted.
paint: BOOLTRUE] RETURNS [ViewerClasses.Viewer];
display it in a viewer
Error: ERROR [msg: ROPE];
}.