annotate SMART/Java/Python/misc/MultipleRPlotter.py @ 6:769e306b7933

Change the repository level.
author yufei-luo
date Fri, 18 Jan 2013 04:54:14 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2012
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 # "http://www.cecill.info".
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 # liability.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 # same conditions as regards security.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 #
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 import os
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 import subprocess
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 import random
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 import math
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 from SMART.Java.Python.misc.RPlotter import RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 NBCOLORS = 9
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 Plot multiple curves with RPlotter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 class MultipleRPlotter(object):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 Plot some curves
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 @ivar fileName: name of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 @type fileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 @ivar height: height of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 @type height: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 @ivar width: width of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 @type width: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 @ivar plots: plots to be included
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 @type plots: list of L{RPlotter{RPlotter}}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 @ivar keep: keep script lines
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 @type keep: boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 @ivar format: format of the file
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 @type format: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 def __init__(self, fileName, verbosity = 0, keep = False):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 Constructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 @param fileName: name of the file to produce
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 @type fileName: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 @param verbosity: verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 @type verbosity: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 @param keep: keep temporary files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 @type keep: boolean
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 self.fileName = fileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 self.verbosity = verbosity
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 self.keep = keep
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.format = "png"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 self.width = 1000
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 self.height = 500
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 self.plots = []
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 self.scriptFileName = "tmpScript-%d.R" % (os.getpid())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 def __del__(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 Destructor
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 Remove script files
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 if not self.keep:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 if os.path.exists(self.scriptFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 os.remove(self.scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 outputFileName = "%sout" % (self.scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 if os.path.exists(outputFileName):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 os.remove(outputFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 def setFormat(self, format):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 Set the format of the picture
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 @param format: the format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 @type format: string
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 if format not in ("png", "pdf", "jpeg", "bmp", "tiff"):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 raise Exception("Format '%s' is not supported by RPlotter" % (format))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 self.format = format
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 def setWidth(self, width):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 Set the dimensions of the image produced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 @param width: width of the image
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 @type width: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 self.width = width
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111 def setHeight(self, height):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 Set the dimensions of the image produced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
114 @param height: heigth of the image
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
115 @type height: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
116 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
117 self.height = height
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
118
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
119
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
120 def setImageSize(self, width, height):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
121 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
122 Set the dimensions of the image produced
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
123 @param width: width of the image
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
124 @type width: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
125 @param height: heigth of the image
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
126 @type height: int
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
127 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
128 self.width = width
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
129 self.height = height
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
130
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
131 def addPlot(self, plot):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
132 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
133 Add a plot
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
134 @param plots: plot to be included
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
135 @type plots: L{RPlotter{RPlotter}}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
136 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
137 self.plots.append(plot)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
138
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
139 def plot(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
140 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
141 Plot the figures
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
142 """
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
143 scriptHandle = open(self.scriptFileName, "w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
144 scriptHandle.write("library(RColorBrewer)\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
145 scriptHandle.write("colorPanel = brewer.pal(n=%d, name=\"Set1\")\n" % (NBCOLORS))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
146 scriptHandle.write("%s(%s = \"%s\", width = %d, height = %d, bg = \"white\")\n" % (self.format, "filename" if self.format != "pdf" else "file", self.fileName, self.width, self.height))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
147 scriptHandle.write("par(mfrow=c(%d, 1))\n" % (len(self.plots)))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
148 for plot in self.plots:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
149 scriptHandle.write(plot.getScript())
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
150 scriptHandle.write("dev.off()\n")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
151 scriptHandle.close()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
152 rCommand = "R"
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
153 if "SMARTRPATH" in os.environ:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
154 rCommand = os.environ["SMARTRPATH"]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
155 command = "\"%s\" CMD BATCH %s" % (rCommand, self.scriptFileName)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
156 status = subprocess.call(command, shell=True)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
157 if status != 0:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
158 self.keep = True
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
159 raise Exception("Problem with the execution of script file %s, status is: %s" % (self.scriptFileName, status))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
160