annotate plotter.py @ 1:a60f7f660872 draft default tip

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