<commit_before>"""Handles Monte Carlo significance-testing."""

import numpy
from gewittergefahr.gg_utils import prob_matched_means as pmm
from gewittergefahr.gg_utils import error_checking

SEPARATOR_STRING = '\n\n' + '*' * 50 + '\n\n'

TRIAL_PMM_MATRICES_KEY = 'list_of_trial_pmm_matrices'
MIN_MATRICES_KEY = 'list_of_min_matrices'
MAX_MATRICES_KEY = 'list_of_max_matrices'
MAX_PMM_PERCENTILE_KEY = 'max_pmm_percentile_level'
NUM_ITERATIONS_KEY = 'num_iterations'
CONFIDENCE_LEVEL_KEY = 'confidence_level'
BASELINE_FILE_KEY = 'baseline_file_name'


def _check_input_args(
        list_of_baseline_matrices, list_of_trial_matrices, num_iterations,
        confidence_level):
    """Error-checks input args for Monte Carlo test.

    :param list_of_baseline_matrices: See doc for `run_monte_carlo_test`.
    :param list_of_trial_matrices: Same.
    :param num_iterations: Same.
    :param confidence_level: Same.
    :raises: ValueError: if number of baseline matrices (input tensors to model)
        != number of trial matrices.
    """

    error_checking.assert_is_integer(num_iterations)
    error_checking.assert_is_geq(num_iterations, 100)
    error_checking.assert_is_geq(confidence_level, 0.)
    error_checking.assert_is_leq(confidence_level, 1.)

    num_baseline_matrices = len(list_of_baseline_matrices)
    num_trial_matrices = len(list_of_trial_matrices)

    if num_baseline_matrices != num_trial_matrices:
        error_string = (
            'Number of baseline matrices ({0:d}) should = number of trial '
            'matrices ({1:d}).'
        ).format(num_baseline_matrices, num_trial_matrices)

        raise ValueError(error_string)

    num_matrices = num_trial_matrices
    num_examples = None

    for i in range(num_matrices):
        error_checking.assert_is_numpy_array(list_of_baseline_matrices[i])

        if num_examples is None:
            num_examples = list_of_baseline_matrices[i].shape[0]

        these_expected_dim = numpy.array(
            (num_examples,) + list_of_baseline_matrices[i].shape[1:], dtype=int
        )

        error_checking.assert_is_numpy_array(
            list_of_baseline_matrices[i], exact_dimensions=these_expected_dim)

        these_expected_dim = numpy.array(
            list_of_baseline_matrices[i].shape, dtype=int)

        error_checking.assert_is_numpy_array(
            list_of_trial_matrices[i], exact_dimensions=these_expected_dim)


def check_output(monte_carlo_dict):
    """Error-checks output from Monte Carlo test.

    :param monte_carlo_dict: Dictionary created by `run_monte_carlo_test`.
    :raises: ValueError: if not (number of trial matrices = number of MIN
        matrices = number of MAX matrices).
    """

    error_checking.assert_is_greater(
        monte_carlo_dict[MAX_PMM_PERCENTILE_KEY], 50.
    )
    error_checking.assert_is_leq(monte_carlo_dict[MAX_PMM_PERCENTILE_KEY], 100.)
    error_checking.assert_is_integer(monte_carlo_dict[NUM_ITERATIONS_KEY])
    error_checking.assert_is_geq(monte_carlo_dict[NUM_ITERATIONS_KEY], 100)
    error_checking.assert_is_geq(monte_carlo_dict[CONFIDENCE_LEVEL_KEY], 0.)
    error_checking.assert_is_leq(monte_carlo_dict[CONFIDENCE_LEVEL_KEY], 1.)

    num_trial_matrices = len(monte_carlo_dict[TRIAL_PMM_MATRICES_KEY])
    num_min_matrices = len(monte_carlo_dict[MIN_MATRICES_KEY])
    num_max_matrices = len(monte_carlo_dict[MAX_MATRICES_KEY])

    if not num_trial_matrices == num_min_matrices == num_max_matrices:
        error_string = (
            'Number of trial ({0:d}), MIN ({1:d}), and MAX ({2:d}) matrices '
            'should be the same.'
        ).format(num_trial_matrices, num_min_matrices, num_max_matrices)

        raise ValueError(error_string)

    num_matrices = num_trial_matrices

    for i in range(num_matrices):
        error_checking.assert_is_numpy_array(
            monte_carlo_dict[TRIAL_PMM_MATRICES_KEY][i]
        )

        these_expected_dim = numpy.array(
            monte_carlo_dict[TRIAL_PMM_MATRICES_KEY][i], dtype=int
        )

        error_checking.assert_is_numpy_array(
            monte_carlo_dict[MIN_MATRICES_KEY][i],
            exact_dimensions=these_expected_dim
        )

        error_checking.assert_is_numpy_array(<commit_msg>Fixed stupid bug in monte_carlo.py.<commit_after>"""Handles Monte Carlo significance-testing."""

import numpy
from gewittergefahr.gg_utils import prob_matched_means as pmm
from gewittergefahr.gg_utils import error_checking

SEPARATOR_STRING = '\n\n' + '*' * 50 + '\n\n'

TRIAL_PMM_MATRICES_KEY = 'list_of_trial_pmm_matrices'
MIN_MATRICES_KEY = 'list_of_min_matrices'
MAX_MATRICES_KEY = 'list_of_max_matrices'
MAX_PMM_PERCENTILE_KEY = 'max_pmm_percentile_level'
NUM_ITERATIONS_KEY = 'num_iterations'
CONFIDENCE_LEVEL_KEY = 'confidence_level'
BASELINE_FILE_KEY = 'baseline_file_name'


def _check_input_args(
        list_of_baseline_matrices, list_of_trial_matrices, num_iterations,
        confidence_level):
    """Error-checks input args for Monte Carlo test.

    :param list_of_baseline_matrices: See doc for `run_monte_carlo_test`.
    :param list_of_trial_matrices: Same.
    :param num_iterations: Same.
    :param confidence_level: Same.
    :raises: ValueError: if number of baseline matrices (input tensors to model)
        != number of trial matrices.
    """

    error_checking.assert_is_integer(num_iterations)
    error_checking.assert_is_geq(num_iterations, 100)
    error_checking.assert_is_geq(confidence_level, 0.)
    error_checking.assert_is_leq(confidence_level, 1.)

    num_baseline_matrices = len(list_of_baseline_matrices)
    num_trial_matrices = len(list_of_trial_matrices)

    if num_baseline_matrices != num_trial_matrices:
        error_string = (
            'Number of baseline matrices ({0:d}) should = number of trial '
            'matrices ({1:d}).'
        ).format(num_baseline_matrices, num_trial_matrices)

        raise ValueError(error_string)

    num_matrices = num_trial_matrices
    num_examples = None

    for i in range(num_matrices):
        error_checking.assert_is_numpy_array(list_of_baseline_matrices[i])

        if num_examples is None:
            num_examples = list_of_baseline_matrices[i].shape[0]

        these_expected_dim = numpy.array(
            (num_examples,) + list_of_baseline_matrices[i].shape[1:], dtype=int
        )

        error_checking.assert_is_numpy_array(
            list_of_baseline_matrices[i], exact_dimensions=these_expected_dim)

        these_expected_dim = numpy.array(
            list_of_baseline_matrices[i].shape, dtype=int)

        error_checking.assert_is_numpy_array(
            list_of_trial_matrices[i], exact_dimensions=these_expected_dim)


def check_output(monte_carlo_dict):
    """Error-checks output from Monte Carlo test.

    :param monte_carlo_dict: Dictionary created by `run_monte_carlo_test`.
    :raises: ValueError: if not (number of trial matrices = number of MIN
        matrices = number of MAX matrices).
    """

    error_checking.assert_is_greater(
        monte_carlo_dict[MAX_PMM_PERCENTILE_KEY], 50.
    )
    error_checking.assert_is_leq(monte_carlo_dict[MAX_PMM_PERCENTILE_KEY], 100.)
    error_checking.assert_is_integer(monte_carlo_dict[NUM_ITERATIONS_KEY])
    error_checking.assert_is_geq(monte_carlo_dict[NUM_ITERATIONS_KEY], 100)
    error_checking.assert_is_geq(monte_carlo_dict[CONFIDENCE_LEVEL_KEY], 0.)
    error_checking.assert_is_leq(monte_carlo_dict[CONFIDENCE_LEVEL_KEY], 1.)

    num_trial_matrices = len(monte_carlo_dict[TRIAL_PMM_MATRICES_KEY])
    num_min_matrices = len(monte_carlo_dict[MIN_MATRICES_KEY])
    num_max_matrices = len(monte_carlo_dict[MAX_MATRICES_KEY])

    if not num_trial_matrices == num_min_matrices == num_max_matrices:
        error_string = (
            'Number of trial ({0:d}), MIN ({1:d}), and MAX ({2:d}) matrices '
            'should be the same.'
        ).format(num_trial_matrices, num_min_matrices, num_max_matrices)

        raise ValueError(error_string)

    num_matrices = num_trial_matrices

    for i in range(num_matrices):
        error_checking.assert_is_numpy_array(
            monte_carlo_dict[TRIAL_PMM_MATRICES_KEY][i]
        )

        these_expected_dim = numpy.array(
            monte_carlo_dict[TRIAL_PMM_MATRICES_KEY][i].shape, dtype=int
        )

        error_checking.assert_is_numpy_array(
            monte_carlo_dict[MIN_MATRICES_KEY][i],
            exact_dimensions=these_expected_dim
        )

        error_checking.assert_is_numpy_array(
{'fraction_non_alphanumeric': 0.08028766800282952, 'fraction_numerical': 0.005659042678613535, 'mean_word_length': 4.013593380614657, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2750045', 'n_tokens_mistral': 3067, 'n_tokens_neox': 2873, 'n_words': 455}