Esempio n. 1
0
    def test_determinize_probabilities(self):
        """Ensures correct output from determinize_probabilities."""

        this_label_matrix = object_based_eval.determinize_probabilities(
            class_probability_matrix=PROBABILITY_MATRIX,
            binarization_threshold=BINARIZATION_THRESHOLD)

        self.assertTrue(
            numpy.array_equal(this_label_matrix, PREDICTED_LABEL_MATRIX))
Esempio n. 2
0
def _run():
    """Plots determinization of front probabilities.

    This is effectively the main method.
    """

    prediction_dict = ml_utils.read_gridded_predictions(PREDICTION_FILE_NAME)
    class_probability_matrix = prediction_dict[ml_utils.PROBABILITY_MATRIX_KEY]

    for this_id in front_utils.VALID_INTEGER_IDS:
        if this_id == front_utils.NO_FRONT_INTEGER_ID:
            class_probability_matrix[..., this_id][numpy.isnan(
                class_probability_matrix[..., this_id])] = 1.
        else:
            class_probability_matrix[..., this_id][numpy.isnan(
                class_probability_matrix[..., this_id])] = 0.

    _plot_predictions(output_file_name=BEFORE_FILE_NAME,
                      title_string='Probabilities',
                      annotation_string='(a)',
                      class_probability_matrix=class_probability_matrix)

    predicted_label_matrix = object_eval.determinize_probabilities(
        class_probability_matrix=class_probability_matrix,
        binarization_threshold=BINARIZATION_THRESHOLD)

    _plot_predictions(output_file_name=AFTER_FILE_NAME,
                      title_string='Deterministic predictions',
                      annotation_string='(b)',
                      predicted_label_matrix=predicted_label_matrix)

    print 'Concatenating figures to: "{0:s}"...'.format(CONCAT_FILE_NAME)

    imagemagick_utils.concatenate_images(
        input_file_names=[BEFORE_FILE_NAME, AFTER_FILE_NAME],
        output_file_name=CONCAT_FILE_NAME,
        num_panel_rows=2,
        num_panel_columns=1)

    imagemagick_utils.resize_image(input_file_name=CONCAT_FILE_NAME,
                                   output_file_name=CONCAT_FILE_NAME,
                                   output_size_pixels=CONCAT_SIZE_PIXELS)
Esempio n. 3
0
def _run():
    """Plots conversion of gridded probabilities into objects.

    This is effectively the main method.
    """

    prediction_dict = ml_utils.read_gridded_predictions(PREDICTION_FILE_NAME)
    class_probability_matrix = prediction_dict[ml_utils.PROBABILITY_MATRIX_KEY]

    for this_id in front_utils.VALID_INTEGER_IDS:
        if this_id == front_utils.NO_FRONT_INTEGER_ID:
            class_probability_matrix[..., this_id][numpy.isnan(
                class_probability_matrix[..., this_id])] = 1.
        else:
            class_probability_matrix[..., this_id][numpy.isnan(
                class_probability_matrix[..., this_id])] = 0.

    predicted_label_matrix = object_eval.determinize_probabilities(
        class_probability_matrix=class_probability_matrix,
        binarization_threshold=BINARIZATION_THRESHOLD)

    _plot_predictions(predicted_label_matrix=predicted_label_matrix,
                      title_string='All frontal regions',
                      annotation_string='(b)',
                      output_file_name=ALL_REGIONS_FILE_NAME)

    valid_time_unix_sec = time_conversion.string_to_unix_sec(
        VALID_TIME_STRING, TIME_FORMAT)
    valid_times_unix_sec = numpy.array([valid_time_unix_sec], dtype=int)
    predicted_region_table = object_eval.images_to_regions(
        predicted_label_matrix=predicted_label_matrix,
        image_times_unix_sec=valid_times_unix_sec)

    num_grid_rows, num_grid_columns = nwp_model_utils.get_grid_dimensions(
        model_name=nwp_model_utils.NARR_MODEL_NAME)
    grid_spacing_metres = nwp_model_utils.get_xy_grid_spacing(
        model_name=nwp_model_utils.NARR_MODEL_NAME)[0]

    predicted_region_table = object_eval.discard_regions_with_small_area(
        predicted_region_table=predicted_region_table,
        x_grid_spacing_metres=grid_spacing_metres,
        y_grid_spacing_metres=grid_spacing_metres,
        min_area_metres2=MIN_REGION_AREA_METRES2)
    predicted_label_matrix = object_eval.regions_to_images(
        predicted_region_table=predicted_region_table,
        num_grid_rows=num_grid_rows,
        num_grid_columns=num_grid_columns)

    _plot_predictions(predicted_label_matrix=predicted_label_matrix,
                      title_string='Large frontal regions',
                      annotation_string='(c)',
                      output_file_name=LARGE_REGIONS_FILE_NAME)

    predicted_region_table = object_eval.skeletonize_frontal_regions(
        predicted_region_table=predicted_region_table,
        num_grid_rows=num_grid_rows,
        num_grid_columns=num_grid_columns)
    predicted_label_matrix = object_eval.regions_to_images(
        predicted_region_table=predicted_region_table,
        num_grid_rows=num_grid_rows,
        num_grid_columns=num_grid_columns)

    _plot_predictions(predicted_label_matrix=predicted_label_matrix,
                      title_string='All skeleton lines',
                      annotation_string='(d)',
                      output_file_name=ALL_SKELETONS_FILE_NAME)

    predicted_region_table = object_eval.find_main_skeletons(
        predicted_region_table=predicted_region_table,
        image_times_unix_sec=valid_times_unix_sec,
        num_grid_rows=num_grid_rows,
        num_grid_columns=num_grid_columns,
        x_grid_spacing_metres=grid_spacing_metres,
        y_grid_spacing_metres=grid_spacing_metres,
        min_endpoint_length_metres=MIN_ENDPOINT_LENGTH_METRES)

    predicted_label_matrix = object_eval.regions_to_images(
        predicted_region_table=predicted_region_table,
        num_grid_rows=num_grid_rows,
        num_grid_columns=num_grid_columns)

    _plot_predictions(predicted_label_matrix=predicted_label_matrix,
                      title_string='Main skeleton lines',
                      annotation_string='(e)',
                      output_file_name=MAIN_SKELETONS_FILE_NAME)

    print 'Concatenating figures to: "{0:s}"...'.format(CONCAT_FILE_NAME)

    panel_file_names = [
        PROBABILITY_FILE_NAME, ALL_REGIONS_FILE_NAME, LARGE_REGIONS_FILE_NAME,
        ALL_SKELETONS_FILE_NAME, MAIN_SKELETONS_FILE_NAME
    ]

    imagemagick_utils.concatenate_images(input_file_names=panel_file_names,
                                         output_file_name=CONCAT_FILE_NAME,
                                         num_panel_rows=2,
                                         num_panel_columns=3)

    imagemagick_utils.resize_image(input_file_name=CONCAT_FILE_NAME,
                                   output_file_name=CONCAT_FILE_NAME,
                                   output_size_pixels=CONCAT_SIZE_PIXELS)
Esempio n. 4
0
def _run(input_prediction_dir_name, first_time_string, last_time_string,
         num_times, binarization_threshold, min_object_area_metres2,
         min_endpoint_length_metres, top_front_line_dir_name,
         output_file_name):
    """Converts gridded CNN predictions to objects.

    This is effectively the main method.

    :param input_prediction_dir_name: See documentation at top of file.
    :param first_time_string: Same.
    :param last_time_string: Same.
    :param num_times: Same.
    :param binarization_threshold: Same.
    :param min_object_area_metres2: Same.
    :param min_endpoint_length_metres: Same.
    :param top_front_line_dir_name: Same.
    :param output_file_name: Same.
    """

    grid_spacing_metres = nwp_model_utils.get_xy_grid_spacing(
        model_name=nwp_model_utils.NARR_MODEL_NAME)[0]
    num_grid_rows, num_grid_columns = nwp_model_utils.get_grid_dimensions(
        model_name=nwp_model_utils.NARR_MODEL_NAME)

    first_time_unix_sec = time_conversion.string_to_unix_sec(
        first_time_string, INPUT_TIME_FORMAT)
    last_time_unix_sec = time_conversion.string_to_unix_sec(
        last_time_string, INPUT_TIME_FORMAT)
    possible_times_unix_sec = time_periods.range_and_interval_to_list(
        start_time_unix_sec=first_time_unix_sec,
        end_time_unix_sec=last_time_unix_sec,
        time_interval_sec=NARR_TIME_INTERVAL_SECONDS,
        include_endpoint=True)

    numpy.random.shuffle(possible_times_unix_sec)

    unix_times_sec = []
    list_of_predicted_region_tables = []
    num_times_done = 0
    narr_mask_matrix = None

    for i in range(len(possible_times_unix_sec)):
        if num_times_done == num_times:
            break

        this_prediction_file_name = ml_utils.find_gridded_prediction_file(
            directory_name=input_prediction_dir_name,
            first_target_time_unix_sec=possible_times_unix_sec[i],
            last_target_time_unix_sec=possible_times_unix_sec[i],
            raise_error_if_missing=False)
        if not os.path.isfile(this_prediction_file_name):
            continue

        num_times_done += 1
        unix_times_sec.append(possible_times_unix_sec[i])

        print 'Reading data from: "{0:s}"...'.format(this_prediction_file_name)
        this_prediction_dict = ml_utils.read_gridded_predictions(
            this_prediction_file_name)

        class_probability_matrix = this_prediction_dict[
            ml_utils.PROBABILITY_MATRIX_KEY]

        if narr_mask_matrix is None:
            narr_mask_matrix = numpy.invert(
                numpy.isnan(class_probability_matrix[0, ..., 0])).astype(int)

        # TODO(thunderhoser): This should be a separate method.
        class_probability_matrix[..., front_utils.NO_FRONT_INTEGER_ID][
            numpy.isnan(class_probability_matrix[
                ..., front_utils.NO_FRONT_INTEGER_ID])] = 1.
        class_probability_matrix[numpy.isnan(class_probability_matrix)] = 0.

        print 'Determinizing probabilities...'
        this_predicted_label_matrix = object_eval.determinize_probabilities(
            class_probability_matrix=this_prediction_dict[
                ml_utils.PROBABILITY_MATRIX_KEY],
            binarization_threshold=binarization_threshold)

        print 'Converting image to frontal regions...'
        list_of_predicted_region_tables.append(
            object_eval.images_to_regions(
                predicted_label_matrix=this_predicted_label_matrix,
                image_times_unix_sec=possible_times_unix_sec[[i]]))

        print 'Throwing out frontal regions with area < {0:f} km^2...'.format(
            METRES2_TO_KM2 * min_object_area_metres2)
        list_of_predicted_region_tables[
            -1] = object_eval.discard_regions_with_small_area(
                predicted_region_table=list_of_predicted_region_tables[-1],
                x_grid_spacing_metres=grid_spacing_metres,
                y_grid_spacing_metres=grid_spacing_metres,
                min_area_metres2=min_object_area_metres2)

        print 'Skeletonizing frontal regions...'
        list_of_predicted_region_tables[
            -1] = object_eval.skeletonize_frontal_regions(
                predicted_region_table=list_of_predicted_region_tables[-1],
                num_grid_rows=num_grid_rows,
                num_grid_columns=num_grid_columns)

        list_of_predicted_region_tables[-1] = object_eval.find_main_skeletons(
            predicted_region_table=list_of_predicted_region_tables[-1],
            image_times_unix_sec=possible_times_unix_sec[[i]],
            num_grid_rows=num_grid_rows,
            num_grid_columns=num_grid_columns,
            x_grid_spacing_metres=grid_spacing_metres,
            y_grid_spacing_metres=grid_spacing_metres,
            min_endpoint_length_metres=min_endpoint_length_metres)

        if num_times_done != num_times:
            print '\n'

        if len(list_of_predicted_region_tables) == 1:
            continue

        list_of_predicted_region_tables[-1] = (
            list_of_predicted_region_tables[-1].align(
                list_of_predicted_region_tables[0], axis=1)[0])

    print SEPARATOR_STRING

    unix_times_sec = numpy.array(unix_times_sec, dtype=int)
    predicted_region_table = pandas.concat(list_of_predicted_region_tables,
                                           axis=0,
                                           ignore_index=True)
    predicted_region_table = object_eval.convert_regions_rowcol_to_narr_xy(
        predicted_region_table=predicted_region_table,
        are_predictions_from_fcn=False)

    actual_polyline_table = _read_actual_polylines(
        top_input_dir_name=top_front_line_dir_name,
        unix_times_sec=unix_times_sec,
        narr_mask_matrix=narr_mask_matrix)
    print SEPARATOR_STRING

    actual_polyline_table = object_eval.project_polylines_latlng_to_narr(
        actual_polyline_table)

    print 'Writing predicted and observed objects to: "{0:s}"...'.format(
        output_file_name)
    object_eval.write_predictions_and_obs(
        predicted_region_table=predicted_region_table,
        actual_polyline_table=actual_polyline_table,
        pickle_file_name=output_file_name)