def test_create_nice_predictor_names_ninth(self):
        """Ensures correct output from create_nice_predictor_names.

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

        these_names_by_matrix = permutation.create_nice_predictor_names(
            predictor_matrices=NINTH_PREDICTOR_MATRICES,
            cnn_metadata_dict=NINTH_METADATA_DICT)

        these_predictor_names = sum(these_names_by_matrix, [])
        self.assertTrue(these_predictor_names == NINTH_NICE_PREDICTOR_NAMES)
    def test_create_nice_predictor_names_eighth(self):
        """Ensures correct output from create_nice_predictor_names.

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

        these_names_by_matrix = permutation.create_nice_predictor_names(
            predictor_matrices=EIGHTH_PREDICTOR_MATRICES,
            cnn_metadata_dict=EIGHTH_METADATA_DICT,
            separate_radar_heights=EIGHTH_SEPARATE_HEIGHTS_FLAG)

        these_predictor_names = sum(these_names_by_matrix, [])
        self.assertTrue(these_predictor_names == EIGHTH_NICE_PREDICTOR_NAMES)
def get_pearson_correlations(predictor_matrices,
                             cnn_metadata_dict,
                             separate_radar_heights=False):
    """Computes Pearson correlation between each pair of predictors.

    P = total number of predictors (over all matrices)

    :param predictor_matrices: See doc for
        `permutation.create_nice_predictor_names`.
    :param cnn_metadata_dict: Same.
    :param separate_radar_heights: Same.
    :return: correlation_matrix: P-by-P numpy array of Pearson correlations.
    :return: predictor_names: length-P list of predictor names.
    """

    error_checking.assert_is_boolean(separate_radar_heights)

    first_num_dimensions = len(predictor_matrices[0].shape)
    separate_radar_heights = (separate_radar_heights
                              and first_num_dimensions == 5)

    predictor_names_by_matrix = permutation.create_nice_predictor_names(
        predictor_matrices=predictor_matrices,
        cnn_metadata_dict=cnn_metadata_dict,
        separate_radar_heights=separate_radar_heights)

    num_matrices = len(predictor_names_by_matrix)
    for i in range(num_matrices):
        print('Predictors in {0:d}th matrix:\n{1:s}\n'.format(
            i + 1, str(predictor_names_by_matrix[i])))

    print(SEPARATOR_STRING)

    predictor_matrices_to_use = copy.deepcopy(predictor_matrices)
    if separate_radar_heights:
        predictor_matrices_to_use[0] = permutation_utils.flatten_last_two_dim(
            predictor_matrices_to_use[0])[0]

    num_predictors_by_matrix = numpy.array(
        [len(n) for n in predictor_names_by_matrix], dtype=int)

    predictor_names = sum(predictor_names_by_matrix, [])
    num_predictors = len(predictor_names)
    correlation_matrix = numpy.full((num_predictors, num_predictors),
                                    numpy.nan)

    for i in range(num_predictors):
        for j in range(i, num_predictors):
            if i == j:
                correlation_matrix[i, j] = 1.
                continue

            i_matrix, i_channel = _linear_idx_to_matrix_channel_idxs(
                linear_index=i,
                num_predictors_by_matrix=num_predictors_by_matrix)

            j_matrix, j_channel = _linear_idx_to_matrix_channel_idxs(
                linear_index=j,
                num_predictors_by_matrix=num_predictors_by_matrix)

            these_first_values = _take_spatial_mean(
                predictor_matrices_to_use[i_matrix][..., i_channel])
            these_second_values = _take_spatial_mean(
                predictor_matrices_to_use[j_matrix][..., j_channel])

            correlation_matrix[i, j] = pearsonr(these_first_values,
                                                these_second_values)[0]
            correlation_matrix[j, i] = correlation_matrix[i, j]

    return correlation_matrix, predictor_names