예제 #1
0
def _targets_numpy_to_dict(scalar_target_matrix, vector_target_matrix,
                           model_metadata_dict):
    """Converts either actual or predicted target values to dictionary.

    This method is a wrapper for `neural_net.targets_numpy_to_dict`.

    :param scalar_target_matrix: numpy array with scalar outputs (either
        predicted or actual target values).
    :param vector_target_matrix: Same but with vector outputs.
    :param model_metadata_dict: Dictionary read by `neural_net.read_metafile`.
    :return: example_dict: Equivalent dictionary, with keys listed in doc for
        `example_io.read_file`.
    """

    net_type_string = model_metadata_dict[neural_net.NET_TYPE_KEY]

    generator_option_dict = copy.deepcopy(
        model_metadata_dict[neural_net.TRAINING_OPTIONS_KEY])
    vector_target_names = (
        generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY])
    add_heating_rate = generator_option_dict[neural_net.OMIT_HEATING_RATE_KEY]

    if net_type_string == neural_net.DENSE_NET_TYPE_STRING:
        target_matrices = [scalar_target_matrix]
    else:
        if (add_heating_rate and vector_target_matrix.shape[-1]
                == len(vector_target_names) - 1):
            heating_rate_index = vector_target_names.index(
                example_utils.SHORTWAVE_HEATING_RATE_NAME)
            vector_target_matrix = numpy.insert(vector_target_matrix,
                                                obj=heating_rate_index,
                                                values=0.,
                                                axis=-1)

        target_matrices = [vector_target_matrix]
        if scalar_target_matrix is not None:
            target_matrices.append(scalar_target_matrix)

    example_dict = {
        example_utils.VECTOR_TARGET_NAMES_KEY:
        generator_option_dict[neural_net.VECTOR_TARGET_NAMES_KEY],
        example_utils.SCALAR_TARGET_NAMES_KEY:
        generator_option_dict[neural_net.SCALAR_TARGET_NAMES_KEY],
        example_utils.HEIGHTS_KEY:
        generator_option_dict[neural_net.HEIGHTS_KEY]
    }

    new_example_dict = neural_net.targets_numpy_to_dict(
        target_matrices=target_matrices,
        example_dict=example_dict,
        net_type_string=net_type_string)
    for this_key in TARGET_VALUE_KEYS:
        example_dict[this_key] = new_example_dict[this_key]

    return example_dict
예제 #2
0
    def test_targets_numpy_to_dict_cnn_no_scalars(self):
        """Ensures correct output from targets_numpy_to_dict.

        In this case, neural-net type is CNN with no scalar outputs.
        """

        this_example_dict = neural_net.targets_numpy_to_dict(
            target_matrices=CNN_TARGET_MATRICES_NO_SCALARS,
            example_dict=EXAMPLE_DICT,
            net_type_string=neural_net.CNN_TYPE_STRING)
        self.assertTrue(
            numpy.allclose(
                this_example_dict[example_utils.VECTOR_TARGET_VALS_KEY],
                VECTOR_TARGET_MATRIX,
                atol=TOLERANCE))
        self.assertTrue(
            this_example_dict[example_utils.SCALAR_TARGET_VALS_KEY].size == 0)
예제 #3
0
    def test_targets_numpy_to_dict_u_net(self):
        """Ensures correct output from targets_numpy_to_dict.

        In this case, neural-net type is U-net.
        """

        this_example_dict = neural_net.targets_numpy_to_dict(
            target_matrices=U_NET_TARGET_MATRICES,
            example_dict=EXAMPLE_DICT,
            net_type_string=neural_net.U_NET_TYPE_STRING)
        self.assertTrue(
            numpy.allclose(
                this_example_dict[example_utils.VECTOR_TARGET_VALS_KEY],
                VECTOR_TARGET_MATRIX,
                atol=TOLERANCE))
        self.assertTrue(
            numpy.allclose(
                this_example_dict[example_utils.SCALAR_TARGET_VALS_KEY],
                SCALAR_TARGET_MATRIX,
                atol=TOLERANCE))