-
Notifications
You must be signed in to change notification settings - Fork 11
/
ggplot.py
executable file
·90 lines (73 loc) · 2.66 KB
/
ggplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
from optparse import OptionParser
import os, subprocess, sys, tempfile
################################################################################
# ggplot.py
#
# Make a plot given an R script, dict data frame, and arguments.
################################################################################
################################################################################
# plot
################################################################################
def plot(r_script, df_dict, args, df_file=None, print_cmd=False, sep=' '):
# open temp file
if df_file == None:
df_fd, df_file = tempfile.mkstemp()
else:
df_fd = None
df_out = open(df_file, 'w')
# get headers
headers = sorted(df_dict.keys())
print >> df_out, sep.join([str(head) for head in headers])
# check list lengths
length = len(df_dict[headers[0]])
for i in range(1,len(headers)):
if length != len(df_dict[headers[i]]):
print >> sys.stderr, 'Lists in dict vary in length.'
exit(1)
# print data frame
for i in range(length):
print >> df_out, sep.join([str(df_dict[head][i]) for head in headers])
df_out.close()
# convert args to one string
args_str = sep.join([str(a) for a in args])
# plot in R
cmd = 'R --slave --args %s %s < %s' % (df_file, args_str, r_script)
if print_cmd:
print >> sys.stderr, cmd
subprocess.call(cmd, shell=True)
# clean
if df_fd != None:
os.close(df_fd)
os.remove(df_file)
################################################################################
# print_df
#
# Just print the given data frame dictionary to the output file given.
################################################################################
def print_df(df_dict, out_file=None):
# open
if out_file == None:
df_fd, df_file = tempfile.mkstemp()
else:
df_file = out_file
df_out = open(df_file, 'w')
# get headers
headers = sorted(df_dict.keys())
print >> df_out, ' '.join([str(head) for head in headers])
# check list lengths
length = len(df_dict[headers[0]])
for i in range(1,len(headers)):
if length != len(df_dict[headers[i]]):
print >> sys.stderr, 'Lists in dict vary in length:'
for j in range(len(headers)):
print >> sys.stderr, headers[j], len(df_dict[headers[j]])
exit(1)
# print data frame
for i in range(length):
print >> df_out, ' '.join([str(df_dict[head][i]) for head in headers])
df_out.close()
if out_file == None:
return df_fd, df_file
else:
return None