Skip to content

Instantly share code, notes, and snippets.

@alaiacano
Created July 7, 2012 14:59
Show Gist options
  • Save alaiacano/3066775 to your computer and use it in GitHub Desktop.
Save alaiacano/3066775 to your computer and use it in GitHub Desktop.
"""
Input is one number per line
Output is json of the form:
[
{
'bin' : bin1,
'value' : value1
},
{
'bin' : bin2,
'value' : value2
},
]
Set UPDATE_EVERY to whatever you want. set it to 10 and you'll get a
new histogram output every 10th input
Invocation:
cat numbers.txt | python -u hists.py
"""
import sys, numpy
UPDATE_EVERY = 5
arr = numpy.array([])
counter = 0
for line in sys.stdin:
counter += 1
arr = numpy.append(arr, float(line.strip()))
if counter % UPDATE_EVERY == 0:
h = numpy.histogram(arr)
output = []
for count, bin in zip(h[0], h[1]):
output.append({'bin':bin, 'count':count})
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment