annotate histogram.py @ 0:9c0d844f4e48 draft

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