コード例 #1
0
    def test_get_pofd_pod_grid(self):
        """Ensures correct output from get_pofd_pod_grid."""

        this_pofd_matrix, this_pod_matrix = model_eval.get_pofd_pod_grid(
            pofd_spacing=NON_POD_GRID_SPACING, pod_spacing=POD_GRID_SPACING)

        self.assertTrue(
            numpy.allclose(this_pofd_matrix, POFD_MATRIX, atol=TOLERANCE))
        self.assertTrue(
            numpy.allclose(this_pod_matrix, POD_MATRIX, atol=TOLERANCE))
コード例 #2
0
def plot_roc_curve(axes_object,
                   pod_by_threshold,
                   pofd_by_threshold,
                   line_colour=DEFAULT_ROC_COLOUR,
                   line_width=DEFAULT_ROC_WIDTH,
                   random_line_colour=DEFAULT_RANDOM_ROC_COLOUR,
                   random_line_width=DEFAULT_RANDOM_ROC_WIDTH):
    """Plots ROC (receiver operating characteristic) curve.

    T = number of binarization thresholds

    For the definition of a "binarization threshold" and the role they play in
    ROC curves, see `model_evaluation.get_points_in_roc_curve`.

    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param pod_by_threshold: length-T numpy array of POD (probability of
        detection) values.
    :param pofd_by_threshold: length-T numpy array of POFD (probability of false
        detection) values.
    :param line_colour: Colour (in any format accepted by `matplotlib.colors`).
    :param line_width: Line width (real positive number).
    :param random_line_colour: Colour of reference line (ROC curve for a random
        predictor).
    :param random_line_width: Width of reference line.
    """

    error_checking.assert_is_numpy_array(pod_by_threshold, num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(pod_by_threshold,
                                             0.,
                                             allow_nan=True)
    error_checking.assert_is_leq_numpy_array(pod_by_threshold,
                                             1.,
                                             allow_nan=True)
    num_thresholds = len(pod_by_threshold)

    error_checking.assert_is_numpy_array(pofd_by_threshold,
                                         exact_dimensions=numpy.array(
                                             [num_thresholds]))
    error_checking.assert_is_geq_numpy_array(pofd_by_threshold,
                                             0.,
                                             allow_nan=True)
    error_checking.assert_is_leq_numpy_array(pofd_by_threshold,
                                             1.,
                                             allow_nan=True)

    pofd_matrix, pod_matrix = model_eval.get_pofd_pod_grid()
    peirce_score_matrix = pod_matrix - pofd_matrix

    this_colour_map_object, this_colour_norm_object = _get_peirce_colour_scheme(
    )

    pyplot.contourf(pofd_matrix,
                    pod_matrix,
                    peirce_score_matrix,
                    LEVELS_FOR_CSI_CONTOURS,
                    cmap=this_colour_map_object,
                    norm=this_colour_norm_object,
                    vmin=0.,
                    vmax=1.,
                    axes=axes_object)

    colour_bar_object = plotting_utils.plot_colour_bar(
        axes_object_or_matrix=axes_object,
        data_matrix=peirce_score_matrix,
        colour_map_object=this_colour_map_object,
        colour_norm_object=this_colour_norm_object,
        orientation_string='vertical',
        extend_min=False,
        extend_max=False)

    colour_bar_object.set_label('Peirce score (POD minus POFD)')

    random_x_coords, random_y_coords = model_eval.get_random_roc_curve()
    axes_object.plot(
        random_x_coords,
        random_y_coords,
        color=plotting_utils.colour_from_numpy_to_tuple(random_line_colour),
        linestyle='dashed',
        linewidth=random_line_width)

    nan_flags = numpy.logical_or(numpy.isnan(pofd_by_threshold),
                                 numpy.isnan(pod_by_threshold))

    if not numpy.all(nan_flags):
        real_indices = numpy.where(numpy.invert(nan_flags))[0]

        axes_object.plot(
            pofd_by_threshold[real_indices],
            pod_by_threshold[real_indices],
            color=plotting_utils.colour_from_numpy_to_tuple(line_colour),
            linestyle='solid',
            linewidth=line_width)

    axes_object.set_xlabel('POFD (probability of false detection)')
    axes_object.set_ylabel('POD (probability of detection)')
    axes_object.set_xlim(0., 1.)
    axes_object.set_ylim(0., 1.)
コード例 #3
0
def plot_roc_curve(axes_object,
                   pod_by_threshold,
                   pofd_by_threshold,
                   line_colour=ROC_CURVE_COLOUR,
                   plot_background=True):
    """Plots ROC (receiver operating characteristic) curve.

    T = number of binarization thresholds

    For the definition of a "binarization threshold" and the role they play in
    ROC curves, see `model_evaluation.get_points_in_roc_curve`.

    :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`.
    :param pod_by_threshold: length-T numpy array of POD (probability of
        detection) values.
    :param pofd_by_threshold: length-T numpy array of POFD (probability of false
        detection) values.
    :param line_colour: Line colour.
    :param plot_background: Boolean flag.  If True, will plot background
        (reference line and Peirce-score contours).
    :return: line_handle: Line handle for ROC curve.
    """

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

    num_thresholds = len(pod_by_threshold)
    expected_dim = numpy.array([num_thresholds], dtype=int)

    error_checking.assert_is_numpy_array(pofd_by_threshold,
                                         exact_dimensions=expected_dim)
    error_checking.assert_is_geq_numpy_array(pofd_by_threshold,
                                             0.,
                                             allow_nan=True)
    error_checking.assert_is_leq_numpy_array(pofd_by_threshold,
                                             1.,
                                             allow_nan=True)

    error_checking.assert_is_boolean(plot_background)

    if plot_background:
        pofd_matrix, pod_matrix = model_eval.get_pofd_pod_grid()
        peirce_score_matrix = pod_matrix - pofd_matrix

        this_colour_map_object, this_colour_norm_object = (
            _get_peirce_colour_scheme())

        pyplot.contourf(pofd_matrix,
                        pod_matrix,
                        peirce_score_matrix,
                        CSI_LEVELS,
                        cmap=this_colour_map_object,
                        norm=this_colour_norm_object,
                        vmin=0.,
                        vmax=1.,
                        axes=axes_object)

        colour_bar_object = plotting_utils.plot_colour_bar(
            axes_object_or_matrix=axes_object,
            data_matrix=peirce_score_matrix,
            colour_map_object=this_colour_map_object,
            colour_norm_object=this_colour_norm_object,
            orientation_string='vertical',
            extend_min=False,
            extend_max=False,
            fraction_of_axis_length=0.8)

        colour_bar_object.set_label('Peirce score (POD minus POFD)')

        random_x_coords, random_y_coords = model_eval.get_random_roc_curve()
        axes_object.plot(
            random_x_coords,
            random_y_coords,
            color=plotting_utils.colour_from_numpy_to_tuple(RANDOM_ROC_COLOUR),
            linestyle='dashed',
            linewidth=RANDOM_ROC_WIDTH)

    nan_flags = numpy.logical_or(numpy.isnan(pofd_by_threshold),
                                 numpy.isnan(pod_by_threshold))

    if numpy.all(nan_flags):
        line_handle = None
    else:
        real_indices = numpy.where(numpy.invert(nan_flags))[0]

        line_handle = axes_object.plot(
            pofd_by_threshold[real_indices],
            pod_by_threshold[real_indices],
            color=plotting_utils.colour_from_numpy_to_tuple(line_colour),
            linestyle='solid',
            linewidth=ROC_CURVE_WIDTH)[0]

    axes_object.set_xlabel('POFD (probability of false detection)')
    axes_object.set_ylabel('POD (probability of detection)')
    axes_object.set_xlim(0., 1.)
    axes_object.set_ylim(0., 1.)

    return line_handle