def test_get_sr_pod_grid(self): """Ensures correct output from get_sr_pod_grid.""" this_success_ratio_matrix, this_pod_matrix = model_eval.get_sr_pod_grid( success_ratio_spacing=NON_POD_GRID_SPACING, pod_spacing=POD_GRID_SPACING) self.assertTrue( numpy.allclose(this_success_ratio_matrix, SUCCESS_RATIO_MATRIX, atol=TOLERANCE)) self.assertTrue( numpy.allclose(this_pod_matrix, POD_MATRIX, atol=TOLERANCE))
def plot_performance_diagram(axes_object, pod_by_threshold, success_ratio_by_threshold, line_colour=DEFAULT_PERFORMANCE_COLOUR, line_width=DEFAULT_PERFORMANCE_WIDTH, bias_line_colour=DEFAULT_FREQ_BIAS_COLOUR, bias_line_width=DEFAULT_FREQ_BIAS_WIDTH): """Plots performance diagram. T = number of binarization thresholds For the definition of a "binarization threshold" and the role they play in performance diagrams, see `model_evaluation.get_points_in_performance_diagram`. :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`. :param pod_by_threshold: length-T numpy array of POD (probability of detection) values. :param success_ratio_by_threshold: length-T numpy array of success ratios. :param line_colour: Colour (in any format accepted by `matplotlib.colors`). :param line_width: Line width (real positive number). :param bias_line_colour: Colour of contour lines for frequency bias. :param bias_line_width: Width of contour lines for frequency bias. """ 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(success_ratio_by_threshold, exact_dimensions=numpy.array( [num_thresholds])) error_checking.assert_is_geq_numpy_array(success_ratio_by_threshold, 0., allow_nan=True) error_checking.assert_is_leq_numpy_array(success_ratio_by_threshold, 1., allow_nan=True) success_ratio_matrix, pod_matrix = model_eval.get_sr_pod_grid() csi_matrix = model_eval.csi_from_sr_and_pod(success_ratio_matrix, pod_matrix) frequency_bias_matrix = model_eval.frequency_bias_from_sr_and_pod( success_ratio_matrix, pod_matrix) this_colour_map_object, this_colour_norm_object = _get_csi_colour_scheme() pyplot.contourf(success_ratio_matrix, pod_matrix, csi_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=csi_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('CSI (critical success index)') bias_colour_tuple = plotting_utils.colour_from_numpy_to_tuple( bias_line_colour) bias_colours_2d_tuple = () for _ in range(len(LEVELS_FOR_FREQ_BIAS_CONTOURS)): bias_colours_2d_tuple += (bias_colour_tuple, ) bias_contour_object = pyplot.contour(success_ratio_matrix, pod_matrix, frequency_bias_matrix, LEVELS_FOR_FREQ_BIAS_CONTOURS, colors=bias_colours_2d_tuple, linewidths=bias_line_width, linestyles='dashed', axes=axes_object) pyplot.clabel(bias_contour_object, inline=True, inline_spacing=PIXEL_PADDING_FOR_FREQ_BIAS_LABELS, fmt=STRING_FORMAT_FOR_FREQ_BIAS_LABELS, fontsize=FONT_SIZE) nan_flags = numpy.logical_or(numpy.isnan(success_ratio_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( success_ratio_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('Success ratio (1 - FAR)') axes_object.set_ylabel('POD (probability of detection)') axes_object.set_xlim(0., 1.) axes_object.set_ylim(0., 1.)
def plot_performance_diagram(axes_object, pod_by_threshold, success_ratio_by_threshold, line_colour=PERF_DIAGRAM_COLOUR, plot_background=True): """Plots performance diagram. T = number of binarization thresholds For the definition of a "binarization threshold" and the role they play in performance diagrams, see `model_evaluation.get_points_in_performance_diagram`. :param axes_object: Instance of `matplotlib.axes._subplots.AxesSubplot`. :param pod_by_threshold: length-T numpy array of POD (probability of detection) values. :param success_ratio_by_threshold: length-T numpy array of success ratios. :param line_colour: Line colour. :param plot_background: Boolean flag. If True, will plot background (frequency-bias and CSI 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(success_ratio_by_threshold, exact_dimensions=expected_dim) error_checking.assert_is_geq_numpy_array(success_ratio_by_threshold, 0., allow_nan=True) error_checking.assert_is_leq_numpy_array(success_ratio_by_threshold, 1., allow_nan=True) error_checking.assert_is_boolean(plot_background) if plot_background: success_ratio_matrix, pod_matrix = model_eval.get_sr_pod_grid() csi_matrix = model_eval.csi_from_sr_and_pod( success_ratio_array=success_ratio_matrix, pod_array=pod_matrix) frequency_bias_matrix = model_eval.frequency_bias_from_sr_and_pod( success_ratio_array=success_ratio_matrix, pod_array=pod_matrix) this_colour_map_object, this_colour_norm_object = ( _get_csi_colour_scheme()) pyplot.contourf(success_ratio_matrix, pod_matrix, csi_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=csi_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('CSI (critical success index)') bias_colour_tuple = plotting_utils.colour_from_numpy_to_tuple( FREQ_BIAS_COLOUR) bias_colours_2d_tuple = () for _ in range(len(FREQ_BIAS_LEVELS)): bias_colours_2d_tuple += (bias_colour_tuple, ) bias_contour_object = pyplot.contour(success_ratio_matrix, pod_matrix, frequency_bias_matrix, FREQ_BIAS_LEVELS, colors=bias_colours_2d_tuple, linewidths=FREQ_BIAS_WIDTH, linestyles='dashed', axes=axes_object) pyplot.clabel(bias_contour_object, inline=True, inline_spacing=FREQ_BIAS_PADDING, fmt=FREQ_BIAS_STRING_FORMAT, fontsize=FONT_SIZE) nan_flags = numpy.logical_or(numpy.isnan(success_ratio_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( success_ratio_by_threshold[real_indices], pod_by_threshold[real_indices], color=plotting_utils.colour_from_numpy_to_tuple(line_colour), linestyle='solid', linewidth=PERF_DIAGRAM_WIDTH)[0] axes_object.set_xlabel('Success ratio (1 - FAR)') axes_object.set_ylabel('POD (probability of detection)') axes_object.set_xlim(0., 1.) axes_object.set_ylim(0., 1.) return line_handle