def check_metadata(
        component_type_string, target_class=None, layer_name=None,
        neuron_index_matrix=None, channel_indices=None):
    """Error-checks metadata for activation calculations.

    C = number of model components (classes, neurons, or channels) for which
        activations were computed

    :param component_type_string: Component type (must be accepted by
        `model_interpretation.check_component_type`).
    :param target_class: See doc for `get_class_activation_for_examples`.
    :param layer_name: See doc for `get_neuron_activation_for_examples` or
        `get_channel_activation_for_examples`.
    :param neuron_index_matrix: [used only if component_type_string = "neuron"]
        C-by-? numpy array, where neuron_index_matrix[j, :] contains array
        indices of the [j]th neuron whose activation was computed.
    :param channel_indices: [used only if component_type_string = "channel"]
        length-C numpy array, where channel_indices[j] is the index of the
        [j]th channel whose activation was computed.
    :return: num_components: Number of model components (classes, neurons, or
        channels) whose activation was computed.
    """

    model_interpretation.check_component_type(component_type_string)
    if (component_type_string ==
            model_interpretation.CLASS_COMPONENT_TYPE_STRING):
        error_checking.assert_is_integer(target_class)
        error_checking.assert_is_geq(target_class, 0)
        num_components = 1

    if component_type_string in [
            model_interpretation.NEURON_COMPONENT_TYPE_STRING,
            model_interpretation.CHANNEL_COMPONENT_TYPE_STRING
    ]:
        error_checking.assert_is_string(layer_name)

    if (component_type_string ==
            model_interpretation.NEURON_COMPONENT_TYPE_STRING):
        error_checking.assert_is_integer_numpy_array(neuron_index_matrix)
        error_checking.assert_is_geq_numpy_array(neuron_index_matrix, 0)
        error_checking.assert_is_numpy_array(
            neuron_index_matrix, num_dimensions=2)
        num_components = neuron_index_matrix.shape[0]

    if (component_type_string ==
            model_interpretation.CHANNEL_COMPONENT_TYPE_STRING):
        error_checking.assert_is_integer_numpy_array(channel_indices)
        error_checking.assert_is_geq_numpy_array(channel_indices, 0)
        num_components = len(channel_indices)

    return num_components
def start_points_and_distances_and_bearings_to_endpoints(
        start_latitudes_deg=None, start_longitudes_deg=None,
        displacements_metres=None, geodetic_bearings_deg=None):
    """Computes endpoint from each start point, displacement, and bearing.

    P = number of start points

    :param start_latitudes_deg: length-P numpy array of beginning latitudes
        (deg N).
    :param start_longitudes_deg: length-P numpy array of beginning longitudes
        (deg E).
    :param displacements_metres: length-P numpy array of displacements.
    :param geodetic_bearings_deg: length-P numpy array of geodetic bearings
        (from start point towards end point, measured clockwise from due north).
    :return: end_latitudes_deg: length-P numpy array of end latitudes (deg N).
    :return: end_longitudes_deg: length-P numpy array of end longitudes (deg E).
    """

    error_checking.assert_is_valid_lat_numpy_array(
        start_latitudes_deg, allow_nan=False)
    error_checking.assert_is_numpy_array(start_latitudes_deg, num_dimensions=1)
    num_points = len(start_latitudes_deg)

    start_longitudes_deg = lng_conversion.convert_lng_positive_in_west(
        start_longitudes_deg, allow_nan=False)
    error_checking.assert_is_numpy_array(
        start_longitudes_deg, exact_dimensions=numpy.array([num_points]))

    error_checking.assert_is_geq_numpy_array(displacements_metres, 0.)
    error_checking.assert_is_numpy_array(
        displacements_metres, exact_dimensions=numpy.array([num_points]))

    error_checking.assert_is_geq_numpy_array(geodetic_bearings_deg, 0.)
    error_checking.assert_is_leq_numpy_array(geodetic_bearings_deg, 360.)
    error_checking.assert_is_numpy_array(
        geodetic_bearings_deg, exact_dimensions=numpy.array([num_points]))

    end_latitudes_deg = numpy.full(num_points, numpy.nan)
    end_longitudes_deg = numpy.full(num_points, numpy.nan)
    for i in range(num_points):
        this_start_point_object = geopy.Point(
            start_latitudes_deg[i], start_longitudes_deg[i])
        this_end_point_object = VincentyDistance(
            meters=displacements_metres[i]).destination(
                this_start_point_object, geodetic_bearings_deg[i])

        end_latitudes_deg[i] = this_end_point_object.latitude
        end_longitudes_deg[i] = this_end_point_object.longitude

    return end_latitudes_deg, lng_conversion.convert_lng_positive_in_west(
        end_longitudes_deg, allow_nan=False)
    def test_get_rotations(self):
        """Ensures correct output from get_rotations."""

        these_ccw_rotation_angles_deg = data_augmentation.get_rotations(
            num_rotations=NUM_ROTATIONS,
            max_absolute_rotation_angle_deg=MAX_ABSOLUTE_ROTATION_ANGLE_DEG)

        self.assertTrue(len(these_ccw_rotation_angles_deg) == NUM_ROTATIONS)
        error_checking.assert_is_geq_numpy_array(
            numpy.absolute(these_ccw_rotation_angles_deg),
            data_augmentation.MIN_ABSOLUTE_ROTATION_ANGLE_DEG)
        error_checking.assert_is_leq_numpy_array(
            numpy.absolute(these_ccw_rotation_angles_deg),
            data_augmentation.MAX_ABSOLUTE_ROTATION_ANGLE_DEG)
Esempio n. 4
0
def get_grid_cell_widths(edge_heights_m_agl):
    """Computes width of each grid cell.

    H = number of grid cells

    :param edge_heights_m_agl: length-(H + 1) numpy array of heights (metres
        above ground level) at edges of grid cells.
    :return: widths_metres: length-H numpy array of grid-cell widths.
    """

    error_checking.assert_is_geq_numpy_array(edge_heights_m_agl, 0.)
    error_checking.assert_is_numpy_array(edge_heights_m_agl, num_dimensions=1)

    return numpy.diff(edge_heights_m_agl)
def vapour_pressure_to_dewpoint(vapour_pressures_pascals, temperatures_kelvins):
    """Converts each vapour pressure to dewpoint.

    Source:
    https://content.meteoblue.com/hu/specifications/weather-variables/humidity

    :param vapour_pressures_pascals: numpy array (any shape) of vapour
        pressures.
    :param temperatures_kelvins: numpy array (same shape) of temperatures.
    :return: dewpoints_kelvins: numpy array (same shape) of dewpoints.
    """

    error_checking.assert_is_geq_numpy_array(
        vapour_pressures_pascals, 0., allow_nan=True
    )
    error_checking.assert_is_geq_numpy_array(
        temperatures_kelvins, 0., allow_nan=True
    )
    error_checking.assert_is_numpy_array(
        temperatures_kelvins,
        exact_dimensions=numpy.array(vapour_pressures_pascals.shape, dtype=int)
    )

    logarithms = numpy.log(
        vapour_pressures_pascals / BASE_VAPOUR_PRESSURE_PASCALS
    )

    temperatures_deg_c = temperature_conv.kelvins_to_celsius(
        temperatures_kelvins
    )

    numerator_coeffs = numpy.full(
        temperatures_deg_c.shape, MAGNUS_DENOMINATOR_COEFF_WATER
    )
    numerator_coeffs[temperatures_deg_c < 0] = MAGNUS_DENOMINATOR_COEFF_ICE
    numerators = numerator_coeffs * logarithms

    denominator_coeffs = numpy.full(
        temperatures_deg_c.shape, MAGNUS_NUMERATOR_COEFF_WATER
    )
    denominator_coeffs[temperatures_deg_c < 0] = MAGNUS_NUMERATOR_COEFF_ICE
    denominators = denominator_coeffs - logarithms

    dewpoints_deg_c = numerators / denominators
    dewpoints_kelvins = temperature_conv.celsius_to_kelvins(dewpoints_deg_c)

    dewpoints_kelvins[numpy.invert(numpy.isfinite(dewpoints_kelvins))] = 0.
    dewpoints_kelvins[dewpoints_deg_c + numerator_coeffs < 0] = 0.

    return dewpoints_kelvins
def specific_humidity_to_mixing_ratio(specific_humidities_kg_kg01):
    """Converts each specific humidity to mixing ratio.

    :param specific_humidities_kg_kg01: numpy array (any shape) of specific
        humidities (kg per kg).
    :return: mixing_ratios_kg_kg01: numpy array (same shape) of mixing ratios
        (kg per kg).
    """

    error_checking.assert_is_geq_numpy_array(
        specific_humidities_kg_kg01, 0., allow_nan=True
    )

    return specific_humidities_kg_kg01 / (1 - specific_humidities_kg_kg01)
def rowcol_to_latlng(
        grid_rows, grid_columns, nw_grid_point_lat_deg, nw_grid_point_lng_deg,
        lat_spacing_deg, lng_spacing_deg):
    """Converts radar coordinates from row-column to lat-long.

    P = number of input grid points

    :param grid_rows: length-P numpy array with row indices of grid points
        (increasing from north to south).
    :param grid_columns: length-P numpy array with column indices of grid points
        (increasing from west to east).
    :param nw_grid_point_lat_deg: Latitude (deg N) of northwesternmost grid
        point.
    :param nw_grid_point_lng_deg: Longitude (deg E) of northwesternmost grid
        point.
    :param lat_spacing_deg: Spacing (deg N) between meridionally adjacent grid
        points.
    :param lng_spacing_deg: Spacing (deg E) between zonally adjacent grid
        points.
    :return: latitudes_deg: length-P numpy array with latitudes (deg N) of grid
        points.
    :return: longitudes_deg: length-P numpy array with longitudes (deg E) of
        grid points.
    """

    error_checking.assert_is_real_numpy_array(grid_rows)
    error_checking.assert_is_geq_numpy_array(grid_rows, -0.5, allow_nan=True)
    error_checking.assert_is_numpy_array(grid_rows, num_dimensions=1)
    num_points = len(grid_rows)

    error_checking.assert_is_real_numpy_array(grid_columns)
    error_checking.assert_is_geq_numpy_array(grid_columns, -0.5, allow_nan=True)
    error_checking.assert_is_numpy_array(
        grid_columns, exact_dimensions=numpy.array([num_points]))

    error_checking.assert_is_valid_latitude(nw_grid_point_lat_deg)
    nw_grid_point_lng_deg = lng_conversion.convert_lng_positive_in_west(
        nw_grid_point_lng_deg, allow_nan=False)

    error_checking.assert_is_greater(lat_spacing_deg, 0.)
    error_checking.assert_is_greater(lng_spacing_deg, 0.)

    latitudes_deg = rounder.round_to_nearest(
        nw_grid_point_lat_deg - lat_spacing_deg * grid_rows,
        lat_spacing_deg / 2)
    longitudes_deg = rounder.round_to_nearest(
        nw_grid_point_lng_deg + lng_spacing_deg * grid_columns,
        lng_spacing_deg / 2)
    return latitudes_deg, lng_conversion.convert_lng_positive_in_west(
        longitudes_deg, allow_nan=True)
def mixing_ratio_to_specific_humidity(mixing_ratios_kg_kg01):
    """Converts each mixing ratio to specific humidity.

    :param mixing_ratios_kg_kg01: numpy array (any shape) of mixing ratios
        (kg per kg).
    :return: specific_humidities_kg_kg01: numpy array (same shape) of specific
        humidities (kg per kg).
    """

    error_checking.assert_is_geq_numpy_array(
        mixing_ratios_kg_kg01, 0., allow_nan=True
    )

    return mixing_ratios_kg_kg01 / (1 + mixing_ratios_kg_kg01)
Esempio n. 9
0
def _plot_inset_histogram_for_attributes_diagram(figure_object,
                                                 num_examples_by_bin):
    """Plots forecast histogram inset in attributes diagram.

    For more on the attributes diagram, see Hsu and Murphy (1986).

    B = number of forecast bins

    :param figure_object: Instance of `matplotlib.figure.Figure`.
    :param num_examples_by_bin: length-B numpy array with number of examples in
        each forecast bin.
    """

    error_checking.assert_is_integer_numpy_array(num_examples_by_bin)
    error_checking.assert_is_numpy_array(num_examples_by_bin, num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(num_examples_by_bin, 0)
    num_forecast_bins = len(num_examples_by_bin)
    error_checking.assert_is_geq(num_forecast_bins, 2)

    example_frequency_by_bin = (num_examples_by_bin.astype(float) /
                                numpy.sum(num_examples_by_bin))

    forecast_bin_edges = numpy.linspace(0., 1., num=num_forecast_bins + 1)
    forecast_bin_width = forecast_bin_edges[1] - forecast_bin_edges[0]
    forecast_bin_centers = forecast_bin_edges[:-1] + forecast_bin_width / 2

    inset_axes_object = figure_object.add_axes([
        HISTOGRAM_LEFT_EDGE, HISTOGRAM_BOTTOM_EDGE, HISTOGRAM_AXES_WIDTH,
        HISTOGRAM_AXES_HEIGHT
    ])

    inset_axes_object.bar(
        forecast_bin_centers,
        example_frequency_by_bin,
        forecast_bin_width,
        color=plotting_utils.colour_from_numpy_to_tuple(BAR_FACE_COLOUR),
        edgecolor=plotting_utils.colour_from_numpy_to_tuple(BAR_EDGE_COLOUR),
        linewidth=BAR_EDGE_WIDTH)

    max_y_tick_value = rounder.floor_to_nearest(
        1.05 * numpy.max(example_frequency_by_bin), HISTOGRAM_Y_SPACING)
    num_y_ticks = 1 + int(numpy.round(max_y_tick_value / HISTOGRAM_Y_SPACING))
    y_tick_values = numpy.linspace(0., max_y_tick_value, num=num_y_ticks)

    pyplot.xticks(HISTOGRAM_X_VALUES, axes=inset_axes_object)
    pyplot.yticks(y_tick_values, axes=inset_axes_object)
    inset_axes_object.set_xlim(0., 1.)
    inset_axes_object.set_ylim(0., 1.05 * numpy.max(example_frequency_by_bin))

    inset_axes_object.set_title('Forecast histogram', fontsize=20)
    def test_get_noisings(self):
        """Ensures correct output get_noisings."""

        these_standard_deviations = data_augmentation.get_noisings(
            num_noisings=NUM_NOISINGS,
            max_standard_deviation=MAX_NOISE_STANDARD_DEVIATION)

        self.assertTrue(len(these_standard_deviations) == NUM_NOISINGS)
        error_checking.assert_is_geq_numpy_array(
            these_standard_deviations,
            data_augmentation.MIN_NOISE_STANDARD_DEVIATION)
        error_checking.assert_is_leq_numpy_array(
            these_standard_deviations,
            data_augmentation.MAX_NOISE_STANDARD_DEVIATION)
Esempio n. 11
0
def rotate_winds(u_winds_grid_relative_m_s01=None,
                 v_winds_grid_relative_m_s01=None,
                 rotation_angle_cosines=None,
                 rotation_angle_sines=None):
    """Rotates wind vectors from grid-relative to Earth-relative.

    The equation is as follows, where alpha is the rotation angle.

    u_Earth = u_grid * cos(alpha) + v_grid * sin(alpha)
    v_Earth = v_grid * cos(alpha) - u_grid * sin(alpha)

    :param u_winds_grid_relative_m_s01: numpy array of grid-relative u-winds
        (towards positive x-direction).
    :param v_winds_grid_relative_m_s01: equivalent-shape numpy array of grid-
        relative v-winds (towards positive y-direction).
    :param rotation_angle_cosines: equivalent-shape numpy array with cosines of
        rotation angles.
    :param rotation_angle_sines: equivalent-shape numpy array with sines of
        rotation angles.
    :return: u_winds_earth_relative_m_s01: equivalent-shape numpy array of
        Earth-relative (northward) u-winds.
    :return: v_winds_earth_relative_m_s01: equivalent-shape numpy array of
        Earth-relative (eastward) v-winds.
    """

    error_checking.assert_is_real_numpy_array(u_winds_grid_relative_m_s01)
    array_dimensions = numpy.asarray(u_winds_grid_relative_m_s01.shape)

    error_checking.assert_is_real_numpy_array(v_winds_grid_relative_m_s01)
    error_checking.assert_is_numpy_array(v_winds_grid_relative_m_s01,
                                         exact_dimensions=array_dimensions)

    error_checking.assert_is_geq_numpy_array(rotation_angle_cosines, -1)
    error_checking.assert_is_leq_numpy_array(rotation_angle_cosines, 1)
    error_checking.assert_is_numpy_array(rotation_angle_cosines,
                                         exact_dimensions=array_dimensions)

    error_checking.assert_is_geq_numpy_array(rotation_angle_sines, -1)
    error_checking.assert_is_leq_numpy_array(rotation_angle_sines, 1)
    error_checking.assert_is_numpy_array(rotation_angle_sines,
                                         exact_dimensions=array_dimensions)

    u_winds_earth_relative_m_s01 = (
        rotation_angle_cosines * u_winds_grid_relative_m_s01 +
        rotation_angle_sines * v_winds_grid_relative_m_s01)
    v_winds_earth_relative_m_s01 = (
        rotation_angle_cosines * v_winds_grid_relative_m_s01 -
        rotation_angle_sines * u_winds_grid_relative_m_s01)
    return u_winds_earth_relative_m_s01, v_winds_earth_relative_m_s01
Esempio n. 12
0
def plot_narr_grid(frontal_grid_matrix,
                   axes_object,
                   basemap_object,
                   first_row_in_narr_grid=0,
                   first_column_in_narr_grid=0,
                   opacity=DEFAULT_GRID_OPACITY):
    """Plots NARR grid points intersected by a warm front or cold front.

    This method plots data over a contiguous subset of the NARR grid, which need
    not be *strictly* a subset.  In other words, the "subset" could be the full
    NARR grid.

    :param frontal_grid_matrix: See documentation for
        `front_utils.frontal_grid_to_points`.
    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param basemap_object: Instance of `mpl_toolkits.basemap.Basemap`.
    :param first_row_in_narr_grid: Row 0 in the subgrid is row
        `first_row_in_narr_grid` in the full NARR grid.
    :param first_column_in_narr_grid: Column 0 in the subgrid is row
        `first_column_in_narr_grid` in the full NARR grid.
    :param opacity: Opacity for colour map (in range 0...1).
    """

    error_checking.assert_is_integer_numpy_array(frontal_grid_matrix)
    error_checking.assert_is_numpy_array(frontal_grid_matrix, num_dimensions=2)

    error_checking.assert_is_geq_numpy_array(
        frontal_grid_matrix, numpy.min(front_utils.VALID_INTEGER_IDS))
    error_checking.assert_is_leq_numpy_array(
        frontal_grid_matrix, numpy.max(front_utils.VALID_INTEGER_IDS))

    colour_map_object, _, colour_bounds = get_colour_map_for_grid()

    frontal_grid_matrix = numpy.ma.masked_where(
        frontal_grid_matrix == front_utils.NO_FRONT_INTEGER_ID,
        frontal_grid_matrix)

    narr_plotting.plot_xy_grid(
        data_matrix=frontal_grid_matrix,
        axes_object=axes_object,
        basemap_object=basemap_object,
        colour_map=colour_map_object,
        colour_minimum=colour_bounds[1],
        colour_maximum=colour_bounds[-2],
        first_row_in_narr_grid=first_row_in_narr_grid,
        first_column_in_narr_grid=first_column_in_narr_grid,
        opacity=opacity)
Esempio n. 13
0
def plot_attributes_diagram(figure_object, axes_object, mean_forecast_by_bin,
                            event_frequency_by_bin, num_examples_by_bin):
    """Plots attributes diagram (Hsu and Murphy 1986).

    :param figure_object: Instance of `matplotlib.figure.Figure`.
    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param mean_forecast_by_bin: See doc for `plot_reliability_curve`.
    :param event_frequency_by_bin: Same.
    :param num_examples_by_bin: See doc for
        `_plot_inset_histogram_for_attributes_diagram`.
    """

    error_checking.assert_is_numpy_array(event_frequency_by_bin,
                                         num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(event_frequency_by_bin,
                                             0.,
                                             allow_nan=True)
    error_checking.assert_is_leq_numpy_array(event_frequency_by_bin,
                                             1.,
                                             allow_nan=True)

    num_bins = len(mean_forecast_by_bin)
    expected_dim = numpy.array([num_bins], dtype=int)

    error_checking.assert_is_integer_numpy_array(num_examples_by_bin)
    error_checking.assert_is_numpy_array(num_examples_by_bin,
                                         exact_dimensions=expected_dim)
    error_checking.assert_is_geq_numpy_array(num_examples_by_bin, 0)

    non_empty_bin_indices = numpy.where(num_examples_by_bin > 0)[0]
    error_checking.assert_is_numpy_array_without_nan(
        event_frequency_by_bin[non_empty_bin_indices])

    climatology = numpy.average(
        event_frequency_by_bin[non_empty_bin_indices],
        weights=num_examples_by_bin[non_empty_bin_indices])

    _plot_background_of_attributes_diagram(axes_object=axes_object,
                                           climatology=climatology)

    _plot_inset_histogram_for_attributes_diagram(
        figure_object=figure_object, num_examples_by_bin=num_examples_by_bin)

    plot_reliability_curve(axes_object=axes_object,
                           mean_forecast_by_bin=mean_forecast_by_bin,
                           event_frequency_by_bin=event_frequency_by_bin)
Esempio n. 14
0
def event_probs_to_multiclass(event_probabilities):
    """Converts 1-D array of event probabilities to 2-D array.

    E = number of examples

    :param event_probabilities: length-E numpy array of event probabilities.
    :return: class_probability_matrix: E-by-2 numpy array, where second column
        contains probabilities of event and first column contains probabilities
        of non-event.
    """

    error_checking.assert_is_numpy_array(event_probabilities, num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(event_probabilities, 0.)
    error_checking.assert_is_leq_numpy_array(event_probabilities, 1.)

    these_probs = numpy.reshape(event_probabilities,
                                (len(event_probabilities), 1))
    return numpy.hstack((1. - these_probs, these_probs))
Esempio n. 15
0
def _check_contingency_table(contingency_table_as_matrix):
    """Checks contingency table for errors.

    :param contingency_table_as_matrix: K-by-K numpy array.
        contingency_table_as_matrix[i, j] is the number of examples for which
        the predicted label is i and the true label is j.
    """

    error_checking.assert_is_numpy_array(
        contingency_table_as_matrix, num_dimensions=2)

    num_classes = contingency_table_as_matrix.shape[0]
    error_checking.assert_is_numpy_array(
        contingency_table_as_matrix,
        exact_dimensions=numpy.array([num_classes, num_classes]))

    error_checking.assert_is_integer_numpy_array(contingency_table_as_matrix)
    error_checking.assert_is_geq_numpy_array(contingency_table_as_matrix, 0)
Esempio n. 16
0
def radar_fields_and_heights_to_panel_names(field_names,
                                            heights_m_agl,
                                            include_units=True):
    """Converts list of radar field/height pairs to panel names.

    P = number of panels

    :param field_names: length-P list with names of radar fields.  Each must be
        accepted by `radar_utils.check_field_name`.
    :param heights_m_agl: length-P numpy array of heights (metres above ground
        level).
    :param include_units: Boolean flag.  If True, panel names will include
        units.
    :return: panel_names: length-P list of panel names (to be printed at bottoms
        of panels).
    """

    error_checking.assert_is_boolean(include_units)

    error_checking.assert_is_string_list(field_names)
    error_checking.assert_is_numpy_array(numpy.array(field_names),
                                         num_dimensions=1)
    num_panels = len(field_names)

    error_checking.assert_is_numpy_array(heights_m_agl,
                                         exact_dimensions=numpy.array(
                                             [num_panels]))
    error_checking.assert_is_geq_numpy_array(heights_m_agl, 0.)
    heights_m_agl = numpy.round(heights_m_agl).astype(int)

    panel_names = [''] * num_panels

    for i in range(num_panels):
        this_field_name_verbose = FIELD_NAME_TO_VERBOSE_DICT[field_names[i]]

        if not include_units:
            this_field_name_verbose = this_field_name_verbose[:
                                                              this_field_name_verbose
                                                              .find(' (')]

        panel_names[i] = '{0:s}\nat {1:.2f} km AGL'.format(
            this_field_name_verbose, heights_m_agl[i] * METRES_TO_KM)

    return panel_names
Esempio n. 17
0
    def test_get_random_sample_points_full_size(self):
        """Ensures correct output from _get_random_sample_points.

        In this case, for_downsized_examples = False.
        """

        (these_row_indices,
         these_column_indices) = evaluation_utils._get_random_sample_points(
             num_points=NUM_POINTS_TO_SAMPLE, for_downsized_examples=False)

        error_checking.assert_is_integer_numpy_array(these_row_indices)
        error_checking.assert_is_geq_numpy_array(these_row_indices, 0)
        error_checking.assert_is_less_than_numpy_array(these_row_indices,
                                                       NUM_ROWS_FOR_FCN_INPUT)

        error_checking.assert_is_integer_numpy_array(these_column_indices)
        error_checking.assert_is_geq_numpy_array(these_column_indices, 0)
        error_checking.assert_is_less_than_numpy_array(
            these_column_indices, NUM_COLUMNS_FOR_FCN_INPUT)
Esempio n. 18
0
def create_height_labels(tick_values_km_agl, use_log_scale):
    """Creates labels for height axis.

    H = number of tick values

    :param tick_values_km_agl: length-H numpy array of tick values (km above
        ground level).
    :param use_log_scale: Boolean flag.  If True, will assume that height axis
        is logarithmic.
    :return: tick_strings: length-H list of text labels.
    """

    error_checking.assert_is_geq_numpy_array(tick_values_km_agl, 0.)
    error_checking.assert_is_numpy_array(tick_values_km_agl, num_dimensions=1)
    error_checking.assert_is_boolean(use_log_scale)

    num_ticks = len(tick_values_km_agl)
    tick_strings = ['foo'] * num_ticks

    for i in range(num_ticks):
        try:
            this_order_of_magnitude = int(numpy.floor(
                numpy.log10(tick_values_km_agl[i])
            ))
        except OverflowError:
            this_order_of_magnitude = -1

        if this_order_of_magnitude >= 0:
            this_num_decimal_places = 1
        else:
            this_num_decimal_places = numpy.absolute(this_order_of_magnitude)

            if not use_log_scale:
                this_num_decimal_places += 1

        this_num_decimal_places = numpy.minimum(this_num_decimal_places, 2)

        this_format_string = (
            '{0:.' + '{0:d}'.format(this_num_decimal_places) + 'f}'
        )
        tick_strings[i] = this_format_string.format(tick_values_km_agl[i])

    return tick_strings
def _check_architecture_args(option_dict):
    """Error-checks input arguments for architecture.

    :param option_dict: See doc for `create_model`.
    :return: option_dict: Same as input, except defaults may have been added.
    """

    orig_option_dict = option_dict.copy()
    option_dict = DEFAULT_ARCHITECTURE_OPTION_DICT.copy()
    option_dict.update(orig_option_dict)

    error_checking.assert_is_integer(option_dict[NUM_INPUTS_KEY])
    error_checking.assert_is_geq(option_dict[NUM_INPUTS_KEY], 10)

    dense_layer_neuron_nums = option_dict[DENSE_LAYER_NEURON_NUMS_KEY]
    error_checking.assert_is_integer_numpy_array(dense_layer_neuron_nums)
    error_checking.assert_is_numpy_array(dense_layer_neuron_nums,
                                         num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(dense_layer_neuron_nums, 1)

    num_layers = len(dense_layer_neuron_nums)
    these_dimensions = numpy.array([num_layers], dtype=int)

    dense_layer_dropout_rates = option_dict[DENSE_LAYER_DROPOUT_RATES_KEY]
    error_checking.assert_is_numpy_array(dense_layer_dropout_rates,
                                         exact_dimensions=these_dimensions)
    error_checking.assert_is_leq_numpy_array(dense_layer_dropout_rates,
                                             1.,
                                             allow_nan=True)

    error_checking.assert_is_geq(option_dict[L1_WEIGHT_KEY], 0.)
    error_checking.assert_is_geq(option_dict[L2_WEIGHT_KEY], 0.)
    error_checking.assert_is_boolean(option_dict[USE_BATCH_NORM_KEY])
    error_checking.assert_is_boolean(option_dict[ZERO_OUT_TOP_HR_KEY])

    if option_dict[ZERO_OUT_TOP_HR_KEY]:
        error_checking.assert_is_integer(
            option_dict[TOP_HEATING_RATE_INDEX_KEY])
        error_checking.assert_is_geq(option_dict[TOP_HEATING_RATE_INDEX_KEY],
                                     0)

    return option_dict
def check_target_array(target_array, num_dimensions, num_classes):
    """Error-checks target values.

    :param target_array: numpy array in one of two formats.
    [1] length-E integer numpy array of target values.  All values are -2
        ("dead storm") or 0...[K - 1], where K = number of classes.
    [2] E-by-K numpy array, where each value is 0 or 1.  If target_array[i, k] =
        1, the [i]th storm object belongs to the [k]th class.  Classes are
        mutually exclusive and collectively exhaustive, so the sum across each
        row of the matrix is 1.

    :param num_dimensions: Number of dimensions expected in `target_array`.
    :param num_classes: Number of classes that should be represented in
        `target_array`.
    """

    error_checking.assert_is_integer(num_dimensions)
    error_checking.assert_is_geq(num_dimensions, 1)
    error_checking.assert_is_leq(num_dimensions, 2)
    error_checking.assert_is_integer(num_classes)
    error_checking.assert_is_geq(num_classes, 2)

    num_examples = target_array.shape[0]

    if num_dimensions == 1:
        error_checking.assert_is_numpy_array(target_array,
                                             exact_dimensions=numpy.array(
                                                 [num_examples]))
        error_checking.assert_is_integer_numpy_array(target_array)

        live_storm_object_indices = numpy.where(
            target_array != target_val_utils.DEAD_STORM_INTEGER)[0]
        error_checking.assert_is_geq_numpy_array(
            target_array[live_storm_object_indices], 0)
        error_checking.assert_is_less_than_numpy_array(target_array,
                                                       num_classes)
    else:
        error_checking.assert_is_numpy_array(target_array,
                                             exact_dimensions=numpy.array(
                                                 [num_examples, num_classes]))
        error_checking.assert_is_geq_numpy_array(target_array, 0)
        error_checking.assert_is_leq_numpy_array(target_array, 1)
Esempio n. 21
0
def _check_polygons(polygon_objects_grid_coords, num_panel_rows,
                    num_panel_columns, panel_row_by_polygon,
                    panel_column_by_polygon):
    """Error-checks list of polygons.

    :param polygon_objects_grid_coords: See doc for
        `polygons_from_pixel_to_grid_coords`.
    :param num_panel_rows: Same.
    :param num_panel_columns: Same.
    :param panel_row_by_polygon: Same.
    :param panel_column_by_polygon: Same.
    """

    error_checking.assert_is_integer(num_panel_rows)
    error_checking.assert_is_greater(num_panel_rows, 0)
    error_checking.assert_is_integer(num_panel_columns)
    error_checking.assert_is_greater(num_panel_columns, 0)

    num_polygons = len(polygon_objects_grid_coords)
    if num_polygons == 0:
        return

    error_checking.assert_is_numpy_array(numpy.array(
        polygon_objects_grid_coords, dtype=object),
                                         num_dimensions=1)

    these_expected_dim = numpy.array([num_polygons], dtype=int)

    error_checking.assert_is_integer_numpy_array(panel_row_by_polygon)
    error_checking.assert_is_numpy_array(panel_row_by_polygon,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_geq_numpy_array(panel_row_by_polygon, 0)
    error_checking.assert_is_less_than_numpy_array(panel_row_by_polygon,
                                                   num_panel_rows)

    error_checking.assert_is_integer_numpy_array(panel_column_by_polygon)
    error_checking.assert_is_numpy_array(panel_column_by_polygon,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_geq_numpy_array(panel_column_by_polygon, 0)
    error_checking.assert_is_less_than_numpy_array(panel_column_by_polygon,
                                                   num_panel_columns)
Esempio n. 22
0
def get_contingency_table(predicted_labels, observed_labels, num_classes):
    """Creates either binary or multi-class contingency table.

    P = number of evaluation pairs
    K = number of classes

    :param predicted_labels: length-P numpy array of predicted class labels
        (integers).
    :param observed_labels: length-P numpy array of true class labels
        (integers).
    :param num_classes: Number of classes.
    :return: contingency_table_as_matrix: K-by-K numpy array.
        contingency_table_as_matrix[i, j] is the number of examples for which
        the predicted label is i and the true label is j.
    """

    error_checking.assert_is_integer(num_classes)
    error_checking.assert_is_greater(num_classes, 2)

    error_checking.assert_is_numpy_array(predicted_labels, num_dimensions=1)
    error_checking.assert_is_integer_numpy_array(predicted_labels)
    error_checking.assert_is_geq_numpy_array(predicted_labels, 0)
    error_checking.assert_is_less_than_numpy_array(
        predicted_labels, num_classes)

    num_evaluation_pairs = len(predicted_labels)
    error_checking.assert_is_numpy_array(
        observed_labels, exact_dimensions=numpy.array([num_evaluation_pairs]))
    error_checking.assert_is_integer_numpy_array(observed_labels)
    error_checking.assert_is_geq_numpy_array(observed_labels, 0)
    error_checking.assert_is_less_than_numpy_array(observed_labels, num_classes)

    contingency_table_as_matrix = numpy.full(
        (num_classes, num_classes), -1, dtype=int)

    for i in range(num_classes):
        for j in range(num_classes):
            contingency_table_as_matrix[i, j] = numpy.sum(
                numpy.logical_and(predicted_labels == i, observed_labels == j))

    return contingency_table_as_matrix
Esempio n. 23
0
    def __init__(self, binary_region_matrix):
        """Creates new instance.

        :param binary_region_matrix: M-by-N numpy array of integers in 0...1.
            If binary_region_matrix[i, j] = 1, grid cell [i, j] is part of the
            connected region.
        """

        error_checking.assert_is_numpy_array(binary_region_matrix,
                                             num_dimensions=2)
        error_checking.assert_is_integer_numpy_array(binary_region_matrix)
        error_checking.assert_is_geq_numpy_array(binary_region_matrix, 0)
        error_checking.assert_is_leq_numpy_array(binary_region_matrix, 1)

        setattr(self, NUM_GRID_ROWS_KEY, binary_region_matrix.shape[0])
        setattr(self, NUM_GRID_COLUMNS_KEY, binary_region_matrix.shape[1])

        # self.num_grid_rows = binary_region_matrix.shape[0]
        # self.num_grid_columns = binary_region_matrix.shape[1]
        self.row_indices_in_region, self.column_indices_in_region = numpy.where(
            binary_region_matrix == 1)
def vapour_pressure_to_mixing_ratio(
        vapour_pressures_pascals, total_pressures_pascals):
    """Converts each vapour pressure to mixing ratio.

    :param vapour_pressures_pascals: numpy array (any shape) of vapour
        pressures.
    :param total_pressures_pascals: numpy array (same shape) of total air
        pressures.
    :return: mixing_ratios_kg_kg01: numpy array (same shape) of mixing ratios
        (kg per kg).
    """

    error_checking.assert_is_geq_numpy_array(
        vapour_pressures_pascals, 0., allow_nan=True
    )
    error_checking.assert_is_geq_numpy_array(
        total_pressures_pascals, 0., allow_nan=True
    )
    error_checking.assert_is_numpy_array(
        total_pressures_pascals,
        exact_dimensions=numpy.array(vapour_pressures_pascals.shape, dtype=int)
    )
    error_checking.assert_is_geq_numpy_array(
        total_pressures_pascals - vapour_pressures_pascals, 0., allow_nan=True
    )

    denominators = total_pressures_pascals - vapour_pressures_pascals
    mixing_ratios_kg_kg01 = EPSILON * vapour_pressures_pascals / denominators
    mixing_ratios_kg_kg01[denominators <= 0] = 0

    return mixing_ratios_kg_kg01
def dewpoint_to_vapour_pressure(dewpoints_kelvins, temperatures_kelvins,
                                total_pressures_pascals):
    """Converts each dewpoint to vapour pressure.

    Source:
    https://content.meteoblue.com/hu/specifications/weather-variables/humidity

    :param dewpoints_kelvins: numpy array (any shape) of dewpoints.
    :param temperatures_kelvins: numpy array (same shape) of temperatures.
    :param total_pressures_pascals: numpy array (same shape) of total air
        pressures.
    :return: vapour_pressures_pascals: numpy array (same shape) of vapour
        pressures.
    """

    error_checking.assert_is_geq_numpy_array(
        dewpoints_kelvins, 0., allow_nan=True
    )
    error_checking.assert_is_geq_numpy_array(
        temperatures_kelvins, 0., allow_nan=True
    )
    error_checking.assert_is_numpy_array(
        temperatures_kelvins,
        exact_dimensions=numpy.array(dewpoints_kelvins.shape, dtype=int)
    )
    error_checking.assert_is_geq_numpy_array(
        total_pressures_pascals, 0., allow_nan=True
    )
    error_checking.assert_is_numpy_array(
        total_pressures_pascals,
        exact_dimensions=numpy.array(dewpoints_kelvins.shape, dtype=int)
    )

    dewpoints_deg_c = temperature_conv.kelvins_to_celsius(dewpoints_kelvins)
    temperatures_deg_c = temperature_conv.kelvins_to_celsius(
        temperatures_kelvins
    )

    numerator_coeffs = numpy.full(
        temperatures_deg_c.shape, MAGNUS_NUMERATOR_COEFF_WATER
    )
    numerator_coeffs[temperatures_deg_c < 0] = MAGNUS_NUMERATOR_COEFF_ICE
    numerators = numerator_coeffs * dewpoints_deg_c

    denominator_coeffs = numpy.full(
        temperatures_deg_c.shape, MAGNUS_DENOMINATOR_COEFF_WATER
    )
    denominator_coeffs[temperatures_deg_c < 0] = MAGNUS_DENOMINATOR_COEFF_ICE
    denominators = denominator_coeffs + dewpoints_deg_c

    vapour_pressures_pascals = (
        BASE_VAPOUR_PRESSURE_PASCALS * numpy.exp(numerators / denominators)
    )

    vapour_pressures_pascals[
        numpy.invert(numpy.isfinite(vapour_pressures_pascals))
    ] = 0.

    vapour_pressures_pascals[denominators <= 0] = 0.
    return numpy.minimum(vapour_pressures_pascals, total_pressures_pascals)
Esempio n. 26
0
def get_grid_cell_edges(heights_m_agl):
    """Computes heights at edges (rather than centers) of grid cells.

    H = number of grid cells

    :param heights_m_agl: length-H numpy array of heights (metres above ground
        level) at centers of grid cells.
    :return: edge_heights_m_agl: length-(H + 1) numpy array of heights (metres
        above ground level) at edges of grid cells.
    """

    error_checking.assert_is_geq_numpy_array(heights_m_agl, 0.)
    error_checking.assert_is_numpy_array(heights_m_agl, num_dimensions=1)

    height_diffs_metres = numpy.diff(heights_m_agl)
    edge_heights_m_agl = heights_m_agl[:-1] + height_diffs_metres / 2
    bottom_edge_m_agl = heights_m_agl[0] - height_diffs_metres[0] / 2
    top_edge_m_agl = heights_m_agl[-1] + height_diffs_metres[-1] / 2

    return numpy.concatenate(
        (numpy.array([bottom_edge_m_agl]), edge_heights_m_agl,
         numpy.array([top_edge_m_agl])))
Esempio n. 27
0
    def test_get_random_sample_points_downsized_no_mask(self):
        """Ensures correct output from _get_random_sample_points.

        In this case,
        `for_downsized_examples = True and narr_mask_matrix is None`.
        """

        (these_row_indices,
         these_column_indices) = evaluation_utils._get_random_sample_points(
             num_points=NUM_POINTS_TO_SAMPLE,
             for_downsized_examples=True,
             narr_mask_matrix=None)

        error_checking.assert_is_integer_numpy_array(these_row_indices)
        error_checking.assert_is_geq_numpy_array(these_row_indices, 0)
        error_checking.assert_is_less_than_numpy_array(
            these_row_indices, NARR_MASK_MATRIX.shape[0])

        error_checking.assert_is_integer_numpy_array(these_column_indices)
        error_checking.assert_is_geq_numpy_array(these_column_indices, 0)
        error_checking.assert_is_less_than_numpy_array(
            these_column_indices, NARR_MASK_MATRIX.shape[1])
Esempio n. 28
0
def get_accuracy(contingency_table_as_matrix):
    """Computes accuracy (either binary or multi-class).

    :param contingency_table_as_matrix: See documentation for
        `_check_contingency_table`.
    :return: accuracy: Accuracy (float in range 0...1).
    """

    error_checking.assert_is_numpy_array(
        contingency_table_as_matrix, num_dimensions=2)

    num_classes = contingency_table_as_matrix.shape[0]
    error_checking.assert_is_numpy_array(
        contingency_table_as_matrix,
        exact_dimensions=numpy.array([num_classes, num_classes]))

    error_checking.assert_is_integer_numpy_array(contingency_table_as_matrix)
    error_checking.assert_is_geq_numpy_array(contingency_table_as_matrix, 0)

    num_evaluation_pairs = numpy.sum(contingency_table_as_matrix)
    num_correct_pairs = numpy.trace(contingency_table_as_matrix)
    return float(num_correct_pairs) / num_evaluation_pairs
Esempio n. 29
0
def check_component_metadata(component_type_string,
                             target_class=None,
                             layer_name=None,
                             neuron_indices=None,
                             channel_index=None):
    """Checks metadata for model component.

    :param component_type_string: Component type (must be accepted by
        `check_component_type`).
    :param target_class: [used only if component_type_string = "class"]
        Target class.  Integer from 0...(K - 1), where K = number of classes.
    :param layer_name:
        [used only if component_type_string = "neuron" or "channel"]
        Name of layer containing neuron or channel.
    :param neuron_indices: [used only if component_type_string = "neuron"]
        1-D numpy array with indices of neuron.
    :param channel_index: [used only if component_type_string = "channel"]
        Index of channel.
    """

    check_component_type(component_type_string)
    if component_type_string == CLASS_COMPONENT_TYPE_STRING:
        error_checking.assert_is_integer(target_class)
        error_checking.assert_is_geq(target_class, 0)

    if component_type_string in [
            NEURON_COMPONENT_TYPE_STRING, CHANNEL_COMPONENT_TYPE_STRING
    ]:
        error_checking.assert_is_string(layer_name)

    if component_type_string == NEURON_COMPONENT_TYPE_STRING:
        error_checking.assert_is_integer_numpy_array(neuron_indices)
        error_checking.assert_is_geq_numpy_array(neuron_indices, 0)
        error_checking.assert_is_numpy_array(neuron_indices, num_dimensions=1)

    if component_type_string == CHANNEL_COMPONENT_TYPE_STRING:
        error_checking.assert_is_integer(channel_index)
        error_checking.assert_is_geq(channel_index, 0)
Esempio n. 30
0
def check_metadata(layer_name, neuron_indices, ideal_activation):
    """Checks metadata for errors.

    The "relevant neuron" is that whose activation will be used in the numerator
    of the saliency equation.  In other words, if the relevant neuron is n,
    the saliency of each predictor x will be d(a_n) / dx, where a_n is the
    activation of n.

    :param layer_name: Name of layer with relevant neuron.
    :param neuron_indices: 1-D numpy array with indices of relevant neuron.
        Must have length D - 1, where D = number of dimensions in layer output.
        The first dimension is the batch dimension, which always has length
        `None` in Keras.
    :param ideal_activation: Ideal neuron activation, used to define loss
        function.  The loss function will be
        (neuron_activation - ideal_activation)**2.
    """

    error_checking.assert_is_string(layer_name)
    error_checking.assert_is_integer_numpy_array(neuron_indices)
    error_checking.assert_is_geq_numpy_array(neuron_indices, 0)
    error_checking.assert_is_numpy_array(neuron_indices, num_dimensions=1)
    error_checking.assert_is_not_nan(ideal_activation)