Skip to content

Instantly share code, notes, and snippets.

@pydsigner
Created October 12, 2016 03:28
Show Gist options
  • Save pydsigner/b1f08c941e613daca0abbacb499f0415 to your computer and use it in GitHub Desktop.
Save pydsigner/b1f08c941e613daca0abbacb499f0415 to your computer and use it in GitHub Desktop.
VizStats Rework
class VizStats(NTPStats):
percs = {} # dictionary of percentages
unit = 's' # display units: s, ppm, etc.
multiplier = 1
percentile_map = {p: 0 for p in (99, 95, 50, 5, 1)}
range_map = {r: 0 for r in [(99, 1), (95, 5)]
mu = 0 # arithmetic mean
pstd = 0 # population standard distribution, one sigma
stats_html = ''
def __init__( self, values, freq=0 ):
values.sort()
self.percs = self.percentiles( (100, 99, 95, 50, 5, 1, 0), values)
# find the target for autoranging
# keep 99% and 5% under 999 in selected units
# but do not let 100% and 1% go over 5000 in selected units
target = max(self.percs[99], -self.percs[1], self.percs[100]/5,
-self.percs[0]/5)
ranging_map = [
(1, "ppm", "s")
(1e-3, "ppb", "ms")
(1e-6, "10e-12", "µs")
]
for pivot, freq_unit, time_unit in ranging_map:
if target >= pivot:
self.multiplier = 1 / pivot
self.unit = freq_unit if freq else time_unit
break
else:
self.multiplier = 1e6
self.unit = "10e-15" if freq else "ns"
self.percs.update({k: v * self.multiplier
for k, v in list(self.percs.items())})
self.percentile_map = {p: self.percs[p] for p in self.percentile_map}
self.range_map = {(high, low): self.percs[high] - self.percs[low]
for high, low in self.range_map}
self.mu = mean( values )
self.pstd = pstdev( values, mu=self.mu ) * self.multiplier
self.mu *= self.multiplier
sections = [
('Percentiles', [('%s%%' % k, v) for k, v in sorted(self.percentile_map.items(), reverse=True)]),
('Ranges', [('%s%% - %s%%' % k, v) for k, v in sorted(self.range_map.items(), reverse=True)]),
('Deviation', [('Mean', self.mu), ('1σ', self.pstd)])
]
chunks = []
for title, values in sections:
section_html = ["%s = %.3f %s" % (k, v, self.unit) for k, v in values]
chunks.append('%s: %s' % (title, ',  \n'.join(section_html)))
self.stats_html = "<p>%s</p" % '<br>\n'.join(chunks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment