1 from __future__
import division, absolute_import
7 __all__ = [
"RunningStats"]
10 """!Keep a running record of some value and return statistics on request
12 def __init__(self, bufferSize=1000, callEvery=500, callFunc=None):
13 """!Construct a RunningStats
15 @param[in] bufferSize number of data points on which to compute statistics
16 @param[in] callEvery after this many items are added, call callFunc (if specified)
17 @param[in] callFunc callback function to call after callEvery items are added, or None;
18 if specified, must accept one argument: this RunningStats object
20 self.
_buffer = collections.deque(maxlen=bufferSize)
24 if callEvery > bufferSize:
25 raise RuntimeError(
"callEvery=%s > %s=bufferSize" % (callEvery, bufferSize))
28 """!Add a measurement and increment the counter
30 self._buffer.append(val)
36 """!Clear the buffer and reset the counter
46 """!Return statistics and reset the counter
48 @return a dict containing:
49 - counter: number of value added since last call to clear or getStats
50 - num: number of values in the buffer
51 - min: minimum value in buffer
52 - max: maximum value in buffer
53 - median: median value in buffer
54 - stdDev: standard deviation of values in buffer
61 median = numpy.median(self.
_buffer),
62 stdDev = numpy.std(self.
_buffer),
def getStats
Return statistics and reset the counter.
Keep a running record of some value and return statistics on request.
def __init__
Construct a RunningStats.
def addValue
Add a measurement and increment the counter.
def clear
Clear the buffer and reset the counter.