annotate tools/plotting/plotter.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # python histogram input_file output_file column bins
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys, os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 import matplotlib; matplotlib.use('Agg')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 from pylab import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 if __name__ == '__main__':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 # parse the arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 if len(sys.argv) != 6:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 stop_err('Usage: python histogram.py input_file column bins output_file style')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 mode = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 HIST = mode == 'hist'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 col = int(float(sys.argv[2]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 if HIST:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 bin = int(float(sys.argv[3]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 # hack, this parameter is the plotting style for scatter plots
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 if sys.argv[3] == 'P':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 style = 'o'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 elif sys.argv[3] == 'LP':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 style = 'o-'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 style = '-'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 msg = 'Parameter were not numbers %s, %s' % (sys.argv[3], sys.argv[4])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 stop_err(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 # validate arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 inp_file = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 out_file = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 if HIST:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 print "Histogram on column %s (%s bins)" % (col, bin)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 print "Scatterplot on column %s" % (col)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 xcol= col -1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 # read the file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 values = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 for line in file(inp_file):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if line and line[0] != '#':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 values.append(float(line.split()[xcol]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 except Exception, e:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 stop_err('%s' % e)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 stop_err("Non numerical data at line %d, column %d" % (count, col) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 # plot the data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 if HIST:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 n, bins, patches = hist(values, bins=bin, normed=0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 plot(values, style)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 xlabel('values')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 ylabel('counts')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if HIST:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 title('Histogram of values over column %s (%s bins)' % (col, len(bins)) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 title('Scatterplot over column %s' % col )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 grid(True)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 # the plotter detects types by file extension
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 png_out = out_file + '.png' # force it to png
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 savefig(png_out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 # shuffle it back and clean up
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 data = file(png_out, 'rb').read()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 fp = open(out_file, 'wb')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 fp.write(data)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 fp.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 os.remove(png_out)