def _read_bwo_file(bwo_file_name):
    """Reads backwards-optimization results from file.

    :param bwo_file_name: Path to input file (will be read by
        `backwards_optimization.read_file`).
    :return: bwo_dictionary: Dictionary returned by
        `backwards_optimization.read_file`.
    :return: model_metadata_dict: Dictionary returned by
        `cnn.read_model_metadata`.
    """

    print('Reading data from: "{0:s}"...'.format(bwo_file_name))
    bwo_dictionary = backwards_opt.read_file(bwo_file_name)[0]

    mean_before_matrices = [
        numpy.expand_dims(a, axis=0)
        for a in bwo_dictionary[backwards_opt.MEAN_INPUT_MATRICES_KEY]
    ]
    mean_after_matrices = [
        numpy.expand_dims(a, axis=0)
        for a in bwo_dictionary[backwards_opt.MEAN_OUTPUT_MATRICES_KEY]
    ]

    model_file_name = bwo_dictionary[backwards_opt.MODEL_FILE_KEY]
    model_metafile_name = cnn.find_metafile(model_file_name)

    print('Reading CNN metadata from: "{0:s}"...'.format(model_metafile_name))
    model_metadata_dict = cnn.read_model_metadata(model_metafile_name)
    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]

    good_indices = numpy.array([
        numpy.where(
            training_option_dict[trainval_io.RADAR_HEIGHTS_KEY] == h)[0][0]
        for h in RADAR_HEIGHTS_M_AGL
    ],
                               dtype=int)

    mean_before_matrices[0] = mean_before_matrices[0][..., good_indices, :]
    mean_after_matrices[0] = mean_after_matrices[0][..., good_indices, :]

    good_indices = numpy.array([
        training_option_dict[trainval_io.RADAR_FIELDS_KEY].index(f)
        for f in RADAR_FIELD_NAMES
    ],
                               dtype=int)

    mean_before_matrices[0] = mean_before_matrices[0][..., good_indices]
    mean_after_matrices[0] = mean_after_matrices[0][..., good_indices]

    bwo_dictionary[
        backwards_opt.MEAN_INPUT_MATRICES_KEY] = mean_before_matrices
    bwo_dictionary[
        backwards_opt.MEAN_OUTPUT_MATRICES_KEY] = mean_after_matrices

    training_option_dict[trainval_io.RADAR_HEIGHTS_KEY] = RADAR_HEIGHTS_M_AGL
    training_option_dict[trainval_io.RADAR_FIELDS_KEY] = RADAR_FIELD_NAMES
    model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY] = training_option_dict

    return bwo_dictionary, model_metadata_dict
def _run():
    """Analyzes backwards-optimization experiment.

    This is effectively the main method.
    """

    num_l2_weights = len(L2_WEIGHTS)
    num_minmax_weights = len(MINMAX_WEIGHTS)

    mean_final_activation_matrix = numpy.full(
        (num_l2_weights, num_minmax_weights), numpy.nan)

    for i in range(num_l2_weights):
        for j in range(num_minmax_weights):
            this_file_name = (
                '{0:s}/bwo_pmm_l2-weight={1:.10f}_minmax-weight={2:.10f}.p'
            ).format(TOP_EXPERIMENT_DIR_NAME, L2_WEIGHTS[i], MINMAX_WEIGHTS[j])

            print('Reading data from: "{0:s}"...'.format(this_file_name))
            this_bwo_dict = backwards_opt.read_file(this_file_name)[0]
            mean_final_activation_matrix[i, j] = this_bwo_dict[
                backwards_opt.MEAN_FINAL_ACTIVATION_KEY]

    x_tick_labels = ['{0:.1f}'.format(r) for r in numpy.log10(MINMAX_WEIGHTS)]
    y_tick_labels = ['{0:.1f}'.format(w) for w in numpy.log10(L2_WEIGHTS)]

    axes_object = model_evaluation.plot_hyperparam_grid(
        score_matrix=mean_final_activation_matrix,
        colour_map_object=COLOUR_MAP_OBJECT,
        min_colour_value=0.,
        max_colour_value=1.)

    axes_object.set_xticklabels(x_tick_labels, rotation=90.)
    axes_object.set_yticklabels(y_tick_labels)

    axes_object.set_xlabel(r'Min-max weight (log$_{10}$)')
    axes_object.set_ylabel(r'L$_2$ weight (log$_{10}$)')

    plotting_utils.plot_linear_colour_bar(
        axes_object_or_matrix=axes_object,
        data_matrix=mean_final_activation_matrix,
        colour_map_object=COLOUR_MAP_OBJECT,
        min_value=0.,
        max_value=1.,
        orientation_string='vertical',
        extend_min=False,
        extend_max=False,
        font_size=FONT_SIZE)

    output_file_name = '{0:s}/mean_final_activations.jpg'.format(
        TOP_EXPERIMENT_DIR_NAME)

    print('Saving figure to: "{0:s}"...'.format(output_file_name))
    pyplot.savefig(output_file_name,
                   dpi=FIGURE_RESOLUTION_DPI,
                   pad_inches=0,
                   bbox_inches='tight')
    pyplot.close()
예제 #3
0
def _composite_backwards_opt(
        input_file_name, max_percentile_level, output_file_name):
    """Composites inputs and outputs for backwards optimization.

    :param input_file_name: Path to input file.  Will be read by
        `backwards_optimization.read_file`.
    :param max_percentile_level: See documentation at top of file.
    :param output_file_name: Path to output file.  Will be written by
        `backwards_optimization.write_pmm_file`.
    """

    print('Reading data from: "{0:s}"...'.format(input_file_name))
    bwo_dictionary = backwards_opt.read_file(input_file_name)[0]

    input_matrices = bwo_dictionary[backwards_opt.INPUT_MATRICES_KEY]
    output_matrices = bwo_dictionary[backwards_opt.OUTPUT_MATRICES_KEY]
    sounding_pressure_matrix_pa = bwo_dictionary[
        backwards_opt.SOUNDING_PRESSURES_KEY]

    print('Compositing non-optimized predictors...')
    mean_input_matrices, mean_sounding_pressures_pa = _composite_predictors(
        predictor_matrices=input_matrices,
        max_percentile_level=max_percentile_level,
        sounding_pressure_matrix_pa=sounding_pressure_matrix_pa)

    print('Compositing optimized predictors...')
    mean_output_matrices = _composite_predictors(
        predictor_matrices=output_matrices,
        max_percentile_level=max_percentile_level
    )[0]

    mean_initial_activation = numpy.mean(
        bwo_dictionary[backwards_opt.INITIAL_ACTIVATIONS_KEY]
    )
    mean_final_activation = numpy.mean(
        bwo_dictionary[backwards_opt.FINAL_ACTIVATIONS_KEY]
    )

    print('Writing output to: "{0:s}"...'.format(output_file_name))
    backwards_opt.write_pmm_file(
        pickle_file_name=output_file_name,
        mean_denorm_input_matrices=mean_input_matrices,
        mean_denorm_output_matrices=mean_output_matrices,
        mean_initial_activation=mean_initial_activation,
        mean_final_activation=mean_final_activation,
        model_file_name=bwo_dictionary[backwards_opt.MODEL_FILE_KEY],
        non_pmm_file_name=input_file_name,
        pmm_max_percentile_level=max_percentile_level,
        mean_sounding_pressures_pa=mean_sounding_pressures_pa)
def _read_bwo_file(bwo_file_name):
    """Reads backwards-optimization results from file.

    :param bwo_file_name: Path to input file (will be read by
        `backwards_optimization.read_file`).
    :return: bwo_dictionary: Dictionary returned by
        `backwards_optimization.read_file`.
    :return: model_metadata_dict: Dictionary returned by
        `cnn.read_model_metadata`.
    """

    print('Reading data from: "{0:s}"...'.format(bwo_file_name))
    bwo_dictionary = backwards_opt.read_file(bwo_file_name)[0]

    mean_before_matrices = [
        numpy.expand_dims(a, axis=0) for a in
        bwo_dictionary[MEAN_INPUT_MATRICES_KEY]
    ]
    mean_after_matrices = [
        numpy.expand_dims(a, axis=0) for a in
        bwo_dictionary[MEAN_OUTPUT_MATRICES_KEY]
    ]

    model_file_name = bwo_dictionary[MODEL_FILE_KEY]
    model_metafile_name = cnn.find_metafile(model_file_name)

    print('Reading CNN metadata from: "{0:s}"...'.format(model_metafile_name))
    model_metadata_dict = cnn.read_model_metadata(model_metafile_name)
    training_option_dict = model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY]
    training_option_dict[trainval_io.UPSAMPLE_REFLECTIVITY_KEY] = False

    all_refl_heights_m_agl = training_option_dict[trainval_io.RADAR_HEIGHTS_KEY]
    good_flags = numpy.array(
        [h in REFL_HEIGHTS_M_AGL for h in all_refl_heights_m_agl], dtype=bool
    )
    good_indices = numpy.where(good_flags)[0]

    mean_before_matrices[0] = mean_before_matrices[0][..., good_indices, :]
    mean_after_matrices[0] = mean_after_matrices[0][..., good_indices, :]

    bwo_dictionary[MEAN_INPUT_MATRICES_KEY] = mean_before_matrices
    bwo_dictionary[MEAN_OUTPUT_MATRICES_KEY] = mean_after_matrices

    training_option_dict[trainval_io.RADAR_HEIGHTS_KEY] = REFL_HEIGHTS_M_AGL
    model_metadata_dict[cnn.TRAINING_OPTION_DICT_KEY] = training_option_dict

    return bwo_dictionary, model_metadata_dict
def _run():
    """Analyzes backwards-optimization experiment.

    This is effectively the main method.
    """

    num_weights = len(MINMAX_WEIGHTS)

    for i in range(num_weights):
        this_file_name = '{0:s}/bwo_minmax-weight={1:014.10f}_pmm.p'.format(
            TOP_EXPERIMENT_DIR_NAME, MINMAX_WEIGHTS[i])

        this_bwo_dict = backwards_opt.read_file(this_file_name)[0]
        this_mean_final_activation = this_bwo_dict[
            backwards_opt.MEAN_FINAL_ACTIVATION_KEY]

        print(
            ('Min-max weight = 10^{0:.1f} ... mean final activation = {1:.4f}'
             ).format(numpy.log10(MINMAX_WEIGHTS[i]),
                      this_mean_final_activation))
def _run(input_file_name, diff_colour_map_name, max_diff_percentile,
         plot_soundings, allow_whitespace, plot_panel_names, add_titles,
         label_colour_bars, colour_bar_length, top_output_dir_name):
    """Plots results of backwards optimization.

    This is effectively the main method.

    :param input_file_name: See documentation at top of file.
    :param diff_colour_map_name: Same.
    :param max_diff_percentile: Same.
    :param plot_soundings: Same.
    :param allow_whitespace: Same.
    :param plot_panel_names: Same.
    :param add_titles: Same.
    :param label_colour_bars: Same.
    :param colour_bar_length: Same.
    :param top_output_dir_name: Same.
    """

    diff_colour_map_object = pyplot.cm.get_cmap(diff_colour_map_name)

    print('Reading data from: "{0:s}"...'.format(input_file_name))
    bwo_dictionary, pmm_flag = backwards_opt.read_file(input_file_name)

    if pmm_flag:
        input_matrices = bwo_dictionary.pop(
            backwards_opt.MEAN_INPUT_MATRICES_KEY)
        output_matrices = bwo_dictionary.pop(
            backwards_opt.MEAN_OUTPUT_MATRICES_KEY)

        full_storm_id_strings = [None]
        storm_times_unix_sec = [None]

        mean_sounding_pressures_pa = bwo_dictionary[
            backwards_opt.MEAN_SOUNDING_PRESSURES_KEY]

        if mean_sounding_pressures_pa is None:
            sounding_pressure_matrix_pa = None
        else:
            sounding_pressure_matrix_pa = numpy.reshape(
                mean_sounding_pressures_pa,
                (1, len(mean_sounding_pressures_pa)))

        for i in range(len(input_matrices)):
            input_matrices[i] = numpy.expand_dims(input_matrices[i], axis=0)
            output_matrices[i] = numpy.expand_dims(output_matrices[i], axis=0)
    else:
        input_matrices = bwo_dictionary.pop(backwards_opt.INPUT_MATRICES_KEY)
        output_matrices = bwo_dictionary.pop(backwards_opt.OUTPUT_MATRICES_KEY)

        full_storm_id_strings = bwo_dictionary[
            backwards_opt.FULL_STORM_IDS_KEY]
        storm_times_unix_sec = bwo_dictionary[backwards_opt.STORM_TIMES_KEY]
        sounding_pressure_matrix_pa = bwo_dictionary[
            backwards_opt.SOUNDING_PRESSURES_KEY]

    model_file_name = bwo_dictionary[backwards_opt.MODEL_FILE_KEY]
    model_metafile_name = '{0:s}/model_metadata.p'.format(
        os.path.split(model_file_name)[0])

    print('Reading metadata from: "{0:s}"...'.format(model_metafile_name))
    model_metadata_dict = cnn.read_model_metadata(model_metafile_name)
    print(SEPARATOR_STRING)

    plot_examples.plot_examples(
        list_of_predictor_matrices=input_matrices,
        model_metadata_dict=model_metadata_dict,
        pmm_flag=pmm_flag,
        output_dir_name='{0:s}/before_optimization'.format(
            top_output_dir_name),
        plot_soundings=plot_soundings,
        sounding_pressure_matrix_pascals=sounding_pressure_matrix_pa,
        allow_whitespace=allow_whitespace,
        plot_panel_names=plot_panel_names,
        add_titles=add_titles,
        label_colour_bars=label_colour_bars,
        colour_bar_length=colour_bar_length,
        plot_radar_diffs=False,
        full_storm_id_strings=full_storm_id_strings,
        storm_times_unix_sec=storm_times_unix_sec)
    print(SEPARATOR_STRING)

    plot_examples.plot_examples(
        list_of_predictor_matrices=output_matrices,
        model_metadata_dict=model_metadata_dict,
        pmm_flag=pmm_flag,
        output_dir_name='{0:s}/after_optimization'.format(top_output_dir_name),
        plot_soundings=plot_soundings,
        sounding_pressure_matrix_pascals=sounding_pressure_matrix_pa,
        allow_whitespace=allow_whitespace,
        plot_panel_names=plot_panel_names,
        add_titles=add_titles,
        label_colour_bars=label_colour_bars,
        colour_bar_length=colour_bar_length,
        plot_radar_diffs=False,
        full_storm_id_strings=full_storm_id_strings,
        storm_times_unix_sec=storm_times_unix_sec)
    print(SEPARATOR_STRING)

    difference_matrices = [
        b - a for b, a in zip(output_matrices, input_matrices)
    ]

    plot_examples.plot_examples(
        list_of_predictor_matrices=difference_matrices,
        model_metadata_dict=model_metadata_dict,
        pmm_flag=pmm_flag,
        output_dir_name='{0:s}/difference'.format(top_output_dir_name),
        plot_soundings=False,
        allow_whitespace=allow_whitespace,
        plot_panel_names=plot_panel_names,
        add_titles=add_titles,
        label_colour_bars=label_colour_bars,
        colour_bar_length=colour_bar_length,
        plot_radar_diffs=True,
        diff_colour_map_object=diff_colour_map_object,
        max_diff_percentile=max_diff_percentile,
        full_storm_id_strings=full_storm_id_strings,
        storm_times_unix_sec=storm_times_unix_sec)