annotate nhist.py @ 58:a3aaf6c085f4 simple

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