def _run(input_file_name, output_dir_name): """Plots mean example created by average_examples.py. This is effectively the main method. :param input_file_name: See documentation at top of file. :param output_dir_name: Same. """ file_system_utils.mkdir_recursive_if_necessary( directory_name=output_dir_name) pickle_file_handle = open(input_file_name, 'rb') mean_example_dict = pickle.load(pickle_file_handle) pickle_file_handle.close() # Plot predictors with liquid-water content (LWC). figure_object = profile_plotting.plot_predictors( example_dict=mean_example_dict, example_index=0, plot_ice=False, use_log_scale=True)[0] panel_file_names = ['foo'] * 3 panel_file_names[0] = '{0:s}/predictors_with_lwc.jpg'.format( output_dir_name) print('Saving figure to: "{0:s}"...'.format(panel_file_names[0])) figure_object.savefig(panel_file_names[0], dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) # Plot predictors with ice-water content (IWC). figure_object = profile_plotting.plot_predictors( example_dict=mean_example_dict, example_index=0, plot_ice=True, use_log_scale=True)[0] panel_file_names[1] = '{0:s}/predictors_with_iwc.jpg'.format( output_dir_name) print('Saving figure to: "{0:s}"...'.format(panel_file_names[1])) figure_object.savefig(panel_file_names[1], dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) # Plot targets. figure_object = profile_plotting.plot_targets( example_dict=mean_example_dict, example_index=0, use_log_scale=True)[0] panel_file_names[2] = '{0:s}/targets.jpg'.format(output_dir_name) print('Saving figure to: "{0:s}"...'.format(panel_file_names[2])) figure_object.savefig(panel_file_names[2], dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) # Concatenate panels. concat_figure_file_name = '{0:s}/predictors_and_targets.jpg'.format( output_dir_name) print( 'Concatenating panels to: "{0:s}"...'.format(concat_figure_file_name)) imagemagick_utils.concatenate_images( input_file_names=panel_file_names, output_file_name=concat_figure_file_name, num_panel_rows=2, num_panel_columns=2, border_width_pixels=50) imagemagick_utils.resize_image(input_file_name=concat_figure_file_name, output_file_name=concat_figure_file_name, output_size_pixels=CONCAT_FIGURE_SIZE_PX)
def _plot_results_one_example(bwo_dict, example_index, model_metadata_dict, use_log_scale, output_dir_name): """Plots results for one example. :param bwo_dict: Dictionary read by `backwards_optimization.read_file`. :param example_index: Will plot results for example with this array index. :param model_metadata_dict: Dictionary read by `neural_net.read_metafile`. :param use_log_scale: See documentation at top of file. :param output_dir_name: Name of output directory. Figure will be saved here. """ # Housekeeping. example_id_string = bwo_dict[bwo.EXAMPLE_IDS_KEY][example_index] generator_option_dict = model_metadata_dict[ neural_net.TRAINING_OPTIONS_KEY] base_example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY] } init_example_dict = copy.deepcopy(base_example_dict) init_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.INIT_SCALAR_PREDICTORS_KEY][[example_index], ...]) init_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.INIT_VECTOR_PREDICTORS_KEY][[example_index], ...]) final_example_dict = copy.deepcopy(base_example_dict) final_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.FINAL_SCALAR_PREDICTORS_KEY][[example_index], ...]) final_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( bwo_dict[bwo.FINAL_VECTOR_PREDICTORS_KEY][[example_index], ...]) diff_example_dict = copy.deepcopy(base_example_dict) diff_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] = ( final_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY] - init_example_dict[example_utils.SCALAR_PREDICTOR_VALS_KEY]) diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( final_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] - init_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY]) predictor_names = ( diff_example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY]) if example_utils.TEMPERATURE_NAME in predictor_names: temperature_index = predictor_names.index( example_utils.TEMPERATURE_NAME) diff_predictor_matrix = ( diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY]) diff_predictor_matrix[..., temperature_index] = ( temperature_conv.celsius_to_kelvins( diff_predictor_matrix[..., temperature_index])) diff_example_dict[example_utils.VECTOR_PREDICTOR_VALS_KEY] = ( diff_predictor_matrix) num_predictor_sets = len(PREDICTOR_NAMES_BY_SET) for k in range(num_predictor_sets): these_flags = numpy.array([ n in base_example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY] for n in PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices] predictor_colours = [ PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] # Plot initial and final values on the same set of axes. handle_dict = profile_plotting.plot_predictors( example_dict=init_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), SOLID_LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=None) profile_plotting.plot_predictors( example_dict=final_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), DASHED_LINE_WIDTH), predictor_line_styles=['dashed'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=handle_dict) output_file_name = '{0:s}/{1:s}_predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) # Plot differences (final minus initial). handle_dict = profile_plotting.plot_predictors( example_dict=diff_example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), SOLID_LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, handle_dict=None) output_file_name = '{0:s}/{1:s}_predictor-set-{2:d}_diffs.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object)
def _plot_one_example(example_dict, example_index, use_log_scale, output_dir_name): """Plots data for one example. :param example_dict: See doc for `example_io.read_file`. :param example_index: Will plot results for example with this array index. :param use_log_scale: See documentation at top of file. :param output_dir_name: Name of output directory. Figures will be saved here. """ example_id_string = ( example_dict[example_utils.EXAMPLE_IDS_KEY][example_index]) num_predictor_sets = len(PREDICTOR_NAMES_BY_SET) for k in range(num_predictor_sets): these_flags = numpy.array([ n in example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY] for n in PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices] predictor_colours = [ PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] handle_dict = profile_plotting.plot_predictors( example_dict=example_dict, example_index=example_index, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale) output_file_name = '{0:s}/{1:s}_predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) handle_dict = profile_plotting.plot_targets(example_dict=example_dict, example_index=example_index, use_log_scale=use_log_scale, line_width=LINE_WIDTH, line_style='solid') output_file_name = '{0:s}/{1:s}_targets.jpg'.format( output_dir_name, example_id_string.replace('_', '-')) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object)
def _plot_saliency_one_example(saliency_dict, example_index, model_metadata_dict, use_log_scale, legend_suffix, output_dir_name): """Plots saliency map for one example. :param saliency_dict: Dictionary read by `saliency.read_file`. :param example_index: Will plot saliency map for example with this array index. :param model_metadata_dict: Dictionary read by `neural_net.read_metafile`. :param use_log_scale: See documentation at top of file. :param legend_suffix: End of figure legend. :param output_dir_name: Name of output directory. Figure will be saved here. """ # Housekeeping. example_id_string = saliency_dict[saliency.EXAMPLE_IDS_KEY][example_index] generator_option_dict = model_metadata_dict[ neural_net.TRAINING_OPTIONS_KEY] example_dict = { example_utils.SCALAR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.SCALAR_PREDICTOR_NAMES_KEY], example_utils.VECTOR_PREDICTOR_NAMES_KEY: generator_option_dict[neural_net.VECTOR_PREDICTOR_NAMES_KEY], example_utils.HEIGHTS_KEY: generator_option_dict[neural_net.HEIGHTS_KEY], example_utils.SCALAR_PREDICTOR_VALS_KEY: saliency_dict[saliency.SCALAR_SALIENCY_KEY][[example_index], ...], example_utils.VECTOR_PREDICTOR_VALS_KEY: saliency_dict[saliency.VECTOR_SALIENCY_KEY][[example_index], ...] } scalar_predictor_names = ( example_dict[example_utils.SCALAR_PREDICTOR_NAMES_KEY]) scalar_saliency_matrix = saliency_dict[saliency.SCALAR_SALIENCY_KEY] num_scalar_dim = len(scalar_saliency_matrix.shape) - 1 num_scalar_predictors = len(scalar_predictor_names) legend_string = '' if num_scalar_dim == 1: for k in range(num_scalar_predictors): if k > 0: legend_string += '\n' legend_string += '{0:s}: {1:.2f}'.format( PREDICTOR_NAME_TO_VERBOSE[scalar_predictor_names[k]], scalar_saliency_matrix[0, k]) if legend_suffix != '': legend_string += '\n' + legend_suffix else: legend_string = legend_suffix num_predictor_sets = len(VECTOR_PREDICTOR_NAMES_BY_SET) for k in range(num_predictor_sets): these_flags = numpy.array([ n in example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY] for n in VECTOR_PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [ VECTOR_PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices ] predictor_colours = [ VECTOR_PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] handle_dict = profile_plotting.plot_predictors( example_dict=example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), 2), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, include_units=False, handle_dict=None) axes_object = handle_dict[profile_plotting.AXES_OBJECTS_KEY][0] if legend_string != '': axes_object.text(0.01, 0.99, legend_string, fontsize=LEGEND_FONT_SIZE, color='k', bbox=LEGEND_BOUNDING_BOX_DICT, horizontalalignment='left', verticalalignment='top', transform=axes_object.transAxes, zorder=1e10) output_file_name = '{0:s}/{1:s}_vector-predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) if num_scalar_dim == 1: return num_predictor_sets = len(SCALAR_PREDICTOR_NAMES_BY_SET) for k in range(num_predictor_sets): these_flags = numpy.array([ n in example_dict[example_utils.SCALAR_PREDICTOR_NAMES_KEY] for n in SCALAR_PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [ SCALAR_PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices ] predictor_colours = [ SCALAR_PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] handle_dict = profile_plotting.plot_predictors( example_dict=example_dict, example_index=0, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), 2), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=use_log_scale, include_units=False, handle_dict=None) output_file_name = '{0:s}/{1:s}_scalar-predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] print('Saving figure to: "{0:s}"...'.format(output_file_name)) figure_object.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object)
def _do_plotting(example_dict, example_index, output_dir_name): """Does the plotting. :param example_dict: See doc for `example_io.read_file`. :param example_index: Will plot predictors and targets for [k]th example in dictionary, where k = `example_index`. :param output_dir_name: Name of output directory. Figures will be saved here. """ example_id_string = ( example_dict[example_utils.EXAMPLE_IDS_KEY][example_index]) num_predictor_sets = len(PREDICTOR_NAMES_BY_SET) letter_label = None panel_file_names = [] for k in range(num_predictor_sets): these_flags = numpy.array([ n in example_dict[example_utils.VECTOR_PREDICTOR_NAMES_KEY] for n in PREDICTOR_NAMES_BY_SET[k] ], dtype=bool) these_indices = numpy.where(these_flags)[0] if len(these_indices) == 0: continue predictor_names = [PREDICTOR_NAMES_BY_SET[k][i] for i in these_indices] predictor_colours = [ PREDICTOR_COLOURS_BY_SET[k][i] for i in these_indices ] handle_dict = profile_plotting.plot_predictors( example_dict=example_dict, example_index=example_index, predictor_names=predictor_names, predictor_colours=predictor_colours, predictor_line_widths=numpy.full(len(these_indices), LINE_WIDTH), predictor_line_styles=['solid'] * len(these_indices), use_log_scale=True) if letter_label is None: letter_label = 'a' else: letter_label = chr(ord(letter_label) + 1) plotting_utils.label_axes( axes_object=handle_dict[profile_plotting.AXES_OBJECTS_KEY][0], label_string='({0:s})'.format(letter_label), font_size=LETTER_LABEL_FONT_SIZE, x_coord_normalized=LETTER_LABEL_X_COORD, y_coord_normalized=LETTER_LABEL_Y_COORD) this_file_name = '{0:s}/{1:s}_predictor-set-{2:d}.jpg'.format( output_dir_name, example_id_string.replace('_', '-'), k) panel_file_names.append(this_file_name) print('Saving figure to: "{0:s}"...'.format(this_file_name)) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] figure_object.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) imagemagick_utils.trim_whitespace(input_file_name=this_file_name, output_file_name=this_file_name) imagemagick_utils.resize_image(input_file_name=this_file_name, output_file_name=this_file_name, output_size_pixels=int(2.5e6)) handle_dict = profile_plotting.plot_targets(example_dict=example_dict, example_index=example_index, use_log_scale=True, line_width=LINE_WIDTH, line_style='solid') letter_label = chr(ord(letter_label) + 1) plotting_utils.label_axes( axes_object=handle_dict[profile_plotting.HEATING_RATE_HANDLE_KEY], label_string='({0:s})'.format(letter_label), font_size=LETTER_LABEL_FONT_SIZE, x_coord_normalized=LETTER_LABEL_X_COORD, y_coord_normalized=LETTER_LABEL_Y_COORD) this_file_name = '{0:s}/{1:s}_targets.jpg'.format( output_dir_name, example_id_string.replace('_', '-')) panel_file_names.append(this_file_name) print('Saving figure to: "{0:s}"...'.format(this_file_name)) figure_object = handle_dict[profile_plotting.FIGURE_HANDLE_KEY] figure_object.savefig(this_file_name, dpi=FIGURE_RESOLUTION_DPI, pad_inches=0, bbox_inches='tight') pyplot.close(figure_object) imagemagick_utils.trim_whitespace(input_file_name=this_file_name, output_file_name=this_file_name) imagemagick_utils.resize_image(input_file_name=this_file_name, output_file_name=this_file_name, output_size_pixels=int(2.5e6)) concat_file_name = '{0:s}/{1:s}_concat.jpg'.format( output_dir_name, example_id_string.replace('_', '-')) print('Concatenating panels to: "{0:s}"...'.format(concat_file_name)) imagemagick_utils.concatenate_images(input_file_names=panel_file_names, output_file_name=concat_file_name, num_panel_rows=2, num_panel_columns=2, border_width_pixels=25) imagemagick_utils.trim_whitespace(input_file_name=concat_file_name, output_file_name=concat_file_name)