-
Notifications
You must be signed in to change notification settings - Fork 6
/
counts.py
executable file
·89 lines (68 loc) · 2.42 KB
/
counts.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
"""Create histogram of data
"""
#--- standard library imports
#
import sys
import argparse
#--- third-party imports
#
import numpy
#--- project specific imports
#
# /
__author__ = "Andreas Wilm"
__version__ = "0.1"
__email__ = "[email protected]"
__license__ = "The MIT License (MIT)"
def main():
"""main function
"""
parser = argparse.ArgumentParser()
default = 10
parser.add_argument("--hist",
action="store_true",
help="Report histogram instead of counts/frequencies")
parser.add_argument("--bins",
type=int,
default=default,
help="Number of equal-width bins (--hist only)"
" (default %d)" % default)
parser.add_argument("--min",
type=float,
help="Lower range for bins")
parser.add_argument("--max",
type=float,
help="Upper range for bins")
parser.add_argument("-i", "--infile",
default="-",
help="Input file containing one value per line"
" (- for stdin, which is default)")
args = parser.parse_args()
if args.infile == "-":
fh = sys.stdin
else:
fh = open(args.infile, 'r')
# taken from describe.py: using an iterable seems to be the most
# efficient way to dynamically grow an array
#
iterable = (float(line) for line in fh if len(line.strip())>0)
arr = numpy.fromiter(iterable, numpy.float)
if fh != sys.stdin:
fh.close()
if args.hist:
bin_range = (args.min if args.min!=None else arr.min(),
args.max if args.max!=None else arr.max())
(hist, bin_edges) = numpy.histogram(arr, bins=args.bins, range=bin_range)
print "#lower bound\tupper bound\tcounts"
print "#note: all but last (righthand-most) bin are half-open, i.e. [...)"
for (i, val) in enumerate(hist):
print "{}\t{}\t{}".format(bin_edges[i], bin_edges[i+1], val)
else:
unique, counts = numpy.unique(arr, return_counts=True)
print "#count\tvalue"
for (u, c) in zip(unique, counts):
print "{}\t{}".format(c, u)
#print numpy.asarray((unique, counts)).T
if __name__ == "__main__":
main()