Esempio n. 1
0
    def test_make_cost_function_fourth(self):
        """Ensures correct output from make_cost_function.

        In this case, using fourth set of inputs.
        """

        this_cost_function = permutation.make_cost_function(
            heating_rate_weight=FOURTH_HEATING_RATE_WEIGHT,
            flux_weight=FOURTH_FLUX_WEIGHT,
            include_net_flux=FOURTH_INCLUDE_NET_FLUX_FLAG
        )

        this_cost = this_cost_function(
            target_matrices=TARGET_MATRICES,
            prediction_matrices=PREDICTION_MATRICES
        )

        self.assertTrue(numpy.isclose(this_cost, FOURTH_COST, atol=TOLERANCE))
Esempio n. 2
0
def _run(model_file_name, example_file_name, num_examples, example_dir_name,
         example_id_file_name, heating_rate_weight, flux_weight,
         include_net_flux, do_backwards_test, shuffle_profiles_together,
         num_bootstrap_reps, output_file_name):
    """Runs permutation-based importance test.

    This is effectively the main method.

    :param model_file_name: See documentation at top of file.
    :param example_file_name: Same.
    :param num_examples: Same.
    :param example_dir_name: Same.
    :param example_id_file_name: Same.
    :param heating_rate_weight: Same.
    :param flux_weight: Same.
    :param include_net_flux: Same.
    :param do_backwards_test: Same.
    :param shuffle_profiles_together: Same.
    :param num_bootstrap_reps: Same.
    :param output_file_name: Same.
    """

    cost_function = permutation.make_cost_function(
        heating_rate_weight=heating_rate_weight, flux_weight=flux_weight,
        include_net_flux=include_net_flux
    )

    print('Reading model from: "{0:s}"...'.format(model_file_name))
    model_object = neural_net.read_model(model_file_name)

    metafile_name = neural_net.find_metafile(
        model_dir_name=os.path.split(model_file_name)[0],
        raise_error_if_missing=True
    )

    print('Reading metadata from: "{0:s}"...'.format(metafile_name))
    metadata_dict = neural_net.read_metafile(metafile_name)

    predictor_matrix, target_matrices = (
        misc_utils.get_examples_for_inference(
            model_metadata_dict=metadata_dict,
            example_file_name=example_file_name,
            num_examples=num_examples, example_dir_name=example_dir_name,
            example_id_file_name=example_id_file_name
        )[:2]
    )
    print(SEPARATOR_STRING)

    if not isinstance(target_matrices, list):
        target_matrices = [target_matrices]

    if do_backwards_test:
        result_dict = permutation.run_backwards_test(
            predictor_matrix=predictor_matrix, target_matrices=target_matrices,
            model_object=model_object, model_metadata_dict=metadata_dict,
            cost_function=cost_function,
            shuffle_profiles_together=shuffle_profiles_together,
            num_bootstrap_reps=num_bootstrap_reps
        )
    else:
        result_dict = permutation.run_forward_test(
            predictor_matrix=predictor_matrix, target_matrices=target_matrices,
            model_object=model_object, model_metadata_dict=metadata_dict,
            cost_function=cost_function,
            shuffle_profiles_together=shuffle_profiles_together,
            num_bootstrap_reps=num_bootstrap_reps
        )

    print(SEPARATOR_STRING)

    print('Writing results of permutation test to: "{0:s}"...'.format(
        output_file_name
    ))

    permutation.write_file(
        result_dict=result_dict, netcdf_file_name=output_file_name
    )