#!/usr/bin/python ''' Created on Oct 22, 2011 @author: crohr ''' ###################################################################### # standard ###################################################################### class standard: _labels = [] _times = [] _vals = [] ###################################################################### # read CSV ###################################################################### def readCSV(self, filename, xDim, yDim, computeSum): import csv, sys import numpy as np import re with open(filename, 'rU') as f: reader = csv.reader(f) rownum = 0 try: for row in reader: if(len(row) > 0): if(rownum == 0): id = 1 prfx = "" if(prefix is not None): prfx = prefix if (computeSum is True): idx = 0 lbl2idx = dict() id2idx = dict() prog = re.compile("(" + prfx + ".*)") for s in row[1::]: val = prog.findall(s) if(len(val) > 0): lbl = s[:s.find('__')] if( not lbl in lbl2idx ): lbl2idx[lbl] = idx id2idx[id] = idx self._labels.append(lbl) idx += 1 else: id2idx[id] = lbl2idx[lbl] id += 1 #print self._labels #print lbl2idx #print row[1::] #print id2idx else: id2idx = [] prog = re.compile(prfx) for label in row[1::]: #val = re.findall("(" + prfx + ".*)", label) #val = re.findall(prfx, label) #if(len(val)>0): val = prog.search(label) if(val is not None): self._labels.append(label) id2idx.append(id) id += 1 else: if (computeSum is True): r = np.zeros(len(lbl2idx), float) for k,v in id2idx.items(): r[v] += float(row[k]) else: r = np.zeros(len(id2idx), float) i = 0 for id in id2idx: r[i] = row[id] i += 1 if(rownum > 0): if(len(self._vals) < rownum): try: self._times.append(float(row[0])) except: self._times.append(float(rownum-1)) self._vals.append(r) else: self._vals[rownum-1] = np.hstack((self._vals[rownum-1],r)) rownum+=1 except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) return ###################################################################### # plot ###################################################################### def plotCSV(self, xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum): import numpy as np if(pBegin <= 0 or pBegin > pEnd): pBegin = 0 if(pEnd <= 0 or pEnd > len(self._times)): pEnd = len(self._times) if(show3D is True): return self.plot3D(xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum) elif(showHistogram is True): return self.plotHistogram(xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum) elif(showHeatMap is True): return self.plotHeatMap(xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum) else: return self.plot2D(xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum) ###################################################################### # plot2D ###################################################################### def plot2D(self, xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum): import numpy as np from matplotlib.lines import Line2D #fig.clear() fig = plt.figure(1) ax = fig.add_subplot(111) v = np.array(self._vals) if(marker is True): for p,m in zip(xrange(0,len(v[0]),1), Line2D.filled_markers): ax.plot(self._times[pBegin:pEnd], v[pBegin:pEnd , p], linestyle='-', marker=m, label = self._labels[p]) else: for p in xrange(0,len(v[0]),1): ax.plot(self._times[pBegin:pEnd], v[pBegin:pEnd , p], linestyle='-', label = self._labels[p]) ax.grid('on') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xscale(xscale) ax.set_yscale(yscale) if(vMin is not None): ax.set_ylim(ymin=vMin) if(vMax is not None): ax.set_ylim(ymax=vMax) if(legend >= 0 and legend <= 10): # Put a legend to the right of the current axis #ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), scatterpoints=1, fancybox=True) #if hasattr(ax.legend, 'framealpha'): # leg = ax.legend(loc=legend, fancybox=True, framealpha=0.5) #else: leg = ax.legend(loc=legend, fancybox=True) leg.get_frame().set_alpha(0.5) #handles, labels = ax.get_legend_handles_labels() #lgd = ax.legend(handles, labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) return plt ###################################################################### # plotHistogram ###################################################################### def plotHistogram(self, xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum): import numpy as np from matplotlib.lines import Line2D #fig.clear() fig = plt.figure(1) ax = fig.add_subplot(111) v = np.array(self._vals) for p in xrange(0,len(v[0]),1): ax.hist(v[pBegin:pEnd , p], bins=100, normed=False) ax.grid('on') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xscale(xscale, nonposy='clip') ax.set_yscale(yscale, nonposy='clip') if(legend >= 0 and legend <= 10): # Put a legend to the right of the current axis #ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), scatterpoints=1, fancybox=True) #if hasattr(ax.legend, 'framealpha'): # leg = ax.legend(loc=legend, fancybox=True, framealpha=0.5) #else: leg = ax.legend(loc=legend, fancybox=True) leg.get_frame().set_alpha(0.5) #handles, labels = ax.get_legend_handles_labels() #lgd = ax.legend(handles, labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) return plt ###################################################################### # plotHeatMap ###################################################################### def plotHeatMap(self, xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum): import numpy as np from matplotlib import cm #fig.clear() fig = plt.figure(1) ax = fig.add_subplot(111) V = np.array(self._vals).T try: Y = np.array(map(float,self._labels)) except: Y = np.arange(len(self._labels)) X = np.array(self._times) dim = [min(X),max(X),min(Y),max(Y)] im = ax.imshow(V, origin="lower", vmin=vMin, vmax=vMax, extent=dim, interpolation="nearest", aspect='auto', cmap=cm.gnuplot ) cbar = fig.colorbar(im, orientation='vertical') #cbar.ax.set_xticklabels(['Low', 'Medium', 'High'])# horizontal colorbar ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) return plt ###################################################################### # plot3D ###################################################################### def plot3D(self, xDim, yDim, pCols, pRows, pBegin, pEnd, computeSum): import math import numpy as np from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm #fig.clear() fig = plt.figure(1) ax = fig.add_subplot(111, projection='3d') #ax = fig.gca(projection='3d') Z = np.array(self._vals).T try: Y = np.array(map(float,self._labels)) except: Y = np.arange(len(self._labels)) X = np.array(self._times) X, Y = np.meshgrid(X, Y) #print X #print Y #print Z #ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1) ax.plot_surface(X, Y, Z, rstride=1, cstride=10, vmin=vMin, vmax=vMax, alpha=0.3, cmap=cm.gnuplot) #cset = ax.contour(X, Y, Z, zdir='z', offset=0, cmap=cm.gnuplot) #cset = ax.contour(X, Y, Z, zdir='x', offset=0) #cset = ax.contour(X, Y, Z, zdir='y', offset=0) ax.view_init(azim=angle) ax.grid('on') ax.set_xlabel(xlabel) #ax.set_xlim(-1, xDim) ax.set_ylabel(ylabel) #ax.set_ylim(-1, yDim) ax.set_zlabel(zlabel) ax.set_zlim(vMin, vMax) #ax.set_zscale('log') return plt ###################################################################### # grid ###################################################################### class grid: _labels = [] _times = [] _vals = [] _pos = [] _min = 0 _max = 0 _scatter = False ###################################################################### # plot ###################################################################### def plotCSV(self, xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm import numpy as np import itertools numPlots = pCols * pRows if(pBegin < 0 or pBegin > len(self._vals)): pBegin = 0 if(pBegin > pEnd or pEnd > len(self._vals)): pEnd = len(self._vals) if (numPlots > 1): fStride = int((pEnd-pBegin) / (numPlots)) #pBegin += fStride else: fStride = 1 if (final is True): pBegin = len(self._vals)-1 pEnd = len(self._vals) if (vMin is None): vMin = self._min if (vMax is None): vMax = self._max fig.clear() if(show3D is True): self.plot3D(xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots) elif(self._scatter is True): self.plotScatter(xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots) else: self.plot2D(xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots) # f=1 # for i in xrange(n,pEnd,fStride): # if(f <= numPlots): # plt.subplot(pRows, pCols, f) # plt.title('t='+self._times[i]) # plt.pcolormesh(self._vals[i], vmin=vmin, vmax=vmax, shading='flat', edgecolors='face', linewidth=1) # plt.axis([0, xDim-1, 0, yDim-1]) # cb = plt.colorbar() # #cb.set_label('N') # f += 1 return ###################################################################### # plot3D ###################################################################### def plot3D(self, xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm import numpy as np import itertools #print str(pBegin) + ", " + str(pEnd) + ", " + str(fStride) idx = range(pBegin,pEnd,fStride) #print idx f=0 for i in idx: if(f < numPlots): ax = fig.add_subplot(pRows,pCols,f, projection='3d') Z = self._vals[i] X = np.arange(0,len(Z),1) Y = np.arange(0,len(Z[0]),1) X, Y = np.meshgrid(X, Y) surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, vmin=vMin, vmax=vMax, alpha=0.5, cmap=cm.gnuplot) #cset = ax.contour(X, Y, Z, zdir='z', offset=0, cmap=cm.gnuplot) #cset = ax.contour(X, Y, Z, zdir='x', offset=0) #cset = ax.contour(X, Y, Z, zdir='y', offset=yDim-1) ax.view_init(azim=angle) if(len(self._pos) > 0): k = 0 while k < 1 and (i-k) > 0: X = self._pos[i-k]['x'] Y = self._pos[i-k]['y'] markers = itertools.cycle(('v','^','<','>','s','p','H','o','D')) colors = iter(cm.jet(np.linspace(0, 1, len(self._labels)))) labels = iter(self._labels) if(k == 0): for x,y in zip(X,Y): im = ax.scatter(x,y, color=next(colors), s = 100, alpha=0.8, marker=next(markers), edgecolor='black', label=next(labels)) else: for x,y in zip(X,Y): im = ax.scatter(x,y, color=next(colors), s = 100, alpha=0.8/(k+1), marker=next(markers), edgecolor='black') k += 1 ax.set_xlabel(xlabel) ax.set_xlim(-1, xDim) ax.set_ylabel(ylabel) ax.set_ylim(-1, yDim) ax.set_zlabel(zlabel) ax.set_zlim(vMin, vMax) #ax.set_zscale('log') #fig.colorbar(surf, shrink=0.5, aspect=10) f += 1 #if(legend is True): # Put a legend to the right of the current axis #grid[-1].legend(loc='center left', bbox_to_anchor=(1, 0.5), fancybox=True, scatterpoints=1) title = "" if(prefix is not None): title = prefix + ", " if(numPlots == 1): title += "T = " + str(self._times[idx[0]]) elif(numPlots == 2): title += "T = " + str(self._times[idx[0]]) + ", " + str(self._times[idx[len(idx)-1]]) else: title += "T = " + str(self._times[pBegin]) + ", ... , " + str(self._times[idx[len(idx)-1]]) fig.suptitle(title) return ###################################################################### # plot2D ###################################################################### def plot2D(self, xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm import numpy as np import itertools grid = ImageGrid(fig, 111, # similar to subplot(111) nrows_ncols = (pRows, pCols), axes_pad = 0.10, label_mode = "L", share_all = True, cbar_location="top", cbar_mode="single", #[None|single|each] cbar_size="2%", cbar_pad="2%", ) #print str(pBegin) + ", " + str(pEnd) + ", " + str(fStride) idx = range(pBegin,pEnd,fStride) #print idx f=0 for i in idx: if(f < numPlots): Z = self._vals[i] #X = np.arange(0,len(Z),1) #Y = np.arange(0,len(Z[0]),1) #X, Y = np.meshgrid(X, Y) ax = grid[f] im = ax.imshow(Z, origin="lower", vmin=vMin, vmax=vMax, interpolation="nearest", cmap=cm.gnuplot) ax.set_xlabel(xlabel) #ax.set_xlim(0, xDim) ax.set_ylabel(ylabel) #ax.set_ylim(0, yDim) ax.grid('on') ax.cax.colorbar(im) f += 1 title = "" if(prefix is not None): title = prefix + ", " if(numPlots == 1): title += "T = " + str(self._times[idx[0]]) elif(numPlots == 2): title += "T = " + str(self._times[idx[0]]) + ", " + str(self._times[idx[len(idx)-1]]) else: title += "T = " + str(self._times[pBegin]) + ", ... , " + str(self._times[idx[len(idx)-1]]) fig.suptitle(title) return ###################################################################### # plotScatter ###################################################################### def plotScatter(self, xDim, yDim, pCols, pRows, pBegin, pEnd, vMin, vMax, final, fStride, numPlots): from mpl_toolkits.mplot3d import Axes3D from mpl_toolkits.axes_grid1 import ImageGrid from mpl_toolkits.axes_grid1 import make_axes_locatable from matplotlib import cm import numpy as np import itertools grid = ImageGrid(fig, 111, # similar to subplot(111) nrows_ncols = (pRows, pCols), axes_pad = 0.10, label_mode = "L", share_all = True, cbar_location="top", cbar_mode="single", cbar_size="2%", cbar_pad="2%", ) #print str(pBegin) + ", " + str(pEnd) + ", " + str(fStride) idx = range(pBegin,pEnd,fStride) #print idx f=0 for i in idx: if(f < numPlots): k = 0 while k < 1 and (i-k) > 0: X = self._pos[i-k]['x'] Y = self._pos[i-k]['y'] Z = self._vals[i] markers = itertools.cycle(('v','^','<','>','s','p','H','o','D')) colors = iter(cm.jet(np.linspace(0, 1, len(self._labels)))) labels = iter(self._labels) ax = grid[f] if(k == 0): for x,y in zip(X,Y): im = ax.scatter(x,y, color=next(colors), s = 100, alpha=0.8, marker=next(markers), edgecolor='black', label=next(labels)) im = ax.imshow(Z, origin="lower", vmin=vMin, vmax=vMax, interpolation="nearest", cmap=cm.gnuplot ) ax.cax.colorbar(im) else: for x,y in zip(X,Y): im = ax.scatter(x,y, color=next(colors), s = 100, alpha=0.8/(k+1), marker=next(markers), edgecolor='black') ax.set_xlabel(xlabel) ax.set_xlim(-1, xDim) ax.set_ylabel(ylabel) ax.set_ylim(-1, yDim) ax.grid('on') k += 1 f += 1 #if(legend is True): # Put a legend to the right of the current axis #grid[-1].legend(loc='center left', bbox_to_anchor=(1, 0.5), fancybox=True, scatterpoints=1) title = "" if(prefix is not None): title = prefix + ", " if(numPlots == 1): title += "T = " + str(self._times[idx[0]]) elif(numPlots == 2): title += "T = " + str(self._times[idx[0]]) + ", " + str(self._times[idx[len(idx)-1]]) else: title += "T = " + str(self._times[pBegin]) + ", ... , " + str(self._times[idx[len(idx)-1]]) fig.suptitle(title) return ###################################################################### # grid 2d ###################################################################### class grid2D(grid): ###################################################################### # read CSV ###################################################################### def readCSV(self, filename, xDim, yDim, computeSum, prefix): import csv, sys import numpy as np import re with open(filename, 'rU') as f: reader = csv.reader(f) rownum = 0 coords = dict() try: for row in reader: if(len(row) > 0): if(rownum == 0 ): if(prefix is None): s = row[1] prefix = s[:s.find('__')] print "automatic prefix selected: " + prefix else: print "manual prefix used: " + prefix i = 1 if(multiple is True): for val in row[1:]: coord = re.findall(prefix + "[_]+(\d+)[_]+(\d+)[_]+([a-zA-Z0-9]+)[_]+", val) if(len(coord) == 1): key = (int(coord[0][0])-1, int(coord[0][1])-1) val = coords.get(key, {}) val[coord[0][2]] = i coords[key] = val i = i + 1 else: for val in row[1:]: coord = re.findall(prefix + "[_]+(\d+)[_]+(\d+)[_]+", val) if(len(coord) == 1): key = (int(coord[0][0])-1, int(coord[0][1])-1) coords[key] = i i = i + 1 else: self._times.append(row[0]) r = np.zeros((yDim,xDim),float) if(multiple is True): for key,val in coords.items(): A = float(row[val['A']]) B = float(row[val['B']]) if(computeSum is True): r[key[1],key[0]] = A + B elif(A > 0 or B > 0): r[key[1],key[0]] = B / (A + B) else: for key,val in coords.items(): r[key[1],key[0]] = row[val] self._min = min(self._min, r.min()) self._max = max(self._max, r.max()) self._vals.append(r) rownum+=1 except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) except IndexError, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) return ###################################################################### # grid XY ###################################################################### class gridXY(grid): ###################################################################### # read CSV ###################################################################### def readCSV(self, filename, xDim, yDim, computeSum, prefix): import csv, sys import numpy as np import re #if(prefix is None): # self._scatter = True #else: # self._scatter = False with open(filename, 'rU') as f: reader = csv.reader(f) rownum = 0 coords = dict() data = dict() try: for row in reader: if(len(row) > 0): if(rownum == 0 ): i = 1 for val in row[1:]: if(prefix is not None): d = re.findall(prefix + "_(.+)", val) if(len(d) == 1): data[d[0]] = i dim = re.findall("([xXyY])_(.+)", val) if(len(dim) == 1): lbl = dim[0][1] coord = coords.get(lbl,(-1,-1)) if(dim[0][0].startswith(('X','x'))): coords[lbl] = (i,coord[1]) self._labels.append(lbl) elif(dim[0][0].startswith(('Y','y'))): coords[lbl] = (coord[0], i) i += 1 else: self._times.append(row[0]) r = np.zeros((yDim,xDim),float) p = dict() p['x'] = [] p['y'] = [] for lbl,coord in coords.items(): y = int(row[coord[1]])-offset x = int(row[coord[0]])-offset if(prefix is None): r[y,x] += 1.0 else: r[y,x] += float(row[data[lbl]]) p['x'].append(x) p['y'].append(y) self._pos.append(p) self._min = min(self._min, r.min()) self._max = max(self._max, r.max()) self._vals.append(r) rownum+=1 except csv.Error, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) except IndexError, e: sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) return ###################################################################### # helping class for making a recursive call on the files/directories ###################################################################### class recursiveCall: ###################################################################### # looks through the content of a directory ###################################################################### def handleDirectory(self, directory, xDim, yDim, cols, rows, begin, end, vMin, vMax, final, computeSum): matplotlib.use('PDF') contents = os.listdir(directory) #contents of the current directory for filename in contents: if ((filename.endswith('.csv') == True or filename.endswith('.dat') == True) and os.path.isfile(directory + "/" + filename) == True): p = grid2D() p.readCSV(directory + "/" + filename, xDim, yDim, computeSum, prefix) p.plotCSV(xDim, yDim, cols, rows, begin, end, vMin, vMax, final) plt.savefig(directory + "/" + filename + ".pdf", format='pdf') elif os.path.isdir(directory + "/" + filename) == True : if (recursive == True): self.handleDirectory(self, directory + "/" + filename, xDim, yDim, cols, rows, begin, end, vMin, vMax, final, computeSum) return ###################################################################### # make movie pdf ###################################################################### def makeMoviePDF(p, output, movie, xDim, yDim, begin, end, vMin, vMax): from matplotlib.backends.backend_pdf import PdfPages pp = PdfPages(output) if(begin < 0 or begin > len(p._vals)): begin = 0 if(begin > end or end > len(p._vals)): end = len(p._vals) pbar = progress(end-begin) for i in xrange(begin,end,1): p.plotCSV(xDim, yDim, 1, 1, i, end, vMin, vMax, False) plt.savefig(pp, format='pdf') pbar.inc() pp.close() return ###################################################################### # make movie png ###################################################################### def makeMovieIMG(p, output, movie, xDim, yDim, begin, end, vMin, vMax): if(begin < 0 or begin > len(p._vals)): begin = 0 if(begin > end or end > len(p._vals)): end = len(p._vals) pbar = progress(end-begin) for i in xrange(begin,end,1): p.plotCSV(xDim, yDim, 1, 1, i, end, vMin, vMax, False) plt.savefig(outputName + '_' + str(i) + '.' + outputFormat, format=outputFormat) pbar.inc() return ###################################################################### # make movie mp4 ###################################################################### def makeMovieMP4(p, output, movie, xDim, yDim, begin, end, vMin, vMax, frames): import matplotlib.animation as animation FFMpegWriter = animation.writers['ffmpeg'] metadata = dict(title=outputName, artist='Matplotlib', comment='') writer = FFMpegWriter(fps=frames, metadata=metadata, codec="libx264", extra_args=['-crf', '23']) #writer = FFMpegWriter(fps=frames, metadata=metadata, extra_args=['-q:v', '5']) if(begin < 0 or begin > len(p._vals)): begin = 0 if(begin > end or end > len(p._vals)): end = len(p._vals) pbar = progress(end-begin) with writer.saving(fig, output, 100): for i in xrange(begin,end,1): p.plotCSV(xDim, yDim, 1, 1, i, end, vMin, vMax, False) writer.grab_frame() pbar.inc() return ###################################################################### # progress ###################################################################### class progress: _expected = 0 _count = 0 _tic = 0 _next_tic = 0 _bar_length = 0 def __init__(self, expected = 1, bar_length = 20): self._expected = expected self._bar_length = bar_length def inc(self): self._count += 1 if ( self._count >= self._next_tic ): tics_needed = int((float(self._count)/float(self._expected))*100.0) while( self._tic < tics_needed ): percent = self._tic hashes = '#' * int(round((percent/100.0) * self._bar_length)) spaces = ' ' * (self._bar_length - len(hashes)) sys.stdout.write("\rProgress: [{0}] {1}%".format(hashes + spaces, percent)) sys.stdout.flush() self._tic += 1 self._next_tic_ = int((self._tic/100.0)*self._expected); if ( self._count == self._expected ): sys.stdout.write("\rProgress: [{0}] {1}%\n".format('#' * int(self._bar_length), 100)) sys.stdout.flush() ###################################################################### # main ###################################################################### import sys import os import optparse from os.path import exists if __name__=="__main__": parser = optparse.OptionParser("usage: %prog [options] file1 file2") # parser.add_option("-f", "--file", dest="filename", # type="string", # help="specify FILENAME to read from") parser.add_option("-o", "--output", dest="output", default=None, type="string", help="specify OUTPUT to write into, possible file types: .png, .pdf, .eps, .svg, .mp4") parser.add_option("-m", "--mode", dest="mode", default="standard", help="specify MODE: standard, grid2D, gridXY") parser.add_option("-p", "--prefix", dest="prefix", default=None, type="string", help="prefix used to identify columns") parser.add_option("-x", "--xdim", dest="xdim", default=1, type="int", help="dimension x of grid") parser.add_option("-y", "--ydim", dest="ydim", default=1, type="int", help="dimension y of grid") parser.add_option("-c", "--columns", dest="cols", default=1, type="int", help="number of columns to plot") parser.add_option("-r", "--rows", dest="rows", default=1, type="int", help="number of rows to plot") parser.add_option("-b", "--begin", dest="begin", default=-1, type="int", help="index of data set to start") parser.add_option("-e", "--end", dest="end", default=-1, type="int", help="index of data set to end") parser.add_option("--min", dest="minimum", default=None, type="float", help="minimum value of data range; if no value is given, it's taken from data (grid2D only)") parser.add_option("--max", dest="maximum", default=None, type="float", help="maximum value of data range; if no value is given, it's taken from data (grid2D only)") parser.add_option("--offset", dest="offset", default=0, type="int", help="offset used for gridXY (default:0)") parser.add_option("-d", "--directory", dest="dir", default=None, type="string", help="directory DIR in that csv files are searched for") parser.add_option("--recursive", dest="recursive", action="store_true", default=False, help="search for files recursively") parser.add_option("-l", "--final", dest="final", action="store_true", default=False, help="use the final snapshot only") parser.add_option("--movie", dest="movie", action="store_true", default=False, help="mode 'grid2D' and 'gridXY': make movie of data sets") parser.add_option("--frames", dest="frames", default=10, type="int", help="mode 'grid2D' and 'gridXY': number of frames used per data point (default: 10)") parser.add_option("--multiple", dest="multiple", action="store_true", default=False, help="mode 'grid2D': multiple values (A and B) on one position") parser.add_option("-s", "--sum", dest="sum", action="store_true", default=False, help="mode 'standard': compute the sum of all values at one single time point; mode 'grid2D': compute the sum of A and B instead of their proportion") parser.add_option("--histogram", dest="showHistogram", action="store_true", default=False, help="show histogram plot") parser.add_option("-H", "--HeatMap", dest="showHeatMap", action="store_true", default=False, help="show heat map plot") parser.add_option("-3", "--3D", dest="show3D", action="store_true", default=False, help="show 3D plot") parser.add_option("-a", "--angle", dest="angle", default=-45, type="float", help="mode 'grid2D' and 'gridXY': set angle in 3D plot (default:-45)") parser.add_option("--marker", dest="marker", action="store_true", default=False, help="show marker in 2D plot") parser.add_option("--xlabel", dest="xlabel", default="x", type="string", help="x-axis label") parser.add_option("--ylabel", dest="ylabel", default="y", type="string", help="y-axis label") parser.add_option("--zlabel", dest="zlabel", default="z", type="string", help="z-axis label") parser.add_option("--xscale", dest="xscale", default="linear", type="string", help="x-axis available scales are: 'linear', 'log', 'logit', 'symlog'") parser.add_option("--yscale", dest="yscale", default="linear", type="string", help="y-axis available scales are: 'linear', 'log', 'logit', 'symlog'") parser.add_option("--zscale", dest="zscale", default="linear", type="string", help="z-axis available scales are: 'linear', 'log', 'logit', 'symlog'") parser.add_option("--legend", dest="legend", default=-1, type="int", help="show legend in plot, codes = {'lower right': 4, 'upper right': 1, 'lower center': 8, 'lower left': 3, 'upper left': 2, 'best': 0, 'right': 5, 'center right': 7, 'upper center': 9, 'center left': 6, 'center': 10}") parser.add_option("--usetex", dest="usetex", action="store_true", default=False, help="use LaTeX to manage all text layout") if (len(sys.argv) == 1): parser.print_help() sys.exit() (options, args) = parser.parse_args() if (len(args) == 0 and options.dir is None): parser.error("incorrect number of arguments") # filename = options.filename output = options.output if (output is not None): outputName = output[:output.rindex('.')] outputFormat = output[output.rindex('.')+1:] mode = options.mode prefix = options.prefix movie = options.movie xDim = options.xdim yDim = options.ydim cols = options.cols rows = options.rows vMin = options.minimum vMax = options.maximum begin = options.begin end = options.end offset = options.offset directory = options.dir recursive = options.recursive final = options.final multiple = options.multiple computeSum = options.sum showHistogram = options.showHistogram showHeatMap = options.showHeatMap show3D = options.show3D angle = options.angle marker = options.marker frames = options.frames xlabel = options.xlabel ylabel = options.ylabel zlabel = options.zlabel xscale = options.xscale yscale = options.yscale zscale = options.zscale legend = options.legend usetex = options.usetex import matplotlib if(usetex is True): matplotlib.rcParams['text.usetex']=True matplotlib.rcParams['text.latex.unicode'] = True matplotlib.rcParams['font.family']='serif' #matplotlib.rcParams['font.serif']=[] #matplotlib.rcParams['font.sans-serif']=[] #matplotlib.rcParams['font.monospace']=[] #matplotlib.rcParams['pgf.texsystem']='pdflatex' #matplotlib.rcParams['pgf.rcfonts']=False else: matplotlib.rcParams['text.usetex']=False matplotlib.rcParams['font.family']='serif' print 'matplotlib version ' + matplotlib.__version__ #print matplotlib.__file__ if (directory is None): for filename in args: if (exists(filename) is False): sys.exit('file %s not found' % (filename)) if (output is not None): matplotlib.use('agg') print 'write output to ' + output import matplotlib.pyplot as plt fig = plt.figure() #print fig.canvas.get_supported_filetypes() if (output is not None): if(fig.canvas.get_supported_filetypes().has_key(outputFormat) is not True and outputFormat != "mp4"): sys.exit('unsupported output format!') if(mode == "standard"): p = standard() for filename in args: p.readCSV(filename, xDim, yDim, computeSum) p.plotCSV(xDim, yDim, cols, rows, begin, end, computeSum) if (output is None): plt.show() else: plt.savefig(output,format=outputFormat) elif(mode == "grid2D" or mode == "gridXY"): if(mode == "grid2D"): p = grid2D() else: p = gridXY() for filename in args: p.readCSV(filename, xDim, yDim, computeSum, prefix) if (movie is True and output is not None): if(outputFormat == "mp4"): makeMovieMP4(p, output, movie, xDim, yDim, begin, end, vMin, vMax, frames) elif(outputFormat == "pdf"): makeMoviePDF(p, output, movie, xDim, yDim, begin, end, vMin, vMax) else: makeMovieIMG(p, output, movie, xDim, yDim, begin, end, vMin, vMax) else: p.plotCSV(xDim, yDim, cols, rows, begin, end, vMin, vMax, final) if (output is None): plt.show() else: #plt.tight_layout() plt.savefig(output,format=outputFormat) else: sys.exit('wrong data mode') else: if (exists(directory) is False): sys.exit('directory %s not found' % (filename)) call = recursiveCall() call.handleDirectory(directory, xDim, yDim, cols, rows, begin, end, vMin, vMax, final, computeSum)