annotate SMART/Java/Python/misc/RPlotter.py @ 71:d96f6c9a39e0 draft default tip

Removed pyc files.
author m-zytnicki
date Thu, 07 Apr 2016 09:25:18 -0400
parents 44d5973c188c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
1 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
2 # Copyright INRA-URGI 2009-2010
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
3 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
4 # This software is governed by the CeCILL license under French law and
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
5 # abiding by the rules of distribution of free software. You can use,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
6 # modify and/ or redistribute the software under the terms of the CeCILL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
7 # license as circulated by CEA, CNRS and INRIA at the following URL
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
8 # "http://www.cecill.info".
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
9 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
10 # As a counterpart to the access to the source code and rights to copy,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
11 # modify and redistribute granted by the license, users are provided only
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
12 # with a limited warranty and the software's author, the holder of the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
13 # economic rights, and the successive licensors have only limited
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
14 # liability.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
15 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
16 # In this respect, the user's attention is drawn to the risks associated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
17 # with loading, using, modifying and/or developing or reproducing the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
18 # software by the user in light of its specific status of free software,
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
19 # that may mean that it is complicated to manipulate, and that also
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
20 # therefore means that it is reserved for developers and experienced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
21 # professionals having in-depth computer knowledge. Users are therefore
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
22 # encouraged to load and test the software's suitability as regards their
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
23 # requirements in conditions enabling the security of their systems and/or
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
24 # data to be ensured and, more generally, to use and operate it in the
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
25 # same conditions as regards security.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
26 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
27 # The fact that you are presently reading this means that you have had
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
28 # knowledge of the CeCILL license and that you accept its terms.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
29 #
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
30
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
31 import os
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
32 import subprocess
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
33 import random
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
34 import math
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
35
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
36 minPositiveValue = 10e-6
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
37
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
38 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
39 Plot simple curves in R
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
40 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
41
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
42 class RPlotter(object):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
43 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
44 Plot some curves
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
45 @ivar nbColors: number of different colors
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
46 @type nbColors: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
47 @ivar fileName: name of the file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
48 @type fileName: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
49 @ivar lines: lines to be plotted
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
50 @type lines: array of dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
51 @ivar names: name of the lines
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
52 @type names: array of strings
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
53 @ivar colors: color of the lines
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
54 @type colors: array of strings
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
55 @ivar types: type of the lines (plain or dashed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
56 @type types: array of strings
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
57 @ivar format: format of the picture
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
58 @type format: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
59 @ivar lineWidth: width of the line in a xy-plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
60 @type lineWidth: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
61 @ivar xMin: minimum value taken on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
62 @type xMin: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
63 @ivar xMax: maximum value taken on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
64 @type xMax: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
65 @ivar yMin: minimum value taken on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
66 @type yMin: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
67 @ivar yMax: maximum value taken on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
68 @type yMax: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
69 @ivar minimumX: minimum value allowed on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
70 @type minimumX: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
71 @ivar maximumX: maximum value allowed on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
72 @type maximumX: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
73 @ivar minimumY: minimum value allowed on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
74 @type minimumY: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
75 @ivar maximumY: maximum value allowed on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
76 @type maximumY: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
77 @ivar leftMargin: add some margin in the left part of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
78 @type leftMargin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
79 @ivar rightMargin: add some margin in the right part of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
80 @type rightMargin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
81 @ivar downMargin: add some margin at the top of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
82 @type downMargin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
83 @ivar upMargin: add some margin at the bottom of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
84 @type upMargin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
85 @ivar logX: use log scale on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
86 @type logX: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
87 @ivar logY: use log scale on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
88 @type logY: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
89 @ivar logZ: use log scale on the z-axis (the color)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
90 @type logZ: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
91 @ival fill: if a value is not given, fill it with given value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
92 @type fill: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
93 @ival bucket: cluster the data into buckets of given size
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
94 @type bucket: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
95 @ival seed: a random number
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
96 @type seed: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
97 @ival regression: plot a linear regression
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
98 @type regression: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
99 @ival legend: set the legend
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
100 @type legend: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
101 @ival legendBySide: set the legend outside of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
102 @type legendBySde: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
103 @ival xLabel: label for the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
104 @type xLabel: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
105 @ival yLabel: label for the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
106 @type yLabel: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
107 @ival title: title of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
108 @type title: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
109 @ival barplot: use a barplot representation instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
110 @type barplot: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
111 @ival points: use a point cloud instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
112 @type points: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
113 @ival heatPoints: use a colored point cloud instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
114 @type heatPoints: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
115 @ival axesLabels: change the names of the axes
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
116 @type axesLabels: vector of 2 int to string dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
117 @ival rotateAxesLabels: rotate the axes labels
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
118 @type rotateAxesLabels: dict of 2 boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
119 @ival verbosity: verbosity of the class
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
120 @type verbosity: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
121 @ival keep: keep temporary files
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
122 @type keep: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
123 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
124
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
125 def __init__(self, fileName, verbosity = 0, keep = False):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
126 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
127 Constructor
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
128 @param fileName: name of the file to produce
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
129 @type fileName: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
130 @param verbosity: verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
131 @type verbosity: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
132 @param keep: keep temporary files
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
133 @type keep: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
134 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
135 self.nbColors = 9
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
136 self.fileName = fileName
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
137 self.verbosity = verbosity
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
138 self.keep = keep
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
139 self.format = "png"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
140 self.fill = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
141 self.bucket = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
142 self.lines = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
143 self.names = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
144 self.colors = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
145 self.types = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
146 self.lineWidth = 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
147 self.xMin = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
148 self.xMax = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
149 self.yMin = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
150 self.yMax = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
151 self.seed = random.randint(0, 10000)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
152 self.minimumX = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
153 self.maximumX = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
154 self.minimumY = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
155 self.maximumY = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
156 self.leftMargin = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
157 self.rightMargin = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
158 self.topMargin = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
159 self.bottomMargin = 0
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
160 self.logX = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
161 self.logY = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
162 self.logZ = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
163 self.regression = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
164 self.width = 1000
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
165 self.height = 500
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
166 self.legend = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
167 self.legendBySide = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
168 self.xLabel = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
169 self.yLabel = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
170 self.title = None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
171 self.points = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
172 self.heatPoints = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
173 self.barplot = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
174 self.axesLabels = {1: None, 2: None}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
175 self.rotateAxesLabels = {1: False, 2: False}
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
176 self.linesToAddBox = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
177
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
178 def __del__(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
179 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
180 Destructor
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
181 Remove tmp files
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
182 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
183 if not self.keep:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
184 scriptFileName = "tmpScript-%d.R" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
185 if os.path.exists(scriptFileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
186 os.remove(scriptFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
187 outputFileName = "%sout" % (scriptFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
188 if os.path.exists(outputFileName):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
189 os.remove(outputFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
190 nbLines = len(self.lines) + (1 if self.heatPoints else 0)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
191 for i in range(nbLines):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
192 if os.path.exists("tmpData-%d-%d.dat" % (self.seed, i)):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
193 os.remove("tmpData-%d-%d.dat" % (self.seed, i))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
194
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
195
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
196 def setMinimumX(self, xMin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
197 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
198 Set the minimum value on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
199 @param xMin:minimum value on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
200 @type xMin: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
201 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
202 self.minimumX = xMin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
203
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
204
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
205 def setMaximumX(self, xMax):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
206 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
207 Set the maximum value on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
208 @param xMax: maximum value on the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
209 @type xMax: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
210 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
211 self.maximumX = xMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
212
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
213
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
214 def setMinimumY(self, yMin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
215 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
216 Set the minimum value on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
217 @param yMin: minimum value on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
218 @type yMin: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
219 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
220 self.minimumY = yMin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
221
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
222
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
223 def setMaximumY(self, yMax):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
224 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
225 Set the maximum value on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
226 @param yMax: maximum value on the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
227 @type xmax: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
228 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
229 self.maximumY = yMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
230
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
231
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
232 def setFill(self, fill):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
233 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
234 Fill empty data with given value
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
235 @param fill: the value to fill with
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
236 @type fill: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
237 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
238 self.fill = fill
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
239
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
240
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
241 def setBuckets(self, bucket):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
242 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
243 Cluster the data into buckets of given size
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
244 @param bucket: the size of the buckets
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
245 @type bucket: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
246 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
247 self.bucket = bucket
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
248
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
249
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
250 def setRegression(self, regression):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
251 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
252 Plot a linear regression line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
253 @param regression: whether to plot the regression
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
254 @type regression: bool
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
255 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
256 self.regression = regression
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
257
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
258
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
259 def setFormat(self, format):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
260 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
261 Set the format of the picture
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
262 @param format: the format
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
263 @type format: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
264 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
265 if format not in ("png", "pdf", "jpeg", "bmp", "tiff"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
266 raise Exception("Format '%s' is not supported by RPlotter" % (format))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
267 self.format = format
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
268
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
269
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
270 def setWidth(self, width):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
271 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
272 Set the dimensions of the image produced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
273 @param width: width of the image
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
274 @type width: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
275 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
276 self.width = width
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
277
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
278
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
279 def setHeight(self, height):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
280 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
281 Set the dimensions of the image produced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
282 @param height: heigth of the image
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
283 @type height: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
284 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
285 self.height = height
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
286
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
287
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
288 def setImageSize(self, width, height):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
289 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
290 Set the dimensions of the image produced
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
291 @param width: width of the image
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
292 @type width: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
293 @param height: heigth of the image
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
294 @type height: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
295 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
296 self.setWidth(width)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
297 self.setHeight(height)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
298
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
299
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
300 def setLegend(self, legend, bySide = False):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
301 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
302 Print a legend or not
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
303 @param legend: print a legend
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
304 @type legend: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
305 @param bySide: put the legend outside of the plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
306 @type bySide: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
307 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
308 self.legend = legend
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
309 self.legendBySide = bySide
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
310
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
311
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
312 def setXLabel(self, label):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
313 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
314 Print a label for the x-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
315 @param label: the label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
316 @type label: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
317 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
318 self.xLabel = label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
319 if self.xLabel != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
320 self.xLabel = self.xLabel.replace("_", " ")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
321
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
322
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
323 def setYLabel(self, label):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
324 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
325 Print a label for the y-axis
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
326 @param label: the label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
327 @type label: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
328 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
329 self.yLabel = label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
330 if self.yLabel != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
331 self.yLabel = self.yLabel.replace("_", " ")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
332
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
333
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
334 def addLeftMargin(self, margin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
335 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
336 Increase the size of the space on the left part of the graph
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
337 @param margin: the space added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
338 @type margin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
339 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
340 self.leftMargin = margin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
341
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
342
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
343 def addRightMargin(self, margin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
344 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
345 Increase the size of the space on the right part of the graph
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
346 @param margin: the space added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
347 @type margin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
348 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
349 self.rightMargin = margin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
350
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
351
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
352 def addTopMargin(self, margin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
353 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
354 Increase the size of the space at the top of the graph
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
355 TopMargin is a percentage if 0 < TopMargin < 1.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
356 TopMargin is a value if TopMargin >= 1.
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
357 @param margin: the space added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
358 @type margin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
359 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
360 self.topMargin = margin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
361
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
362
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
363 def addBottomMargin(self, margin):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
364 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
365 Increase the size of the space at the bottom of the graph
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
366 @param margin: the space added
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
367 @type margin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
368 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
369 self.bottomMargin = margin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
370
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
371
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
372 def getNewYMaxWithTopMargin(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
373 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
374 Return new xMin coordinate with left margin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
375 @param xMin: coordinate
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
376 @type xMin: float
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
377 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
378 yMax = self.yMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
379 if 0 < self.topMargin and self.topMargin < 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
380 topMargin = self.topMargin * self.yMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
381 yMax = self.yMax + topMargin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
382 elif self.topMargin >= 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
383 yMax = self.yMax + self.topMargin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
384 return yMax
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
385
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
386
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
387 def setTitle(self, title):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
388 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
389 Print a title for graph
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
390 @param title: a title
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
391 @type title: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
392 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
393 self.title = title
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
394 if self.title != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
395 self.title = self.title.replace("_", " ")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
396
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
397
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
398 def setAxisLabel(self, i, labels):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
399 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
400 Change x- or y-labels
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
401 @param i: x for x-label, y for y-label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
402 @type i: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
403 @param labels: new labels
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
404 @type labels: int to string dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
405 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
406 i = i.lower()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
407 if i not in ("x", "y"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
408 raise Exception("Label name '" + i + "' should by 'x' or 'y' while changing axis labels.")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
409 self.axesLabels[{"x": 1, "y": 2}[i]] = labels
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
410
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
411
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
412 def rotateAxisLabel(self, i, b = True):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
413 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
414 Rotate x- or y-labels
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
415 @param i: x for x-label, y for y-label
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
416 @type i: string
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
417 @param b: whether the labels should be rotated
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
418 @type b: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
419 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
420 i = i.lower()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
421 if i not in ("x", "y"):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
422 raise Exception("Label name '" + i + "' should by 'x' or 'y' while rotating axis labels.")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
423 self.rotateAxesLabels[{"x": 1, "y": 2}[i]] = b
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
424
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
425 def setLineWidth(self, width):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
426 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
427 Set the line width in a xy-plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
428 @param width: the new line width
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
429 @type width: int
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
430 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
431 self.lineWidth = width
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
432
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
433 def setLog(self, log):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
434 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
435 Use log-scale for axes
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
436 @param log: use log scale
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
437 @type log: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
438 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
439 self.logX = ("x" in log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
440 self.logY = ("y" in log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
441 self.logZ = ("z" in log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
442
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
443
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
444 def setBarplot(self, barplot):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
445 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
446 Use barplot representation instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
447 @param barplot: barplot representation
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
448 @type barplot: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
449 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
450 self.barplot = barplot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
451
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
452
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
453 def setPoints(self, points):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
454 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
455 Use points cloud representation instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
456 @param points: points cloud representation
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
457 @type points: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
458 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
459 self.points = points
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
460
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
461
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
462 def setHeatPoints(self, heatPoints):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
463 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
464 Use points cloud representation with color representing another variable instead
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
465 @param points: colored points cloud representation
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
466 @type points: boolean
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
467 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
468 self.heatPoints = heatPoints
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
469
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
470
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
471 def addBox(self, lXCoordList, minY, maxY):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
472 for lXCoord in lXCoordList:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
473 self.linesToAddBox += "rect(%s,%s,%s,%s,density=50, col='grey',border='transparent')\n" % (lXCoord[0], minY, lXCoord[1], maxY)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
474
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
475 def addLine(self, line, name = "", color = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
476 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
477 Add a line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
478 @param line: a line to plot
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
479 @type line: dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
480 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
481 # prepare data
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
482 plot = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
483 if self.points or self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
484 values = line.values()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
485 elif self.fill == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
486 values = sorted(line.keys())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
487 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
488 values = range(min(line.keys()), max(line.keys()) + 1)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
489
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
490 for element in values:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
491 if self.points or self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
492 x = element[0]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
493 y = element[1]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
494 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
495 x = element
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
496 if x not in line:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
497 y = self.fill
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
498 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
499 y = line[x]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
500
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
501 if self.minimumX != None and x < self.minimumX:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
502 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
503 if self.maximumX != None and x > self.maximumX:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
504 continue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
505
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
506 if x == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
507 raise Exception("Problem! x is None. Aborting...")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
508 if y == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
509 raise Exception("Problem! y is None. Aborting...")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
510 if x == 0 and self.logX:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
511 x = minPositiveValue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
512 if y == 0 and self.logY:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
513 y = minPositiveValue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
514 if self.xMin == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
515 if not self.logX or x != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
516 self.xMin = x
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
517 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
518 if not self.logX or x != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
519 self.xMin = min(self.xMin, x)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
520 if self.xMax == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
521 self.xMax = x
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
522 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
523 self.xMax = max(self.xMax, x)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
524 if self.yMin == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
525 if not self.logY or y != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
526 self.yMin = y
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
527 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
528 if not self.logY or y != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
529 if y != "NA":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
530 self.yMin = min(self.yMin, y)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
531 if self.yMax == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
532 self.yMax = y
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
533 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
534 if y != "NA":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
535 self.yMax = max(self.yMax, y)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
536
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
537 plot.append((x, y))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
538
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
539 # cluster the data into buckets
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
540 if self.bucket != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
541 buckets = dict([((int(value) / int(self.bucket)) * self.bucket, 0) for value in xrange(min(line.keys()), max(line.keys())+1)])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
542 for distance, nb in line.iteritems():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
543 buckets[(int(distance) / int(self.bucket)) * self.bucket] += nb
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
544 self.yMax = max(buckets.values())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
545 plot = []
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
546 for x, y in buckets.iteritems():
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
547 plot.append((x, y))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
548
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
549 # write file
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
550 dataFileName = "tmpData-%d-%d.dat" % (self.seed, len(self.lines))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
551 dataHandle = open(dataFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
552 if not self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
553 plot.sort()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
554 for (x, y) in plot:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
555 if y != "NA":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
556 dataHandle.write("%f\t%f\n" % (x, y))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
557 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
558 dataHandle.write("%f\t%s\n" % (x, y))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
559 dataHandle.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
560
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
561 self.lines.append(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
562 self.names.append(name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
563
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
564 if color == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
565 colorNumber = len(self.colors) % (self.nbColors - 1) + 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
566 type = "solid"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
567 if len(self.colors) >= self.nbColors:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
568 type = "dashed"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
569 color = "colorPanel[%d]" % (colorNumber)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
570 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
571 color = "\"%s\"" % (color)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
572 type = "solid"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
573 self.colors.append(color)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
574 self.types.append(type)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
575
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
576
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
577 def addHeatLine(self, line, name = "", color = None):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
578 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
579 Add the heat line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
580 @param line: the line which gives the color of the points
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
581 @type line: dict
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
582 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
583 if not self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
584 raise Exception("Error! Trying to add a heat point whereas not mentioned to earlier! Aborting.")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
585
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
586 dataFileName = "tmpData-%d-%d.dat" % (self.seed, len(self.lines))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
587 dataHandle = open(dataFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
588
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
589 minimumHeat = min(line.values())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
590 maximumHeat = max(line.values())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
591 minLogValue = 0.00001
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
592 log = self.logZ
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
593
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
594 if log:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
595 if minimumHeat == 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
596 for element in line:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
597 line[element] += minLogValue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
598 minimumHeat += minLogValue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
599 maximumHeat += minLogValue
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
600 minimumHeat = math.log10(minimumHeat)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
601 maximumHeat = math.log10(maximumHeat)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
602
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
603 coeff = 255.0 / (maximumHeat - minimumHeat)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
604
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
605 for element in line:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
606 value = line[element]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
607 if log:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
608 value = math.log10(max(minLogValue, value))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
609 dataHandle.write("\"#%02X%02X00\"\n" % (int((value - minimumHeat) * coeff), 255 - int((value - minimumHeat) * coeff)))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
610
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
611 dataHandle.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
612 self.names.append(name)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
613 if color == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
614 colorNumber = len(self.colors) % (self.nbColors - 1) + 1
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
615 type = "solid"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
616 if len(self.colors) >= self.nbColors:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
617 type = "dashed"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
618 color = "colorPanel[%d]" % (colorNumber)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
619 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
620 color = "\"%s\"" % (color)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
621 type = "solid"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
622 self.colors.append(color)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
623 self.types.append(type)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
624
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
625
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
626 def getScript(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
627 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
628 Write (unfinished) R script
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
629 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
630 script = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
631
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
632 xMin = self.xMin - self.leftMargin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
633 if self.minimumX != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
634 xMin = max(xMin, self.minimumX)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
635 xMax = self.xMax + self.rightMargin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
636 if self.maximumX != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
637 xMax = min(xMax, self.maximumX)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
638 yMin = self.yMin - self.bottomMargin
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
639 if self.minimumY != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
640 yMin = self.minimumY
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
641 yMax = self.getNewYMaxWithTopMargin()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
642 yMax += min(1, yMax / 100.0)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
643 if self.maximumY != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
644 yMax = self.maximumY
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
645
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
646 log = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
647 if self.logX:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
648 log += "x"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
649 if self.logY:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
650 log += "y"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
651 if log != "":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
652 log = ", log=\"%s\"" % (log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
653
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
654 title = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
655 if self.title != None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
656 title = ", main = \"%s\"" % (self.title)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
657
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
658 if self.legend and self.legendBySide:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
659 script += "layout(matrix(c(1,2), 1, 2), widths=c(5,1))\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
660
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
661 if self.rotateAxesLabels[2]:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
662 script += "par(mar=c(5,12,4,2))\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
663 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
664 script += "par(mar=c(5,5,4,2))\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
665
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
666 addAxes = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
667
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
668 if self.barplot:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
669 script += "data = scan(\"tmpData-%d-0.dat\", list(x = -666, y = -666))\n" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
670 if len(self.lines) == 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
671 script += "barplot(data$y, name = data$x, xlab=\"%s\", ylab=\"%s\", ylim = c(%f, %f), cex.axis = 2, cex.names = 2, cex.lab = 2%s%s)\n" % (self.xLabel, self.yLabel, yMin, yMax, title, log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
672 addAxes = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
673 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
674 script += "data1 = scan(\"tmpData-%d-1.dat\", list(x = -666, y = -666))\n" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
675 script += "barplot(rbind(data$y, data1$y), name = data$x, xlab=\"%s\", ylab=\"%s\", cex.axis = 2, cex.names = 2, cex.lab = 2%s, beside = TRUE, space=c(-1,0), axes = FALSE%s)\n" % (self.xLabel, self.yLabel, title, log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
676 elif self.points:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
677 script += "data = scan(\"tmpData-%d-0.dat\", list(x = -666, y = -666))\n" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
678 script += "plot(data$x, data$y, xlab=\"%s\", ylab=\"%s\", cex.axis = 2, cex.lab = 2, axes = FALSE%s%s)\n" % (self.xLabel, self.yLabel, title, log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
679 if self.regression:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
680 x = "log10(data$x)" if self.logX else "data$x"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
681 y = "log10(data$y)" if self.logY else "data$y"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
682 script += "abline(lm(%s ~ %s))\n" % (y, x)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
683 elif self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
684 if len(self.lines) != 1:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
685 raise Exception("Error! Bad number of input data! Aborting...")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
686 script += "data = scan(\"tmpData-%d-0.dat\", list(x = -666, y = -666))\n" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
687 script += "heatData = scan(\"tmpData-%d-1.dat\", list(x = \"\"))\n" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
688 script += "plot(data$x, data$y, col=heatData$x, xlab=\"%s\", ylab=\"%s\", cex.axis = 2, cex.lab = 2, axes = FALSE%s%s)\n" % (self.xLabel, self.yLabel, title, log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
689 if self.regression:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
690 x = "log10(data$x)" if self.logX else "data$x"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
691 y = "log10(data$y)" if self.logY else "data$y"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
692 script += "abline(lm(%s ~ %s))\n" % (y, x)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
693 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
694 script += "plot(x = NA, y = NA, panel.first = grid(lwd = 1.0), xlab=\"%s\", ylab=\"%s\", xlim = c(%f, %f), ylim = c(%f, %f), cex.axis = 2, cex.lab = 2, axes = FALSE%s%s)\n" % (self.xLabel, self.yLabel, xMin, xMax, yMin, yMax, title, log)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
695 for i in range(0, len(self.lines)):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
696 script += "data = scan(\"tmpData-%d-%d.dat\", list(x = -666.666, y = -666.666))\n" % (self.seed, i)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
697 script += "lines(x = data$x, y = data$y, col = %s, lty = \"%s\", lwd = %d)\n" % (self.colors[i], self.types[i], self.lineWidth)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
698
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
699 script += self.linesToAddBox
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
700
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
701 if addAxes:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
702 for i in self.axesLabels:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
703 rotation = ", las = 2" if self.rotateAxesLabels[i] else ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
704 if self.axesLabels[i] == None:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
705 script += "axis(%d, cex.axis = 2, cex.lab = 2%s)\n" % (i, rotation)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
706 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
707 oldKeys = ", ".join(["%d" % (key) for key in sorted(self.axesLabels[i].keys())])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
708 newKeys = ", ".join(["\"%s\"" % (self.axesLabels[i][key]) for key in sorted(self.axesLabels[i].keys())])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
709 script += "axis(%d, at=c(%s), lab=c(%s), cex.axis = 2, cex.lab = 2%s)\n" % (i, oldKeys, newKeys, rotation)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
710 script += "box()\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
711
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
712 if self.legend:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
713 if self.legendBySide:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
714 script += "plot.new()\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
715 script += "par(mar=c(0,0,0,0))\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
716 script += "plot.window(c(0,1), c(0,1))\n"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
717 script += "legends = c(%s)\n" % ", ".join(["\"%s\"" % name for name in self.names])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
718 script += "colors = c(%s)\n" % ", ".join(["%s" % color for color in self.colors])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
719 script += "lineTypes = c(%s)\n" % ", ".join(["\"%s\"" % type for type in self.types])
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
720 if self.legendBySide:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
721 script += "legend(0, 1, legend = legends, xjust = 0, yjust = 1, col = colors, lty = lineTypes, lwd = %d, cex = 1.5, ncol = 1, bg = \"white\")\n" % (self.lineWidth)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
722 else:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
723 script += "legend(\"topright\", legend = legends, xjust = 0, yjust = 1, col = colors, lty = lineTypes, lwd = %d, cex = 1.5, ncol = 1, bg = \"white\")\n" % (self.lineWidth)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
724
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
725 return script
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
726
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
727
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
728
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
729 def plot(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
730 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
731 Plot the lines
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
732 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
733 scriptFileName = "tmpScript-%d.R" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
734 scriptHandle = open(scriptFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
735 scriptHandle.write("library(RColorBrewer)\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
736 scriptHandle.write("colorPanel = brewer.pal(n=%d, name=\"Set1\")\n" % (self.nbColors))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
737 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))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
738 scriptHandle.write(self.getScript())
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
739 scriptHandle.write("dev.off()\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
740 scriptHandle.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
741 rCommand = "R"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
742 if "SMARTRPATH" in os.environ:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
743 rCommand = os.environ["SMARTRPATH"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
744 command = "\"%s\" CMD BATCH %s" % (rCommand, scriptFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
745 status = subprocess.call(command, shell=True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
746
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
747 if status != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
748 self.keep = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
749 raise Exception("Problem with the execution of script file %s, status is: %s" % (scriptFileName, status))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
750
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
751
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
752 def getCorrelationData(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
753 if not self.regression:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
754 return ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
755 scriptFileName = "tmpScript-%d.R" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
756 rScript = open(scriptFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
757 rScript.write("data = scan(\"tmpData-%d-0.dat\", list(x = -0.000000, y = -0.000000))\n" % (self.seed))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
758 x = "log10(data$x)" if self.logX else "data$x"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
759 y = "log10(data$y)" if self.logY else "data$y"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
760 rScript.write("summary(lm(%s ~ %s))\n" % (y, x))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
761 rScript.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
762 rCommand = "R"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
763 if "SMARTRPATH" in os.environ:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
764 rCommand = os.environ["SMARTRPATH"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
765 command = "\"%s\" CMD BATCH %s" % (rCommand, scriptFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
766 status = subprocess.call(command, shell=True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
767 if status != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
768 self.keep = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
769 raise Exception("Problem with the execution of script file %s computing the correlation, status is: %s" % (scriptFileName, status))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
770 outputRFile = open("%sout" % (scriptFileName))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
771 output = ""
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
772 start = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
773 end = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
774 for line in outputRFile:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
775 if start and "> " in line:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
776 end = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
777 if start and not end:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
778 output += line
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
779 if "summary" in line:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
780 start = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
781 return output
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
782
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
783
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
784 def getSpearmanRho(self):
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
785 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
786 Get the Spearman rho correlation using R
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
787 """
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
788 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
789 if not self.points and not self.barplot and not self.heatPoints:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
790 raise Exception("Cannot compute Spearman rho correlation whereas not in 'points' or 'bar' mode.")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
791
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
792 scriptFileName = "tmpScript-%d.R" % (self.seed)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
793 rScript = open(scriptFileName, "w")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
794 rScript.write("library(Hmisc)\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
795 rScript.write("data = scan(\"tmpData-%d-0.dat\", list(x = -0.000000, y = -0.000000))\n" % (self.seed))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
796 rScript.write("spearman(data$x, data$y)\n")
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
797 rScript.close()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
798
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
799 rCommand = "R"
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
800 if "SMARTRPATH" in os.environ:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
801 rCommand = os.environ["SMARTRPATH"]
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
802 command = "\"%s\" CMD BATCH %s" % (rCommand, scriptFileName)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
803 status = subprocess.call(command, shell=True)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
804
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
805 if status != 0:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
806 self.keep = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
807 raise Exception("Problem with the execution of script file %s, status is: %s" % (scriptFileName, status))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
808
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
809 outputRFile = open("%sout" % (scriptFileName))
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
810 nextLine = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
811 for line in outputRFile:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
812 line = line.strip()
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
813 if nextLine:
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
814 if line == "NA":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
815 return None
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
816 return float(line)
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
817 nextLine = False
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
818 if line == "rho":
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
819 nextLine = True
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
820
44d5973c188c Uploaded
m-zytnicki
parents:
diff changeset
821 return None