annotate tools/plotting/histogram.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 #Greg Von Kuster
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 from rpy import *
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 assert sys.version_info[:2] >= ( 2, 4 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 def stop_err(msg):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 sys.stderr.write(msg)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 sys.exit()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 # Handle input params
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 in_fname = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 out_fname = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 column = int( sys.argv[3] ) - 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 stop_err( "Column not specified, your query does not contain a column of numerical data." )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 title = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 xlab = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 breaks = int( sys.argv[6] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 if breaks == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 breaks = "Sturges"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 if sys.argv[7] == "true":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 density = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 else: density = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 if len( sys.argv ) >= 9 and sys.argv[8] == "true":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 frequency = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 else: frequency = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 matrix = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 skipped_lines = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 first_invalid_line = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 invalid_value = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 i = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 for i, line in enumerate( file( in_fname ) ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 valid = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 line = line.rstrip('\r\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 # Skip comments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if line and not line.startswith( '#' ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 # Extract values and convert to floats
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 row = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 fields = line.split( "\t" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 val = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 if val.lower() == "na":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 row.append( float( "nan" ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 row.append( float( val ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 invalid_value = fields[column]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 valid = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 skipped_lines += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if not first_invalid_line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 first_invalid_line = i+1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 if valid:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 matrix += row
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 if skipped_lines < i:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 a = r.array( matrix )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 r.pdf( out_fname, 8, 8 )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 histogram = r.hist( a, probability=not frequency, main=title, xlab=xlab, breaks=breaks )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 if density:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 density = r.density( a )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 if frequency:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 scale_factor = len( matrix ) * ( histogram['mids'][1] - histogram['mids'][0] ) #uniform bandwidth taken from first 2 midpoints
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 density[ 'y' ] = map( lambda x: x * scale_factor, density[ 'y' ] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 r.lines( density )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 r.dev_off()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 except Exception, exc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 stop_err( "%s" %str( exc ) )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 if i == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 stop_err("Input dataset is empty.")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 stop_err( "All values in column %s are non-numeric." %sys.argv[3] )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 print "Histogram of column %s. " %sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 if skipped_lines > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 print "Skipped %d invalid lines starting with line #%d, '%s'." % ( skipped_lines, first_invalid_line, invalid_value )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 r.quit( save="no" )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 main()