def __init__(self, tb): self.cin = StringIO() sys.stdin = self.cin self.cout = StringIO() sys.stdout = self.cout sys.stderr = self.cout self.debugger = pdb.Pdb(stdin=self.cin, stdout=self.cout) self.debugger.reset() self.debugger.setup(tb.tb_frame, tb)
def _repr_html_(self): ret = '' for k, v in self.plots.iteritems(): canvas = FigureCanvasAgg(v) f = StringIO() canvas.print_figure(f) f.seek(0) img = base64.b64encode(f.read()) ret += '<img src="data:image/png;base64,%s"><br>' % img return ret
def paste_traceback(exc_type, exc, tb): """ This is a traceback handler that knows how to paste to the pastebin. Should only be used in sys.excepthook. """ sys.__excepthook__(exc_type, exc, tb) from yt.extern.six.moves import StringIO, xmlrpc_client p = xmlrpc_client.ServerProxy("http://paste.yt-project.org/xmlrpc/", allow_none=True) s = StringIO() traceback.print_exception(exc_type, exc, tb, file=s) s = s.getvalue() ret = p.pastes.newPaste('pytb', s, None, '', '', True) print() print("Traceback pasted to http://paste.yt-project.org/show/%s" % (ret)) print()
def paste_traceback_detailed(exc_type, exc, tb): """ This is a traceback handler that knows how to paste to the pastebin. Should only be used in sys.excepthook. """ import cgitb from yt.extern.six.moves import StringIO, xmlrpc_client s = StringIO() handler = cgitb.Hook(format="text", file=s) handler(exc_type, exc, tb) s = s.getvalue() print(s) p = xmlrpc_client.ServerProxy("http://paste.yt-project.org/xmlrpc/", allow_none=True) ret = p.pastes.newPaste('text', s, None, '', '', True) print() print("Traceback pasted to http://paste.yt-project.org/show/%s" % (ret)) print()
def execute(self, code): """Execute the given code in self.locals and return any stdout/sterr.""" out = StringIO() oldout = sys.stdout olderr = sys.stderr sys.stdout = sys.stderr = out try: try: exec code in self.locals except: result = traceback.format_exc() else: result = out.getvalue() finally: sys.stdout = oldout sys.stderr = olderr out.close() return result
def plot(self, fn=None, profile_field=None, profile_weight=None): """ Save the current transfer function to a bitmap, or display it inline. Parameters ---------- fn: string, optional Filename to save the image to. If None, the returns an image to an IPython session. Returns ------- If fn is None, will return an image to an IPython notebook. """ if self.tf is None: self.build_transfer_function() tf = self.tf if self.log: xfunc = np.logspace xmi, xma = np.log10(self.bounds[0]), np.log10(self.bounds[1]) else: xfunc = np.linspace # Need to strip units off of the bounds to avoid a recursion error # in matplotlib 1.3.1 xmi, xma = [np.float64(b) for b in self.bounds] x = xfunc(xmi, xma, tf.nbins) y = tf.funcs[3].y w = np.append(x[1:] - x[:-1], x[-1] - x[-2]) colors = np.array( [tf.funcs[0].y, tf.funcs[1].y, tf.funcs[2].y, np.ones_like(x)]).T fig = Figure(figsize=[6, 3]) canvas = FigureCanvasAgg(fig) ax = fig.add_axes([0.2, 0.2, 0.75, 0.75]) ax.bar(x, tf.funcs[3].y, w, edgecolor=[0.0, 0.0, 0.0, 0.0], log=self.log, color=colors, bottom=[0]) if profile_field is not None: try: prof = self.profiles[self.field] except KeyError: self.setup_profile(profile_field, profile_weight) prof = self.profiles[self.field] if profile_field not in prof.keys(): prof.add_fields([profile_field], fractional=False, weight=profile_weight) # Strip units, if any, for matplotlib 1.3.1 xplot = np.array(prof[self.field]) yplot = np.array(prof[profile_field] * tf.funcs[3].y.max() / prof[profile_field].max()) ax.plot(xplot, yplot, color='w', linewidth=3) ax.plot(xplot, yplot, color='k') ax.set_xscale({True: 'log', False: 'linear'}[self.log]) ax.set_xlim(x.min(), x.max()) ax.set_xlabel(self.ds._get_field_info(self.field).get_label()) ax.set_ylabel(r'$\mathrm{alpha}$') ax.set_ylim(y.max() * 1.0e-3, y.max() * 2) if fn is None: from IPython.core.display import Image f = StringIO() canvas.print_figure(f) f.seek(0) img = f.read() return Image(img) else: fig.savefig(fn)
def _repr_png_(self): canvas = FigureCanvasAgg(self.figure) f = StringIO() canvas.print_figure(f) f.seek(0) return f.read()