Esempio n. 1
0
    def collect(self,
                blocks,
                template_name,
                outdir,
                rstdir,
                builddir,
                srcdir,
                content,
                display_options,
                tracker_id,
                links={}):
        '''collect one or more matplotlib figures and

        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()

        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats(
            display_options)
        all_formats = [default_format, ] + additional_formats

        # create all required images
        for figman in fig_managers:

            # create all images
            figid = figman.num

            # select the correct figure
            figure = plt.figure(figid)

            # save explicit formats
            outname = "%s_%02d" % (template_name, figid)

            for id, format, dpi in all_formats:

                outpath = os.path.join(outdir, '%s.%s' % (outname, format))

                try:
                    figman.canvas.figure.savefig(outpath, dpi=dpi)
                except:
                    s = Utils.collectExceptionAsString(
                        "Exception running plot %s" % outpath)
                    warnings.warn(s)
                    return []

            # insert display figure
            is_html = False
            script_text = None
            if USE_MPLD3:
                outpath = os.path.join(
                    outdir,
                    '%s.html' % (outname))

                # plt.legend()
                # write to file
                with open(outpath, "w") as outf:
                    outf.write(mpld3.fig_to_html(figure))
                is_html = True

            elif USE_BOKEH:
                outpath = os.path.join(
                    outdir,
                    '%s.html' % (outname))
                is_html = True

                # (try to) convert to bokeh figure
                try:
                    # pd_job - use pandas object
                    # xkcd - use xkcd style
                    renderer = bokeh.mpl.BokehRenderer(pd_obj=True,
                                                       xkcd=False)
                    exporter = bokeh.mplexporter.exporter.Exporter(renderer)
                    exporter.run(figure)
                    bpl = renderer.fig

                except NotImplementedError:
                    # fall back to matplotlib
                    is_html = False

                if is_html:
                    bokeh_id = bpl._id
                    res = bokeh.resources.CDN
                    script_path = os.path.join(
                        '_static/report_directive/',
                        "%s.js" % bokeh_id)

                    # get js figure and html snippet
                    js_text, script_text = bokeh.embed.autoload_static(
                        bpl, res, script_path)

                    with open(script_path, "w") as outf:
                        outf.write(js_text)

                    with open(outpath, "w") as outf:
                        outf.write(script_text)

            # create the text element
            rst_output = Utils.buildRstWithImage(
                outname,
                outdir,
                rstdir,
                builddir,
                srcdir,
                additional_formats,
                tracker_id,
                links,
                display_options,
                default_format,
                is_html=is_html,
                text=script_text)

            map_figure2text["#$mpl %i$#" % figid] = rst_output

        return map_figure2text
Esempio n. 2
0
    def collect(self, blocks, figure_key="", subfig=0):
        '''collect one or more R figures.

        Plots are collected from all active devices.
        Plots are also collected from result-blocks
        containing a 'ggplot' attribute.

        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        # disable plotting if no rpy installed
        if R is None:
            return {}

        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats(
            self.display_options)
        all_formats = [default_format, ] + additional_formats
        image_options = Utils.getImageOptions(self.display_options)

        ##########################################
        ##########################################
        ##########################################
        # iterate over devices
        devices = R["dev.list"]()
        try:
            maxid = max(R["dev.list"]())
        except TypeError:
            maxid = 0

        for figid in range(2, maxid + 1):

            for id, format, dpi in all_formats:

                R["dev.set"](figid)

                outname = "%s_%02d" % (self.template_name, figid)
                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                if format.endswith("png"):
                    # for busy images there is a problem with figure margins
                    # simply increase dpi until it works.
                    R["dev.set"](figid)

                    width = height = 480 * dpi / 80
                    x = 0
                    while 1:
                        try:
                            R["dev.copy"](device=R.png,
                                          filename=outpath,
                                          res=dpi,
                                          width=width,
                                          height=height)
                            R["dev.off"]()
                        except rpy2.rinterface.RRuntimeError:
                            width *= 2
                            height *= 2
                            if x < 5:
                                continue
                        break

                elif format.endswith("svg"):
                    R["dev.copy"](device=R.svg,
                                  filename=outpath)
                    R["dev.off"]()

                elif format.endswith("eps"):
                    R["dev.copy"](device=R.postscript,
                                  paper='special',
                                  width=6,
                                  height=6,
                                  file=outpath,
                                  onefile=True)
                    R["dev.off"]()
                elif format.endswith("pdf"):
                    R["dev.copy"](device=R.pdf,
                                  paper='special',
                                  width=6,
                                  height=6,
                                  file=outpath,
                                  onefile=True)
                    R["dev.off"]()
                else:
                    raise ValueError("format '%s' not supported" % format)

                if not os.path.exists(outpath):
                    continue
                    # raise ValueError("rendering problem: image file was not be created: %s" % outpath)

                if format == 'png':
                    thumbdir = os.path.join(self.outdir, 'thumbnails')
                    try:
                        os.makedirs(thumbdir)
                    except OSError:
                        pass
                    thumbfile = str('%s.png' % os.path.join(thumbdir, outname))
                    captionfile = str(
                        '%s.txt' % os.path.join(thumbdir, outname))
                    if not os.path.exists(thumbfile):
                        # thumbnail only available in matplotlib >= 0.98.4
                        try:
                            figthumb = image.thumbnail(
                                str(outpath), str(thumbfile), scale=0.3)
                        except AttributeError:
                            pass

                    outfile = open(captionfile, "w")
                    outfile.write("\n".join(self.content) + "\n")
                    outfile.close()

                R["dev.off"](figid)

            # create the text element
            rst_output = Utils.buildRstWithImage(outname,
                                                 self.outdir,
                                                 self.rstdir,
                                                 self.builddir,
                                                 self.srcdir,
                                                 additional_formats,
                                                 self.tracker_id,
                                                 self.links,
                                                 self.display_options)

            map_figure2text["#$rpl %i$#" % figid] = rst_output

        figid = maxid
        ##########################################
        ##########################################
        ##########################################
        # iterate over ggplot plots
        for block in blocks:
            if not hasattr(block, "rggplot"):
                continue
            pp = block.rggplot
            figname = block.figname

            outname = "%s_%s" % (self.template_name, figname)

            for id, format, dpi in all_formats:
                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                try:
                    R.ggsave(outpath, plot=pp, dpi=dpi)
                except rpy2.rinterface.RRuntimeError as msg:
                    raise

                # width, height = 3 * dpi, 3 * dpi
                # if format.endswith("png"):
                #     R.png(outpath,
                #            width = width,
                #            height = height)
                # elif format.endswith("svg"):
                #     R.svg(outpath)
                # elif format.endswith("eps"):
                #     R.postscript(outpath)
                # elif format.endswith("pdf"):
                #     R.pdf(outpath)
                # R.plot(pp)
                # R["dev.off"]()

            # create the text element
            rst_output = Utils.buildRstWithImage(outname,
                                                 self.outdir,
                                                 self.rstdir,
                                                 self.builddir,
                                                 self.srcdir,
                                                 additional_formats,
                                                 self.tracker_id,
                                                 self.links,
                                                 self.display_options)

            map_figure2text["#$ggplot %s$#" % figname] = rst_output

        return map_figure2text
Esempio n. 3
0
    def collect(self, blocks, figure_key=None, subfig=0):
        '''collect one or more matplotlib figures and

        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()

        self.debug("figure_key: {}, subfig: {}: collecting {} matplotlib images".format(
            figure_key, subfig, len(fig_managers)))
        
        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats(
            self.display_options)
        all_formats = [default_format, ] + additional_formats

        # create all required images
        for figman in fig_managers:

            # create all images
            figid = figman.num

            # select the correct figure
            figure = plt.figure(figid)

            # save explicit formats
            outname = "%s_%s_%02d_%02d" % (self.template_name, figure_key, subfig, figid)

            has_output = False

            for id, format, dpi in all_formats:

                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                # sanitize figure size for Agg.
                if figure.get_figwidth() > 32768 or figure.get_figheight() > 32768:
                    warnings.warn(
                        "figure size unexpected large {} x {}, "
                        "patching to 10x10 inches".format(
                            figure.get_figwidth(), figure.get_figheight()))
                    figure.set_size_inches(10, 10)

                try:
                    figman.canvas.figure.savefig(outpath, dpi=dpi)
                except Exception as ex:
                    s = Utils.collectExceptionAsString(
                        "exception raised while building plot '%s': %s" %
                        (outpath, str(ex)))
                    warnings.warn(s)
                    continue
                has_output = True

            if not has_output:
                warnings.warn("no output for '%s'" % outpath)
                continue

            # insert display figure
            is_html = False
            script_text = None
            if HAVE_MPLD3 and \
               Utils.PARAMS.get("report_mpl", None) == "mpld3":
                outpath = os.path.join(
                    self.outdir,
                    '%s.html' % (outname))

                # plt.legend()
                # write to file
                with open(outpath, "w") as outf:
                    outf.write(mpld3.fig_to_html(figure))
                is_html = True

            elif HAVE_BOKEH and \
                    Utils.PARAMS.get("report_mpl", None) == "bokeh":

                outpath = os.path.join(
                    self.outdir,
                    '%s.html' % (outname))
                is_html = True

                # (try to) convert to bokeh figure
                try:
                    bokeh_figure = bokeh.mpl.to_bokeh(figure,
                                                      use_pandas=True,
                                                      xkcd=False)

                except NotImplementedError:
                    # fall back to matplotlib
                    is_html = False

                if is_html:
                    bokeh_id = bokeh_figure._id
                    res = bokeh.resources.CDN
                    script_path = os.path.join(
                        '_static/report_directive/',
                        "%s.js" % bokeh_id)

                    # get js figure and html snippet
                    js_text, script_text = bokeh.embed.autoload_static(
                        bokeh_figure, res, script_path)

                    with open(script_path, "w") as outf:
                        outf.write(js_text)

                    with open(outpath, "w") as outf:
                        outf.write(script_text)

            # release memory
            plt.close(figure)

            # create the text element
            rst_output = Utils.buildRstWithImage(
                outname,
                self.outdir,
                self.rstdir,
                self.builddir,
                self.srcdir,
                additional_formats,
                self.tracker_id,
                self.links,
                self.display_options,
                default_format,
                is_html=is_html,
                text=script_text)

            if figure_key is None:
                f = figid
            else:
                f = "{}.{}".format(figure_key, figid)
            map_figure2text["#$mpl {}$#".format(f)] = rst_output

        return map_figure2text
Esempio n. 4
0
    def collect(self, blocks, figure_key=None, subfig=0):
        '''collect one or more matplotlib figures and

        1. save as png, hires-png and pdf
        2. save thumbnail
        3. insert rendering code at placeholders in output

        returns a map of place holder to placeholder text.
        '''
        fig_managers = _pylab_helpers.Gcf.get_all_fig_managers()

        self.debug("figure_key: {}, subfig: {}: collecting {} matplotlib images".format(
            figure_key, subfig, len(fig_managers)))

        map_figure2text = {}

        # determine the image formats to create
        default_format, additional_formats = Utils.getImageFormats(
            self.display_options)
        all_formats = [default_format, ] + additional_formats

        # create all required images
        for figman in fig_managers:

            # create all images
            figid = figman.num

            # select the correct figure
            figure = plt.figure(figid)

            # save explicit formats
            outname = "%s_%s_%02d_%02d" % (self.template_name, figure_key, subfig, figid)

            has_output = False

            for id, format, dpi in all_formats:

                outpath = os.path.join(self.outdir, '%s.%s' % (outname, format))

                # sanitize figure size for Agg.
                if figure.get_figwidth() > 32768 or figure.get_figheight() > 32768:
                    warnings.warn(
                        "figure size unexpected large {} x {}, "
                        "patching to 10x10 inches".format(
                            figure.get_figwidth(), figure.get_figheight()))
                    figure.set_size_inches(10, 10)

                try:
                    figman.canvas.figure.savefig(outpath, dpi=dpi)
                except Exception as ex:
                    s = Utils.collectExceptionAsString(
                        "exception raised while building plot '%s': %s" %
                        (outpath, str(ex)))
                    warnings.warn(s)
                    continue
                has_output = True

            if not has_output:
                warnings.warn("no output for '%s'" % outpath)
                continue

            # insert display figure
            is_html = False
            script_text = None

            # release memory
            plt.close(figure)

            # create the text element
            rst_output = Utils.buildRstWithImage(
                outname,
                self.outdir,
                self.rstdir,
                self.builddir,
                self.srcdir,
                additional_formats,
                self.tracker_id,
                self.links,
                self.display_options,
                default_format,
                is_html=is_html,
                text=script_text)

            if figure_key is None:
                f = figid
            else:
                f = "{}.{}".format(figure_key, figid)
            map_figure2text["#$mpl {}$#".format(f)] = rst_output

        return map_figure2text