-
Notifications
You must be signed in to change notification settings - Fork 41
/
hist
executable file
·44 lines (34 loc) · 1.08 KB
/
hist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import numpy as np
import sys
import os
av = sys.argv
if len(av) > 1 and av[1] == '-h':
print("usage: {:} [--density|--cumulative] [--bins BINS] [--range L R]".format(os.path.basename(av[0])))
print('''
print histogram of STDIN as columns [center,density,left,right]
''')
exit(1)
def msg(s):
sys.stderr.write(str(s) + "\n")
bins = int(av[av.index("--bins") + 1]) if "--bins" in av else "auto"
if "--range" in av:
i = av.index("--range")
rng = (float(av[i + 1]), float(av[i + 2]))
else:
rng = None
d = np.loadtxt(sys.stdin)
assert d.ndim == 1 or (d.ndim == 2 and d.shape[1] == 2)
cumulative = "--cumulative" in av
density = "--density" in av
assert not (density and cumulative)
if d.ndim == 1:
val, edge = np.histogram(d, bins=bins, density=density, range=rng)
elif d.ndim == 2 and d.shape[1] == 2:
val, edge = np.histogram(d[:,0], weights=d[:, 1], bins=bins, density=density, range=rng)
if cumulative:
val = np.cumsum(val)
left = edge[:-1]
right = edge[1:]
center = (left + right) * 0.5
np.savetxt(sys.stdout, np.transpose([center, val, left, right]))