comparison nhist.py @ 5:bd1db1ed4c25

found on ecclerig
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Mon, 09 Mar 2020 17:39:38 +0000
parents
children
comparison
equal deleted inserted replaced
4:2d7c91f89f6b 5:bd1db1ed4c25
1 #!/usr/bin/python
2 # histogram counts of numeric input, uses existing counts if given
3 # Usage: nhist.py [-c] [-p] [binwidth] [pointCol countCol]
4 # Default binwidth is 100
5 import sys
6 bins={}
7 minv=sys.maxint
8 maxv=-sys.maxint-1
9 cum=False
10 percent=False
11 while len(sys.argv)>1:
12 if sys.argv[1]=='-c':
13 sys.argv.pop(1)
14 cum=True
15 elif sys.argv[1]=='-p':
16 sys.argv.pop(1)
17 cum=True
18 percent=True
19 tot=0
20 else:
21 break
22 if len(sys.argv)>1:
23 w=int(sys.argv[1])
24 else:
25 w=100
26 if len(sys.argv)>2:
27 pc=int(sys.argv[2])
28 cc=int(sys.argv[3])
29 counts=True
30 else:
31 counts=False
32 for l in sys.stdin:
33 if counts:
34 ff=l.split()
35 n=int(ff[pc])
36 c=int(ff[cc])
37 else:
38 n=int(l)
39 c=1
40 v=n/w
41 if percent:
42 tot+=c
43 bins[v]=bins.get(v,0)+c
44 if n<minv:
45 minv=min
46 if n>maxv:
47 maxv=max
48 if cum:
49 cumTot=0
50 for k in sorted(bins.keys()):
51 if cum:
52 cumTot+=bins[k]
53 print k,bins[k],
54 if cum:
55 print cumTot,
56 if percent:
57 print "%5.2f"%(float(cumTot)*100/tot)
58 else:
59 print
60
61