Beispiel #1
0
 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
Beispiel #2
0
class pdb_handler(object):
    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 execute(self, line):
        tt = self.cout.tell()
        self.debugger.onecmd(line)
        self.cout.seek(tt)
        return self.cout.read()
class pdb_handler(object):
    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 execute(self, line):
        tt = self.cout.tell()
        self.debugger.onecmd(line)
        self.cout.seek(tt)
        return self.cout.read()
    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 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)
Beispiel #6
0
 def _repr_png_(self):
     canvas = FigureCanvasAgg(self.figure)
     f = StringIO()
     canvas.print_figure(f)
     f.seek(0)
     return f.read()