home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) import cStringIO import os import string import sys import tempfile import time import types import Gnuplot as Gnuplot_ori import Numeric from IPython.genutils import popkey, xsys gp = Gnuplot_ori.gp try: OptionException = Gnuplot_ori.PlotItems.OptionException except AttributeError: OptionException = Gnuplot_ori.Errors.OptionError Data = Gnuplot_ori.Data Func = Gnuplot_ori.Func GridData = Gnuplot_ori.GridData PlotItem = Gnuplot_ori.PlotItem PlotItems = Gnuplot_ori.PlotItems if Gnuplot_ori.__version__ <= '1.6': _BaseFileItem = PlotItems.File _BaseTempFileItem = PlotItems.TempFile class File(_BaseFileItem): _option_list = _BaseFileItem._option_list.copy() _option_list.update({ 'index': (lambda self, index: self.set_option_index(index)) }) def __init__(self, *args, **kw): self._option_sequence = [ 'binary', 'index', 'using', 'smooth', 'axes', 'title', 'with'] _BaseFileItem.__init__(self, *args, **kw) __newdoc = "Additional Keyword arguments added by IPython:\n\n 'index=<int>' -- similar to the `index` keyword in Gnuplot.\n This allows only some of the datasets in a file to be\n plotted. Datasets within a file are assumed to be separated\n by _pairs_ of blank lines, and the first one is numbered as\n 0 (similar to C/Python usage)." __init__.__doc__ = PlotItems.File.__init__.__doc__ + __newdoc def set_option_index(self, index): if index is None: self.clear_option('index') elif type(index) in [ type(''), type(1)]: self._options['index'] = (index, 'index %s' % index) elif type(index) is type(()): self._options['index'] = (index, 'index %s' % string.join(map(repr, index), ':')) else: raise OptionException('index=%s' % (index,)) return index is None _FileClass = File elif Gnuplot_ori.__version__ == '1.7': _FileClass = _BaseFileItem = PlotItems._FileItem _BaseTempFileItem = PlotItems._TempFileItem File = PlotItems.File else: _FileClass = _BaseFileItem = PlotItems._FileItem _BaseTempFileItem = PlotItems._NewFileItem File = PlotItems.File def eps_fix_bbox(fname): print 'Fixing eps file: <%s>' % fname xsys('ps2eps -f -q -l %s' % fname) if fname.endswith('.eps'): os.rename(fname + '.eps', fname) def is_list1d(x, containers = [ types.ListType, types.TupleType]): x_type = type(x) if (x_type == Numeric.ArrayType or len(x.shape) == 1) and x_type in containers: pass return type(x[0]) not in containers + [ Numeric.ArrayType] def zip_items(items, titles = None): class StandaloneItem(Exception): pass def get_titles(titles): try: title = titles[tit_ct[0]] except IndexError: titles = None title = None tit_ct[0] += 1 return (title, titles) new_items = [] if titles: tit_ct = [ 0] n = 0 while n < len(items): item = items[n] try: if is_list1d(item): if n == len(items) - 1: raise StandaloneItem n == len(items) - 1 next_item = items[n + 1] if next_item is None: n += 1 raise StandaloneItem next_item is None if is_list1d(next_item): if titles: (title, titles) = get_titles(titles) else: title = None new_items.append(Data(zip(item, next_item), title = title)) n += 1 else: raise StandaloneItem is_list1d(next_item) new_items.append(item) except StandaloneItem: if titles: (title, titles) = get_titles(titles) else: title = None new_items.append(Data(zip(range(len(item)), item), title = title)) except AttributeError: new_items.append(item) n += 1 return new_items class String(_FileClass): def __init__(self, data_str, **keyw): self.tmpfile = _BaseTempFileItem() tmpfile = file(self.tmpfile.filename, 'w') tmpfile.write(data_str) _BaseFileItem.__init__(self, self.tmpfile, **keyw) class Gnuplot(Gnuplot_ori.Gnuplot): def xrange(self, min = '*', max = '*'): self('set xrange [%s:%s]' % (min, max)) def yrange(self, min = '*', max = '*'): self('set yrange [%s:%s]' % (min, max)) def zrange(self, min = '*', max = '*'): self('set zrange [%s:%s]' % (min, max)) def x2range(self, min = '*', max = '*'): self('set xrange [%s:%s]' % (min, max)) def y2range(self, min = '*', max = '*'): self('set yrange [%s:%s]' % (min, max)) def z2range(self, min = '*', max = '*'): self('set zrange [%s:%s]' % (min, max)) def rrange(self, min = '*', max = '*'): self('set rrange [%s:%s]' % (min, max)) def trange(self, min = '*', max = '*'): self('set trange [%s:%s]' % (min, max)) def urange(self, min = '*', max = '*'): self('set urange [%s:%s]' % (min, max)) def vrange(self, min = '*', max = '*'): self('set vrange [%s:%s]' % (min, max)) def set_ps(self, option): self('set terminal postscript %s ' % option) self('set terminal %s' % gp.GnuplotOpts.default_term) def __plot_ps(self, plot_method, *items, **keyw): psargs = { 'filename': None, 'mode': None, 'eps': None, 'enhanced': None, 'color': None, 'solid': None, 'duplexing': None, 'fontname': None, 'fontsize': None, 'debug': 0 } for k in psargs.keys(): if keyw.has_key(k): psargs[k] = keyw[k] del keyw[k] continue hardcopy = popkey(keyw, 'hardcopy', psargs['filename'] is not None) titles = popkey(keyw, 'titles', 0) if hardcopy: if psargs['filename'] is None: raise ValueError, 'If you request hardcopy, you must give a filename.' psargs['filename'] is None self('set term dumb') if os.name == 'posix': self('set output "/dev/null"') new_items = zip_items(items, titles) plot_method(self, *new_items, **keyw) if hardcopy: if psargs['filename'].endswith('.eps'): psargs['eps'] = 1 self.hardcopy(**psargs) def plot(self, *items, **keyw): self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.plot, *items, **keyw) def plot2(self, arg, **kw): if hasattr(arg, 'keys'): keys = arg.keys() keys.sort() else: keys = range(len(arg)) pitems = [ Data(zip(range(len(arg[k])), arg[k]), title = `k`) for k in keys ] self.plot(*pitems, **kw) def splot(self, *items, **keyw): self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.splot, *items, **keyw) def replot(self, *items, **keyw): self._Gnuplot__plot_ps(Gnuplot_ori.Gnuplot.replot, *items, **keyw) def hardcopy(self, filename = None, mode = None, eps = None, enhanced = None, color = None, solid = None, duplexing = None, fontname = None, fontsize = None, debug = 0): if filename is None: filename = gp.GnuplotOpts.default_lpr lpr_output = 1 elif filename.endswith('.eps'): eps = 1 lpr_output = 0 setterm = [ 'set', 'terminal', 'postscript'] if eps: setterm.append('eps') elif mode is not None: setterm.append(mode) if enhanced is None: enhanced = gp.GnuplotOpts.prefer_enhanced_postscript if enhanced is not None: if enhanced: setterm.append('enhanced') else: setterm.append('noenhanced') if color is not None: if color: setterm.append('color') else: setterm.append('monochrome') if solid is not None: if solid: setterm.append('solid') else: setterm.append('dashed') if duplexing is not None: setterm.append(duplexing) if fontname is not None: setterm.append('"%s"' % fontname) if fontsize is not None: setterm.append('%s' % fontsize) self(string.join(setterm)) self.set_string('output', filename) self.refresh() if not lpr_output: maxtries = 20 delay = 0.1 for i in range(maxtries): time.sleep(0.05) if os.path.isfile(filename): if debug: print 'Hardcopy to file <%s> success at attempt #%s.' % (filename, i + 1) break time.sleep(delay) self(string.join(setterm)) self.set_string('output', filename) self.refresh() if not os.path.isfile(filename): print >>sys.stderr, 'ERROR: Tried %s times and failed to create hardcopy file `%s`' % (maxtries, filename) self('set terminal %s' % gp.GnuplotOpts.default_term) self.set_string('output')