Beispiel #1
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)
Beispiel #2
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)
def _run(valid_time_strings):
    """Plots predicted and observed fronts for one time step.

    This is effectively the main method.

    :param valid_time_strings: See documentation at top of file.
    """

    num_times = 2
    error_checking.assert_is_numpy_array(
        numpy.array(valid_time_strings),
        exact_dimensions=numpy.array([num_times]))

    valid_times_unix_sec = numpy.array(
        [time_conversion.string_to_unix_sec(s, INPUT_TIME_FORMAT)
         for s in valid_time_strings],
        dtype=int)

    num_grid_rows, num_grid_columns = nwp_model_utils.get_grid_dimensions(
        model_name=nwp_model_utils.NARR_MODEL_NAME)

    print 'Reading data from: "{0:s}"...'.format(OBJECT_PREDICTION_FILE_NAME)
    predicted_region_table = object_eval.read_predictions_and_obs(
        OBJECT_PREDICTION_FILE_NAME)[0]

    figure_file_names = numpy.full((3, num_times), '', dtype=object)

    for i in range(num_times):
        this_prediction_file_name = ml_utils.find_gridded_prediction_file(
            directory_name=TOP_PREDICTION_DIR_NAME,
            first_target_time_unix_sec=valid_times_unix_sec[i],
            last_target_time_unix_sec=valid_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)
        this_probability_matrix = this_prediction_dict[
            ml_utils.PROBABILITY_MATRIX_KEY]
        this_probability_matrix[numpy.isnan(this_probability_matrix)] = 0.

        figure_file_names[0, i] = '{0:s}/gridded_predictions_{1:s}.jpg'.format(
            OUTPUT_DIR_NAME, valid_time_strings[i])
        this_title_string = 'Gridded predictions at {0:s}'.format(
            time_conversion.unix_sec_to_string(
                valid_times_unix_sec[i], OUTPUT_TIME_FORMAT)
        )

        _plot_predictions_one_time(
            output_file_name=figure_file_names[0, i],
            title_string=this_title_string,
            annotation_string=GRIDDED_PREDICTION_PANEL_LABELS[i],
            class_probability_matrix=this_probability_matrix,
            plot_warm_colour_bar=i == 0,
            plot_cold_colour_bar=i == num_times - 1)

        this_predicted_region_table = predicted_region_table.loc[
            predicted_region_table[front_utils.TIME_COLUMN] ==
            valid_times_unix_sec[i]
        ]
        this_predicted_label_matrix = object_eval.regions_to_images(
            predicted_region_table=this_predicted_region_table,
            num_grid_rows=num_grid_rows, num_grid_columns=num_grid_columns)

        figure_file_names[1, i] = '{0:s}/object_predictions_{1:s}.jpg'.format(
            OUTPUT_DIR_NAME, valid_time_strings[i])
        this_title_string = 'Object-based predictions at {0:s}'.format(
            time_conversion.unix_sec_to_string(
                valid_times_unix_sec[i], OUTPUT_TIME_FORMAT)
        )

        _plot_predictions_one_time(
            output_file_name=figure_file_names[1, i],
            title_string=this_title_string,
            annotation_string=OBJECT_PREDICTION_PANEL_LABELS[i],
            predicted_label_matrix=this_predicted_label_matrix)

        figure_file_names[2, i] = '{0:s}/observations_{1:s}.jpg'.format(
            OUTPUT_DIR_NAME, valid_time_strings[i])
        this_title_string = 'Observations at {0:s}'.format(
            time_conversion.unix_sec_to_string(
                valid_times_unix_sec[i], OUTPUT_TIME_FORMAT)
        )

        _plot_observations_one_time(
            valid_time_string=valid_time_strings[i],
            title_string=this_title_string,
            annotation_string=OBSERVATION_PANEL_LABELS[i],
            output_file_name=figure_file_names[2, i])
        print '\n'

    print 'Concatenating figures to: "{0:s}"...'.format(CONCAT_FILE_NAME)
    imagemagick_utils.concatenate_images(
        input_file_names=numpy.ravel(figure_file_names).tolist(),
        output_file_name=CONCAT_FILE_NAME,
        num_panel_rows=NUM_PANEL_ROWS, num_panel_columns=NUM_PANEL_COLUMNS,
        output_size_pixels=FIGURE_SIZE_PIXELS)
Beispiel #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)
def _run(input_grid_dir_name, input_object_file_name, first_time_string,
         last_time_string, method_name, use_nfa_ensemble, first_letter_label,
         letter_interval, output_dir_name):
    """Plots predictions on full NARR grid.

    This is effectively the main method.

    :param input_grid_dir_name: See documentation at top of file.
    :param input_object_file_name: Same.
    :param first_time_string: Same.
    :param last_time_string: Same.
    :param method_name: Same.
    :param use_nfa_ensemble: Same.
    :param first_letter_label: Same.
    :param letter_interval: Same.
    :param output_dir_name: Same.
    :raises: ValueError: if `method_name not in VALID_METHOD_NAMES`.
    """

    if first_letter_label in ['', 'None']:
        first_letter_label = None

    file_system_utils.mkdir_recursive_if_necessary(
        directory_name=output_dir_name)

    if method_name not in VALID_METHOD_NAMES:
        error_string = (
            '\n{0:s}\nValid method names (listed above) do not include "{1:s}".'
        ).format(str(VALID_METHOD_NAMES), method_name)

        raise ValueError(error_string)

    print 'Reading data from: "{0:s}"...'.format(input_object_file_name)
    predicted_region_table = object_eval.read_predictions_and_obs(
        input_object_file_name)[0]

    first_time_unix_sec = time_conversion.string_to_unix_sec(
        first_time_string, DEFAULT_TIME_FORMAT)
    last_time_unix_sec = time_conversion.string_to_unix_sec(
        last_time_string, DEFAULT_TIME_FORMAT)

    # region_times_unix_sec = predicted_region_table[
    #     front_utils.TIME_COLUMN].values
    #
    # good_indices = numpy.where(numpy.logical_and(
    #     region_times_unix_sec >= first_time_unix_sec,
    #     region_times_unix_sec <= last_time_unix_sec
    # ))[0]
    #
    # predicted_region_table = predicted_region_table.iloc[good_indices]

    predicted_region_table = predicted_region_table.loc[
        (predicted_region_table[front_utils.TIME_COLUMN] >= first_time_unix_sec
         )
        & (predicted_region_table[front_utils.TIME_COLUMN] <=
           last_time_unix_sec)]

    valid_times_unix_sec = numpy.unique(
        predicted_region_table[front_utils.TIME_COLUMN].values)

    this_class_probability_matrix = None
    this_label_matrix = None
    this_letter_label = None

    plot_wf_colour_bar = False
    plot_cf_colour_bar = True

    for this_time_unix_sec in valid_times_unix_sec:
        if method_name == CNN_METHOD_NAME:
            this_file_name = ml_utils.find_gridded_prediction_file(
                directory_name=input_grid_dir_name,
                first_target_time_unix_sec=this_time_unix_sec,
                last_target_time_unix_sec=this_time_unix_sec)

            print 'Reading data from: "{0:s}"...'.format(this_file_name)
            this_class_probability_matrix = ml_utils.read_gridded_predictions(
                this_file_name)[ml_utils.PROBABILITY_MATRIX_KEY][0, ...]

        else:
            this_file_name = nfa.find_prediction_file(
                directory_name=input_grid_dir_name,
                first_valid_time_unix_sec=this_time_unix_sec,
                last_valid_time_unix_sec=this_time_unix_sec,
                ensembled=use_nfa_ensemble)

            print 'Reading data from: "{0:s}"...'.format(this_file_name)

            if use_nfa_ensemble:
                this_class_probability_matrix = nfa.read_ensembled_predictions(
                    this_file_name)[nfa.CLASS_PROBABILITIES_KEY][0, ...]
            else:
                this_label_matrix = nfa.read_gridded_predictions(
                    this_file_name)[0][0, ...]

        this_predicted_region_table = predicted_region_table.loc[
            predicted_region_table[
                front_utils.TIME_COLUMN] == this_time_unix_sec]

        this_title_string = '{0:s} predictions at {1:s}'.format(
            method_name.upper(),
            time_conversion.unix_sec_to_string(this_time_unix_sec,
                                               NICE_TIME_FORMAT))

        this_output_file_name = '{0:s}/predictions_{1:s}.jpg'.format(
            output_dir_name,
            time_conversion.unix_sec_to_string(this_time_unix_sec,
                                               DEFAULT_TIME_FORMAT))

        if first_letter_label is not None:
            if this_letter_label is None:
                this_letter_label = first_letter_label
            else:
                this_letter_label = chr(
                    ord(this_letter_label) + letter_interval)

        plot_wf_colour_bar = not plot_wf_colour_bar
        plot_cf_colour_bar = not plot_cf_colour_bar

        _plot_one_time(predicted_region_table=this_predicted_region_table,
                       title_string=this_title_string,
                       letter_label=this_letter_label,
                       output_file_name=this_output_file_name,
                       class_probability_matrix=this_class_probability_matrix,
                       predicted_label_matrix=this_label_matrix,
                       plot_wf_colour_bar=plot_wf_colour_bar,
                       plot_cf_colour_bar=plot_cf_colour_bar)

        print '\n'