def save_figure(cls, figname="", figure=None, filename_tmpl=None, figtypes=None, remap_dot_to_underscore=False): if remap_dot_to_underscore: figname = figname.replace(".", "-") # assert False if not filename_tmpl: filename_tmpl = cls.autosave_default_image_filename_tmpl if not figtypes: figtypes = cls.autosave_image_formats assert isinstance(figtypes, list) # Get the figure: fig = figure if figure else pylab.gcf() # assert f.number == cls.fig_num # Some small changes: fig.subplots_adjust(bottom=0.15) # Find the module this function was called from: modname = ScriptUtils.get_calling_script_file(include_ext=False) # Print what we are saving: subst_dict = { "modulename": modname, "fignum": PlotManager.fig_num, "figname": figname, "figtype": "{%s}" % ",".join(figtypes), } print "PlotMnager:Saving ", filename_tmpl.format(**subst_dict) # For each filetype: for figtype in figtypes: # Create the filename: subst_dict = {"modulename": modname, "fignum": PlotManager.fig_num, "figname": figname, "figtype": figtype} filename = filename_tmpl.format(**subst_dict) filename = filename.replace(":", "=") assert not ":" in filename, "For windows compatibility" # Save the figure: full_filename = os.path.join(os.getcwd(), filename) mreorg.ensure_directory_exists(full_filename) fig.savefig(full_filename) PlotManager.figures_saved.append(fig) PlotManager.figures_saved_nums.append(fig.number) PlotManager.figures_saved_filenames.append(full_filename) # print 'Saving File', filename # Increment the fignum: PlotManager.fig_num = PlotManager.fig_num + 1
def savefig(filename, *args, **kwargs): from mreorg.layouts import FigureOptions from mreorg.utils import ScriptUtils if ScriptFlags.MREORG_SAVEFIGADDINFO or FigureOptions.is_draft: F = pylab.gcf() (x, y) = F.get_size_inches() txt = 'Size: x=%2.2f y=%2.2f (inches)' % (x, y) txt += '\n' + 'On: %s' % datetime.datetime.today().strftime('%d, %h %Y (%H:%M)') txt += '\n' + 'Using MPLCONFIGFILE: %s' % ScriptFlags.MREORG_MPLCONFIG_FILE txt += '\n' + filename.split('/')[-1] txt += '\n' + ScriptUtils.get_calling_script_file(include_ext=True) pylab.figtext(0.0, 0.5, txt, backgroundcolor='white') if ScriptFlags.MREORG_AUTOMAKEDIRS: mreorg.ensure_directory_exists(filename) return orig_mplsavefig(filename, *args, transparent=True, **kwargs)
def save_figure(cls, figname='', figure=None, filename_tmpl=None, figtypes=None, remap_dot_to_underscore=False): if remap_dot_to_underscore: figname = figname.replace(".", "-") # assert False if not filename_tmpl: filename_tmpl = cls.autosave_default_image_filename_tmpl if not figtypes: figtypes = cls.autosave_image_formats assert isinstance(figtypes, list) # Get the figure: fig = (figure if figure else pylab.gcf()) #assert f.number == cls.fig_num # Some small changes: fig.subplots_adjust(bottom=0.15) # Find the module this function was called from: modname = ScriptUtils.get_calling_script_file(include_ext=False) # Print what we are saving: subst_dict = { 'modulename': modname, 'fignum': PlotManager.fig_num, 'figname': figname, 'figtype': '{%s}' % ','.join(figtypes), } print 'PlotMnager:Saving ', filename_tmpl.format(**subst_dict) # For each filetype: for figtype in figtypes: # Create the filename: subst_dict = { 'modulename': modname, 'fignum': PlotManager.fig_num, 'figname': figname, 'figtype': figtype, } filename = filename_tmpl.format(**subst_dict) filename = filename.replace(':', '=') assert not ':' in filename, 'For windows compatibility' # Save the figure: full_filename = os.path.join(os.getcwd(), filename) mreorg.ensure_directory_exists(full_filename) fig.savefig(full_filename) PlotManager.figures_saved.append(fig) PlotManager.figures_saved_nums.append(fig.number) PlotManager.figures_saved_filenames.append(full_filename) #print 'Saving File', filename # Increment the fignum: PlotManager.fig_num = PlotManager.fig_num + 1
def save_figure( cls, figname='', figure=None, filename_tmpl=None, figtypes=None, remap_dot_to_underscore=False, ): if remap_dot_to_underscore: figname = figname.replace('.', '-') if not filename_tmpl: filename_tmpl = cls.autosave_default_image_filename_tmpl if not figtypes: # Has it been set explicity in the class?? if cls.autosave_image_formats is not None: figtypes = cls.autosave_image_formats # Is it specified in a layout config file: elif FigureOptions.default_autosave_formats is not None: figtypes = FigureOptions.default_autosave_formats # Or the rc file? elif 'Settings' in MReOrgConfig.config and \ 'mreorg' in MReOrgConfig.config['Settings'] and \ 'default_autosave_formats' in MReOrgConfig.config['Settings']['mreorg']: figtypes = MReOrgConfig.config['Settings']['mreorg']['default_autosave_formats'] # No?, then lets default to everything: else: figtypes = cls._all_autosave_image_formats assert isinstance(figtypes, list), 'figtypes: %s <%s>'%(figtypes, type(figtypes)) # Get the figure: import pylab fig = (figure if figure else pylab.gcf()) # Some small changes: fig.subplots_adjust(bottom=0.15) # Find the module this function was called from: modname = ScriptUtils.get_calling_script_file(include_ext=False) # Print what we are saving: subst_dict = { 'modulename': modname, 'fignum': PlotManager.fig_num, 'figname': figname, 'figtype': '{%s}' % ','.join(figtypes), } print 'PlotManger saving: ', filename_tmpl.format(**subst_dict) # For each filetype: for figtype in figtypes: # Create the filename: subst_dict = { 'modulename': modname, 'fignum': PlotManager.fig_num, 'figname': figname, 'figtype': figtype, } filename = filename_tmpl.format(**subst_dict) filename = filename.replace(':', '=') assert not ':' in filename, 'For windows compatibility' # Save the figure: full_filename = os.path.join(os.getcwd(), filename) mreorg.ensure_directory_exists(full_filename) try: fig.savefig(full_filename) except ValueError as e: print 'mreorg error: unable to save figure: ', full_filename print e print '(ignoring error)' PlotManager.figures_saved.append(fig) PlotManager.figures_saved_nums.append(fig.number) PlotManager.figures_saved_filenames.append(full_filename) # Increment the fignum: PlotManager.fig_num = PlotManager.fig_num + 1