Package Biskit :: Module gnuplot
[hide private]
[frames] | no frames]

Source Code for Module Biskit.gnuplot

  1  ## Automatically adapted for numpy.oldnumeric Mar 26, 2007 by alter_code1.py 
  2   
  3  # Simple Gnuplot interface. 
  4  # 
  5  # Written by Konrad Hinsen <hinsen@ibs.ibs.fr> 
  6  # last revision: 1998-1-19 
  7  # 
  8  # Caution: If you use a Gnuplot version earlier than 3.6beta, 
  9  # every call for a screen display creates another gnuplot 
 10  # process; these processes are never closed. There seems to be no 
 11  # other way to make gnuplot behave as it should. 
 12   
 13  # Modified by Niklas Blomberg, EMBL to include box and scatterplots 
 14   
 15  # Modified by Raik Gruenberg, IP: 
 16  #   allow importing even if gnuplot is not installed, 
 17  #   check gnuplot.installed == 1, to be sure the program runs 
 18   
 19  """ 
 20  Simple Gnuplot interface. 
 21  """ 
 22   
 23  import os, string, tempfile 
 24   
 25  installed = 0 
 26   
 27  # 
 28  # Test if Gnuplot is new enough to know the option -persist 
 29  # 
 30  try: 
 31      filename = tempfile.mktemp() 
 32      file = open(filename, 'w') 
 33      file.write('\n') 
 34      file.close() 
 35      gnuplot = os.popen('gnuplot -persist ' + filename + ' 2>&1', 'r') 
 36      response = gnuplot.readlines() 
 37      gnuplot.close() 
 38      os.unlink(filename) 
 39      old_version = response and string.index(response[0], '-persist') >= 0 
 40      installed = 1 
 41  except: 
 42      pass 
 43   
 44  # 
 45  # List in which pipes to Gnuplot processes are stored to prevent them from 
 46  # being closed at the end of the plot function. 
 47  # 
 48  _gnuplot_pipes = [] 
 49   
 50  # 
 51  # Check if data object is a sequence 
 52  # 
53 -def _isSequence(object):
54 n = -1 55 try: n = len(object) 56 except: pass 57 return n >= 0
58 59 # 60 # Generate the Gnuplot data files and command string for standard plots 61 #
62 -def _plotData(data):
63 plotlist = [] 64 filelist = [] 65 for set in data: 66 filename = tempfile.mktemp() 67 file = open(filename, 'w') 68 is_sequence = _isSequence(set[0]) 69 for point in set: 70 if is_sequence: 71 for coordinate in point: 72 file.write(`coordinate` + ' ') 73 else: 74 file.write(`point`) 75 file.write('\n') 76 file.close() 77 if is_sequence: 78 plotlist.append((filename, len(set[0]))) 79 else: 80 plotlist.append((filename, 1)) 81 filelist.append(filename) 82 command = 'plot ' 83 for item in plotlist: 84 filename, n = item 85 if n == 1: 86 command = command + '"' + filename + '" notitle w l, ' 87 else: 88 for i in range(n-1): 89 command = command + '"' + filename + \ 90 '" using 1:' + `i+2` + ' notitle w l, ' 91 command = command[:-2] + '\n' 92 return command, filelist
93 94 # 95 # plot of multiple lists with labels 96 #
97 -def _plotWithLabels(data):
98 plotlist = [] 99 filelist = [] 100 if not type(data).__name__ == 'dictionary': 101 data = map(None, data, len(data)*['']) 102 else: 103 data = data.items() 104 for set, key in data: 105 filename = tempfile.mktemp() 106 file = open(filename, 'w') 107 is_sequence = _isSequence(set[0]) 108 for point in set: 109 if is_sequence: 110 for coordinate in point: 111 file.write(`coordinate` + ' ') 112 else: 113 file.write(`point`) 114 file.write('\n') 115 file.close() 116 if is_sequence: 117 plotlist.append((key, filename, len(set[0]))) 118 else: 119 plotlist.append((key, filename, 1)) 120 filelist.append(filename) 121 command = 'plot ' 122 for item in plotlist: 123 key, filename, n = item 124 if n == 1: 125 command = command + '"' + filename + '" title "%s" w l, ' %key 126 else: 127 for i in range(n-1): 128 command = command + '"' + filename + \ 129 '" using 1:' + `i+2` + ' title "%s" w l, ' %key 130 command = command[:-2] + '\n' 131 return command, filelist
132 133 ########################################################################### 134 # Barplot of data 135 ########################################################################### 136
137 -def _barGraphPlotData(data):
138 """ 139 _barGraphPlotData(data): 140 """ 141 plotlist = [] 142 filelist = [] 143 for set in data: 144 filename = tempfile.mktemp() 145 file = open(filename, 'w') 146 is_sequence = _isSequence(set[0]) 147 for point in set: 148 if is_sequence: 149 for coordinate in point: 150 file.write(`coordinate` + ' ') 151 else: 152 file.write(`point`) 153 file.write('\n') 154 file.close() 155 if is_sequence: 156 plotlist.append((filename, len(set[0]))) 157 else: 158 plotlist.append((filename, 1)) 159 filelist.append(filename) 160 command = 'plot ' 161 for item in plotlist: 162 filename, n = item 163 if n == 1: 164 command = command + '"' + filename + '" notitle with boxes , ' 165 else: 166 for i in range(n-1): 167 command = command + '"' + filename + \ 168 '" using 1:' + `i+2` + 'notitle with boxes , ' 169 command = command[:-2] + '\n' 170 #print command 171 return command, filelist
172 173 ########################################################################### 174 # Scatterplots 175 ########################################################################### 176
177 -def _scatterData(data, marker = 'points'):
178 plotlist = [] 179 filelist = [] 180 for set in data: 181 filename = tempfile.mktemp() 182 file = open(filename, 'w') 183 is_sequence = _isSequence(set[0]) 184 for point in set: 185 if is_sequence: 186 for coordinate in point: 187 file.write(`coordinate` + ' ') 188 else: 189 file.write(`point`) 190 file.write('\n') 191 file.close() 192 if is_sequence: 193 plotlist.append((filename, len(set[0]))) 194 else: 195 plotlist.append((filename, 1)) 196 filelist.append(filename) 197 command = 'plot ' 198 for item in plotlist: 199 filename, n = item 200 if n == 1: 201 command = command + '"' + filename + '" notitle with %s, ' \ 202 %marker 203 else: 204 for i in range(n-1): 205 command = command + '"' + filename + \ 206 '" using 1:' + `i+2` + ' notitle with %s, ' \ 207 %marker 208 command = command[:-2] + '\n' 209 return command, filelist
210 211 212 # additional 213
214 -def _scatterData3D(data):
215 plotlist = [] 216 filelist = [] 217 for set in data: 218 filename = tempfile.mktemp() 219 file = open(filename, 'w') 220 is_sequence = _isSequence(set[0]) 221 for point in set: 222 if is_sequence: 223 for coordinate in point: 224 file.write(`coordinate` + ' ') 225 else: 226 file.write(`point`) 227 file.write('\n') 228 file.close() 229 if is_sequence: 230 plotlist.append((filename, len(set[0]))) 231 else: 232 plotlist.append((filename, 1)) 233 filelist.append(filename) 234 command = 'splot ' 235 for item in plotlist: 236 filename, n = item 237 if n == 1: 238 command = command + '"' + filename + '" notitle with points, ' 239 else: 240 for i in range(n-1): 241 command = command + '"' + filename + \ 242 '" using 1:' + `i+2` + ' notitle with points, ' 243 command = command[:-2] + '\n' 244 return command, filelist
245 246 247 248 # 249 # Generate the Gnuplot data files and command string for parallel-axes plots 250 #
251 -def _parallelAxesPlotData(data, origin):
252 naxes = len(data[0]) 253 filename = tempfile.mktemp() 254 filelist = [filename] 255 file = open(filename, 'w') 256 lower = data[0][0] 257 upper = lower 258 for point in data: 259 for i in range(naxes): 260 value = point[i] 261 file.write('%d %g\n' % (i+origin, value)) 262 lower = min(lower, value) 263 upper = max(upper, value) 264 file.write('\n') 265 margin = 0.05*(upper-lower) 266 for i in range(0, naxes): 267 file.write('\n') 268 file.write('%d %g\n' % (i+origin, lower-margin)) 269 file.write('%d %g\n' % (i+origin, upper+margin)) 270 file.close() 271 command = 'plot ' + '[%d:%d] [%g:%g]' % (origin, origin+naxes-1, 272 lower-margin, upper+margin) + \ 273 '"' + filename + '" index 0 notitle w l, "' + \ 274 filename + '" index 1 notitle w l lt -1\n' 275 return command, filelist
276 277 # 278 # Execute a Gnuplot command 279 #
280 -def _execute(command, filelist, keywords):
281 if keywords.has_key('file'): 282 filename = tempfile.mktemp() 283 file = open(filename, 'w') 284 file.write('set terminal postscript\n') 285 file.write('set output "' + keywords['file'] + '"\n') 286 file.write(command) 287 file.close() 288 filelist.append(filename) 289 os.system('gnuplot ' + filename) 290 else: 291 if old_version: 292 gnuplot = os.popen('gnuplot 1> /dev/null 2>&1', 'w') 293 gnuplot.write('set terminal x11\n') 294 gnuplot.write(command) 295 gnuplot.flush() 296 _gnuplot_pipes.append(gnuplot) 297 os.system('sleep 2s') 298 else: 299 gnuplot = os.popen('gnuplot -persist 1> /dev/null 2>&1', 'w') 300 gnuplot.write('set terminal x11\n') 301 gnuplot.write(command) 302 gnuplot.write('quit\n') 303 gnuplot.close() 304 for file in filelist: 305 os.unlink(file)
306 307 308 309 # 310 # Generate a plot 311 #
312 -def plot(*data, **keywords):
313 command, filelist = _plotData(data) 314 _execute(command, filelist, keywords)
315 316
317 -def plotWithLabels(data, **keywords):
318 command, filelist = _plotWithLabels(data) 319 _execute(command, filelist, keywords)
320 # 321 #Generate Boxplot 322 #
323 -def boxPlot(*data, **keywords):
324 """ 325 boxPlot 326 """ 327 command, filelist = _barGraphPlotData(data) 328 _execute(command, filelist, keywords) 329 return
330 # 331 #Scatterplot 332 #
333 -def scatter(*data, **keywords):
334 try: 335 marker = keywords['marker'] 336 except: 337 marker = 'points' 338 command, filelist = _scatterData(data, marker = marker) 339 _execute(command, filelist, keywords)
340 341 342 # 343 # Generate a parallel-axes plot 344 #
345 -def parallelAxesPlot(data, **keywords):
346 try: 347 origin = keywords['origin'] 348 except KeyError: 349 origin = 0 350 command, filelist = _parallelAxesPlotData(data, origin) 351 _execute(command, filelist, keywords)
352 353 354 ############# 355 ## TESTING 356 ############# 357 import Biskit.test as BT 358
359 -class Test(BT.BiskitTest):
360 """Test case""" 361
362 - def prepare(self):
363 self.fout = tempfile.mktemp('ps','testgnuplot_')
364
365 - def cleanUp(self):
366 import Biskit.tools as T 367 T.tryRemove( self.fout )
368
369 - def test_plot2ps(self):
370 """gnuplot.plot to file test""" 371 plot([1, 5, 3, 4], file = self.fout) 372 if self.local: 373 print 'plot written to ', self.fout
374
375 - def test_scatter(self):
376 """gnuplot.scatter test (interactive only)""" 377 from numpy.oldnumeric.random_array import poisson 378 if self.local: 379 self.p = scatter( poisson(50,(1000,2)) )
380
381 - def test_plot( self ):
382 """gnuplot.plot test""" 383 # List of (x, y) pairs 384 # plot([(0.,1),(1.,5),(2.,3),(3.,4)]) 385 # plot( zip( range(10), range(10) ) ) 386 387 # Two plots; each given by a 2d array 388 import numpy.oldnumeric as N 389 x = N.arange(10) 390 y1 = x**2 391 y2 = (10-x)**2 392 plot( N.transpose(N.array([x, y1])), N.transpose(N.array([x, y2])))
393
394 - def test_parallelAxesPlot(self):
395 """gnuplot.parallelAxesPlot test (interactive only)""" 396 if self.local: 397 data = [[0., 1., 0.], [1., -1., 0.], [0.5, 0., 1.]] 398 parallelAxesPlot(data)
399 400 401 if __name__ == '__main__': 402 403 BT.localTest() 404