def run_savefig(plot_path, basename, tmpdir, destdir, formats): """ Once a plot script has been imported, this function runs savefig on all of the figures in all of the desired formats. """ fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for j, (format, dpi) in enumerate(formats): if len(fig_managers) == 1: outname = basename else: outname = "%s_%02d" % (basename, i) outname = outname + "." + format outpath = os.path.join(tmpdir, outname) try: figman.canvas.figure.savefig(outpath, dpi=dpi) except: s = cbook.exception_to_str( "Exception saving plot %s" % plot_path) warnings.warn(s, PlotWarning) return 0 if j > 0: shutil.copyfile(outpath, os.path.join(destdir, outname)) return len(fig_managers)
def run_savefig(plot_path, basename, tmpdir, destdir, formats): """ Once a plot script has been imported, this function runs savefig on all of the figures in all of the desired formats. """ fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for j, (format, dpi) in enumerate(formats): if len(fig_managers) == 1: outname = basename else: outname = "%s_%02d" % (basename, i) outname = outname + "." + format outpath = os.path.join(tmpdir, outname) try: figman.canvas.figure.savefig(outpath, dpi=dpi) except: s = cbook.exception_to_str("Exception saving plot %s" % plot_path) warnings.warn(s) return 0 if j > 0: shutil.copyfile(outpath, os.path.join(destdir, outname)) return len(fig_managers)
def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, formats, context=False): """ Run a pyplot script and save the low and high res PNGs and a PDF in outdir. """ plot_path = str(plot_path) # todo, why is unicode breaking this basedir, fname = os.path.split(plot_path) basename, ext = os.path.splitext(fname) all_exists = True # Look for single-figure output files first for format, dpi in formats: outname = os.path.join(tmpdir, "%s.%s" % (basename, format)) if out_of_date(plot_path, outname): all_exists = False break if not context and all_exists: return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join(tmpdir, "%s_%02d.%s" % (basename, i, format)) if out_of_date(plot_path, outname): all_exists = False break if all_exists: i += 1 else: break if not context and i != 0: return i # We didn't find the files, so build them if not context: clear_state() try: run_code(plot_path, function_name, plot_code, context=context) except: s = cbook.exception_to_str("Exception running plot %s" % plot_path) warnings.warn(s, PlotWarning) return 0 if not all_exists: num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats) if "__plot__" in sys.modules: del sys.modules["__plot__"] return num_figs else: return 1
def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, formats): """ Run a pyplot script and save the low and high res PNGs and a PDF in outdir. """ plot_path = str(plot_path) # todo, why is unicode breaking this basedir, fname = os.path.split(plot_path) basename, ext = os.path.splitext(fname) all_exists = True # Look for single-figure output files first for format, dpi in formats: outname = os.path.join(tmpdir, '%s.%s' % (basename, format)) if out_of_date(plot_path, outname): all_exists = False break if all_exists: return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join(tmpdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(plot_path, outname): all_exists = False break if all_exists: i += 1 else: break if i != 0: return i # We didn't find the files, so build them clear_state() try: run_code(plot_path, function_name, plot_code) except: s = cbook.exception_to_str("Exception running plot %s" % plot_path) warnings.warn(s) return 0 num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats) if '__plot__' in sys.modules: del sys.modules['__plot__'] return num_figs
def position_edited(self, renderer, path, newtext, position): position = int(position) format = self.formats[position] rownum = int(path) colname = self.headers[position] oldval = self.r[rownum][colname] try: newval = format.fromstr(newtext) except ValueError: msg = cbook.exception_to_str('Error converting "%s"'%newtext) error_message(msg, title='Error') return self.r[rownum][colname] = newval self[path][position] = format.tostr(newval) self.callbacks.process('cell_changed', self, rownum, colname, oldval, newval)
def render_figures(plot_path, function_name, plot_code, tmpdir, destdir, formats): """ Run a pyplot script and save the low and high res PNGs and a PDF in outdir. """ plot_path = str(plot_path) # todo, why is unicode breaking this basedir, fname = os.path.split(plot_path) basename, ext = os.path.splitext(fname) all_exists = True for format, dpi in formats: outname = os.path.join(tmpdir, '%s.%s' % (basename, format)) if out_of_date(plot_path, outname): all_exists = False break if all_exists: return 1 i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join( tmpdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(plot_path, outname): all_exists = False break if all_exists: i += 1 else: break if i != 0: return i clear_state() try: run_code(plot_path, function_name, plot_code) except: s = cbook.exception_to_str("Exception running plot %s" % plot_path) warnings.warn(s) return 0 num_figs = run_savefig(plot_path, basename, tmpdir, destdir, formats) if '__plot__' in sys.modules: del sys.modules['__plot__'] return num_figs
def make_image(fullpath, code, outdir, context='', options={}): """ run a script and save the PNG in _static """ fullpath = str(fullpath) # todo, why is unicode breaking this basedir, fname = os.path.split(fullpath) basename, ext = os.path.splitext(fname) if str(basename) == "None": import pdb pdb.set_trace() # Look for output file outpath = os.path.join(outdir, basename + '.png') if not out_of_date(fullpath, outpath): write_char('.') return 1 # We didn't find the files, so build them if code is not None: exec(code) else: try: module = runfile(fullpath) panel = module.panel except: warnings.warn("current path " + os.getcwd()) s = cbook.exception_to_str("Exception running wx %s %s" % (fullpath, context)) warnings.warn(s) return False try: labels except: labels = [] img = capture_image(panel, labels) write_png(outpath, img) return True
def make_image(fullpath, code, outdir, context='', options={}): """ run a script and save the PNG in _static """ fullpath = str(fullpath) # todo, why is unicode breaking this basedir, fname = os.path.split(fullpath) basename, ext = os.path.splitext(fname) if str(basename) == "None": import pdb pdb.set_trace() # Look for output file outpath = os.path.join(outdir, basename+'.png') if not out_of_date(fullpath, outpath): write_char('.') return 1 # We didn't find the files, so build them if code is not None: exec(code) else: try: module = runfile(fullpath) panel = module.panel except: warnings.warn("current path "+os.getcwd()) s = cbook.exception_to_str("Exception running wx %s %s" % (fullpath,context)) warnings.warn(s) return False try: labels except: labels = [] img = capture_image(panel,labels) write_png(outpath, img) return True
def makefig(fullpath, code, outdir): """ run a pyplot script and save the low and high res PNGs and a PDF in _static """ formats = [('png', 80), ('hires.png', 200), ('pdf', 50)] fullpath = str(fullpath) # todo, why is unicode breaking this basedir, fname = os.path.split(fullpath) basename, ext = os.path.splitext(fname) if str(basename) == "None": import pdb pdb.set_trace() all_exists = True # Look for single-figure output files first for format, dpi in formats: outname = os.path.join(outdir, '%s.%s' % (basename, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: write_char('.' * len(formats)) return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join(outdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: i += 1 else: break if i != 0: write_char('.' * i * len(formats)) return i # We didn't find the files, so build them plt.close('all') # we need to clear between runs matplotlib.rcdefaults() # Set a figure size that doesn't overflow typical browser windows matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) if code is not None: exec(code) else: try: runfile(fullpath) except: traceback.print_exc() s = ("Exception running plot %s" % fullpath) warnings.warn(s) return 0 fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for format, dpi in formats: if len(fig_managers) == 1: outname = basename else: outname = "%s_%02d" % (basename, i) outpath = os.path.join(outdir, '%s.%s' % (outname, format)) try: figman.canvas.figure.savefig(outpath, dpi=dpi) except: s = cbook.exception_to_str("Exception running plot %s" % fullpath) warnings.warn(s) return 0 write_char('*') return len(fig_managers)
def collect( self, blocks, template_name, outdir, rstdir, rst2rootdir, rst2builddir, rst2srcdir, content, display_options, linked_codename, tracker_id): '''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() all_formats = [default_format,] + additional_formats urls = Utils.asList( Utils.PARAMS["report_urls"] ) # create all the images for figman in fig_managers: # create all images figid = figman.num for id, format, dpi in all_formats: outname = "%s_%02d" % (template_name, figid) outpath = os.path.join(outdir, '%s.%s' % (outname, format)) try: figman.canvas.figure.savefig( outpath, dpi=dpi ) except: s = exception_to_str("Exception running plot %s" % outpath) warnings.warn(s) return [] if format=='png': thumbdir = os.path.join(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( content ) + "\n" ) outfile.close() # create the text element rst_output = "" # for image directive - image path is relative from rst file to external build dir imagepath = re.sub( "\\\\", "/", os.path.join( rst2builddir, outname ) ) # for links - path is from rst file to internal root dir # one rst file for all relative_imagepath_rst = re.sub( "\\\\", "/", os.path.join( rst2rootdir, template_name ) ) relative_imagepath_img = re.sub( "\\\\", "/", os.path.join( rst2rootdir, outname ) ) linked_text = relative_imagepath_rst + ".txt" if Config.HTML_IMAGE_FORMAT: id, format, dpi = Config.HTML_IMAGE_FORMAT template = ''' .. htmlonly:: .. image:: %(linked_image)s %(display_options)s [%(code_url)s %(rst_url)s %(data_url)s %(extra_images)s] ''' linked_image = imagepath + ".%s" % format extra_images=[] for id, format, dpi in additional_formats: extra_images.append( "`%(id)s <%(relative_imagepath_img)s.%(format)s>`__" % locals()) if extra_images: extra_images = " " + " ".join( extra_images) else: extra_images = "" # construct additional urls code_url, data_url, rst_url, table_url = "", "", "", "" if "code" in urls: code_url = "`code <%(linked_codename)s>`__" % locals() if "data" in urls: data_url = "`data </data/%(tracker_id)s>`__" % locals() if "rst" in urls: rst_url = "`rst <%(linked_text)s>`__" % locals() rst_output += template % locals() # treat latex separately if Config.LATEX_IMAGE_FORMAT: id, format, dpi = Config.LATEX_IMAGE_FORMAT template = ''' .. latexonly:: .. image:: %(linked_image)s %(display_options)s ''' linked_image = imagepath + ".%s" % format rst_output += template % locals() map_figure2text[ "#$mpl %i$#" % figid] = rst_output return map_figure2text
def makefig(code, code_path, output_dir, output_base, config): """ run a pyplot script and save the low and high res PNGs and a PDF in _static """ formats = [ ('png', 100), ('hires.png', 200), ('pdf', 50), ] all_exists = True # Look for single-figure output files first for format, dpi in formats: output_path = os.path.join(output_dir, '%s.%s' % (output_base, format)) if out_of_date(code_path, output_path): all_exists = False break if all_exists: return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: output_path = os.path.join(output_dir, '%s_%02d.%s' % (output_base, i, format)) if out_of_date(code_path, output_path): all_exists = False break if all_exists: i += 1 else: break if i != 0: return i # We didn't find the files, so build them print "-- Plotting figures %s" % output_base # Clear between runs plt.close('all') # Reset figure parameters matplotlib.rcdefaults() matplotlib.rcParams.update(config.plot_rcparams) try: run_code(code, code_path) except: raise s = cbook.exception_to_str("Exception running plot %s" % code_path) warnings.warn(s) return 0 fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for format, dpi in formats: if len(fig_managers) == 1: name = output_base else: name = "%s_%02d" % (output_base, i) path = os.path.join(output_dir, '%s.%s' % (name, format)) try: figman.canvas.figure.savefig(path, dpi=dpi) except: s = cbook.exception_to_str("Exception running plot %s" % code_path) warnings.warn(s) return 0 return len(fig_managers)
def wx_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Handle the plot directive. """ # The user may provide a filename *or* Python code content, but not both if len(arguments) == 1: reference = directives.uri(arguments[0]) basedir, fname = os.path.split(reference) basename, ext = os.path.splitext(fname) basedir = relpath(basedir, setup.app.builder.srcdir) if len(content): raise ValueError("wx directive may not specify both a filename and inline content") content = None else: basedir = "inline" content = '\n'.join(content) # Since we don't have a filename, use a hash based on the content reference = basename = md5(content).hexdigest()[-10:] fname = None # Get the directory of the rst file, and determine the relative # path from the resulting html file to the plot_directive links # (linkdir). This relative path is used for html links *only*, # and not the embedded image. That is given an absolute path to # the temporary directory, and then sphinx moves the file to # build/html/_images for us later. rstdir, rstfile = os.path.split(state_machine.document.attributes['source']) reldir = rstdir[len(setup.confdir)+1:] relparts = [p for p in os.path.split(reldir) if p.strip()] nparts = len(relparts) outdir = os.path.join('wx_directive', basedir) linkdir = ('../' * nparts) + outdir context = "at %s:%d"%(rstfile,lineno) # tmpdir is where we build all the output files. This way the # plots won't have to be redone when generating latex after html. # Prior to Sphinx 0.6, absolute image paths were treated as # relative to the root of the filesystem. 0.6 and after, they are # treated as relative to the root of the documentation tree. We need # to support both methods here. tmpdir = os.path.join('_build', outdir) if sphinx_version < (0, 6): tmpdir = os.path.abspath(tmpdir) prefix = '' else: prefix = '/' if not os.path.exists(tmpdir): cbook.mkdirs(tmpdir) # destdir is the directory within the output to store files # that we'll be linking to -- not the embedded images. destdir = os.path.abspath(os.path.join(setup.app.builder.outdir, outdir)) if not os.path.exists(destdir): cbook.mkdirs(destdir) # Generate the figures, and return the number of them success = make_image(reference, content, tmpdir, context=context, options=options) if 'include-source' in options: if content is None: content = open(reference, 'r').read() lines = ['::', ''] + [' %s'%row.rstrip() for row in content.split('\n')] del options['include-source'] else: lines = [] if success: options = [' :%s: %s' % (key, val) for key, val in options.items()] options = "\n".join(options) if fname is not None: try: shutil.copyfile(reference, os.path.join(destdir, fname)) except: s = cbook.exception_to_str("Exception copying plot %s %s" % (reference,context)) warnings.warn(s) return 0 outname = basename+'.png' # Copy the linked-to files to the destination within the build tree, # and add a link for them shutil.copyfile(os.path.join(tmpdir, outname), os.path.join(destdir, outname)) # Output the resulting reST lines.extend((template % locals()).split('\n')) else: lines.extend((exception_template % locals()).split('\n')) if len(lines): state_machine.insert_input( lines, state_machine.input_lines.source(0)) return []
def makefig(fullpath, outdir): """ run a pyplot script<t and save the low and high res PNGs and a PDF in _static """ fullpath = str(fullpath) # todo, why is unicode breaking this print ' makefig: fullpath=%s, outdir=%s'%( fullpath, outdir) formats = [('png', 80), ('hires.png', 200), ('pdf', 50), ] basedir, fname = os.path.split(fullpath) basename, ext = os.path.splitext(fname) all_exists = True if basedir != outdir: shutil.copyfile(fullpath, os.path.join(outdir, fname)) # Look for single-figure output files first for format, dpi in formats: outname = os.path.join(outdir, '%s.%s' % (basename, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: print ' already have %s'%fullpath return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join(outdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: i += 1 else: break if i != 0: print ' already have %d figures for %s' % (i, fullpath) return i # We didn't find the files, so build them print ' building %s'%fullpath plt.close('all') # we need to clear between runs matplotlib.rcdefaults() # Set a figure size that doesn't overflow typical browser windows matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) try: runfile(fullpath) except: s = cbook.exception_to_str("Exception running plot %s" % fullpath) warnings.warn(s) return 0 fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for format, dpi in formats: if len(fig_managers) == 1: outname = basename else: outname = "%s_%02d" % (basename, i) outpath = os.path.join(outdir, '%s.%s' % (outname, format)) try: figman.canvas.figure.savefig(outpath, dpi=dpi) except: s = cbook.exception_to_str("Exception running plot %s" % fullpath) warnings.warn(s) return 0 if format=='png': thumbdir = os.path.join(outdir, 'thumbnails') if not os.path.exists(thumbdir): os.makedirs(thumbdir) thumbfile = str('%s.png'%os.path.join(thumbdir, outname) ) if not os.path.exists(thumbfile): figthumb = image.thumbnail(str(outpath), str(thumbfile), scale=0.3) print ' makefig: saved thumbnail of %s to %s'%(outpath, thumbfile) return len(fig_managers)
def wx_directive(name, arguments, options, content, lineno, content_offset, block_text, state, state_machine): """ Handle the plot directive. """ # The user may provide a filename *or* Python code content, but not both if len(arguments) == 1: reference = directives.uri(arguments[0]) basedir, fname = os.path.split(reference) basename, ext = os.path.splitext(fname) basedir = relpath(basedir, setup.app.builder.srcdir) if len(content): raise ValueError( "wx directive may not specify both a filename and inline content" ) content = None else: basedir = "inline" content = '\n'.join(content) # Since we don't have a filename, use a hash based on the content reference = basename = md5(content).hexdigest()[-10:] fname = None # Get the directory of the rst file, and determine the relative # path from the resulting html file to the plot_directive links # (linkdir). This relative path is used for html links *only*, # and not the embedded image. That is given an absolute path to # the temporary directory, and then sphinx moves the file to # build/html/_images for us later. rstdir, rstfile = os.path.split( state_machine.document.attributes['source']) reldir = rstdir[len(setup.confdir) + 1:] relparts = [p for p in os.path.split(reldir) if p.strip()] nparts = len(relparts) outdir = os.path.join('wx_directive', basedir) linkdir = ('../' * nparts) + outdir context = "at %s:%d" % (rstfile, lineno) # tmpdir is where we build all the output files. This way the # plots won't have to be redone when generating latex after html. # Prior to Sphinx 0.6, absolute image paths were treated as # relative to the root of the filesystem. 0.6 and after, they are # treated as relative to the root of the documentation tree. We need # to support both methods here. tmpdir = os.path.join('_build', outdir) if sphinx_version < (0, 6): tmpdir = os.path.abspath(tmpdir) prefix = '' else: prefix = '/' if not os.path.exists(tmpdir): cbook.mkdirs(tmpdir) # destdir is the directory within the output to store files # that we'll be linking to -- not the embedded images. destdir = os.path.abspath(os.path.join(setup.app.builder.outdir, outdir)) if not os.path.exists(destdir): cbook.mkdirs(destdir) # Generate the figures, and return the number of them success = make_image(reference, content, tmpdir, context=context, options=options) if 'include-source' in options: if content is None: content = open(reference, 'r').read() lines = ['::', '' ] + [' %s' % row.rstrip() for row in content.split('\n')] del options['include-source'] else: lines = [] if success: options = [ ' :%s: %s' % (key, val) for key, val in options.items() ] options = "\n".join(options) if fname is not None: try: shutil.copyfile(reference, os.path.join(destdir, fname)) except: s = cbook.exception_to_str("Exception copying plot %s %s" % (reference, context)) warnings.warn(s) return 0 outname = basename + '.png' # Copy the linked-to files to the destination within the build tree, # and add a link for them shutil.copyfile(os.path.join(tmpdir, outname), os.path.join(destdir, outname)) # Output the resulting reST lines.extend((template % locals()).split('\n')) else: lines.extend((exception_template % locals()).split('\n')) if len(lines): state_machine.insert_input(lines, state_machine.input_lines.source(0)) return []
def makefig(fullpath, outdir): """ run a pyplot script<t and save the low and high res PNGs and a PDF in _static """ fullpath = str(fullpath) # todo, why is unicode breaking this print ' makefig: fullpath=%s, outdir=%s' % (fullpath, outdir) formats = [ ('png', 80), ('hires.png', 200), ('pdf', 50), ] basedir, fname = os.path.split(fullpath) basename, ext = os.path.splitext(fname) all_exists = True if basedir != outdir: shutil.copyfile(fullpath, os.path.join(outdir, fname)) # Look for single-figure output files first for format, dpi in formats: outname = os.path.join(outdir, '%s.%s' % (basename, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: print ' already have %s' % fullpath return 1 # Then look for multi-figure output files, assuming # if we have some we have all... i = 0 while True: all_exists = True for format, dpi in formats: outname = os.path.join(outdir, '%s_%02d.%s' % (basename, i, format)) if out_of_date(fullpath, outname): all_exists = False break if all_exists: i += 1 else: break if i != 0: print ' already have %d figures for %s' % (i, fullpath) return i # We didn't find the files, so build them print ' building %s' % fullpath plt.close('all') # we need to clear between runs matplotlib.rcdefaults() # Set a figure size that doesn't overflow typical browser windows matplotlib.rcParams['figure.figsize'] = (5.5, 4.5) try: runfile(fullpath) except: s = cbook.exception_to_str("Exception running plot %s" % fullpath) warnings.warn(s) return 0 fig_managers = _pylab_helpers.Gcf.get_all_fig_managers() for i, figman in enumerate(fig_managers): for format, dpi in formats: if len(fig_managers) == 1: outname = basename else: outname = "%s_%02d" % (basename, i) outpath = os.path.join(outdir, '%s.%s' % (outname, format)) try: figman.canvas.figure.savefig(outpath, dpi=dpi) except: s = cbook.exception_to_str("Exception running plot %s" % fullpath) warnings.warn(s) return 0 if format == 'png': thumbdir = os.path.join(outdir, 'thumbnails') if not os.path.exists(thumbdir): os.makedirs(thumbdir) thumbfile = str('%s.png' % os.path.join(thumbdir, outname)) if not os.path.exists(thumbfile): figthumb = image.thumbnail(str(outpath), str(thumbfile), scale=0.3) print ' makefig: saved thumbnail of %s to %s' % ( outpath, thumbfile) return len(fig_managers)