def test_multivariate_te_init():
    analysis_opts = {'cmi_calc_name': 'jidt_kraskov'}
    max_lag_target = 5
    max_lag_sources = 7
    min_lag_sources = 4
    target = 0
    sources = [2, 3, 4]
    dat = Data()
    dat.generate_mute_data(100, 5)
    nw_0 = Multivariate_te(max_lag_sources, min_lag_sources, max_lag_target,
                           analysis_opts)
    nw_0.analyse_single_target(dat, target, sources)

    # This should just run: Test what happens if the target max lag is bigger
    # than the source max lag
    max_lag_sources = 5
    max_lag_target = 7
    nw_1 = Multivariate_te(max_lag_sources, min_lag_sources, max_lag_target,
                           analysis_opts)
    nw_1.analyse_single_target(dat, target, sources)

    # The following should crash: min lag bigger than max lag
    max_lag_sources = 5
    min_lag_sources = 7
    nw_2 = Multivariate_te(max_lag_sources, min_lag_sources, max_lag_target,
                           analysis_opts)
    with pytest.raises(AssertionError):
        nw_2.analyse_single_target(dat, target, sources)
Exemple #2
0
def test_plot_selected_vars():
    dat = Data()
    dat.generate_mute_data(100, 5)
    max_lag = 5
    min_lag = 4
    analysis_opts = {
        'cmi_calc_name': 'jidt_kraskov',
        'n_perm_max_stat': 25,
        'n_perm_min_stat': 25,
        'n_perm_omnibus': 50,
        'n_perm_max_seq': 50,
    }
    network_analysis = Multivariate_te(max_lag, min_lag, analysis_opts)
    res = network_analysis.analyse_single_target(dat, target=2)
    vis.plot_selected_vars(res)
Exemple #3
0
def test_multivariate_te_lorenz_2():
    """Test multivariate TE estimation on bivariately couled Lorenz systems.

    Run the multivariate TE algorithm on two Lorenz systems with a coupling
    from first to second system with delay u = 45 samples. Both directions are
    analyzed, the algorithm should not find a coupling from system two to one.

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """

    d = np.load(
        os.path.join(os.path.dirname(__file__),
                     'data/lorenz_2_exampledata.npy'))
    dat = Data()
    dat.set_data(d, 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_discrete',
        'discretise_method': 'max_ent',
        'n_perm_max_stat': 21,  # 200
        'n_perm_min_stat': 21,  # 200
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,  # this should be equal to the min stats b/c we
        # reuse the surrogate table from the min stats
    }
    lorenz_analysis = Multivariate_te(max_lag_sources=47,
                                      min_lag_sources=42,
                                      max_lag_target=20,
                                      tau_target=2,
                                      options=analysis_opts)
    # FOR DEBUGGING: add the whole history for k = 20, tau = 2 to the
    # estimation, this makes things faster, b/c these don't have to be
    # tested again.
    analysis_opts['add_conditionals'] = [(1, 44), (1, 42), (1, 40), (1, 38),
                                         (1, 36), (1, 34), (1, 32), (1, 30),
                                         (1, 28)]
    lorenz_analysis = Multivariate_te(max_lag_sources=60,
                                      min_lag_sources=31,
                                      tau_sources=2,
                                      max_lag_target=0,
                                      tau_target=1,
                                      options=analysis_opts)
    # res = lorenz_analysis.analyse_network(dat)
    # res_0 = lorenz_analysis.analyse_single_target(dat, 0)  # no coupling
    # print(res_0)
    res_1 = lorenz_analysis.analyse_single_target(dat, 1)  # coupling
    print(res_1)
Exemple #4
0
def test_multivariate_te_lagged_copies():
    """Test multivariate TE estimation on a lagged copy of random data.

    Run the multivariate TE algorithm on two sets of random data, where the
    second set is a lagged copy of the first. This test should find no
    significant conditionals at all (neither in the target's nor in the
    source's past).

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """
    lag = 3
    d_0 = np.random.rand(1, 1000, 20)
    d_1 = np.hstack((np.random.rand(1, lag, 20), d_0[:, lag:, :]))

    dat = Data()
    dat.set_data(np.vstack((d_0, d_1)), 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_discrete',
        'discretise_method': 'max_ent',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
    }
    random_analysis = Multivariate_te(max_lag_sources=5, options=analysis_opts)
    # Assert that there are no significant conditionals in either direction
    # other than the mandatory single sample in the target's past (which
    # ensures that we calculate a proper TE at any time in the algorithm).
    for target in range(2):
        res = random_analysis.analyse_single_target(dat, target)
        assert (len(res['conditional_full']) == 1), ('Conditional contains '
                                                     'more/less than 1 '
                                                     'variables.')
        assert (not res['conditional_sources']), ('Conditional sources is not '
                                                  'empty.')
        assert (len(res['conditional_target']) == 1), ('Conditional target '
                                                       'contains more/less '
                                                       'than 1 variable.')
        assert (res['cond_sources_pval'] is None), ('Conditional p-value is '
                                                    'not None.')
        assert (res['omnibus_pval'] is None), ('Omnibus p-value is not None.')
        assert (res['omnibus_sign'] is None), ('Omnibus significance is not '
                                               'None.')
        assert (res['conditional_sources_te'] is None), ('Conditional TE '
                                                         'values is not None.')
def test_multivariate_te_lagged_copies():
    """Test multivariate TE estimation on a lagged copy of random data.

    Run the multivariate TE algorithm on two sets of random data, where the
    second set is a lagged copy of the first. This test should find no
    significant conditionals at all (neither in the target's nor in the
    source's past).

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """
    lag = 3
    d_0 = np.random.rand(1, 1000, 20)
    d_1 = np.hstack((np.random.rand(1, lag, 20), d_0[:, lag:, :]))

    dat = Data()
    dat.set_data(np.vstack((d_0, d_1)), 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_discrete',
        'discretise_method': 'max_ent',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
    random_analysis = Multivariate_te(max_lag_sources=5, options=analysis_opts)
    # Assert that there are no significant conditionals in either direction
    # other than the mandatory single sample in the target's past (which
    # ensures that we calculate a proper TE at any time in the algorithm).
    for target in range(2):
        res = random_analysis.analyse_single_target(dat, target)
        assert (len(res['conditional_full']) == 1), ('Conditional contains '
                                                     'more/less than 1 '
                                                     'variables.')
        assert (not res['conditional_sources']), ('Conditional sources is not '
                                                  'empty.')
        assert (len(res['conditional_target']) == 1), ('Conditional target '
                                                       'contains more/less '
                                                       'than 1 variable.')
        assert (res['cond_sources_pval'] is None), ('Conditional p-value is '
                                                    'not None.')
        assert (res['omnibus_pval'] is None), ('Omnibus p-value is not None.')
        assert (res['omnibus_sign'] is None), ('Omnibus significance is not '
                                               'None.')
        assert (res['conditional_sources_te'] is None), ('Conditional TE '
                                                         'values is not None.')
def test_multivariate_te_lorenz_2():
    """Test multivariate TE estimation on bivariately couled Lorenz systems.

    Run the multivariate TE algorithm on two Lorenz systems with a coupling
    from first to second system with delay u = 45 samples. Both directions are
    analyzed, the algorithm should not find a coupling from system two to one.

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """

    d = np.load(os.path.join(os.path.dirname(__file__),
                'data/lorenz_2_exampledata.npy'))
    dat = Data()
    dat.set_data(d, 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_discrete',
        'discretise_method': 'max_ent',
        'n_perm_max_stat': 21,  # 200
        'n_perm_min_stat': 21,  # 200
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,  # this should be equal to the min stats b/c we
                               # reuse the surrogate table from the min stats
        }
    lorenz_analysis = Multivariate_te(max_lag_sources=47, min_lag_sources=42,
                                      max_lag_target=20, tau_target=2,
                                      options=analysis_opts)
    # FOR DEBUGGING: add the whole history for k = 20, tau = 2 to the
    # estimation, this makes things faster, b/c these don't have to be
    # tested again.
    analysis_opts['add_conditionals'] = [(1, 44), (1, 42), (1, 40), (1, 38),
                                         (1, 36), (1, 34), (1, 32), (1, 30),
                                         (1, 28)]
    lorenz_analysis = Multivariate_te(max_lag_sources=60, min_lag_sources=31,
                                      tau_sources=2,
                                      max_lag_target=0, tau_target=1,
                                      options=analysis_opts)
    # res = lorenz_analysis.analyse_network(dat)
    # res_0 = lorenz_analysis.analyse_single_target(dat, 0)  # no coupling
    # print(res_0)
    res_1 = lorenz_analysis.analyse_single_target(dat, 1)  # coupling
    print(res_1)
Exemple #7
0
def test_multivariate_te_random():
    """Test multivariate TE estimation on two random data sets.

    Run the multivariate TE algorithm on two sets of random data with no
    coupling. This test should find no significant conditionals at all (neither
    in the target's nor in the source's past).

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """
    d = np.random.rand(2, 1000, 20)
    dat = Data()
    dat.set_data(d, 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_kraskov',
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
    }
    random_analysis = Multivariate_te(max_lag_sources=5, options=analysis_opts)
    # Assert that there are no significant conditionals in either direction
    # other than the mandatory single sample in the target's past (which
    # ensures that we calculate a proper TE at any time in the algorithm).
    for target in range(2):
        res = random_analysis.analyse_single_target(dat, target)
        assert (len(res['conditional_full']) == 1), ('Conditional contains '
                                                     'more/less than 1 '
                                                     'variables.')
        assert (not res['conditional_sources']), ('Conditional sources is not '
                                                  'empty.')
        assert (len(res['conditional_target']) == 1), ('Conditional target '
                                                       'contains more/less '
                                                       'than 1 variable.')
        assert (res['cond_sources_pval'] is None), ('Conditional p-value is '
                                                    'not None.')
        assert (res['omnibus_pval'] is None), ('Omnibus p-value is not None.')
        assert (res['omnibus_sign'] is None), ('Omnibus significance is not '
                                               'None.')
        assert (res['conditional_sources_te'] is None), ('Conditional TE '
                                                         'values is not None.')
def test_multivariate_te_random():
    """Test multivariate TE estimation on two random data sets.

    Run the multivariate TE algorithm on two sets of random data with no
    coupling. This test should find no significant conditionals at all (neither
    in the target's nor in the source's past).

    Note:
        This test takes several hours and may take one to two days on some
        machines.
    """
    d = np.random.rand(2, 1000, 20)
    dat = Data()
    dat.set_data(d, 'psr')
    analysis_opts = {
        'cmi_calc_name': 'jidt_kraskov',
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
    random_analysis = Multivariate_te(max_lag_sources=5, options=analysis_opts)
    # Assert that there are no significant conditionals in either direction
    # other than the mandatory single sample in the target's past (which
    # ensures that we calculate a proper TE at any time in the algorithm).
    for target in range(2):
        res = random_analysis.analyse_single_target(dat, target)
        assert (len(res['conditional_full']) == 1), ('Conditional contains '
                                                     'more/less than 1 '
                                                     'variables.')
        assert (not res['conditional_sources']), ('Conditional sources is not '
                                                  'empty.')
        assert (len(res['conditional_target']) == 1), ('Conditional target '
                                                       'contains more/less '
                                                       'than 1 variable.')
        assert (res['cond_sources_pval'] is None), ('Conditional p-value is '
                                                    'not None.')
        assert (res['omnibus_pval'] is None), ('Omnibus p-value is not None.')
        assert (res['omnibus_sign'] is None), ('Omnibus significance is not '
                                               'None.')
        assert (res['conditional_sources_te'] is None), ('Conditional TE '
                                                         'values is not None.')
Exemple #9
0
import os
import time
import numpy as np
from idtxl.multivariate_te import Multivariate_te
from idtxl.data import Data

start_time = time.time()
# load simulated data from 2 coupled Lorenz systems 1->2, u = 45 ms
d = np.load(os.path.join(os.path.dirname(__file__),
            'data/lorenz_2_exampledata.npy'))
dat = Data()
dat.set_data(d[:, :, 0:100], 'psr')
analysis_opts = {
        'cmi_calc_name': 'jidt_kraskov',
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
lorenz_analysis = Multivariate_te(max_lag_sources=50, min_lag_sources=40,
                                  max_lag_target=30, tau_sources=1,
                                  tau_target=3, options=analysis_opts)
res_1 = lorenz_analysis.analyse_single_target(dat, 0)
res_2 = lorenz_analysis.analyse_single_target(dat, 1)
runtime = time.time() - start_time
print("---- {0} minutes".format(runtime / 60))

path = os.path.dirname(__file__) + 'output/'
np.savez(path + 'test_lorenz', res_1, res_2)
np.save(path + 'test_lorenz_time', runtime)
Exemple #10
0
def test_multivariate_te_corr_gaussian(estimator=None):
    """Test multivariate TE estimation on correlated Gaussians.

    Run the multivariate TE algorithm on two sets of random Gaussian data with
    a given covariance. The second data set is shifted by one sample creating
    a source-target delay of one sample. This example is modeled after the
    JIDT demo 4 for transfer entropy. The resulting TE can be compared to the
    analytical result (but expect some error in the estimate).

    The simulated delay is 1 sample, i.e., the algorithm should find
    significant TE from sample (0, 1), a sample in process 0 with lag/delay 1.
    The final target sample should always be (1, 1), the mandatory sample at
    lat 1, because there is no memory in the process.

    Note:
        This test runs considerably faster than other system tests.
        This produces strange small values for non-coupled sources.  TODO
    """
    if estimator is None:
        estimator = 'jidt_kraskov'

    n = 1000
    cov = 0.4
    source_1 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    # source_2 = [rn.normalvariate(0, 1) for r in range(n)]  # uncorrelated src
    target = [
        sum(pair)
        for pair in zip([cov * y for y in source_1],
                        [(1 - cov) * y
                         for y in [rn.normalvariate(0, 1) for r in range(n)]])
    ]
    # Cast everything to numpy so the idtxl estimator understands it.
    source_1 = np.expand_dims(np.array(source_1), axis=1)
    # source_2 = np.expand_dims(np.array(source_2), axis=1)
    target = np.expand_dims(np.array(target), axis=1)

    dat = Data(normalise=True)
    dat.set_data(np.vstack((source_1[1:].T, target[:-1].T)), 'ps')
    analysis_opts = {
        'cmi_calc_name': estimator,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
    }
    random_analysis = Multivariate_te(max_lag_sources=5,
                                      min_lag_sources=1,
                                      max_lag_target=5,
                                      options=analysis_opts)
    # res = random_analysis.analyse_network(dat)  # full network
    # utils.print_dict(res)
    res_1 = random_analysis.analyse_single_target(dat, 1)  # coupled direction
    # Assert that there are significant conditionals from the source for target
    # 1. For 500 repetitions I got mean errors of 0.02097686 and 0.01454073 for
    # examples 1 and 2 respectively. The maximum errors were 0.093841 and
    # 0.05833172 repectively. This inspired the following error boundaries.
    expected_res = np.log(1 / (1 - np.power(cov, 2)))
    diff = np.abs(max(res_1['cond_sources_te']) - expected_res)
    print('Expected source sample: (0, 1)\nExpected target sample: (1, 1)')
    print(('Estimated TE: {0:5.4f}, analytical result: {1:5.4f}, error:'
           '{2:2.2f} % ').format(max(res_1['cond_sources_te']), expected_res,
                                 diff / expected_res))
    assert (diff < 0.1), ('Multivariate TE calculation for correlated '
                          'Gaussians failed (error larger 0.1: {0}, expected: '
                          '{1}, actual: {2}).'.format(
                              diff, expected_res, res_1['cond_sources_te']))
def test_multivariate_te_corr_gaussian(estimator=None):
    """Test multivariate TE estimation on correlated Gaussians.

    Run the multivariate TE algorithm on two sets of random Gaussian data with
    a given covariance. The second data set is shifted by one sample creating
    a source-target delay of one sample. This example is modeled after the
    JIDT demo 4 for transfer entropy. The resulting TE can be compared to the
    analytical result (but expect some error in the estimate).

    The simulated delay is 1 sample, i.e., the algorithm should find
    significant TE from sample (0, 1), a sample in process 0 with lag/delay 1.
    The final target sample should always be (1, 1), the mandatory sample at
    lat 1, because there is no memory in the process.

    Note:
        This test runs considerably faster than other system tests.
        This produces strange small values for non-coupled sources.  TODO
    """
    if estimator is None:
        estimator = 'jidt_kraskov'

    n = 1000
    cov = 0.4
    source_1 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    # source_2 = [rn.normalvariate(0, 1) for r in range(n)]  # uncorrelated src
    target = [sum(pair) for pair in zip(
        [cov * y for y in source_1],
        [(1 - cov) * y for y in [rn.normalvariate(0, 1) for r in range(n)]])]
    # Cast everything to numpy so the idtxl estimator understands it.
    source_1 = np.expand_dims(np.array(source_1), axis=1)
    # source_2 = np.expand_dims(np.array(source_2), axis=1)
    target = np.expand_dims(np.array(target), axis=1)

    dat = Data(normalise=True)
    dat.set_data(np.vstack((source_1[1:].T, target[:-1].T)), 'ps')
    analysis_opts = {
        'cmi_calc_name': estimator,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
        }
    random_analysis = Multivariate_te(max_lag_sources=5, min_lag_sources=1,
                                      max_lag_target=5, options=analysis_opts)
    # res = random_analysis.analyse_network(dat)  # full network
    # utils.print_dict(res)
    res_1 = random_analysis.analyse_single_target(dat, 1)  # coupled direction
    # Assert that there are significant conditionals from the source for target
    # 1. For 500 repetitions I got mean errors of 0.02097686 and 0.01454073 for
    # examples 1 and 2 respectively. The maximum errors were 0.093841 and
    # 0.05833172 repectively. This inspired the following error boundaries.
    expected_res = np.log(1 / (1 - np.power(cov, 2)))
    diff = np.abs(max(res_1['cond_sources_te']) - expected_res)
    print('Expected source sample: (0, 1)\nExpected target sample: (1, 1)')
    print(('Estimated TE: {0:5.4f}, analytical result: {1:5.4f}, error:'
           '{2:2.2f} % ').format(max(res_1['cond_sources_te']), expected_res,
                                 diff / expected_res))
    assert (diff < 0.1), ('Multivariate TE calculation for correlated '
                          'Gaussians failed (error larger 0.1: {0}, expected: '
                          '{1}, actual: {2}).'.format(diff,
                                                      expected_res,
                                                      res_1['cond_sources_te']
                                                      ))
Exemple #12
0
"""Plot graph output from multivariate TE estimation.

author: patricia
"""
from idtxl.data import Data
from idtxl.multivariate_te import Multivariate_te
from idtxl import visualise_graph

# Generate some example output
data = Data()
data.generate_mute_data(n_replications=2, n_samples=500)
print('Demo data with {0} procs, {1} samples, {2} reps.'.format(
                data.n_processes, data.n_samples, data.n_replications))
opts = {'cmi_calc_name': 'jidt_kraskov'}
mte = Multivariate_te(max_lag_sources=3, max_lag_target=3,
                      min_lag_sources=1, options=opts)
res_single = mte.analyse_single_target(data=data, target=3)
res_full = mte.analyse_network(data=data)

# generate graph plots
g_single = visualise_graph.plot_selected_vars(res_single, mte)
g_full = visualise_graph.plot_network(res_full)
import time
import numpy as np
from idtxl.multivariate_te import Multivariate_te
from idtxl.data import Data

start_time = time.time()
# load simulated data from 2 coupled Lorenz systems 1->2, u = 45 ms
d = np.load(os.path.join(os.path.dirname(__file__),
            'data/lorenz_2_exampledata.npy'))
dat = Data()
dat.set_data(d[:, :, 0:100], 'psr')
analysis_opts = {
        'cmi_calc_name': 'opencl_kraskov',
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
lorenz_analysis = Multivariate_te(max_lag_sources=50, min_lag_sources=40,
                                  max_lag_target=30, tau_sources=1,
                                  tau_target=3, options=analysis_opts)
res_1 = lorenz_analysis.analyse_single_target(dat, 0)
res_2 = lorenz_analysis.analyse_single_target(dat, 1)
runtime = time.time() - start_time
print("---- {0} minutes".format(runtime / 60))

np.savez('/home/patriciaw/Dropbox/BIC/#idtxl/test/test_lorenz',
         res_1, res_2)
np.save('/home/patriciaw/Dropbox/BIC/#idtxl/test/test_lorenz_time',
        runtime)
Exemple #14
0
def test_multivariate_te_init():
    """Test instance creation for Multivariate_te class."""
    # Test error on missing estimator
    with pytest.raises(KeyError):
        Multivariate_te(max_lag_sources=5,
                        min_lag_sources=3,
                        max_lag_target=7,
                        options={})

    # Test setting of min and max lags
    analysis_opts = {'cmi_calc_name': 'jidt_kraskov'}
    dat = Data()
    dat.generate_mute_data(100, 5)

    # Valid: max lag sources bigger than max lag target
    Multivariate_te(max_lag_sources=5,
                    min_lag_sources=3,
                    max_lag_target=7,
                    options=analysis_opts)

    # Valid: max lag sources smaller than max lag target
    Multivariate_te(max_lag_sources=7,
                    min_lag_sources=3,
                    max_lag_target=5,
                    options=analysis_opts)

    # Invalid: min lag sources bigger than max lag
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=7,
                             min_lag_sources=8,
                             max_lag_target=5,
                             options=analysis_opts)

    # Invalid: taus bigger than lags
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=4,
                             min_lag_sources=2,
                             max_lag_target=5,
                             tau_sources=10,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=4,
                             min_lag_sources=2,
                             max_lag_target=5,
                             tau_target=10,
                             options=analysis_opts)

    # Invalid: negative lags or taus
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=-7,
                             min_lag_sources=-4,
                             max_lag_target=-1,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=1,
                             min_lag_sources=-4,
                             max_lag_target=-1,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=1,
                             min_lag_sources=1,
                             max_lag_target=-1,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=1,
                             min_lag_sources=1,
                             max_lag_target=1,
                             tau_sources=-1,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=1,
                             min_lag_sources=1,
                             max_lag_target=1,
                             tau_target=-1,
                             options=analysis_opts)

    # Invalid: lags or taus are not positive integers
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=3,
                             min_lag_sources=1.5,
                             max_lag_target=5,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=3.5,
                             min_lag_sources=1,
                             max_lag_target=5,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=3,
                             min_lag_sources=-1,
                             max_lag_target=5,
                             options=analysis_opts)
    with pytest.raises(RuntimeError):
        nw = Multivariate_te(max_lag_sources=-1,
                             min_lag_sources=1,
                             max_lag_target=5,
                             options=analysis_opts)

    # Invalid: sources or target is no int
    nw = Multivariate_te(max_lag_sources=3,
                         min_lag_sources=1,
                         max_lag_target=2,
                         options=analysis_opts)
    with pytest.raises(RuntimeError):  # no int
        nw.analyse_single_target(data=dat, target=1.5, sources='all')
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(data=dat, target=-1, sources='all')
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(data=dat, target=10, sources='all')
    with pytest.raises(RuntimeError):  # wrong type
        nw.analyse_single_target(data=dat, target={}, sources='all')
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(data=dat, target=0, sources=-1)
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(data=dat, target=0, sources=[-1])
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(data=dat, target=0, sources=20)
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(data=dat, target=0, sources=[20])

    # Force conditionals
    analysis_opts['add_conditionals'] = [(0, 1), (1, 3)]
    nw = Multivariate_te(max_lag_sources=3,
                         min_lag_sources=1,
                         max_lag_target=3,
                         options=analysis_opts)
    analysis_opts['add_conditionals'] = (8, 0)
    nw = Multivariate_te(max_lag_sources=3,
                         min_lag_sources=1,
                         max_lag_target=3,
                         options=analysis_opts)