Skip to content

Instantly share code, notes, and snippets.

@travis-g
Last active November 9, 2019 21:21
Show Gist options
  • Save travis-g/3550fed5cde487dbd0dfaa3c63607fbe to your computer and use it in GitHub Desktop.
Save travis-g/3550fed5cde487dbd0dfaa3c63607fbe to your computer and use it in GitHub Desktop.
Converts data from stdin to histogram bins
#!/usr/bin/env python3
import sys
import argparse
import datetime
import numpy as np
import pandas as pd
parser = argparse.ArgumentParser()
parser.add_argument("--log", help="logarithmically scale results", action="store_true")
parser.add_argument("-s", "--size", help="number of bins", action="count", default=40)
args = parser.parse_args()
inputs = [line.strip() for line in sys.stdin]
nums = []
try:
# if times
nums = [pd.to_datetime(num, infer_datetime_format=True).tz_localize(None) for num in inputs]
except:
# if values
nums = np.array(inputs).astype(np.float)
# create the histogram
hist, _ = np.histogram(np.array(nums, dtype='datetime64').astype(np.int64), args.size)
# if logarithmic, do it
if args.log:
for i in range(len(hist)):
if hist[i] != 0:
hist[i] = np.log2(hist[i])
print(" ".join(np.char.mod("%f", np.ravel(hist))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment