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