annotate tools/plotting/bar_chart.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
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
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 histogram_gnuplot.py <datafile> <xtic column> <column_list> <title> <ylabel> <yrange_min> <yrange_max> <grath_file>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 a generic histogram builder based on gnuplot backend
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 data_file - tab delimited file with data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 xtic_column - column containing labels for x ticks [integer, 0 means no ticks]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 column_list - comma separated list of columns to plot
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 title - title for the entire histrogram
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 ylabel - y axis label
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 yrange_max - minimal value at the y axis (integer)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 yrange_max - maximal value at the y_axis (integer)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 to set yrange to autoscaling assign 0 to yrange_min and yrange_max
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 graph_file - file to write histogram image to
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 img_size - as X,Y pair in pixels (e.g., 800,600 or 600,800 etc.)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 This tool required gnuplot and gnuplot.py
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 anton nekrutenko | anton@bx.psu.edu
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 import Gnuplot, Gnuplot.funcutils
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 import sys, string, tempfile, os
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 def main(tmpFileName):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 skipped_lines_count = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 skipped_lines_index = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 gf = open(tmpFileName, 'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 in_file = open( sys.argv[1], 'r' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 xtic = int( sys.argv[2] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 col_list = string.split( sys.argv[3],"," )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 title = 'set title "' + sys.argv[4] + '"'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 ylabel = 'set ylabel "' + sys.argv[5] + '"'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 ymin = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 ymax = sys.argv[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 img_file = sys.argv[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 img_size = sys.argv[9]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 stop_err("Check arguments\n")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 int( col_list[0] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 stop_err('You forgot to set columns for plotting\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 for i, line in enumerate( in_file ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 valid = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 row = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 fields = line.split( '\t' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 for col in col_list:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 row.append( str( float( fields[int( col )-1] ) ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 skipped_lines_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 skipped_lines_index.append(i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 skipped_lines_count += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 skipped_lines_index.append(i)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 if valid and xtic > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 row.append( fields[xtic-1] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 elif valid and xtic == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 row.append( str( i ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 if valid:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 gf.write( '\t'.join( row ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 gf.write( '\n' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if skipped_lines_count < i:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 #prepare 'using' clause of plot statement
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 g_plot_command = ' ';
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 #set the first column
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 if xtic > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 g_plot_command = "'%s' using 1:xticlabels(%s) ti 'Column %s', " % ( tmpFileName, str( len( row ) ), col_list[0] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 g_plot_command = "'%s' using 1 ti 'Column %s', " % ( tmpFileName, col_list[0] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 #set subsequent columns
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 for i in range(1,len(col_list)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 g_plot_command += "'%s' using %s t 'Column %s', " % ( tmpFileName, str( i+1 ), col_list[i] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 g_plot_command = g_plot_command.rstrip( ', ' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 yrange = 'set yrange [' + ymin + ":" + ymax + ']'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 g = Gnuplot.Gnuplot()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 g('reset')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 g('set boxwidth 0.9 absolute')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 g('set style fill solid 1.00 border -1')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 g('set style histogram clustered gap 5 title offset character 0, 0, 0')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 g('set xtics border in scale 1,0.5 nomirror rotate by 90 offset character 0, 0, 0')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 g('set key invert reverse Left outside')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 if xtic == 0: g('unset xtics')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 g(title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 g(ylabel)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 g_term = 'set terminal png tiny size ' + img_size
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 g(g_term)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 g_out = 'set output "' + img_file + '"'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 if ymin != ymax:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 g(yrange)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 g(g_out)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 g('set style data histograms')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 g.plot(g_plot_command)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 stop_err("Gnuplot error: Data cannot be plotted")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 sys.stderr.write('Column(s) %s of your dataset do not contain valid numeric data' %sys.argv[3] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 if skipped_lines_count > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 sys.stdout.write('\nWARNING. You dataset contain(s) %d invalid lines starting with line #%d. These lines were skipped while building the graph.\n' % ( skipped_lines_count, skipped_lines_index[0]+1 ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 # The tempfile initialization is here because while inside the main() it seems to create a condition
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 # when the file is removed before gnuplot has a chance of accessing it
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 gp_data_file = tempfile.NamedTemporaryFile('w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 Gnuplot.gp.GnuplotOpts.default_term = 'png'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 main(gp_data_file.name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144