# Path: experiments/exp_setup.py
# SDCIT_RESULT_DIR = SDCIT_WORKING_DIR + '/results'
# 
# PARALLEL_JOBS = multiprocessing.cpu_count() // 2
# 
# Path: sdcit/kcipt.py
# def c_KCIPT(Kx, Ky, Kz, Dz, B, b, M, n_jobs=1, seed=None):
#     """Python implementation of KCIPT by Doran et al. to test X _||_ Y | Z
# 
#     Parameters
#     ----------
#     Kx : np.ndarray
#         An N by N kernel matrix of X
#     Ky : np.ndarray
#         An N by N kernel matrix of Y
#     Kz : np.ndarray
#         An N by N kernel matrix of Z
#     Dz : np.ndarray
#         An N by N pairwise distance matrix of Z
#     B : int
#         The number of outer bootstrap
#     b : int
#         The number of inner bootstrap
#     M : int
#         The number of Monte Carlo simulation
#     n_jobs : int
#         The number of threads to be used.
#     seed : int
#         Random seed to be used
# 
#     References
#     ----------
#     Doran, G., Muandet, K., Zhang, K., & Schölkopf, B. (2014). A Permutation-Based Kernel Conditional Independence Test.
#     In Proceedings of the Thirtieth Conference on Uncertainty in Artificial Intelligence (pp. 132–141). Corvallis, Oregon: AUAI Press.
#     """
#     if seed is None:
#         seed = random_seeds()
# 
#     Kx, Ky, Kz, Dz = cythonize(Kx, Ky, Kz, Dz)
#     inner_null = np.zeros((B, b), dtype='float64')
#     outer_null = np.zeros((M,), dtype='float64')
#     mmds = np.zeros((B,), dtype='float64')
# 
#     cy_kcipt(Kx, Ky, Kz, Dz, B, b, inner_null, mmds, seed, n_jobs, outer_null, M)
# 
#     # null correction for test statistic based on a set of unbiased estimates of squared MMD
#     if M > 0:
#         outer_null -= outer_null.mean()
# 
#     test_statistic = mmds.mean()
# 
#     return p_value_of(test_statistic, outer_null) if M > 0 else float('nan'), mmds, inner_null, outer_null


import time
import scipy.io
import scipy.stats
from os.path import exists
from joblib import Parallel
from joblib import delayed
from tqdm import trange
from experiments.exp_setup import SDCIT_RESULT_DIR, PARALLEL_JOBS
from experiments.exp_utils import *
from sdcit.kcipt import c_KCIPT
from sdcit.utils import *
        for independent, N, gamma in chaotic_configs():
            print(independent, N, gamma)
            time.sleep(1)
            outs = Parallel(PARALLEL_JOBS)(delayed(test_chaotic)(independent, gamma, trial, N, 25) for trial in range(300))
            with open(SDCIT_RESULT_DIR + '/kcipt_chaotic.csv', 'a') as f:
                for out in outs:
                    if out is not None:
                        print(*out, sep=',', file=f, flush=True)
    else:
        print('skipping KCIPT on chaotic time series data')

    # #
    if not exists(SDCIT_RESULT_DIR + '/kcipt_postnonlinear.csv'):
        for noise, independent, N in postnonlinear_noise_configs():
            print(noise, independent, N)
            time.sleep(1)
            outs = Parallel(PARALLEL_JOBS)(delayed(test_postnonlinear)(independent, noise, trial, N, 25) for trial in range(300))
            with open(SDCIT_RESULT_DIR + '/kcipt_postnonlinear.csv', 'a') as f:
                for out in outs:
                    print(*out, sep=',', file=f, flush=True)
    else:
        print('skipping KCIPT on post nonlinear noise data')

    independent, gamma, N = 1, '0.0', 400
    for B in [5000, 20000]:
        if not exists(SDCIT_RESULT_DIR + '/kcipt_chaotic_{}.csv'.format(B)):
            print(independent, gamma, N, B)
            time.sleep(1)
            for trial in trange(300):
                out = test_chaotic(independent, gamma, trial, N, B=B, n_jobs=PARALLEL_JOBS)

# Path: experiments/exp_setup.py
# SDCIT_RESULT_DIR = SDCIT_WORKING_DIR + '/results'
# 
# PARALLEL_JOBS = multiprocessing.cpu_count() // 2
# 
# Path: sdcit/kcipt.py
# def c_KCIPT(Kx, Ky, Kz, Dz, B, b, M, n_jobs=1, seed=None):
#     """Python implementation of KCIPT by Doran et al. to test X _||_ Y | Z
# 
#     Parameters
#     ----------
#     Kx : np.ndarray
#         An N by N kernel matrix of X
#     Ky : np.ndarray
#         An N by N kernel matrix of Y
#     Kz : np.ndarray
#         An N by N kernel matrix of Z
#     Dz : np.ndarray
#         An N by N pairwise distance matrix of Z
#     B : int
#         The number of outer bootstrap
#     b : int
#         The number of inner bootstrap
#     M : int
#         The number of Monte Carlo simulation
#     n_jobs : int
#         The number of threads to be used.
#     seed : int
#         Random seed to be used
# 
#     References
#     ----------
#     Doran, G., Muandet, K., Zhang, K., & Schölkopf, B. (2014). A Permutation-Based Kernel Conditional Independence Test.
#     In Proceedings of the Thirtieth Conference on Uncertainty in Artificial Intelligence (pp. 132–141). Corvallis, Oregon: AUAI Press.
#     """
#     if seed is None:
#         seed = random_seeds()
# 
#     Kx, Ky, Kz, Dz = cythonize(Kx, Ky, Kz, Dz)
#     inner_null = np.zeros((B, b), dtype='float64')
#     outer_null = np.zeros((M,), dtype='float64')
#     mmds = np.zeros((B,), dtype='float64')
# 
#     cy_kcipt(Kx, Ky, Kz, Dz, B, b, inner_null, mmds, seed, n_jobs, outer_null, M)
# 
#     # null correction for test statistic based on a set of unbiased estimates of squared MMD
#     if M > 0:
#         outer_null -= outer_null.mean()
# 
#     test_statistic = mmds.mean()
# 
#     return p_value_of(test_statistic, outer_null) if M > 0 else float('nan'), mmds, inner_null, outer_null


# Path: experiments/run_KCIPT.py
import time
import scipy.io
import scipy.stats
from os.path import exists
from joblib import Parallel
from joblib import delayed
from tqdm import trange
from experiments.exp_setup import SDCIT_RESULT_DIR, PARALLEL_JOBS
from experiments.exp_utils import *
from sdcit.kcipt import c_KCIPT
from sdcit.utils import *

        for independent, N, gamma in chaotic_configs():
            print(independent, N, gamma)
            time.sleep(1)
            outs = Parallel(PARALLEL_JOBS)(delayed(test_chaotic)(independent, gamma, trial, N, 25) for trial in range(300))
            with open(SDCIT_RESULT_DIR + '/kcipt_chaotic.csv', 'a') as f:
                for out in outs:
                    if out is not None:
                        print(*out, sep=',', file=f, flush=True)
    else:
        print('skipping KCIPT on chaotic time series data')

    # #
    if not exists(SDCIT_RESULT_DIR + '/kcipt_postnonlinear.csv'):
        for noise, independent, N in postnonlinear_noise_configs():
            print(noise, independent, N)
            time.sleep(1)
            outs = Parallel(PARALLEL_JOBS)(delayed(test_postnonlinear)(independent, noise, trial, N, 25) for trial in range(300))
            with open(SDCIT_RESULT_DIR + '/kcipt_postnonlinear.csv', 'a') as f:
                for out in outs:
                    print(*out, sep=',', file=f, flush=True)
    else:
        print('skipping KCIPT on post nonlinear noise data')

    independent, gamma, N = 1, '0.0', 400
    for B in [5000, 20000]:
        if not exists(SDCIT_RESULT_DIR + '/kcipt_chaotic_{}.csv'.format(B)):
            print(independent, gamma, N, B)
            time.sleep(1)
            for trial in trange(300):
                out = test_chaotic(independent, gamma, trial, N, B=B, n_jobs=PARALLEL_JOBS)

                with open(SDCIT_RESULT_DIR + '/kcipt_chaotic_{}.csv'.format(B), 'a') as f: