Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active August 30, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokno/77d39b73b0aa0ff581b634cfaecd2af0 to your computer and use it in GitHub Desktop.
Save Lokno/77d39b73b0aa0ff581b634cfaecd2af0 to your computer and use it in GitHub Desktop.
Prints a histogram of a binary file to file. Never stores the entire file in memory.
# Written with the aid of OpenAI Codex
# Implements a histogram and draws each bar with proportional number of '*'
#
# Example Usage:
#
# data = []
# for i in range(0,10):
# data.append(random.uniform(-3.4, 6.7))
# minVal = min(data)
# maxVal = max(data)
# numBins = 10
# maxGlyphs = 20
# histogram = Histogram( minVal, maxVal, numBins, maxGlyphs )
#
# for i in range(0,10):
# histogram.add(data[i])
#
# print(histogram)
#
import struct, sys
class Histogram:
def __init__(self, minVal, maxVal, numBins, maxGlyphs):
self.minVal = minVal
self.maxVal = maxVal
self.numBins = numBins
self.maxGlyphs = maxGlyphs
self.bins = [0] * numBins
self.binWidth = (maxVal - minVal) / numBins
self.binMin = minVal
self.binMax = minVal + self.binWidth
self.largestBinValue = 0
def add(self, value):
if value < self.minVal:
return
if value > self.maxVal:
return
bin = int((value - self.minVal) / self.binWidth)
if bin >= self.numBins:
return
self.bins[bin] += 1
if self.bins[bin] > self.largestBinValue:
self.largestBinValue = self.bins[bin]
def __str__(self):
result = ""
for i in range(0, self.numBins):
result += f'[{self.binMin:8.4f},{self.binMax:8.4f}): '
result += "*" * int(self.bins[i] * self.maxGlyphs / self.largestBinValue)
result += "\n"
self.binMin += self.binWidth
self.binMax += self.binWidth
return result
def read_binary_floats_into_histogram( filename, numBins, numGlyphs ):
# get minVal and maxVal in first pass
minVal = float("inf")
maxVal = float("-inf")
with open(filename, "rb") as f:
while True:
try:
value = struct.unpack('f', f.read(4))[0]
if value < minVal:
minVal = value
if value > maxVal:
maxVal = value
except:
break
histogram = Histogram(minVal, maxVal, numBins, numGlyphs)
with open(filename, "rb") as f:
while True:
try:
value = struct.unpack('f', f.read(4))[0]
histogram.add(value)
except:
break
return histogram
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: python3 histogram.py <filename> [numBins] [numGlyphs]")
sys.exit(1)
filename = sys.argv[1]
numBins = 10
numGlyphs = 20
if len(sys.argv) > 2:
numBins = int(sys.argv[2])
if len(sys.argv) > 3:
numGlyphs = int(sys.argv[3])
histogram = read_binary_floats_into_histogram(filename, numBins, numGlyphs)
print(histogram)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment