def save_figure(fig, path, ext=None, default_ext = '.pdf'): """ :param fig: The figure to show :param path: The absolute path to the figure. :param default_ext: The default extension to use, if none is specified. :return: """ if ext is None: _, ext = os.path.splitext(path) if ext == '': path += default_ext else: assert ext in _supported_filetypes, "We inferred the extension '{}' from your filename, but it was not in the list of supported extensions: {}" \ .format(ext, _supported_filetypes) else: path += ext if ext.startswith('.') else '.'+ext if '%L' in path: path = path.replace('%L', fig.get_label() if fig.get_label() is not '' else 'unnamed') path = format_filename(path) make_file_dir(path) if ext=='.pkl': with open(path, 'wb') as f: pickle.dump(fig, f, protocol=pickle.HIGHEST_PROTOCOL) else: fig.savefig(path) ARTEMIS_LOGGER.info('Saved Figure: %s' % path) return path
def save_figure(fig, path, ext=None, default_ext='.pdf'): """ :param fig: The figure to show :param path: The absolute path to the figure. :param default_ext: The default extension to use, if none is specified. :return: """ if ext is None: _, ext = os.path.splitext(path) if ext == '': path += default_ext else: assert ext in _supported_filetypes, "We inferred the extension '{}' from your filename, but it was not in the list of supported extensions: {}" \ .format(ext, _supported_filetypes) else: path += ext if ext.startswith('.') else '.' + ext if '%L' in path: path = path.replace( '%L', fig.get_label() if fig.get_label() is not '' else 'unnamed') path = format_filename(path) make_file_dir(path) if ext == '.pkl': with open(path, 'wb') as f: pickle.dump(fig, f, protocol=pickle.HIGHEST_PROTOCOL) else: fig.savefig(path) ARTEMIS_LOGGER.info('Saved Figure: %s' % path) return path
def save_current_figure(path=""): fig = plt.gcf() file_name = format_filename(file_string = '%T', current_time = datetime.now()) if path != "": save_path = os.path.join(path,"%s.pdf"%file_name) else: save_path = get_artemis_data_path('output/{file_name}.png'.format(file_name=file_name)) save_figure(fig,path=save_path)
def save_current_figure(): print("Attempting to save figure") fig = plt.gcf() file_name = format_filename(file_string='%T', current_time=datetime.now()) save_path = get_local_path( 'output/{file_name}.pdf'.format(file_name=file_name)) print("Current figure saved to {}".format(save_path)) save_figure(fig, path=save_path)
def record_experiment(identifier='%T-%N', name = 'unnamed', info = '', print_to_console = True, show_figs = None, save_figs = True, saved_figure_ext = '.pdf', use_temp_dir = False): """ :param identifier: The string that uniquely identifies this experiment record. Convention is that it should be in the format :param name: Base-name of the experiment :param print_to_console: If True, print statements still go to console - if False, they're just rerouted to file. :param show_figs: Show figures when the experiment produces them. Can be: 'hang': Show and hang 'draw': Show but keep on going False: Don't show figures """ # Note: matplotlib imports are internal in order to avoid trouble for people who may import this module without having # a working matplotlib (which can occasionally be tricky to install). identifier = format_filename(file_string = identifier, base_name=name, current_time = datetime.now()) if show_figs is None: show_figs = 'draw' if is_test_mode() else 'hang' assert show_figs in ('hang', 'draw', False) if use_temp_dir: experiment_directory = tempfile.mkdtemp() atexit.register(lambda: shutil.rmtree(experiment_directory)) else: experiment_directory = get_local_path('experiments/{identifier}'.format(identifier=identifier)) make_dir(experiment_directory) make_file_dir(experiment_directory) log_file_name = os.path.join(experiment_directory, 'output.txt') log_capture_context = PrintAndStoreLogger(log_file_path = log_file_name, print_to_console = print_to_console) log_capture_context.__enter__() from artemis.plotting.manage_plotting import WhatToDoOnShow blocking_show_context = WhatToDoOnShow(show_figs) blocking_show_context.__enter__() if save_figs: from artemis.plotting.saving_plots import SaveFiguresOnShow figure_save_context = SaveFiguresOnShow(path = os.path.join(experiment_directory, 'fig-%T-%L'+saved_figure_ext)) figure_save_context.__enter__() _register_current_experiment(name, identifier) global _CURRENT_EXPERIMENT_RECORD _CURRENT_EXPERIMENT_RECORD = ExperimentRecord(experiment_directory) _CURRENT_EXPERIMENT_RECORD.add_info('Name: %s' % (name, )) _CURRENT_EXPERIMENT_RECORD.add_info('Identifier: %s' % (identifier, )) _CURRENT_EXPERIMENT_RECORD.add_info('Directory: %s' % (_CURRENT_EXPERIMENT_RECORD.get_dir(), )) yield _CURRENT_EXPERIMENT_RECORD _CURRENT_EXPERIMENT_RECORD = None blocking_show_context.__exit__(None, None, None) log_capture_context.__exit__(None, None, None) if save_figs: figure_save_context.__exit__(None, None, None) _deregister_current_experiment()
def record_experiment(identifier='%T-%N', name='unnamed', print_to_console=True, show_figs=None, save_figs=True, saved_figure_ext='.fig.pkl', use_temp_dir=False, date=None, prefix=None): """ :param identifier: The string that uniquely identifies this experiment record. Convention is that it should be in the format :param name: Base-name of the experiment :param print_to_console: If True, print statements still go to console - if False, they're just rerouted to file. :param show_figs: Show figures when the experiment produces them. Can be: 'hang': Show and hang 'draw': Show but keep on going False: Don't show figures """ # Note: matplotlib imports are internal in order to avoid trouble for people who may import this module without having # a working matplotlib (which can occasionally be tricky to install). if date is None: date = datetime.now() identifier = format_filename(file_string=identifier, base_name=name, current_time=date) if show_figs is None: show_figs = 'draw' if is_test_mode() else 'hang' assert show_figs in ('hang', 'draw', False) if use_temp_dir: experiment_directory = tempfile.mkdtemp() atexit.register(lambda: shutil.rmtree(experiment_directory)) else: experiment_directory = get_local_experiment_path(identifier) make_dir(experiment_directory) this_record = ExperimentRecord(experiment_directory) # Create context that sets the current experiment record # and the context which captures stdout (print statements) and logs them. contexts = [ hold_current_experiment_record(this_record), CaptureStdOut(log_file_path=os.path.join(experiment_directory, 'output.txt'), print_to_console=print_to_console, prefix=prefix) ] if is_matplotlib_imported(): from artemis.plotting.manage_plotting import WhatToDoOnShow # Add context that modifies how matplotlib figures are shown. contexts.append(WhatToDoOnShow(show_figs)) if save_figs: from artemis.plotting.saving_plots import SaveFiguresOnShow # Add context that saves figures when show is called. contexts.append(SaveFiguresOnShow(path=os.path.join(experiment_directory, 'fig-%T-%L' + saved_figure_ext))) with nested(*contexts): yield this_record
def save_figure(fig, path, default_ext='.pdf'): """ :param fig: The figure to show :param path: The absolute path to the figure. :param default_ext: The default extension to use, if none is specified. :return: """ _, ext = os.path.splitext(path) if ext == '': path += default_ext if '%L' in path: path = path.replace( '%L', fig.get_label() if fig.get_label() is not '' else 'unnamed') path = format_filename(path) make_file_dir(path) fig.savefig(path) ARTEMIS_LOGGER.info('Saved Figure: %s' % path) return path