# Path: Echobase/Common/errors.py
# def check_dims(arr, nd):
# def check_type(obj, typ):
# def check_function(obj):
# def check_path(path):
# def make_path(path):
# def check_path_overwrite(path):
# def check_has_key(dictionary, key_ref):
# 
# Path: Echobase/Sigproc/ts_surr.py
# def AAFTsur(xV, rand_phase=None):
#     T = np.argsort(xV)
#     T2 = np.argsort(yftV)


import numpy as np
from statsmodels.tsa.api import VAR
from ...Common import errors
from ...Sigproc import ts_surr
    # Standard param checks
    errors.check_type(data, np.ndarray)
    errors.check_dims(data, 2)
    errors.check_type(lagPar, int)
    errors.check_type(signif_value, float)
    errors.check_type(n_perm, int)
    if signif_value < 1.0/n_perm:
        raise Exception('Type I error rate is too large relative to number of permutations')

    # Get data attributes
    n_samp, n_chan = data.shape

    adj = np.zeros((n_chan, n_chan))
    adj_filter = np.zeros((n_chan, n_chan))
    for ii in range(0, n_chan):
        for jj in range(0, n_chan):
            if ii == jj:
                continue

            signal1 = data[:, ii]
            signal2 = data[:, jj]
            signal = np.vstack([signal1, signal2]).T
            model = VAR(signal);
            results = model.fit(lagPar);
            granger_test = results.test_causality('y1', ['y2'], kind='f',
                                                  verbose=False);

            granger_test_scrambled2 = np.zeros(n_perm)
            for perm_i in range(0, n_perm):
                permsignal1 = ts_surr.AAFTsur(signal1)

# Path: Echobase/Common/errors.py
# def check_dims(arr, nd):
# def check_type(obj, typ):
# def check_function(obj):
# def check_path(path):
# def make_path(path):
# def check_path_overwrite(path):
# def check_has_key(dictionary, key_ref):
# 
# Path: Echobase/Sigproc/ts_surr.py
# def AAFTsur(xV, rand_phase=None):
#     T = np.argsort(xV)
#     T2 = np.argsort(yftV)


# Path: Echobase/Network/Functional/causality.py
import numpy as np
from statsmodels.tsa.api import VAR
from ...Common import errors
from ...Sigproc import ts_surr

    # Standard param checks
    errors.check_type(data, np.ndarray)
    errors.check_dims(data, 2)
    errors.check_type(lagPar, int)
    errors.check_type(signif_value, float)
    errors.check_type(n_perm, int)
    if signif_value < 1.0/n_perm:
        raise Exception('Type I error rate is too large relative to number of permutations')

    # Get data attributes
    n_samp, n_chan = data.shape

    adj = np.zeros((n_chan, n_chan))
    adj_filter = np.zeros((n_chan, n_chan))
    for ii in range(0, n_chan):
        for jj in range(0, n_chan):
            if ii == jj:
                continue

            signal1 = data[:, ii]
            signal2 = data[:, jj]
            signal = np.vstack([signal1, signal2]).T
            model = VAR(signal);
            results = model.fit(lagPar);
            granger_test = results.test_causality('y1', ['y2'], kind='f',
                                                  verbose=False);

            granger_test_scrambled2 = np.zeros(n_perm)
            for perm_i in range(0, n_perm):
                permsignal1 = ts_surr.AAFTsur(signal1)

                permsignal2 = ts_surr.AAFTsur(signal2)