def _plot_feature_map_after_conv(feature_matrix): """Plots new feature map (after convolution). M = number of rows in grid N = number of columns in grid :param feature_matrix: Feature map as M-by-N numpy array. """ dummy_matrix = numpy.full(feature_matrix.shape, numpy.nan) dummy_matrix[:2, :2] = HIGHLIGHTED_VALUE _, axes_object = pyplot.subplots(1, 1, figsize=(FIGURE_WIDTH_INCHES, FIGURE_HEIGHT_INCHES)) pyplot.imshow(dummy_matrix, cmap=COLOUR_MAP_OBJECT, vmin=HIGHLIGHTED_VALUE - 1, vmax=HIGHLIGHTED_VALUE, axes=axes_object, origin='upper') pyplot.xticks([], []) pyplot.yticks([], []) for i in range(feature_matrix.shape[1]): for j in range(feature_matrix.shape[0]): if i == j == 1: this_colour = SPECIAL_COLOUR + 0. else: this_colour = MAIN_COLOUR + 0. axes_object.text(i, j, '{0:.1f}'.format(feature_matrix[j, i]), fontsize=OVERLAY_FONT_SIZE, color=this_colour, horizontalalignment='center', verticalalignment='center') # polygon_x_coords = numpy.array([0, 2, 2, 0, 0], dtype=float) - 0.5 # polygon_y_coords = numpy.array([2, 2, 0, 0, 2], dtype=float) - 0.5 # axes_object.plot( # polygon_x_coords, polygon_y_coords, color=LINE_COLOUR, # linewidth=LINE_WIDTH) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string='(c)', font_colour=ANNOTATION_COLOUR) print 'Saving figure to: "{0:s}"...'.format(AFTER_CONV_FILE_NAME) file_system_utils.mkdir_recursive_if_necessary( file_name=AFTER_CONV_FILE_NAME) pyplot.savefig(AFTER_CONV_FILE_NAME, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=AFTER_CONV_FILE_NAME, output_file_name=AFTER_CONV_FILE_NAME) return feature_matrix
def _plot_locating_variable( locating_var_matrix_m01_s01, title_string, annotation_string, output_file_name): """Plots locating variable. M = number of rows in grid N = number of columns in grid :param locating_var_matrix_m01_s01: M-by-N numpy array with values of locating variable. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits, axes_object, basemap_object ) = _init_basemap(BORDER_COLOUR) matrix_to_plot = locating_var_matrix_m01_s01[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] * LOCATING_VAR_MULTIPLIER max_colour_value = numpy.nanpercentile( numpy.absolute(matrix_to_plot), MAX_COLOUR_PERCENTILE) min_colour_value = -1 * max_colour_value nwp_plotting.plot_subgrid( field_matrix=matrix_to_plot, model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=LOCATING_VAR_COLOUR_MAP_OBJECT, min_value_in_colour_map=min_colour_value, max_value_in_colour_map=max_colour_value, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0]) plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=matrix_to_plot, colour_map=LOCATING_VAR_COLOUR_MAP_OBJECT, colour_min=min_colour_value, colour_max=max_colour_value, orientation='vertical', extend_min=True, extend_max=True, fraction_of_axis_length=COLOUR_BAR_LENGTH_FRACTION) pyplot.title(title_string) plotting_utils.annotate_axes( axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_kernel(): """Plots convolutional kernel. J = number of rows in kernel K = number of columns in kernel :return: kernel_matrix: Kernel as J-by-K numpy array. """ kernel_matrix = numpy.random.choice(a=POSSIBLE_KERNEL_VALUES, size=(NUM_ROWS_IN_KERNEL, NUM_COLUMNS_IN_KERNEL), replace=True) dummy_matrix = numpy.full((NUM_ROWS_IN_KERNEL, NUM_COLUMNS_IN_KERNEL), HIGHLIGHTED_VALUE) _, axes_object = pyplot.subplots(1, 1, figsize=(FIGURE_WIDTH_INCHES, FIGURE_HEIGHT_INCHES)) pyplot.imshow(dummy_matrix, cmap=COLOUR_MAP_OBJECT, vmin=HIGHLIGHTED_VALUE - 1, vmax=HIGHLIGHTED_VALUE, axes=axes_object, origin='upper') pyplot.xticks([], []) pyplot.yticks([], []) for i in range(kernel_matrix.shape[1]): for j in range(kernel_matrix.shape[0]): axes_object.text(i, j, '{0:.1f}'.format(kernel_matrix[j, i]), fontsize=OVERLAY_FONT_SIZE, color=MAIN_COLOUR, horizontalalignment='center', verticalalignment='center') plotting_utils.annotate_axes(axes_object=axes_object, annotation_string='(b)', font_colour=ANNOTATION_COLOUR) print 'Saving figure to: "{0:s}"...'.format(KERNEL_FILE_NAME) file_system_utils.mkdir_recursive_if_necessary(file_name=KERNEL_FILE_NAME) pyplot.savefig(KERNEL_FILE_NAME, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=KERNEL_FILE_NAME, output_file_name=KERNEL_FILE_NAME) return kernel_matrix
def _plot_binary_table(): """Plots binary contingency table.""" _, axes_object = pyplot.subplots(1, 1, figsize=(FIGURE_WIDTH_INCHES, FIGURE_HEIGHT_INCHES)) pyplot.imshow(numpy.zeros((2, 2)), cmap=COLOUR_MAP_OBJECT, vmin=LARGE_NUMBER, vmax=LARGE_NUMBER + 1, axes=axes_object, origin='upper') tick_locations = numpy.array([0, 1], dtype=int) tick_labels = ['Yes', 'No'] pyplot.xticks(tick_locations, tick_labels) pyplot.xlabel('Actual') pyplot.yticks(tick_locations, tick_labels) pyplot.ylabel('Predicted') for i in range(2): for j in range(2): axes_object.text(i, j, str(BINARY_VARIABLE_NAME_MATRIX[i, j]), fontsize=OVERLAY_FONT_SIZE, color=MAIN_COLOUR, horizontalalignment='center', verticalalignment='center') plotting_utils.annotate_axes(axes_object=axes_object, annotation_string='(a)', font_colour=ANNOTATION_COLOUR) print 'Saving figure to: "{0:s}"...'.format(BINARY_CT_FILE_NAME) file_system_utils.mkdir_recursive_if_necessary( file_name=BINARY_CT_FILE_NAME) pyplot.savefig(BINARY_CT_FILE_NAME, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=BINARY_CT_FILE_NAME, output_file_name=BINARY_CT_FILE_NAME)
def _plot_front_types(predicted_label_matrix, title_string, annotation_string, output_file_name): """Plots front type at each grid cell. M = number of rows in grid N = number of columns in grid :param predicted_label_matrix: M-by-N numpy array with predicted front type at each grid cell. Each front type is from the list `front_utils.VALID_INTEGER_IDS`. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits, axes_object, basemap_object ) = _init_basemap(BORDER_COLOUR) matrix_to_plot = predicted_label_matrix[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] front_plotting.plot_narr_grid( frontal_grid_matrix=matrix_to_plot, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0]) pyplot.title(title_string) plotting_utils.annotate_axes( axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_fronts(actual_binary_matrix, predicted_binary_matrix, title_string, annotation_string, output_file_name): """Plots actual and predicted fronts. M = number of rows in grid N = number of columns in grid :param actual_binary_matrix: M-by-N numpy array. If actual_binary_matrix[i, j] = 1, there is an actual front passing through grid cell [i, j]. :param predicted_binary_matrix: Same but for predicted fronts. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits) = nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1], resolution_string='i') plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) this_colour_map_object, this_colour_norm_object = _get_colour_map(True) this_matrix = actual_binary_matrix[0, narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] nwp_plotting.plot_subgrid( field_matrix=this_matrix, model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=this_colour_map_object, min_value_in_colour_map=this_colour_norm_object.boundaries[0], max_value_in_colour_map=this_colour_norm_object.boundaries[-1], first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], opacity=ACTUAL_FRONT_OPACITY) this_colour_map_object, this_colour_norm_object = _get_colour_map(False) this_matrix = predicted_binary_matrix[0, narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] nwp_plotting.plot_subgrid( field_matrix=this_matrix, model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=this_colour_map_object, min_value_in_colour_map=this_colour_norm_object.boundaries[0], max_value_in_colour_map=this_colour_norm_object.boundaries[-1], first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], opacity=PREDICTED_FRONT_OPACITY) pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_one_time( predictor_matrix, predictor_names, front_polyline_table, high_low_table, thermal_colour_map_object, max_thermal_prctile_for_colours, narr_row_limits, narr_column_limits, title_string, letter_label, output_file_name): """Plots predictors at one time. M = number of rows in grid N = number of columns in grid C = number of channels (predictors) :param predictor_matrix: M-by-N-by-C numpy array of predictor values. :param predictor_names: length-C list of predictor names. :param front_polyline_table: pandas DataFrame returned by `fronts_io.read_polylines_from_file`. :param high_low_table: pandas DataFrame returned by `wpc_bulletin_io.read_highs_and_lows`. :param thermal_colour_map_object: See documentation at top of file. :param max_thermal_prctile_for_colours: Same. :param narr_row_limits: length-2 numpy array, indicating the first and last NARR rows in `predictor_matrix`. If narr_row_limits = [i, k], `predictor_matrix` spans rows i...k of the full NARR grid. :param narr_column_limits: Same but for columns. :param title_string: Title (will be placed above figure). :param letter_label: Letter label. If this is "a", the label "(a)" will be printed at the top left of the figure. :param output_file_name: Path to output file (figure will be saved here). """ _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1] ) plotting_utils.plot_coastlines( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR ) plotting_utils.plot_countries( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR ) plotting_utils.plot_states_and_provinces( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR ) plotting_utils.plot_parallels( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG ) plotting_utils.plot_meridians( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG ) num_predictors = len(predictor_names) for j in range(num_predictors): if predictor_names[j] in WIND_FIELD_NAMES: continue min_colour_value = numpy.percentile( predictor_matrix[..., j], 100. - max_thermal_prctile_for_colours) max_colour_value = numpy.percentile( predictor_matrix[..., j], max_thermal_prctile_for_colours) nwp_plotting.plot_subgrid( field_matrix=predictor_matrix[..., j], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=thermal_colour_map_object, min_value_in_colour_map=min_colour_value, max_value_in_colour_map=max_colour_value, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0] ) plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=predictor_matrix[..., j], colour_map=thermal_colour_map_object, colour_min=min_colour_value, colour_max=max_colour_value, orientation='horizontal', extend_min=True, extend_max=True, fraction_of_axis_length=0.9) u_wind_index = predictor_names.index( processed_narr_io.U_WIND_GRID_RELATIVE_NAME) v_wind_index = predictor_names.index( processed_narr_io.V_WIND_GRID_RELATIVE_NAME) nwp_plotting.plot_wind_barbs_on_subgrid( u_wind_matrix_m_s01=predictor_matrix[..., u_wind_index], v_wind_matrix_m_s01=predictor_matrix[..., v_wind_index], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], plot_every_k_rows=PLOT_EVERY_KTH_WIND_BARB, plot_every_k_columns=PLOT_EVERY_KTH_WIND_BARB, barb_length=WIND_BARB_LENGTH, empty_barb_radius=EMPTY_WIND_BARB_RADIUS, fill_empty_barb=False, colour_map=WIND_COLOUR_MAP_OBJECT, colour_minimum_kt=MIN_COLOUR_WIND_SPEED_KT, colour_maximum_kt=MAX_COLOUR_WIND_SPEED_KT) if high_low_table is None: num_pressure_systems = 0 else: num_pressure_systems = len(high_low_table.index) for i in range(num_pressure_systems): this_system_type_string = high_low_table[ wpc_bulletin_io.SYSTEM_TYPE_COLUMN].values[i] if this_system_type_string == wpc_bulletin_io.HIGH_PRESSURE_STRING: this_string = 'H' else: this_string = 'L' this_x_coord_metres, this_y_coord_metres = basemap_object( high_low_table[wpc_bulletin_io.LONGITUDE_COLUMN].values[i], high_low_table[wpc_bulletin_io.LATITUDE_COLUMN].values[i] ) axes_object.text( this_x_coord_metres, this_y_coord_metres, this_string, fontsize=PRESSURE_SYSTEM_FONT_SIZE, color=PRESSURE_SYSTEM_COLOUR, fontweight='bold', horizontalalignment='center', verticalalignment='center') num_fronts = len(front_polyline_table.index) for i in range(num_fronts): this_front_type_string = front_polyline_table[ front_utils.FRONT_TYPE_COLUMN].values[i] if this_front_type_string == front_utils.WARM_FRONT_STRING_ID: this_colour = WARM_FRONT_COLOUR else: this_colour = COLD_FRONT_COLOUR front_plotting.plot_front_with_markers( line_latitudes_deg=front_polyline_table[ front_utils.LATITUDES_COLUMN].values[i], line_longitudes_deg=front_polyline_table[ front_utils.LONGITUDES_COLUMN].values[i], axes_object=axes_object, basemap_object=basemap_object, front_type_string=front_polyline_table[ front_utils.FRONT_TYPE_COLUMN].values[i], marker_colour=this_colour) pyplot.title(title_string) if letter_label is not None: plotting_utils.annotate_axes( axes_object=axes_object, annotation_string='({0:s})'.format(letter_label) ) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_narr_fields( wet_bulb_theta_matrix_kelvins, u_wind_matrix_m_s01, v_wind_matrix_m_s01, title_string, annotation_string, output_file_name): """Plots NARR fields. M = number of rows in grid N = number of columns in grid :param wet_bulb_theta_matrix_kelvins: M-by-N numpy array of wet-bulb potential temperatures. :param u_wind_matrix_m_s01: M-by-N numpy array of u-wind components (metres per second). :param v_wind_matrix_m_s01: Same but for v-wind. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits, axes_object, basemap_object ) = _init_basemap(BORDER_COLOUR) wet_bulb_theta_matrix_to_plot = wet_bulb_theta_matrix_kelvins[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] - ZERO_CELSIUS_IN_KELVINS u_wind_matrix_to_plot = u_wind_matrix_m_s01[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] v_wind_matrix_to_plot = v_wind_matrix_m_s01[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] nwp_plotting.plot_subgrid( field_matrix=wet_bulb_theta_matrix_to_plot, model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=THERMAL_COLOUR_MAP_OBJECT, min_value_in_colour_map=numpy.nanpercentile( wet_bulb_theta_matrix_to_plot, MIN_COLOUR_PERCENTILE), max_value_in_colour_map=numpy.nanpercentile( wet_bulb_theta_matrix_to_plot, MAX_COLOUR_PERCENTILE), first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0]) plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=wet_bulb_theta_matrix_to_plot, colour_map=THERMAL_COLOUR_MAP_OBJECT, colour_min=numpy.nanpercentile( wet_bulb_theta_matrix_to_plot, MIN_COLOUR_PERCENTILE), colour_max=numpy.nanpercentile( wet_bulb_theta_matrix_to_plot, MAX_COLOUR_PERCENTILE), orientation='vertical', extend_min=True, extend_max=True, fraction_of_axis_length=COLOUR_BAR_LENGTH_FRACTION) nwp_plotting.plot_wind_barbs_on_subgrid( u_wind_matrix_m_s01=u_wind_matrix_to_plot, v_wind_matrix_m_s01=v_wind_matrix_to_plot, model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], plot_every_k_rows=PLOT_EVERY_KTH_WIND_BARB, plot_every_k_columns=PLOT_EVERY_KTH_WIND_BARB, barb_length=WIND_BARB_LENGTH, empty_barb_radius=EMPTY_WIND_BARB_RADIUS, fill_empty_barb=False, colour_map=WIND_COLOUR_MAP_OBJECT, colour_minimum_kt=MIN_COLOUR_WIND_SPEED_KT, colour_maximum_kt=MAX_COLOUR_WIND_SPEED_KT) pyplot.title(title_string) plotting_utils.annotate_axes( axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_fronts(front_line_table, ternary_front_matrix, title_string, annotation_string, output_file_name): """Plots one set of WPC fronts (either before or after dilation). :param front_line_table: See doc for `fronts_io.write_polylines_to_file`. :param ternary_front_matrix: numpy array created by `machine_learning_utils.dilate_ternary_target_images`. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits) = nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) this_matrix = ternary_front_matrix[0, narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] front_plotting.plot_narr_grid( frontal_grid_matrix=this_matrix, axes_object=axes_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], basemap_object=basemap_object, opacity=FRONT_LINE_OPACITY) num_fronts = len(front_line_table.index) for i in range(num_fronts): front_plotting.plot_polyline( latitudes_deg=front_line_table[ front_utils.LATITUDES_COLUMN].values[i], longitudes_deg=front_line_table[ front_utils.LONGITUDES_COLUMN].values[i], basemap_object=basemap_object, axes_object=axes_object, front_type=front_line_table[ front_utils.FRONT_TYPE_COLUMN].values[i], line_width=FRONT_LINE_WIDTH) pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_predictions(predicted_label_matrix, title_string, annotation_string, output_file_name): """Plots predicted front locations. :param predicted_label_matrix: See doc for `target_matrix` in `machine_learning_utils.write_gridded_predictions`. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits) = nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) this_matrix = predicted_label_matrix[0, narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] front_plotting.plot_narr_grid( frontal_grid_matrix=this_matrix, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=1.) pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_predictions_one_time( output_file_name, title_string, annotation_string, predicted_label_matrix=None, class_probability_matrix=None, plot_warm_colour_bar=True, plot_cold_colour_bar=True): """Plots predictions (objects or probability grid) for one valid time. :param output_file_name: Path to output file (figure will be saved here). :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param predicted_label_matrix: See doc for `target_matrix` in `machine_learning_utils.write_gridded_predictions`. :param class_probability_matrix: [used iff `predicted_label_matrix is None`] See doc for `machine_learning_utils.write_gridded_predictions`. :param plot_warm_colour_bar: [used iff `predicted_label_matrix is None`] Boolean flag, indicating whether or not to plot colour bar for warm- front probability. :param plot_cold_colour_bar: Same but for cold-front probability. """ (narr_row_limits, narr_column_limits ) = nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) if class_probability_matrix is None: this_matrix = predicted_label_matrix[ 0, narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] front_plotting.plot_narr_grid( frontal_grid_matrix=this_matrix, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=DETERMINISTIC_OPACITY) else: this_matrix = class_probability_matrix[ 0, narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1), front_utils.WARM_FRONT_INTEGER_ID ] prediction_plotting.plot_narr_grid( probability_matrix=this_matrix, front_string_id=front_utils.WARM_FRONT_STRING_ID, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=PROBABILISTIC_OPACITY) this_matrix = class_probability_matrix[ 0, narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1), front_utils.COLD_FRONT_INTEGER_ID ] prediction_plotting.plot_narr_grid( probability_matrix=this_matrix, front_string_id=front_utils.COLD_FRONT_STRING_ID, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=PROBABILISTIC_OPACITY) if plot_warm_colour_bar: (this_colour_map_object, this_colour_norm_object ) = prediction_plotting.get_warm_front_colour_map()[:2] plotting_utils.add_colour_bar( axes_object_or_list=axes_object, colour_map=this_colour_map_object, colour_norm_object=this_colour_norm_object, values_to_colour=class_probability_matrix[ ..., front_utils.WARM_FRONT_INTEGER_ID], orientation='vertical', extend_min=True, extend_max=False, fraction_of_axis_length=LENGTH_FRACTION_FOR_PROB_COLOUR_BAR) if plot_cold_colour_bar: (this_colour_map_object, this_colour_norm_object ) = prediction_plotting.get_cold_front_colour_map()[:2] plotting_utils.add_colour_bar( axes_object_or_list=axes_object, colour_map=this_colour_map_object, colour_norm_object=this_colour_norm_object, values_to_colour=class_probability_matrix[ ..., front_utils.COLD_FRONT_INTEGER_ID], orientation='vertical', extend_min=True, extend_max=False, fraction_of_axis_length=LENGTH_FRACTION_FOR_PROB_COLOUR_BAR) pyplot.title(title_string) plotting_utils.annotate_axes( axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace( input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_observations_one_time( valid_time_string, title_string, annotation_string, output_file_name): """Plots observations (NARR predictors and WPC fronts) for one valid time. :param valid_time_string: Valid time (format "yyyy-mm-dd-HH"). :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ (narr_row_limits, narr_column_limits ) = nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME) valid_time_unix_sec = time_conversion.string_to_unix_sec( valid_time_string, INPUT_TIME_FORMAT) front_file_name = fronts_io.find_file_for_one_time( top_directory_name=TOP_FRONT_DIR_NAME, file_type=fronts_io.POLYLINE_FILE_TYPE, valid_time_unix_sec=valid_time_unix_sec) print 'Reading data from: "{0:s}"...'.format(front_file_name) front_line_table = fronts_io.read_polylines_from_file(front_file_name) num_narr_fields = len(NARR_FIELD_NAMES) narr_matrix_by_field = [numpy.array([])] * num_narr_fields for j in range(num_narr_fields): if NARR_FIELD_NAMES[j] in WIND_FIELD_NAMES: this_directory_name = TOP_NARR_WIND_DIR_NAME + '' else: this_directory_name = TOP_NARR_DIR_NAME + '' this_file_name = processed_narr_io.find_file_for_one_time( top_directory_name=this_directory_name, field_name=NARR_FIELD_NAMES[j], pressure_level_mb=PRESSURE_LEVEL_MB, valid_time_unix_sec=valid_time_unix_sec) print 'Reading data from: "{0:s}"...'.format(this_file_name) narr_matrix_by_field[j] = processed_narr_io.read_fields_from_file( this_file_name)[0][0, ...] narr_matrix_by_field[j] = utils.fill_nans(narr_matrix_by_field[j]) narr_matrix_by_field[j] = narr_matrix_by_field[j][ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1) ] if NARR_FIELD_NAMES[j] == processed_narr_io.WET_BULB_THETA_NAME: narr_matrix_by_field[j] = ( narr_matrix_by_field[j] - ZERO_CELSIUS_IN_KELVINS ) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces( basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians( basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) for j in range(num_narr_fields): if NARR_FIELD_NAMES[j] in WIND_FIELD_NAMES: continue min_colour_value = numpy.percentile( narr_matrix_by_field[j], MIN_COLOUR_PERCENTILE) max_colour_value = numpy.percentile( narr_matrix_by_field[j], MAX_COLOUR_PERCENTILE) nwp_plotting.plot_subgrid( field_matrix=narr_matrix_by_field[j], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=THERMAL_COLOUR_MAP_OBJECT, min_value_in_colour_map=min_colour_value, max_value_in_colour_map=max_colour_value, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0]) plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=narr_matrix_by_field[j], colour_map=THERMAL_COLOUR_MAP_OBJECT, colour_min=min_colour_value, colour_max=max_colour_value, orientation='vertical', extend_min=True, extend_max=True, fraction_of_axis_length=LENGTH_FRACTION_FOR_THETA_COLOUR_BAR) u_wind_index = NARR_FIELD_NAMES.index( processed_narr_io.U_WIND_EARTH_RELATIVE_NAME) v_wind_index = NARR_FIELD_NAMES.index( processed_narr_io.V_WIND_EARTH_RELATIVE_NAME) nwp_plotting.plot_wind_barbs_on_subgrid( u_wind_matrix_m_s01=narr_matrix_by_field[u_wind_index], v_wind_matrix_m_s01=narr_matrix_by_field[v_wind_index], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], plot_every_k_rows=PLOT_EVERY_KTH_WIND_BARB, plot_every_k_columns=PLOT_EVERY_KTH_WIND_BARB, barb_length=WIND_BARB_LENGTH, empty_barb_radius=EMPTY_WIND_BARB_RADIUS, colour_map=WIND_COLOUR_MAP_OBJECT, colour_minimum_kt=MIN_COLOUR_WIND_SPEED_KT, colour_maximum_kt=MAX_COLOUR_WIND_SPEED_KT) num_fronts = len(front_line_table.index) for i in range(num_fronts): this_front_type_string = front_line_table[ front_utils.FRONT_TYPE_COLUMN].values[i] if this_front_type_string == front_utils.WARM_FRONT_STRING_ID: this_colour = WARM_FRONT_COLOUR else: this_colour = COLD_FRONT_COLOUR front_plotting.plot_polyline( latitudes_deg=front_line_table[ front_utils.LATITUDES_COLUMN].values[i], longitudes_deg=front_line_table[ front_utils.LONGITUDES_COLUMN].values[i], basemap_object=basemap_object, axes_object=axes_object, front_type=front_line_table[ front_utils.FRONT_TYPE_COLUMN].values[i], line_width=FRONT_LINE_WIDTH, line_colour=this_colour) pyplot.title(title_string) plotting_utils.annotate_axes( axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_histogram(num_fronts_by_bin, front_type_string, title_string, annotation_string, output_file_name): """Plots histogram of either cold-front or warm-front lengths. B = number of bins :param num_fronts_by_bin: length-B numpy array with number of fronts in each bin. :param front_type_string: Front type (must be accepted by `front_utils.check_front_type`). :param title_string: Figure title (will be placed above the figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output file (figure will be saved here). """ bin_frequencies = (num_fronts_by_bin.astype(float) / numpy.sum(num_fronts_by_bin)) num_bins = len(num_fronts_by_bin) bin_edges_metres = numpy.linspace(MIN_HISTOGRAM_LENGTH_METRES, MAX_HISTOGRAM_LENGTH_METRES, num=num_bins + 1) bin_width_metres = bin_edges_metres[1] - bin_edges_metres[0] bin_centers_metres = bin_edges_metres[:-1] + bin_width_metres / 2 _, axes_object = pyplot.subplots(1, 1, figsize=(FIGURE_WIDTH_INCHES, FIGURE_HEIGHT_INCHES)) if front_type_string == front_utils.WARM_FRONT_STRING_ID: this_colour = front_plotting.DEFAULT_WARM_FRONT_COLOUR else: this_colour = front_plotting.DEFAULT_COLD_FRONT_COLOUR axes_object.bar(bin_centers_metres * METRES_TO_KM, bin_frequencies, bin_width_metres * METRES_TO_KM, color=this_colour, edgecolor=LINE_COLOUR, linewidth=LINE_WIDTH) max_y_tick_value = rounder.floor_to_nearest( 1.05 * numpy.max(bin_frequencies), Y_TICK_SPACING) num_y_ticks = 1 + int(numpy.round(max_y_tick_value / Y_TICK_SPACING)) y_tick_values = numpy.linspace(0., max_y_tick_value, num=num_y_ticks) pyplot.yticks(y_tick_values) axes_object.set_xlim(MIN_HISTOGRAM_LENGTH_METRES * METRES_TO_KM, MAX_HISTOGRAM_LENGTH_METRES * METRES_TO_KM) axes_object.set_ylim(0., 1.05 * numpy.max(bin_frequencies)) x_tick_values, _ = pyplot.xticks() x_tick_labels = [] for i in range(len(x_tick_values)): this_label = '{0:d}'.format(int(numpy.round(x_tick_values[i]))) if i == len(x_tick_values) - 1: this_label += '+' x_tick_labels.append(this_label) pyplot.xticks(x_tick_values, x_tick_labels) pyplot.xlabel('Front length (km)') pyplot.ylabel('Frequency') pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name) pyplot.savefig(output_file_name, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_front_densities(num_fronts_matrix, colour_map_object, title_string, annotation_string, output_file_name, mask_matrix=None, add_colour_bar=True): """Plots number of fronts at each NARR grid cell. M = number of grid rows (unique y-coordinates at grid points) N = number of grid columns (unique x-coordinates at grid points) :param num_fronts_matrix: M-by-N numpy array with number of fronts at each grid cell. :param colour_map_object: Instance of `matplotlib.pyplot.cm`. :param title_string: Title (will be placed above figure). :param annotation_string: Text annotation (will be placed in top left of figure). :param output_file_name: Path to output (image) file. The figure will be saved here. :param mask_matrix: M-by-N numpy array of integers. If mask_matrix[i, j] = 0, grid cell [i, j] will be masked out in the map. If `mask_matrix is None`, there will be no masking. :param add_colour_bar: Boolean flag. If True, will add colour bar. """ _, axes_object, basemap_object = narr_plotting.init_basemap() plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) num_fronts_matrix = num_fronts_matrix.astype(float) max_colour_value = numpy.percentile(num_fronts_matrix, MAX_COLOUR_PERCENTILE) if mask_matrix is not None: num_fronts_matrix[mask_matrix == 0] = numpy.nan narr_plotting.plot_xy_grid(data_matrix=num_fronts_matrix, axes_object=axes_object, basemap_object=basemap_object, colour_map=colour_map_object, colour_minimum=0., colour_maximum=max_colour_value) if add_colour_bar: plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=num_fronts_matrix, colour_map=colour_map_object, colour_min=0., colour_max=max_colour_value, orientation='horizontal', extend_min=False, extend_max=True) pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)
def _plot_one_time(valid_time_string, pressure_level_mb, title_string, annotation_string, narr_rotation_cos_matrix, narr_rotation_sin_matrix): """Plots WPC fronts and NARR fields at one time. M = number of grid rows in the full NARR N = number of grid columns in the full NARR :param valid_time_string: Valid time (format "yyyy-mm-dd-HH"). :param pressure_level_mb: Pressure level (millibars). :param title_string: Title (will be placed above figure). :param annotation_string: Annotation (will be placed above and left of figure). :param narr_rotation_cos_matrix: M-by-N numpy array of cosines for wind- rotation angles. :param narr_rotation_sin_matrix: M-by-N numpy array of sines for wind- rotation angles. """ narr_row_limits, narr_column_limits = ( nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME)) valid_time_unix_sec = time_conversion.string_to_unix_sec( valid_time_string, DEFAULT_TIME_FORMAT) front_file_name = fronts_io.find_file_for_one_time( top_directory_name=TOP_FRONT_DIR_NAME, file_type=fronts_io.POLYLINE_FILE_TYPE, valid_time_unix_sec=valid_time_unix_sec) print 'Reading data from: "{0:s}"...'.format(front_file_name) front_line_table = fronts_io.read_polylines_from_file(front_file_name) num_narr_fields = len(NARR_FIELD_NAMES) narr_matrix_by_field = [numpy.array([])] * num_narr_fields for j in range(num_narr_fields): this_file_name = processed_narr_io.find_file_for_one_time( top_directory_name=TOP_NARR_DIRECTORY_NAME, field_name=NARR_FIELD_NAMES[j], pressure_level_mb=pressure_level_mb, valid_time_unix_sec=valid_time_unix_sec) print 'Reading data from: "{0:s}"...'.format(this_file_name) narr_matrix_by_field[j] = processed_narr_io.read_fields_from_file( this_file_name)[0][0, ...] narr_matrix_by_field[j] = utils.fill_nans(narr_matrix_by_field[j]) narr_matrix_by_field[j] = narr_matrix_by_field[j][narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] if NARR_FIELD_NAMES[j] == processed_narr_io.WET_BULB_THETA_NAME: narr_matrix_by_field[j] = (narr_matrix_by_field[j] - ZERO_CELSIUS_IN_KELVINS) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) for j in range(num_narr_fields): if NARR_FIELD_NAMES[j] in WIND_FIELD_NAMES: continue min_colour_value = numpy.percentile(narr_matrix_by_field[j], MIN_COLOUR_PERCENTILE) max_colour_value = numpy.percentile(narr_matrix_by_field[j], MAX_COLOUR_PERCENTILE) nwp_plotting.plot_subgrid( field_matrix=narr_matrix_by_field[j], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, colour_map=THERMAL_COLOUR_MAP_OBJECT, min_value_in_colour_map=min_colour_value, max_value_in_colour_map=max_colour_value, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0]) plotting_utils.add_linear_colour_bar( axes_object_or_list=axes_object, values_to_colour=narr_matrix_by_field[j], colour_map=THERMAL_COLOUR_MAP_OBJECT, colour_min=min_colour_value, colour_max=max_colour_value, orientation='horizontal', extend_min=True, extend_max=True, fraction_of_axis_length=0.9) this_cos_matrix = narr_rotation_cos_matrix[narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] this_sin_matrix = narr_rotation_sin_matrix[narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] u_wind_index = NARR_FIELD_NAMES.index( processed_narr_io.U_WIND_GRID_RELATIVE_NAME) v_wind_index = NARR_FIELD_NAMES.index( processed_narr_io.V_WIND_GRID_RELATIVE_NAME) narr_matrix_by_field[u_wind_index], narr_matrix_by_field[v_wind_index] = ( nwp_model_utils.rotate_winds_to_earth_relative( u_winds_grid_relative_m_s01=narr_matrix_by_field[u_wind_index], v_winds_grid_relative_m_s01=narr_matrix_by_field[v_wind_index], rotation_angle_cosines=this_cos_matrix, rotation_angle_sines=this_sin_matrix)) nwp_plotting.plot_wind_barbs_on_subgrid( u_wind_matrix_m_s01=narr_matrix_by_field[u_wind_index], v_wind_matrix_m_s01=narr_matrix_by_field[v_wind_index], model_name=nwp_model_utils.NARR_MODEL_NAME, axes_object=axes_object, basemap_object=basemap_object, first_row_in_full_grid=narr_row_limits[0], first_column_in_full_grid=narr_column_limits[0], plot_every_k_rows=PLOT_EVERY_KTH_WIND_BARB, plot_every_k_columns=PLOT_EVERY_KTH_WIND_BARB, barb_length=WIND_BARB_LENGTH, empty_barb_radius=EMPTY_WIND_BARB_RADIUS, fill_empty_barb=False, colour_map=WIND_COLOUR_MAP_OBJECT, colour_minimum_kt=MIN_COLOUR_WIND_SPEED_KT, colour_maximum_kt=MAX_COLOUR_WIND_SPEED_KT) num_fronts = len(front_line_table.index) for i in range(num_fronts): this_front_type_string = front_line_table[ front_utils.FRONT_TYPE_COLUMN].values[i] if this_front_type_string == front_utils.WARM_FRONT_STRING_ID: this_colour = WARM_FRONT_COLOUR else: this_colour = COLD_FRONT_COLOUR front_plotting.plot_front_with_markers( line_latitudes_deg=front_line_table[ front_utils.LATITUDES_COLUMN].values[i], line_longitudes_deg=front_line_table[ front_utils.LONGITUDES_COLUMN].values[i], axes_object=axes_object, basemap_object=basemap_object, front_type_string=front_line_table[ front_utils.FRONT_TYPE_COLUMN].values[i], marker_colour=this_colour) pyplot.title(title_string) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string=annotation_string) file_system_utils.mkdir_recursive_if_necessary( directory_name=OUTPUT_DIR_NAME) figure_file_name = '{0:s}/fronts_{1:04d}mb_{2:s}.jpg'.format( OUTPUT_DIR_NAME, pressure_level_mb, valid_time_string) print 'Saving figure to: "{0:s}"...'.format(figure_file_name) pyplot.savefig(figure_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=figure_file_name, output_file_name=figure_file_name) return figure_file_name
def _plot_feature_map_before_conv(): """Plots original feature map (before convolution). M = number of rows in grid N = number of columns in grid :return: feature_matrix: Feature map as M-by-N numpy array. """ feature_matrix = numpy.random.random_integers( low=MIN_FEATURE_VALUE, high=MAX_FEATURE_VALUE, size=(NUM_ROWS_BEFORE_POOLING, NUM_COLUMNS_BEFORE_POOLING)) dummy_matrix = numpy.full( (NUM_ROWS_BEFORE_POOLING, NUM_COLUMNS_BEFORE_POOLING), numpy.nan) dummy_matrix[:3, :3] = HIGHLIGHTED_VALUE dummy_matrix = numpy.ma.masked_where(numpy.isnan(dummy_matrix), dummy_matrix) _, axes_object = pyplot.subplots(1, 1, figsize=(FIGURE_WIDTH_INCHES, FIGURE_HEIGHT_INCHES)) pyplot.imshow(dummy_matrix, cmap=COLOUR_MAP_OBJECT, vmin=HIGHLIGHTED_VALUE - 1, vmax=HIGHLIGHTED_VALUE, axes=axes_object, origin='upper') pyplot.xticks([], []) pyplot.yticks([], []) for i in range(feature_matrix.shape[1]): for j in range(feature_matrix.shape[0]): if i == j == 1: this_colour = SPECIAL_COLOUR + 0. else: this_colour = MAIN_COLOUR + 0. axes_object.text(i, j, '{0:d}'.format(feature_matrix[j, i]), fontsize=OVERLAY_FONT_SIZE, color=this_colour, horizontalalignment='center', verticalalignment='center') # polygon_x_coords = numpy.array([0, 3, 3, 0, 0], dtype=float) - 0.5 # polygon_y_coords = numpy.array([3, 3, 0, 0, 3], dtype=float) - 0.5 # axes_object.plot( # polygon_x_coords, polygon_y_coords, color=LINE_COLOUR, # linewidth=LINE_WIDTH) plotting_utils.annotate_axes(axes_object=axes_object, annotation_string='(a)', font_colour=ANNOTATION_COLOUR) print 'Saving figure to: "{0:s}"...'.format(BEFORE_CONV_FILE_NAME) file_system_utils.mkdir_recursive_if_necessary( file_name=BEFORE_CONV_FILE_NAME) pyplot.savefig(BEFORE_CONV_FILE_NAME, dpi=OUTPUT_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=BEFORE_CONV_FILE_NAME, output_file_name=BEFORE_CONV_FILE_NAME) return feature_matrix
def _plot_one_time(predicted_region_table, title_string, letter_label, output_file_name, class_probability_matrix=None, predicted_label_matrix=None, plot_wf_colour_bar=True, plot_cf_colour_bar=True): """Plots predictions at one time. Either `class_probability_matrix` or `predicted_label_matrix` will be plotted -- not both. M = number of rows in NARR grid N = number of columns in NARR grid :param predicted_region_table: Subset of pandas DataFrame returned by `object_eval.read_predictions_and_obs`, containing predicted fronts at only one time. :param title_string: Title (will be placed above figure). :param letter_label: Letter label. If this is "a", the label "(a)" will be printed at the top left of the figure. :param output_file_name: Path to output file. :param class_probability_matrix: M-by-N-by-3 numpy array of class probabilities. :param predicted_label_matrix: M-by-N numpy array of predicted labels (integers in `front_utils.VALID_INTEGER_IDS`). :param plot_wf_colour_bar: Boolean flag. If True, will plot colour bar for warm-front probability. :param plot_cf_colour_bar: Boolean flag. If True, will plot colour bar for cold-front probability. """ narr_row_limits, narr_column_limits = ( nwp_plotting.latlng_limits_to_rowcol_limits( min_latitude_deg=MIN_LATITUDE_DEG, max_latitude_deg=MAX_LATITUDE_DEG, min_longitude_deg=MIN_LONGITUDE_DEG, max_longitude_deg=MAX_LONGITUDE_DEG, model_name=nwp_model_utils.NARR_MODEL_NAME)) _, axes_object, basemap_object = nwp_plotting.init_basemap( model_name=nwp_model_utils.NARR_MODEL_NAME, first_row_in_full_grid=narr_row_limits[0], last_row_in_full_grid=narr_row_limits[1], first_column_in_full_grid=narr_column_limits[0], last_column_in_full_grid=narr_column_limits[1]) plotting_utils.plot_coastlines(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_countries(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_states_and_provinces(basemap_object=basemap_object, axes_object=axes_object, line_colour=BORDER_COLOUR) plotting_utils.plot_parallels(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lat_deg=-90., upper_right_lat_deg=90., parallel_spacing_deg=PARALLEL_SPACING_DEG) plotting_utils.plot_meridians(basemap_object=basemap_object, axes_object=axes_object, bottom_left_lng_deg=0., upper_right_lng_deg=360., meridian_spacing_deg=MERIDIAN_SPACING_DEG) if class_probability_matrix is None: this_matrix = predicted_label_matrix[narr_row_limits[0]:( narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1)] front_plotting.plot_narr_grid( frontal_grid_matrix=this_matrix, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=0.25) else: this_wf_probability_matrix = class_probability_matrix[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1), front_utils.WARM_FRONT_INTEGER_ID] this_wf_probability_matrix[numpy.isnan( this_wf_probability_matrix)] = 0. prediction_plotting.plot_narr_grid( probability_matrix=this_wf_probability_matrix, front_string_id=front_utils.WARM_FRONT_STRING_ID, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=0.5) this_cf_probability_matrix = class_probability_matrix[ narr_row_limits[0]:(narr_row_limits[1] + 1), narr_column_limits[0]:(narr_column_limits[1] + 1), front_utils.COLD_FRONT_INTEGER_ID] this_cf_probability_matrix[numpy.isnan( this_cf_probability_matrix)] = 0. prediction_plotting.plot_narr_grid( probability_matrix=this_cf_probability_matrix, front_string_id=front_utils.COLD_FRONT_STRING_ID, axes_object=axes_object, basemap_object=basemap_object, first_row_in_narr_grid=narr_row_limits[0], first_column_in_narr_grid=narr_column_limits[0], opacity=0.5) if plot_wf_colour_bar: this_colour_map_object, this_colour_norm_object = ( prediction_plotting.get_warm_front_colour_map()[:2]) plotting_utils.add_colour_bar( axes_object_or_list=axes_object, colour_map=this_colour_map_object, colour_norm_object=this_colour_norm_object, values_to_colour=this_wf_probability_matrix, orientation='horizontal', extend_min=True, extend_max=False, fraction_of_axis_length=0.9) if plot_cf_colour_bar: this_colour_map_object, this_colour_norm_object = ( prediction_plotting.get_cold_front_colour_map()[:2]) plotting_utils.add_colour_bar( axes_object_or_list=axes_object, colour_map=this_colour_map_object, colour_norm_object=this_colour_norm_object, values_to_colour=this_cf_probability_matrix, orientation='horizontal', extend_min=True, extend_max=False, fraction_of_axis_length=0.9) narr_latitude_matrix_deg, narr_longitude_matrix_deg = ( nwp_model_utils.get_latlng_grid_point_matrices( model_name=nwp_model_utils.NARR_MODEL_NAME)) num_objects = len(predicted_region_table.index) for i in range(num_objects): these_rows = predicted_region_table[ object_eval.ROW_INDICES_COLUMN].values[i] these_columns = predicted_region_table[ object_eval.COLUMN_INDICES_COLUMN].values[i] front_plotting.plot_polyline( latitudes_deg=narr_latitude_matrix_deg[these_rows, these_columns], longitudes_deg=narr_longitude_matrix_deg[these_rows, these_columns], axes_object=axes_object, basemap_object=basemap_object, front_type=predicted_region_table[ front_utils.FRONT_TYPE_COLUMN].values[i], line_width=4) # predicted_object_matrix = object_eval.regions_to_images( # predicted_region_table=predicted_region_table, # num_grid_rows=num_grid_rows, num_grid_columns=num_grid_columns) # # this_matrix = predicted_object_matrix[ # 0, # narr_row_limits[0]:(narr_row_limits[1] + 1), # narr_column_limits[0]:(narr_column_limits[1] + 1) # ] # # front_plotting.plot_narr_grid( # frontal_grid_matrix=this_matrix, axes_object=axes_object, # basemap_object=basemap_object, # first_row_in_narr_grid=narr_row_limits[0], # first_column_in_narr_grid=narr_column_limits[0], opacity=1.) pyplot.title(title_string) if letter_label is not None: plotting_utils.annotate_axes( axes_object=axes_object, annotation_string='({0:s})'.format(letter_label)) print 'Saving figure to: "{0:s}"...'.format(output_file_name) pyplot.savefig(output_file_name, dpi=FIGURE_RESOLUTION_DPI) pyplot.close() imagemagick_utils.trim_whitespace(input_file_name=output_file_name, output_file_name=output_file_name)