# Path: modality/util/printutil.py
# def print_rank0(comm, str):
#     if comm.Get_rank() == 0:
#         lines = str.split('\n')
#         prefix = '{:02d}: '.format(MPI.COMM_WORLD.Get_rank())
#         str = ('\n'+prefix).join(lines)
#         str = prefix+str
#         print(str)


from mpi4py import MPI
from scipy.stats import binom
from . import print_rank0
import numpy as np
                            batch=5, comm=MPI.COMM_SELF,
                            print_per_batch=False, printing=True):
    N_test_max = 20000
    vals = np.zeros((0,))
    s = "gamma_lower, gamma_upper = {}, {}".format(gamma_lower, gamma_upper)
    while True:
        vals_new_samp = bootstrap(fun_resample, batch, comm=comm)
        vals = np.hstack([vals, vals_new_samp])
        upper_bound_pval = binom.cdf(np.sum(vals), len(vals), gamma_upper)
        lower_bound_pval = 1 - binom.cdf(np.sum(vals)-1, len(vals), gamma_lower)
        s += ("\nnp.mean(vals) = {}".format(np.mean(vals)) +
              "\nlen(vals) = {}".format(len(vals)) +
              "\nupper_bound_pval = {}".format(upper_bound_pval) +
              "\nlower_bound_pval = {}".format(lower_bound_pval))
        if upper_bound_pval < significance_first:
            if lower_bound_pval < significance_second:
                s += '\n===\nin interval\n==='
                print_rank0(comm, s)
                return 'in interval'
            if 1-binom.cdf(int(np.round(np.mean(vals)*N_test_max))-1, N_test_max, gamma_lower) < significance_second:
                batch = len(vals)
                continue  # Expecting less than N_test_max tests to verify lower bound
            s += '\n===\nbelow upper bound\n==='
            if printing:
                print_rank0(comm, s)
            return 'below upper bound'
        if lower_bound_pval < significance_first:
            if upper_bound_pval < significance_second:
                s += '\n===\nin interval\n==='
                if printing:

# Path: modality/util/printutil.py
# def print_rank0(comm, str):
#     if comm.Get_rank() == 0:
#         lines = str.split('\n')
#         prefix = '{:02d}: '.format(MPI.COMM_WORLD.Get_rank())
#         str = ('\n'+prefix).join(lines)
#         str = prefix+str
#         print(str)


# Path: modality/util/bootstrap_MPI.py
from mpi4py import MPI
from scipy.stats import binom
from . import print_rank0
import numpy as np

                            batch=5, comm=MPI.COMM_SELF,
                            print_per_batch=False, printing=True):
    N_test_max = 20000
    vals = np.zeros((0,))
    s = "gamma_lower, gamma_upper = {}, {}".format(gamma_lower, gamma_upper)
    while True:
        vals_new_samp = bootstrap(fun_resample, batch, comm=comm)
        vals = np.hstack([vals, vals_new_samp])
        upper_bound_pval = binom.cdf(np.sum(vals), len(vals), gamma_upper)
        lower_bound_pval = 1 - binom.cdf(np.sum(vals)-1, len(vals), gamma_lower)
        s += ("\nnp.mean(vals) = {}".format(np.mean(vals)) +
              "\nlen(vals) = {}".format(len(vals)) +
              "\nupper_bound_pval = {}".format(upper_bound_pval) +
              "\nlower_bound_pval = {}".format(lower_bound_pval))
        if upper_bound_pval < significance_first:
            if lower_bound_pval < significance_second:
                s += '\n===\nin interval\n==='
                print_rank0(comm, s)
                return 'in interval'
            if 1-binom.cdf(int(np.round(np.mean(vals)*N_test_max))-1, N_test_max, gamma_lower) < significance_second:
                batch = len(vals)
                continue  # Expecting less than N_test_max tests to verify lower bound
            s += '\n===\nbelow upper bound\n==='
            if printing:
                print_rank0(comm, s)
            return 'below upper bound'
        if lower_bound_pval < significance_first:
            if upper_bound_pval < significance_second:
                s += '\n===\nin interval\n==='
                if printing:

                    print_rank0(comm, s)