コード例 #1
0
def test_multivariate_te_mute():
    """Test multivariate TE estimation on the MUTE example network.

    Test data comes from a network that is used as an example in the paper on
    the MuTE toolbox (Montalto, PLOS ONE, 2014, eq. 14). The network has the
    following (non-linear) couplings:

    0 -> 1, u = 2
    0 -> 2, u = 3
    0 -> 3, u = 2 (non-linear)
    3 -> 4, u = 1
    4 -> 3, u = 1

    The maximum order of any single AR process is never higher than 2.
    """
    data = Data()
    data.generate_mute_data(n_samples=1000, n_replications=10)
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 3,
        'min_lag_sources': 1,
        'max_lag_target': 3,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        '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

    network_analysis = MultivariateTE()
    network_analysis.analyse_network(settings, data, targets=[1, 2])
コード例 #2
0
ファイル: test_multivariate_te.py プロジェクト: dgmurx/IDTxl
def test_multivariate_te_one_realisation_per_replication():
    """Test boundary case of one realisation per replication."""
    # Create a data set where one pattern fits into the time series exactly
    # once, this way, we get one realisation per replication for each variable.
    # This is easyer to assert/verify later. We also test data.get_realisations
    # this way.
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'max_lag_target': 5,
        'max_lag_sources': 5,
        'min_lag_sources': 4
    }
    target = 0
    data = Data(normalise=False)
    n_repl = 10
    n_procs = 2
    n_points = n_procs * (settings['max_lag_sources'] + 1) * n_repl
    data.set_data(
        np.arange(n_points).reshape(n_procs, settings['max_lag_sources'] + 1,
                                    n_repl), 'psr')
    nw_0 = MultivariateTE()
    nw_0._initialise(settings, data, 'all', target)
    assert (not nw_0.selected_vars_full)
    assert (not nw_0.selected_vars_sources)
    assert (not nw_0.selected_vars_target)
    assert ((nw_0._replication_index == np.arange(n_repl)).all())
    assert (nw_0._current_value == (target,
                                    max(settings['max_lag_sources'],
                                        settings['max_lag_target'])))
    assert (nw_0._current_value_realisations[:, 0] == data.data[target,
                                                                -1, :]).all()
コード例 #3
0
ファイル: test_stats.py プロジェクト: razib764/IDTxl
def test_analytical_surrogates():
    # Test generation of analytical surrogates.
    # Generate data and discretise it such that we can use analytical
    # surrogates.
    expected_mi, source1, source2, target = _get_gauss_data(covariance=0.4)
    settings = {'discretise_method': 'equal', 'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings)
    source_dis, target_dis = est._discretise_vars(var1=source1, var2=target)
    data = Data(np.hstack((source_dis, target_dis)),
                dim_order='sp',
                normalise=False)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 100,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'max_lag_target': 5
    }
    nw = MultivariateTE()
    res = nw.analyse_single_target(settings, data, target=1)
    # Check if generation of analytical surrogates is documented in the
    # settings.
    assert res.settings.analytical_surrogates, (
        'Surrogates were not created analytically.')
コード例 #4
0
def test_discrete_input():
    """Test multivariate TE estimation from discrete data."""
    # Generate Gaussian test data
    covariance = 0.4
    data = _get_discrete_gauss_data(covariance=covariance,
                                    n=10000,
                                    delay=1,
                                    normalise=False,
                                    seed=SEED)
    corr_expected = covariance / (
        1 * np.sqrt(covariance**2 + (1-covariance)**2))
    expected_mi = calculate_mi(corr_expected)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'min_lag_sources': 1,
        'max_lag_sources': 2,
        'max_lag_target': 1}
    nw = MultivariateTE()
    res = nw.analyse_single_target(settings=settings, data=data, target=1)
    assert np.isclose(
        res._single_target[1].omnibus_te, expected_mi, atol=0.05), (
            'Estimated TE for discrete variables is not correct. Expected: '
            '{0}, Actual results: {1}.'.format(
                expected_mi, res._single_target[1].omnibus_te))
コード例 #5
0
def test_analytical_surrogates():
    # Generate discrete test data.
    covariance = 0.4
    n = 10000
    delay = 1
    source = np.random.normal(0, 1, size=n)
    target = (covariance * source + (1 - covariance) *
              np.random.normal(0, 1, size=n))
    source = source[delay:]
    target = target[:-delay]
    settings = {'discretise_method': 'equal',
                'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings)
    source_dis, target_dis = est._discretise_vars(var1=source, var2=target)
    data = Data(np.vstack((source_dis, target_dis)),
                dim_order='ps', normalise=False)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 100,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'max_lag_target': 5
        }
    nw = MultivariateTE()
    res = nw.analyse_single_target(settings, data, target=1)
    assert res.settings.analytical_surrogates, (
        'Surrogates were not created analytically.')
コード例 #6
0
ファイル: test_results.py プロジェクト: monperrus/IDTxl
def test_add_single_result():
    """Test adding results for a single target/process."""
    data = _get_discrete_gauss_data()
    nw = MultivariateTE()
    res_network = nw.analyse_single_target(settings=settings,
                                           data=data,
                                           target=1)

    # Test adding target results that already exists
    with pytest.raises(RuntimeError):
        res_network._add_single_result(target=1,
                                       settings=res_network.settings,
                                       results={})
    # Test adding target results with unequal settings
    settings_test = cp.deepcopy(res_network.settings)
    settings_test.add_conditionals = 'Test'
    with pytest.raises(RuntimeError):
        res_network._add_single_result(target=0,
                                       settings=settings_test,
                                       results=res_network._single_target[1])
    # Test adding a target with additional settings, results.settings should be
    # updated
    settings_test = cp.deepcopy(res_network.settings)
    settings_test.new_setting = 'Test'
    res_network._add_single_result(target=0,
                                   settings=settings_test,
                                   results=res_network._single_target[1])
    assert 'new_setting' in res_network.settings.keys(), (
        'Settings dict was not updated.')
    assert res_network.settings.new_setting == 'Test', (
        'Settings dict was not updated correctly.')
コード例 #7
0
def test_checkpoint():
    """Test method for full network analysis."""
    n_processes = 5  # the MuTE network has 5 nodes
    data = Data(seed=SEED)
    data.generate_mute_data(10, 5)
    filename_ckp = './my_checkpoint'
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5,
        'write_ckp': True,
        'filename_ckp': filename_ckp}
    nw_0 = MultivariateTE()

    # Test all to all analysis
    results = nw_0.analyse_network(
        settings, data, targets='all', sources='all')
    targets_analysed = results.targets_analysed
    sources = np.arange(n_processes)
    assert all(np.array(targets_analysed) == np.arange(n_processes)), (
                'Network analysis did not run on all targets.')
    for t in results.targets_analysed:
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(results._single_target[t].sources_tested) == s), (
                    'Network analysis did not run on all sources for target '
                    '{0}'. format(t))
    # Test analysis for subset of targets
    target_list = [1, 2, 3]
    results = nw_0.analyse_network(
        settings, data, targets=target_list, sources='all')
    targets_analysed = results.targets_analysed
    assert all(np.array(targets_analysed) == np.array(target_list)), (
                'Network analysis did not run on correct subset of targets.')
    for t in results.targets_analysed:
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(results._single_target[t].sources_tested) == s), (
                    'Network analysis did not run on all sources for target '
                    '{0}'. format(t))

    # Test analysis for subset of sources
    source_list = [1, 2, 3]
    target_list = [0, 4]
    results = nw_0.analyse_network(settings, data, targets=target_list,
                                   sources=source_list)

    targets_analysed = results.targets_analysed
    assert all(np.array(targets_analysed) == np.array(target_list)), (
                'Network analysis did not run for all targets.')
    for t in results.targets_analysed:
        assert all(results._single_target[t].sources_tested ==
                   np.array(source_list)), (
                        'Network analysis did not run on the correct subset '
                        'of sources for target {0}'.format(t))

    _clear_ckp(filename_ckp)
コード例 #8
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_add_single_result():
    """Test adding results for a single target/process."""
    data = _generate_gauss_data()
    nw = MultivariateTE()
    res_network = nw.analyse_single_target(
        settings=settings, data=data, target=1)

    # Test adding target results that already exists
    with pytest.raises(RuntimeError):
        res_network._add_single_result(target=1,
                                       settings=res_network.settings,
                                       results={})
    # Test adding target results with unequal settings
    settings_test = cp.deepcopy(res_network.settings)
    settings_test.add_conditionals = 'Test'
    with pytest.raises(RuntimeError):
        res_network._add_single_result(target=0,
                                       settings=settings_test,
                                       results=res_network._single_target[1])
    # Test adding a target with additional settings, results.settings should be
    # updated
    settings_test = cp.deepcopy(res_network.settings)
    settings_test.new_setting = 'Test'
    res_network._add_single_result(target=0,
                                   settings=settings_test,
                                   results=res_network._single_target[1])
    assert 'new_setting' in res_network.settings.keys(), (
        'Settings dict was not updated.')
    assert res_network.settings.new_setting == 'Test', (
        'Settings dict was not updated correctly.')
コード例 #9
0
def test_multivariate_te_one_realisation_per_replication():
    """Test boundary case of one realisation per replication."""
    # Create a data set where one pattern fits into the time series exactly
    # once, this way, we get one realisation per replication for each variable.
    # This is easyer to assert/verify later. We also test data.get_realisations
    # this way.
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'max_lag_target': 5,
        'max_lag_sources': 5,
        'min_lag_sources': 4}
    target = 0
    data = Data(normalise=False)
    n_repl = 10
    n_procs = 2
    n_points = n_procs * (settings['max_lag_sources'] + 1) * n_repl
    data.set_data(np.arange(n_points).reshape(
                                        n_procs,
                                        settings['max_lag_sources'] + 1,
                                        n_repl), 'psr')
    nw_0 = MultivariateTE()
    nw_0._initialise(settings, data, 'all', target)
    assert (not nw_0.selected_vars_full)
    assert (not nw_0.selected_vars_sources)
    assert (not nw_0.selected_vars_target)
    assert ((nw_0._replication_index == np.arange(n_repl)).all())
    assert (nw_0._current_value == (target, max(
           settings['max_lag_sources'], settings['max_lag_target'])))
    assert (nw_0._current_value_realisations[:, 0] ==
            data.data[target, -1, :]).all()
コード例 #10
0
def test_visualise_multivariate_te():
    """Visualise output of multivariate TE estimation."""
    data = Data()
    data.generate_mute_data(100, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'n_perm_max_stat': 25,
        'n_perm_min_stat': 25,
        'n_perm_omnibus': 50,
        'n_perm_max_seq': 50,
    }
    network_analysis = MultivariateTE()
    results = network_analysis.analyse_network(settings,
                                               data,
                                               targets=[0, 1, 2])
    # generate graph plots
    visualise_graph.plot_selected_vars(results, target=1, sign_sources=False)
    plt.show()
    visualise_graph.plot_network(results, fdr=False)
    plt.show()
    visualise_graph.plot_network(results, fdr=True)
    plt.show()
    visualise_graph.plot_selected_vars(results, target=1, sign_sources=True)
    plt.show()
コード例 #11
0
def te(mdf1):
    import numpy as np
    from idtxl.multivariate_te import MultivariateTE
    from idtxl.data import Data
    n_procs = 1
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'n_perm_max_stat': 21,
        'max_lag_target': 5,
        'max_lag_sources': 5,
        'min_lag_sources': 4
    }
    settings['cmi_estimator'] = 'JidtDiscreteCMI'
    binary_trains = []
    for spiketrain in mdf1.spiketrains:
        x = conv.BinnedSpikeTrain(spiketrain,
                                  binsize=5 * pq.ms,
                                  t_start=0 * pq.s)
        binary_trains.append(x.to_array())
    print(binary_trains)
    dat = Data(np.array(binary_trains), dim_order='spr')
    dat.n_procs = n_procs
    settings = {
        'cmi_estimator': 'JidtKraskov',
        'max_lag_sources': 3,
        'max_lag_target': 3,
        'min_lag_sources': 1
    }
    print(dat)
    mte = MultivariateTE()
    res_full = mte.analyse_network(settings=settings, data=dat)

    # generate graph plots
    g_single = visualise_graph.plot_selected_vars(res_single, mte)
    g_full = visualise_graph.plot_network(res_full)
コード例 #12
0
def test_visualise_multivariate_te():
    """Visualise output of multivariate TE estimation."""
    data = Data()
    data.generate_mute_data(100, 5)
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'n_perm_max_stat': 25,
        'n_perm_min_stat': 25,
        'n_perm_omnibus': 50,
        'n_perm_max_seq': 50,
        }
    network_analysis = MultivariateTE()
    results = network_analysis.analyse_network(settings, data,
                                               targets=[0, 1, 2])
    # generate graph plots
    visualise_graph.plot_selected_vars(results, target=1, sign_sources=False)
    plt.show()
    visualise_graph.plot_network(results, fdr=False)
    plt.show()
    visualise_graph.plot_network(results, fdr=True)
    plt.show()
    visualise_graph.plot_selected_vars(results, target=1, sign_sources=True)
    plt.show()
コード例 #13
0
def test_multivariate_te_mute():
    """Test multivariate TE estimation on the MUTE example network.

    Test data comes from a network that is used as an example in the paper on
    the MuTE toolbox (Montalto, PLOS ONE, 2014, eq. 14). The network has the
    following (non-linear) couplings:

    0 -> 1, u = 2
    0 -> 2, u = 3
    0 -> 3, u = 2 (non-linear)
    3 -> 4, u = 1
    4 -> 3, u = 1

    The maximum order of any single AR process is never higher than 2.
    """
    data = Data()
    data.generate_mute_data(n_samples=1000, n_replications=10)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 3,
        'min_lag_sources': 1,
        'max_lag_target': 3,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        '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

    network_analysis = MultivariateTE()
    network_analysis.analyse_network(settings, data, targets=[1, 2])
コード例 #14
0
def test_analyse_network():
    """Test method for full network analysis."""
    n_processes = 5  # the MuTE network has 5 nodes
    data = Data()
    data.generate_mute_data(10, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5}
    nw_0 = MultivariateTE()

    # Test all to all analysis
    results = nw_0.analyse_network(
        settings, data, targets='all', sources='all')
    targets_analysed = results.targets_analysed
    sources = np.arange(n_processes)
    assert all(np.array(targets_analysed) == np.arange(n_processes)), (
                'Network analysis did not run on all targets.')
    for t in results.targets_analysed:
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(results._single_target[t].sources_tested) == s), (
                    'Network analysis did not run on all sources for target '
                    '{0}'. format(t))
    # Test analysis for subset of targets
    target_list = [1, 2, 3]
    results = nw_0.analyse_network(
        settings, data, targets=target_list, sources='all')
    targets_analysed = results.targets_analysed
    assert all(np.array(targets_analysed) == np.array(target_list)), (
                'Network analysis did not run on correct subset of targets.')
    for t in results.targets_analysed:
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(results._single_target[t].sources_tested) == s), (
                    'Network analysis did not run on all sources for target '
                    '{0}'. format(t))

    # Test analysis for subset of sources
    source_list = [1, 2, 3]
    target_list = [0, 4]
    results = nw_0.analyse_network(settings, data, targets=target_list,
                                   sources=source_list)

    targets_analysed = results.targets_analysed
    assert all(np.array(targets_analysed) == np.array(target_list)), (
                'Network analysis did not run for all targets.')
    for t in results.targets_analysed:
        assert all(results._single_target[t].sources_tested ==
                   np.array(source_list)), (
                        'Network analysis did not run on the correct subset '
                        'of sources for target {0}'.format(t))
コード例 #15
0
def test_max_statistic_sequential():
    data = Data()
    data.generate_mute_data(104, 10)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'max_lag_target': 5
        }
    setup = MultivariateTE()
    setup._initialise(settings, data, sources=[0, 1], target=2)
    setup.current_value = (0, 4)
    setup.selected_vars_sources = [(1, 1), (1, 2)]
    setup.selected_vars_full = [(0, 1), (1, 1), (1, 2)]
    setup._selected_vars_realisations = np.random.rand(
                                    data.n_realisations(setup.current_value),
                                    len(setup.selected_vars_full))
    setup._current_value_realisations = np.random.rand(
                                    data.n_realisations(setup.current_value),
                                    1)
    [sign, p, te] = stats.max_statistic_sequential(analysis_setup=setup,
                                                   data=data)
コード例 #16
0
def test_plot_network():
    """Test results class for multivariate TE network inference."""
    covariance = 0.4
    n = 10000
    delay = 1
    normalisation = False
    source = np.random.normal(0, 1, size=n)
    target_1 = (covariance * source +
                (1 - covariance) * np.random.normal(0, 1, size=n))
    target_2 = (covariance * source +
                (1 - covariance) * np.random.normal(0, 1, size=n))
    source = source[delay:]
    target_1 = target_1[:-delay]
    target_2 = target_2[:-delay]

    # Discretise data for speed
    settings_dis = {'discretise_method': 'equal', 'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings_dis)
    source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1)
    source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2)

    data = Data(np.vstack((source_dis, target_1_dis, target_2_dis)),
                dim_order='ps',
                normalise=normalisation)

    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'min_lag_sources': 1,
        'max_lag_sources': 2,
        'max_lag_target': 1,
        'alpha_fdr': 0.5
    }
    nw = MultivariateTE()

    # Analyse a single target and the whole network
    res_single = nw.analyse_single_target(settings=settings,
                                          data=data,
                                          target=1)
    res_network = nw.analyse_network(settings=settings, data=data)
    graph, fig = plot_network(res_single, 'max_te_lag', fdr=False)
    plt.close(fig)
    graph, fig = plot_network(res_network, 'max_te_lag', fdr=False)
    plt.close(fig)
    for sign_sources in [True, False]:
        graph, fig = plot_selected_vars(res_network,
                                        target=1,
                                        sign_sources=True,
                                        fdr=False)
        plt.close(fig)
def test_multivariate_te_mute():
    """Test multivariate TE estimation on the MUTE example network.

    Test data comes from a network that is used as an example in the paper on
    the MuTE toolbox (Montalto, PLOS ONE, 2014, eq. 14). The network has the
    following (non-linear) couplings:

    0 -> 1, u = 2
    0 -> 2, u = 3
    0 -> 3, u = 2 (non-linear)
    3 -> 4, u = 1
    4 -> 3, u = 1

    The maximum order of any single AR process is never higher than 2.
    """
    data = Data()
    data.generate_mute_data(n_samples=1000, n_replications=10)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'max_ent',
        'max_lag_sources': 3,
        'min_lag_sources': 1,
        'max_lag_target': 3,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        '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
        }

    network_analysis = MultivariateTE()
    results_me = network_analysis.analyse_network(settings, data,
                                                  targets=[1, 2])
    settings['discretise_method'] = 'equal'
    results_eq = network_analysis.analyse_network(settings, data,
                                                  targets=[1, 2])

    for t in [1, 2]:
        print('Target {0}: equal binning: {1}, max. ent. binning: {2}'.format(
            t,
            results_eq.get_single_target(t, fdr=False).omnibus_te,
            results_me.get_single_target(t, fdr=False).omnibus_te
        ))
        # Skip comparison of estimates if analyses returned different source
        # sets. This will always lead to different estimates.
        if (results_eq.get_single_target(t, fdr=False).selected_vars_sources ==
                results_me.get_single_target(t, fdr=False).selected_vars_sources):
            assert (np.isclose(
                results_eq.get_single_target(1, fdr=False).omnibus_te,
                results_me.get_single_target(1, fdr=False).omnibus_te,
                rtol=0.05)), ('Target {0}: unequl results for both binning '
                              'methods.'.format(t))
        else:
            continue
コード例 #18
0
ファイル: test_idtxl_io.py プロジェクト: monperrus/IDTxl
def test_export_networkx():
    """Test export to networkx DiGrap() object."""
    # Test export of graph with unconnected nodes.
    max_lag = 3
    data = Data(seed=SEED)
    data.generate_mute_data(500, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'noise_level': 0,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': max_lag,
        'min_lag_sources': 1,
        'max_lag_target': max_lag
    }
    target = 3
    sources = [0, 4]
    te = MultivariateTE()
    results = te.analyse_single_target(settings,
                                       data,
                                       target=target,
                                       sources=sources)
    weights = 'binary'
    adj_matrix = results.get_adjacency_matrix(weights=weights, fdr=False)
    digraph = io.export_networkx_graph(adjacency_matrix=adj_matrix,
                                       weights=weights)
    np.testing.assert_array_equal(np.sort(digraph.nodes),
                                  np.arange(data.n_processes),
                                  err_msg='Wrong nodes in exported DiGraph.')

    # raise AssertionError('Test not yet implemented.')
    # Test export of networx graph for network inference results.
    weights = 'binary'
    adj_matrix = res_0.get_adjacency_matrix(weights=weights, fdr=False)
    io.export_networkx_graph(adjacency_matrix=adj_matrix, weights=weights)

    # Test export of source graph
    for s in [True, False]:
        io.export_networkx_source_graph(results=res_0,
                                        target=1,
                                        sign_sources=s,
                                        fdr=False)

    # Test export of networx graph for network comparison results.
    for weight in ['union', 'comparison', 'pvalue', 'diff_abs']:
        adj_matrix = res_within.get_adjacency_matrix(weights=weight)
        io.export_networkx_graph(adjacency_matrix=adj_matrix, weights=weight)
        for s in [True, False]:
            io.export_networkx_source_graph(results=res_0,
                                            target=1,
                                            sign_sources=s,
                                            fdr=False)
コード例 #19
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_combine_results():
    """Test combination of results objects."""
    data = _generate_gauss_data()
    nw = MultivariateTE()
    res_network_1 = nw.analyse_network(settings=settings, data=data)

    # Test error for unequal settings
    res_network_2 = cp.deepcopy(res_network_1)
    res_network_2.settings.add_conditionals = 'Test'
    with pytest.raises(RuntimeError):
        res_network_1.combine_results(res_network_2)
コード例 #20
0
ファイル: test_results.py プロジェクト: monperrus/IDTxl
def test_combine_results():
    """Test combination of results objects."""
    data = _get_discrete_gauss_data()
    nw = MultivariateTE()
    res_network_1 = nw.analyse_network(settings=settings, data=data)

    # Test error for unequal settings
    res_network_2 = cp.deepcopy(res_network_1)
    res_network_2.settings.add_conditionals = 'Test'
    with pytest.raises(RuntimeError):
        res_network_1.combine_results(res_network_2)
コード例 #21
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_pickle_results():
    """Test pickling results objects."""
    data = _generate_gauss_data()
    nw = MultivariateTE()
    res_single = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    res_network = nw.analyse_network(settings=settings, data=data)

    outfile = TemporaryFile()
    pickle.dump(res_single, outfile)
    pickle.dump(res_network, outfile)
コード例 #22
0
def test_pickle_results():
    """Test pickling results objects."""
    data = _generate_gauss_data()
    nw = MultivariateTE()
    res_single = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    res_network = nw.analyse_network(settings=settings, data=data)

    outfile = TemporaryFile()
    pickle.dump(res_single, outfile)
    pickle.dump(res_network, outfile)
コード例 #23
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:, :]))

    data = Data()
    data.set_data(np.vstack((d_0, d_1)), 'psr')
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'max_ent',
        'max_lag_sources': 5,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
    }
    random_analysis = MultivariateTE()
    # 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 t in range(2):
        results = random_analysis.analyse_single_target(settings, data, t)
        assert len(results.get_single_target(
            t, fdr=False).selected_vars_full) == 1, (
                'Conditional contains more/less than 1 variables.')
        assert not results.get_single_target(
            t, fdr=False).selected_vars_sources.size, (
                'Conditional sources is not empty.')
        assert len(
            results.get_single_target(
                t, fdr=False).selected_vars_target) == 1, (
                    'Conditional target contains more/less than 1 variable.')
        assert results.get_single_target(
            t, fdr=False).selected_sources_pval is None, (
                'Conditional p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_pval is None, (
            'Omnibus p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_sign is None, (
            'Omnibus significance is not None.')
        assert results.get_single_target(
            t, fdr=False).selected_sources_te is None, (
                'Conditional TE values is not None.')
コード例 #24
0
def test_console_output():
    dat = Data()
    dat.generate_mute_data(n_samples=10, n_replications=5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5
    }
    nw = MultivariateTE()
    r = nw.analyse_network(settings, dat, targets='all', sources='all')
    print_res_to_console(dat, r, fdr=False)
コード例 #25
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_console_output():
    data = Data()
    data.generate_mute_data(n_samples=10, n_replications=5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5
        }
    nw = MultivariateTE()
    r = nw.analyse_network(settings, data, targets='all', sources='all')
    r.print_edge_list(fdr=False, weights='binary')
コード例 #26
0
ファイル: test_results.py プロジェクト: monperrus/IDTxl
def test_console_output():
    data = Data()
    data.generate_mute_data(n_samples=10, n_replications=5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5
    }
    nw = MultivariateTE()
    r = nw.analyse_network(settings, data, targets='all', sources='all')
    r.print_edge_list(fdr=False, weights='binary')
コード例 #27
0
def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 5
    data = Data()
    data.generate_mute_data(500, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'local_values': True,  # request calculation of local values
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': max_lag,
        'min_lag_sources': 4,
        'max_lag_target': max_lag
    }
    target = 1
    te = MultivariateTE()
    results = te.analyse_network(settings, data, targets=[target])

    # Test if any sources were inferred. If not, return (this may happen
    # sometimes due to too few samples, however, a higher no. samples is not
    # feasible for a unit test).
    if results.get_single_target(target, fdr=False)['te'] is None:
        return

    lte = results.get_single_target(target, fdr=False)['te']
    n_sources = len(results.get_target_sources(target, fdr=False))
    assert type(lte) is np.ndarray, (
        'LTE estimation did not return an array of values: {0}'.format(lte))
    assert lte.shape[0] == n_sources, (
        'Wrong dim (no. sources) in LTE estimate: {0}'.format(lte.shape))
    assert lte.shape[1] == data.n_realisations_samples(
        (0, max_lag)), ('Wrong dim (no. samples) in LTE estimate: {0}'.format(
            lte.shape))
    assert lte.shape[2] == data.n_replications, (
        'Wrong dim (no. replications) in LTE estimate: {0}'.format(lte.shape))

    # Test for correctnes of single link TE estimation by comparing it to the
    # omnibus TE. In this case (single source), the two should be the same.
    settings['local_values'] = False
    results_avg = te.analyse_network(settings, data, targets=[target])
    if results_avg.get_single_target(target, fdr=False)['te'] is None:
        return
    te_single_link = results_avg.get_single_target(target, fdr=False)['te'][0]
    te_omnibus = results_avg.get_single_target(target, fdr=False)['omnibus_te']
    assert np.isclose(te_single_link, te_omnibus), (
        'Single link TE is not equal to omnibus information transfer.')
    # Compare mean local TE to average TE.
    assert np.isclose(
        te_single_link,
        np.mean(lte)), ('Single link average TE and mean LTE deviate.')
コード例 #28
0
def analyse_discrete_data():
    """Run network inference on discrete data."""
    data = generate_discrete_data()
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'min_lag_sources': 1,
        'max_lag_sources': 3,
        'max_lag_target': 1}

    nw = MultivariateTE()
    res = nw.analyse_network(settings=settings, data=data)
    pickle.dump(res, open('{0}discrete_results_mte_{1}.p'.format(
        path, settings['cmi_estimator']), 'wb'))

    nw = BivariateTE()
    res = nw.analyse_network(settings=settings, data=data)
    pickle.dump(res, open('{0}discrete_results_bte_{1}.p'.format(
        path, settings['cmi_estimator']), 'wb'))

    nw = MultivariateMI()
    res = nw.analyse_network(settings=settings, data=data)
    pickle.dump(res, open('{0}discrete_results_mmi_{1}.p'.format(
        path, settings['cmi_estimator']), 'wb'))

    nw = BivariateMI()
    res = nw.analyse_network(settings=settings, data=data)
    pickle.dump(res, open('{0}discrete_results_bmi_{1}.p'.format(
        path, settings['cmi_estimator']), 'wb'))
コード例 #29
0
def test_define_candidates():
    """Test candidate definition from a list of procs and a list of samples."""
    target = 1
    tau_target = 3
    max_lag_target = 10
    current_val = (target, 10)
    procs = [target]
    samples = np.arange(current_val[1] - 1, current_val[1] - max_lag_target,
                        -tau_target)
    nw = MultivariateTE()
    candidates = nw._define_candidates(procs, samples)
    assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
    assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
    assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
コード例 #30
0
def test_define_candidates():
    """Test candidate definition from a list of procs and a list of samples."""
    target = 1
    tau_target = 3
    max_lag_target = 10
    current_val = (target, 10)
    procs = [target]
    samples = np.arange(current_val[1] - 1, current_val[1] - max_lag_target,
                        -tau_target)
    nw = MultivariateTE()
    candidates = nw._define_candidates(procs, samples)
    assert (1, 9) in candidates, 'Sample missing from candidates: (1, 9).'
    assert (1, 6) in candidates, 'Sample missing from candidates: (1, 6).'
    assert (1, 3) in candidates, 'Sample missing from candidates: (1, 3).'
コード例 #31
0
def analyse_mute_te_data():
    # Generate example data: the following was ran once to generate example
    # data, which is now in the data sub-folder of the test-folder.
    data = Data()
    data.generate_mute_data(100, 5)
    # analysis settings
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 50,
        'n_perm_min_stat': 50,
        'n_perm_omnibus': 200,
        'n_perm_max_seq': 50,
        'max_lag_target': 5,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'permute_in_time': True
    }
    # network inference for individual data sets
    nw_0 = MultivariateTE()
    res_0 = nw_0.analyse_network(settings, data, targets=[0, 1], sources='all')
    pickle.dump(res_0, open(path + 'mute_results_0.p', 'wb'))
    res_1 = nw_0.analyse_network(settings, data, targets=[1, 2], sources='all')
    pickle.dump(res_1, open(path + 'mute_results_1.p', 'wb'))
    res_2 = nw_0.analyse_network(settings, data, targets=[0, 2], sources='all')
    pickle.dump(res_2, open(path + 'mute_results_2.p', 'wb'))
    res_3 = nw_0.analyse_network(settings,
                                 data,
                                 targets=[0, 1, 2],
                                 sources='all')
    pickle.dump(res_3, open(path + 'mute_results_3.p', 'wb'))
    res_4 = nw_0.analyse_network(settings, data, targets=[1, 2], sources='all')
    pickle.dump(res_4, open(path + 'mute_results_4.p', 'wb'))
    res_5 = nw_0.analyse_network(settings, data)
    pickle.dump(res_5, open(path + 'mute_results_full.p', 'wb'))
コード例 #32
0
def analyse_continuous_data():
    """Run network inference on continuous data."""
    data = generate_continuous_data()
    settings = {
        'min_lag_sources': 1,
        'max_lag_sources': 3,
        'max_lag_target': 1}

    nw = MultivariateTE()
    for estimator in ['JidtGaussianCMI', 'JidtKraskovCMI']:
        settings['cmi_estimator'] = estimator
        res = nw.analyse_network(settings=settings, data=data)
        pickle.dump(res, open('{0}continuous_results_mte_{1}.p'.format(
            path, estimator), 'wb'))

    nw = BivariateTE()
    for estimator in ['JidtGaussianCMI', 'JidtKraskovCMI']:
        settings['cmi_estimator'] = estimator
        res = nw.analyse_network(settings=settings, data=data)
        pickle.dump(res, open('{0}continuous_results_bte_{1}.p'.format(
            path, estimator), 'wb'))

    nw = MultivariateMI()
    for estimator in ['JidtGaussianCMI', 'JidtKraskovCMI']:
        settings['cmi_estimator'] = estimator
        res = nw.analyse_network(settings=settings, data=data)
        pickle.dump(res, open('{0}continuous_results_mmi_{1}.p'.format(
            path, estimator), 'wb'))

    nw = BivariateMI()
    for estimator in ['JidtGaussianCMI', 'JidtKraskovCMI']:
        settings['cmi_estimator'] = estimator
        res = nw.analyse_network(settings=settings, data=data)
        pickle.dump(res, open('{0}continuous_results_bmi_{1}.p'.format(
            path, estimator), 'wb'))
コード例 #33
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)
    data = Data()
    data.set_data(d, 'psr')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
    }
    random_analysis = MultivariateTE()
    # 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 t in range(2):
        results = random_analysis.analyse_single_target(settings, data, t)
        assert len(results.get_single_target(
            t, fdr=False).selected_vars_full) == 1, (
                'Conditional contains more/less than 1 variables.')
        assert not results.get_single_target(
            t, fdr=False).selected_vars_sources, (
                'Conditional sources is not empty.')
        assert len(
            results.get_single_target(
                t, fdr=False).selected_vars_target) == 1, (
                    'Conditional target contains more/less than 1 variable.')
        assert results.get_single_target(
            t, fdr=False).selected_sources_pval is None, (
                'Conditional p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_pval is None, (
            'Omnibus p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_sign is None, (
            'Omnibus significance is not None.')
        assert results.get_single_target(
            t, fdr=False).selected_sources_te is None, (
                'Conditional TE values is not None.')
コード例 #34
0
def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 5
    data = Data()
    data.generate_mute_data(500, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'local_values': True,  # request calculation of local values
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': max_lag,
        'min_lag_sources': 4,
        'max_lag_target': max_lag}
    target = 1
    te = MultivariateTE()
    results = te.analyse_network(settings, data, targets=[target])

    # Test if any sources were inferred. If not, return (this may happen
    # sometimes due to too few samples, however, a higher no. samples is not
    # feasible for a unit test).
    if results.get_single_target(target, fdr=False)['te'] is None:
        return

    lte = results.get_single_target(target, fdr=False)['te']
    n_sources = len(results.get_target_sources(target, fdr=False))
    assert type(lte) is np.ndarray, (
        'LTE estimation did not return an array of values: {0}'.format(lte))
    assert lte.shape[0] == n_sources, (
        'Wrong dim (no. sources) in LTE estimate: {0}'.format(lte.shape))
    assert lte.shape[1] == data.n_realisations_samples((0, max_lag)), (
        'Wrong dim (no. samples) in LTE estimate: {0}'.format(lte.shape))
    assert lte.shape[2] == data.n_replications, (
        'Wrong dim (no. replications) in LTE estimate: {0}'.format(lte.shape))

    # Test for correctnes of single link TE estimation by comparing it to the
    # omnibus TE. In this case (single source), the two should be the same.
    settings['local_values'] = False
    results_avg = te.analyse_network(settings, data, targets=[target])
    if results_avg.get_single_target(target, fdr=False)['te'] is None:
        return
    te_single_link = results_avg.get_single_target(target, fdr=False)['te'][0]
    te_omnibus = results_avg.get_single_target(target, fdr=False)['omnibus_te']
    assert np.isclose(te_single_link, te_omnibus), (
        'Single link TE is not equal to omnibus information transfer.')
    # Compare mean local TE to average TE.
    assert np.isclose(te_single_link, np.mean(lte)), (
        'Single link average TE and mean LTE deviate.')
コード例 #35
0
def test_multivariate_te_mute():
    """Test multivariate TE estimation on the MUTE example network.

    Test data comes from a network that is used as an example in the paper on
    the MuTE toolbox (Montalto, PLOS ONE, 2014, eq. 14). The network has the
    following (non-linear) couplings:

    0 -> 1, u = 2
    0 -> 2, u = 3
    0 -> 3, u = 2 (non-linear)
    3 -> 4, u = 1
    4 -> 3, u = 1

    The maximum order of any single AR process is never higher than 2.
    """
    data = Data()
    data.generate_mute_data(n_samples=1000, n_replications=10)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'max_ent',
        'max_lag_sources': 3,
        'min_lag_sources': 1,
        'max_lag_target': 3,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        '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
    }

    network_analysis = MultivariateTE()
    results_me = network_analysis.analyse_network(settings,
                                                  data,
                                                  targets=[1, 2])
    settings['discretise_method'] = 'equal'
    results_eq = network_analysis.analyse_network(settings,
                                                  data,
                                                  targets=[1, 2])

    assert (np.isclose(
        results_eq.get_single_target(1, fdr=False).omnibus_te,
        results_me.get_single_target(1, fdr=False).omnibus_te,
        rtol=0.05)), (
            'TE into first target is not equal for both binning methods.')
    assert (np.isclose(
        results_eq.get_single_target(2, fdr=False).omnibus_te,
        results_me.get_single_target(2, fdr=False).omnibus_te,
        rtol=0.05)), (
            'TE into second target is not equal for both binning methods.')
コード例 #36
0
def test_plot_selected_vars():
    dat = Data()
    dat.generate_mute_data(100, 5)
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'n_perm_max_stat': 25,
        'n_perm_min_stat': 25,
        'n_perm_omnibus': 50,
        'n_perm_max_seq': 50,
        }
    network_analysis = MultivariateTE()
    res = network_analysis.analyse_single_target(settings, dat, target=2)
    vis.plot_selected_vars(res)
コード例 #37
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.
    """
    # 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, 'psr')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 47,
        'min_lag_sources': 42,
        'max_lag_target': 20,
        'tau_target': 2,
        '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 = MultivariateTE()
    # 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.
    settings['add_conditionals'] = [(1, 44), (1, 42), (1, 40), (1, 38),
                                    (1, 36), (1, 34), (1, 32), (1, 30),
                                    (1, 28)]
    # res = lorenz_analysis.analyse_network(settings, dat)
    # res_0 = lorenz_analysis.analyse_single_target(settings, dat, 0)  # no coupling
    # print(res_0)

    settings['max_lag_sources'] = 60
    settings['min_lag_sources'] = 31
    settings['tau_sources'] = 2
    settings['max_lag_target'] = 0
    settings['tau_target'] = 1

    res_1 = lorenz_analysis.analyse_single_target(settings, dat, 1)  # coupling
    print(res_1)
コード例 #38
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.
    """
    # 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'))
    data = Data()
    data.set_data(d, 'psr')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag_sources': 47,
        'min_lag_sources': 42,
        'max_lag_target': 20,
        'tau_target': 2,
        '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 = MultivariateTE()
    # 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. Note conditionals are specified using lags.
    settings['add_conditionals'] = [(1, 19), (1, 17), (1, 15), (1, 13),
                                    (1, 11), (1, 9), (1, 7), (1, 5), (1, 3),
                                    (1, 1)]

    settings['max_lag_sources'] = 60
    settings['min_lag_sources'] = 31
    settings['tau_sources'] = 2
    settings['max_lag_target'] = 1
    settings['tau_target'] = 1

    # Just analyse the direction of coupling
    results = lorenz_analysis.analyse_single_target(settings, data, target=1)
    print(results._single_target)
    adj_matrix = results.get_adjacency_matrix(weights='binary', fdr=False)
    adj_matrix.print_matrix()
コード例 #39
0
ファイル: test_multivariate_te.py プロジェクト: dgmurx/IDTxl
def test_gauss_data():
    """Test multivariate TE estimation from correlated Gaussians."""
    # Generate data and add a delay one one sample.
    expected_mi, source, source_uncorr, target = _get_gauss_data()
    source = source[1:]
    source_uncorr = source_uncorr[1:]
    target = target[:-1]
    data = Data(np.hstack((source, source_uncorr, target)),
                dim_order='sp',
                normalise=False)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': 2,
        'min_lag_sources': 1
    }
    nw = MultivariateTE()
    results = nw.analyse_single_target(settings,
                                       data,
                                       target=2,
                                       sources=[0, 1])
    te = results.get_single_target(2, fdr=False)['te'][0]
    sources = results.get_target_sources(2, fdr=False)

    # Assert that only the correlated source was detected.
    assert len(sources) == 1, 'Wrong no. inferred sources: {0}.'.format(
        len(sources))
    assert sources[0] == 0, 'Wrong inferred source: {0}.'.format(sources[0])
    # Compare BivarateMI() estimate to JIDT estimate. Mimick realisations used
    # internally by the algorithm.
    est = JidtKraskovTE({
        'history_target': 1,
        'history_source': 1,
        'source_target_delay': 1,
        'normalise': False
    })
    jidt_cmi = est.estimate(source=source, target=target)
    print('Estimated MI: {0:0.6f}, estimated MI using JIDT core estimator: '
          '{1:0.6f} (expected: {2:0.6f}).'.format(te, jidt_cmi, expected_mi))
    assert np.isclose(te, jidt_cmi, atol=0.005), (
        'Estimated MI {0:0.6f} differs from JIDT estimate {1:0.6f} (expected: '
        'MI {2:0.6f}).'.format(te, jidt_cmi, expected_mi))
    assert np.isclose(te, expected_mi, atol=0.05), (
        'Estimated TE {0:0.6f} differs from expected TE {1:0.6f}.'.format(
            te, expected_mi))
コード例 #40
0
def test_faes_method():
    """Check if the Faes method is working."""
    settings = {'cmi_estimator': 'JidtKraskovCMI',
                'add_conditionals': 'faes',
                'max_lag_sources': 5,
                'min_lag_sources': 3,
                'max_lag_target': 7}
    nw_1 = MultivariateTE()
    data = Data(seed=SEED)
    data.generate_mute_data()
    sources = [1, 2, 3]
    target = 0
    nw_1._initialise(settings, data, sources, target)
    assert (nw_1._selected_vars_sources ==
            [i for i in it.product(sources, [nw_1.current_value[1]])]), (
                'Did not add correct additional conditioning vars.')
コード例 #41
0
def test_visualise_multivariate_te():
    """Visualise output of multivariate TE estimation."""
    dat = Data()
    dat.generate_mute_data(100, 5)
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'n_perm_max_stat': 25,
        'n_perm_min_stat': 25,
        'n_perm_omnibus': 50,
        'n_perm_max_seq': 50,
        }
    network_analysis = MultivariateTE()
    res = network_analysis.analyse_network(settings, dat, targets=[0, 1, 2])
    vis.plot_network(res)
コード例 #42
0
def test_add_conditional_manually():
    """Adda variable that is not in the data set."""
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'add_conditionals': (8, 0),
        'max_lag_sources': 5,
        'min_lag_sources': 3,
        'max_lag_target': 7
    }
    nw_1 = MultivariateTE()
    dat = Data()
    dat.generate_mute_data()
    sources = [1, 2, 3]
    target = 0
    with pytest.raises(IndexError):
        nw_1._initialise(settings, dat, sources, target)
コード例 #43
0
def test_faes_method():
    """Check if the Faes method is working."""
    settings = {'cmi_estimator': 'JidtKraskovCMI',
                'add_conditionals': 'faes',
                'max_lag_sources': 5,
                'min_lag_sources': 3,
                'max_lag_target': 7}
    nw_1 = MultivariateTE()
    data = Data()
    data.generate_mute_data()
    sources = [1, 2, 3]
    target = 0
    nw_1._initialise(settings, data, sources, target)
    assert (nw_1._selected_vars_sources ==
            [i for i in it.product(sources, [nw_1.current_value[1]])]), (
                'Did not add correct additional conditioning vars.')
コード例 #44
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.
    """
    # 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'))
    data = Data()
    data.set_data(d, 'psr')
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 47,
        'min_lag_sources': 42,
        'max_lag_target': 20,
        'tau_target': 2,
        '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 = MultivariateTE()
    # 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. Note conditionals are specified using lags.
    settings['add_conditionals'] = [(1, 19), (1, 17), (1, 15), (1, 13),
                                    (1, 11), (1, 9), (1, 7), (1, 5), (1, 3),
                                    (1, 1)]

    settings['max_lag_sources'] = 60
    settings['min_lag_sources'] = 31
    settings['tau_sources'] = 2
    settings['max_lag_target'] = 1
    settings['tau_target'] = 1

    # Just analyse the direction of coupling
    results = lorenz_analysis.analyse_single_target(settings, data, target=1)
    print(results._single_target)
    print(results.get_adjacency_matrix('binary'))
コード例 #45
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:, :]))

    data = Data()
    data.set_data(np.vstack((d_0, d_1)), 'psr')
    settings = {
        'cmi_estimator':  'JidtKraskovCMI',
        'max_lag_sources': 5,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
    random_analysis = MultivariateTE()
    # 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 t in range(2):
        results = random_analysis.analyse_single_target(settings, data, t)
        assert len(results.get_single_target(t, fdr=False).selected_vars_full) == 1, (
                    'Conditional contains more/less than 1 variables.')
        assert not results.get_single_target(t, fdr=False).selected_vars_sources.size, (
                    'Conditional sources is not empty.')
        assert len(results.get_single_target(t, fdr=False).selected_vars_target) == 1, (
            'Conditional target contains more/less than 1 variable.')
        assert results.get_single_target(t, fdr=False).selected_sources_pval is None, (
            'Conditional p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_pval is None, (
            'Omnibus p-value is not None.')
        assert results.get_single_target(t, fdr=False).omnibus_sign is None, (
            'Omnibus significance is not None.')
        assert results.get_single_target(t, fdr=False).selected_sources_te is None, (
            'Conditional TE values is not None.')
コード例 #46
0
def test_permute_time():
    """Create surrogates by permuting data in time instead of over replic."""
    # Test if perm type is set to default
    default = 'random'
    data = Data()
    data.generate_mute_data(10, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 21,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5,
        'permute_in_time': True}
    nw_0 = MultivariateTE()
    results = nw_0.analyse_network(
        settings, data, targets='all', sources='all')
    assert results.settings.perm_type == default, (
        'Perm type was not set to default.')
コード例 #47
0
def test_add_conditional_manually():
    """Enforce the conditioning on additional variables."""
    settings = {'cmi_estimator': 'JidtKraskovCMI',
                'max_lag_sources': 5,
                'min_lag_sources': 3,
                'max_lag_target': 7}
    nw = MultivariateTE()
    data = Data()
    data.generate_mute_data()

    # Add a conditional with a lag bigger than the max_lag requested above
    settings['add_conditionals'] = (8, 0)
    with pytest.raises(IndexError):
        nw._initialise(settings, data, sources=[1, 2], target=0)

    # Add valid conditionals and test if they were added
    settings['add_conditionals'] = [(0, 1), (1, 3)]
    nw._initialise(settings=settings, data=data, target=0, sources=[1, 2])
    # Get list of conditionals after intialisation and convert absolute samples
    # back to lags for comparison.
    cond_list = nw._idx_to_lag(nw.selected_vars_full)
    assert settings['add_conditionals'][0] in cond_list, (
        'First enforced conditional is missing from results.')
    assert settings['add_conditionals'][1] in cond_list, (
        'Second enforced conditional is missing from results.')
コード例 #48
0
def test_discrete_input():
    """Test multivariate TE estimation from discrete data."""
    # Generate Gaussian test data
    covariance = 0.4
    n = 10000
    delay = 1
    source = np.random.normal(0, 1, size=n)
    target = (covariance * source + (1 - covariance) *
              np.random.normal(0, 1, size=n))
    corr_expected = covariance / (1 * np.sqrt(covariance**2 + (1-covariance)**2))
    expected_mi = calculate_mi(corr_expected)
    source = source[delay:]
    target = target[:-delay]

    # Discretise data
    settings = {'discretise_method': 'equal',
                'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings)
    source_dis, target_dis = est._discretise_vars(var1=source, var2=target)
    data = Data(np.vstack((source_dis, target_dis)),
                dim_order='ps', normalise=False)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'min_lag_sources': 1,
        'max_lag_sources': 2,
        'max_lag_target': 1}
    nw = MultivariateTE()
    res = nw.analyse_single_target(settings=settings, data=data, target=1)
    assert np.isclose(
        res._single_target[1].omnibus_te, expected_mi, atol=0.05), (
            'Estimated TE for discrete variables is not correct. Expected: '
            '{0}, Actual results: {1}.'.format(
                expected_mi, res._single_target[1].omnibus_te))
コード例 #49
0
def test_multivariate_te_multiple_runs():
    """Test TE estimation using multiple runs on the GPU.

    Test if data is correctly split over multiple runs, if the problem size
    exceeds the GPU global memory and thus requires multiple runs. Using a
    number of permutations of 7000 requires two runs on a GPU with global
    memory of about 6 GB.
    """
    data = Data()
    data.generate_mute_data(n_samples=1000, n_replications=10)
    settings = {
        'cmi_estimator': 'OpenCLKraskovCMI',
        'max_lag_sources': 3,
        'min_lag_sources': 1,
        'max_lag_target': 3,
        'n_perm_max_stat': 7000,
        'n_perm_min_stat': 7000,
        '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

    network_analysis = MultivariateTE()
    network_analysis.analyse_network(settings, data, targets=[1, 2])
コード例 #50
0
def analyse_mute_te_data():
    # Generate example data: the following was ran once to generate example
    # data, which is now in the data sub-folder of the test-folder.
    data = Data()
    data.generate_mute_data(100, 5)
    # analysis settings
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 50,
        'n_perm_min_stat': 50,
        'n_perm_omnibus': 200,
        'n_perm_max_seq': 50,
        'max_lag_target': 5,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'permute_in_time': True
        }
    # network inference for individual data sets
    nw_0 = MultivariateTE()
    res_0 = nw_0.analyse_network(
        settings, data, targets=[0, 1], sources='all')
    pickle.dump(res_0, open(path + 'mute_results_0.p', 'wb'))
    res_1 = nw_0.analyse_network(
        settings, data,  targets=[1, 2], sources='all')
    pickle.dump(res_1, open(path + 'mute_results_1.p', 'wb'))
    res_2 = nw_0.analyse_network(
        settings, data,  targets=[0, 2], sources='all')
    pickle.dump(res_2, open(path + 'mute_results_2.p', 'wb'))
    res_3 = nw_0.analyse_network(
        settings, data,  targets=[0, 1, 2], sources='all')
    pickle.dump(res_3, open(path + 'mute_results_3.p', 'wb'))
    res_4 = nw_0.analyse_network(
        settings, data,  targets=[1, 2], sources='all')
    pickle.dump(res_4, open(path + 'mute_results_4.p', 'wb'))
    res_5 = nw_0.analyse_network(settings, data)
    pickle.dump(res_5, open(path + 'mute_results_full.p', 'wb'))
コード例 #51
0
from idtxl.multivariate_te import MultivariateTE
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'))
data = Data()
data.set_data(d[:, :, 0:100], 'psr')
settings = {
        'cmi_estimator':  'OpenCLKraskovCMI',
        'max_lag_sources': 50,
        'min_lag_sources': 40,
        'max_lag_target': 30,
        'tau_sources': 1,
        'tau_target': 3,
        'n_perm_max_stat': 200,
        'n_perm_min_stat': 200,
        'n_perm_omnibus': 500,
        'n_perm_max_seq': 500,
        }
lorenz_analysis = MultivariateTE()
res_0 = lorenz_analysis.analyse_single_target(settings, data, 0)
res_1 = lorenz_analysis.analyse_single_target(settings, data, 1)
runtime = time.time() - start_time
print("---- {0} minutes".format(runtime / 60))

path = '{0}data/'.format(os.path.dirname(__file__))
pickle.dump(res_0, open('{0}test_lorenz_opencl_res_{1}'.format(path, 0), 'wb'))
pickle.dump(res_1, open('{0}test_lorenz_opencl_res_{1}'.format(path, 1), 'wb'))
コード例 #52
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_results_network_inference():
    """Test results class for multivariate TE network inference."""
    covariance = 0.4
    n = 10000
    delay = 1
    normalisation = False
    source = np.random.normal(0, 1, size=n)
    target_1 = (covariance * source + (1 - covariance) *
                np.random.normal(0, 1, size=n))
    target_2 = (covariance * source + (1 - covariance) *
                np.random.normal(0, 1, size=n))
    corr_expected = covariance / (
        1 * np.sqrt(covariance**2 + (1-covariance)**2))
    expected_mi = calculate_mi(corr_expected)
    source = source[delay:]
    target_1 = target_1[:-delay]
    target_2 = target_2[:-delay]

    # Discretise data for speed
    settings_dis = {'discretise_method': 'equal',
                    'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings_dis)
    source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1)
    source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2)
    data = Data(np.vstack((source_dis, target_1_dis, target_2_dis)),
                dim_order='ps', normalise=normalisation)

    nw = MultivariateTE()
    # TE - single target
    res_single_multi_te = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    # TE whole network
    res_network_multi_te = nw.analyse_network(settings=settings, data=data)

    nw = BivariateTE()
    # TE - single target
    res_single_biv_te = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    # TE whole network
    res_network_biv_te = nw.analyse_network(settings=settings, data=data)

    nw = MultivariateMI()
    # TE - single target
    res_single_multi_mi = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    # TE whole network
    res_network_multi_mi = nw.analyse_network(settings=settings, data=data)

    nw = BivariateMI()
    # TE - single target
    res_single_biv_mi = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    # TE whole network
    res_network_biv_mi = nw.analyse_network(settings=settings, data=data)

    res_te = [res_single_multi_te, res_network_multi_te, res_single_biv_te,
              res_network_biv_te]
    res_mi = [res_single_multi_mi, res_network_multi_mi, res_single_biv_mi,
              res_network_biv_mi]
    res_all = res_te + res_mi

    # Check estimated values
    for res in res_te:
        est_te = res._single_target[1].omnibus_te
        assert np.isclose(est_te, expected_mi, atol=0.05), (
            'Estimated TE for discrete variables is not correct. Expected: '
            '{0}, Actual results: {1}.'.format(expected_mi, est_te))
    for res in res_mi:
        est_mi = res._single_target[1].omnibus_mi
        assert np.isclose(est_mi, expected_mi, atol=0.05), (
            'Estimated TE for discrete variables is not correct. Expected: '
            '{0}, Actual results: {1}.'.format(expected_mi, est_mi))

    est_te = res_network_multi_te._single_target[2].omnibus_te
    assert np.isclose(est_te, expected_mi, atol=0.05), (
        'Estimated TE for discrete variables is not correct. Expected: {0}, '
        'Actual results: {1}.'.format(expected_mi, est_te))
    est_mi = res_network_multi_mi._single_target[2].omnibus_mi
    assert np.isclose(est_mi, expected_mi, atol=0.05), (
        'Estimated TE for discrete variables is not correct. Expected: {0}, '
        'Actual results: {1}.'.format(expected_mi, est_mi))

    # Check data parameters in results objects
    n_nodes = 3
    n_realisations = n - delay - max(
        settings['max_lag_sources'], settings['max_lag_target'])
    for res in res_all:
        assert res.data_properties.n_nodes == n_nodes, 'Incorrect no. nodes.'
        assert res.data_properties.n_nodes == n_nodes, 'Incorrect no. nodes.'
        assert res.data_properties.n_realisations == n_realisations, (
            'Incorrect no. realisations.')
        assert res.data_properties.n_realisations == n_realisations, (
            'Incorrect no. realisations.')
        assert res.data_properties.normalised == normalisation, (
            'Incorrect value for data normalisation.')
        assert res.data_properties.normalised == normalisation, (
            'Incorrect value for data normalisation.')
        adj_matrix = res.get_adjacency_matrix('binary', fdr=False)
        assert adj_matrix.shape[0] == n_nodes, (
            'Incorrect number of rows in adjacency matrix.')
        assert adj_matrix.shape[1] == n_nodes, (
            'Incorrect number of columns in adjacency matrix.')
        assert adj_matrix.shape[0] == n_nodes, (
            'Incorrect number of rows in adjacency matrix.')
        assert adj_matrix.shape[1] == n_nodes, (
            'Incorrect number of columns in adjacency matrix.')
コード例 #53
0
ファイル: test_results.py プロジェクト: SimonStreicher/IDTxl
def test_delay_reconstruction():
    """Test the reconstruction of information transfer delays from results."""
    covariance = 0.4
    corr_expected = covariance / (
        1 * np.sqrt(covariance**2 + (1-covariance)**2))
    expected_mi = calculate_mi(corr_expected)
    n = 10000
    delay_1 = 1
    delay_2 = 3
    delay_3 = 5
    normalisation = False
    source = np.random.normal(0, 1, size=n)
    target_1 = (covariance * source + (1 - covariance) *
                np.random.normal(0, 1, size=n))
    target_2 = (covariance * source + (1 - covariance) *
                np.random.normal(0, 1, size=n))
    target_3 = (covariance * source + (1 - covariance) *
                np.random.normal(0, 1, size=n))
    source = source[delay_3:]
    target_1 = target_1[(delay_3-delay_1):-delay_1]
    target_2 = target_2[(delay_3-delay_2):-delay_2]
    target_3 = target_3[:-delay_3]

    # Discretise data for speed
    settings_dis = {'discretise_method': 'equal',
                    'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings_dis)
    source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1)
    source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2)
    source_dis, target_3_dis = est._discretise_vars(var1=source, var2=target_3)
    data = Data(
        np.vstack((source_dis, target_1_dis, target_2_dis, target_3_dis)),
        dim_order='ps', normalise=normalisation)

    nw = MultivariateTE()
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables analysed
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'min_lag_sources': 1,
        'max_lag_sources': delay_3 + 1,
        'max_lag_target': 1}

    res_network = nw.analyse_single_target(
        settings=settings, data=data, target=1)
    res_network.combine_results(nw.analyse_single_target(
        settings=settings, data=data, target=2))
    res_network.combine_results(nw.analyse_single_target(
        settings=settings, data=data, target=3))
    adj_mat = res_network.get_adjacency_matrix('max_te_lag', fdr=False)
    print(adj_mat)
    assert adj_mat[0, 1] == delay_1, ('Estimate for delay 1 is not correct.')
    assert adj_mat[0, 2] == delay_2, ('Estimate for delay 2 is not correct.')
    assert adj_mat[0, 3] == delay_3, ('Estimate for delay 3 is not correct.')

    for target in range(1, 4):
        est_mi = res_network._single_target[target].omnibus_te
        assert np.isclose(est_mi, expected_mi, atol=0.05), (
            'Estimated TE for target {0} is not correct. Expected: {1}, '
            'Actual results: {2}.'.format(target, expected_mi, est_mi))
コード例 #54
0
def test_check_source_set():
    """Test the method _check_source_set.

    This method sets the list of source processes from which candidates are
    taken for multivariate TE estimation.
    """
    data = Data()
    data.generate_mute_data(100, 5)
    nw_0 = MultivariateTE()
    nw_0.settings = {'verbose': True}
    # Add list of sources.
    sources = [1, 2, 3]
    nw_0._check_source_set(sources, data.n_processes)
    assert nw_0.source_set == sources, 'Sources were not added correctly.'

    # Assert that initialisation fails if the target is also in the source list
    sources = [0, 1, 2, 3]
    nw_0.target = 0
    with pytest.raises(RuntimeError):
        nw_0._check_source_set(sources=[0, 1, 2, 3],
                               n_processes=data.n_processes)

    # Test if a single source, no list is added correctly.
    sources = 1
    nw_0._check_source_set(sources, data.n_processes)
    assert (type(nw_0.source_set) is list)

    # Test if 'all' is handled correctly
    nw_0.target = 0
    nw_0._check_source_set('all', data.n_processes)
    assert nw_0.source_set == [1, 2, 3, 4], 'Sources were not added correctly.'

    # Test invalid inputs.
    with pytest.raises(RuntimeError):   # sources greater than no. procs
        nw_0._check_source_set(8, data.n_processes)
    with pytest.raises(RuntimeError):  # negative value as source
        nw_0._check_source_set(-3, data.n_processes)
コード例 #55
0
ファイル: test_stats.py プロジェクト: SimonStreicher/IDTxl
def test_network_fdr():
    settings = {'n_perm_max_seq': 1000, 'n_perm_omnibus': 1000}
    target_0 = {
        'selected_vars_sources': [(1, 1), (1, 2), (1, 3), (2, 1), (2, 0)],
        'selected_vars_full': [(0, 1), (0, 2), (0, 3), (1, 1), (1, 2), (1, 3),
                               (2, 1), (2, 0)],
        'omnibus_pval': 0.0001,
        'omnibus_sign': True,
        'selected_sources_pval': np.array([0.001, 0.0014, 0.01, 0.045, 0.047]),
        'selected_sources_te': np.array([1.1, 1.0, 0.8, 0.7, 0.63]),
        }
    target_1 = {
        'selected_vars_sources': [(1, 2), (2, 1), (2, 2)],
        'selected_vars_full': [(1, 0), (1, 1), (1, 2), (2, 1), (2, 2)],
        'omnibus_pval': 0.031,
        'omnibus_sign': True,
        'selected_sources_pval': np.array([0.00001, 0.00014, 0.01]),
        'selected_sources_te': np.array([1.8, 1.75, 0.75]),
        }
    target_2 = {
        'selected_vars_sources': [],
        'selected_vars_full': [(2, 0), (2, 1)],
        'omnibus_pval': 0.41,
        'omnibus_sign': False,
        'selected_sources_pval': None,
        'selected_sources_te': np.array([]),
        }
    res_1 = ResultsNetworkInference(
        n_nodes=3, n_realisations=1000, normalised=True)
    res_1._add_single_result(target=0, settings=settings, results=target_0)
    res_1._add_single_result(target=1, settings=settings, results=target_1)
    res_2 = ResultsNetworkInference(
        n_nodes=3, n_realisations=1000, normalised=True)
    res_2._add_single_result(target=2, settings=settings, results=target_2)

    for correct_by_target in [True, False]:
        settings = {
            'cmi_estimator': 'JidtKraskovCMI',
            'alpha_fdr': 0.05,
            'max_lag_sources': 3,
            'min_lag_sources': 1,
            'max_lag_target': 3,
            'correct_by_target': correct_by_target}
        data = Data()
        data.generate_mute_data(n_samples=100, n_replications=3)
        analysis_setup = MultivariateTE()
        analysis_setup._initialise(settings=settings, data=data,
                                   sources=[1, 2], target=0)
        res_pruned = stats.network_fdr(settings, res_1, res_2)
        assert (not res_pruned._single_target[2].selected_vars_sources), (
            'Target 2 has not been pruned from results.')

        for k in res_pruned.targets_analysed:
            if res_pruned._single_target[k]['selected_sources_pval'] is None:
                assert (
                    not res_pruned._single_target[k]['selected_vars_sources'])
            else:
                assert (
                    len(res_pruned._single_target[k]['selected_vars_sources']) ==
                    len(res_pruned._single_target[k]['selected_sources_pval'])), (
                        'Source list and list of p-values should have '
                        'the same length.')

    # Test function call for single result
    res_pruned = stats.network_fdr(settings, res_1)
    print('successful call on single result dict.')

    # Test None result for insufficient no. permutations, no FDR-corrected
    # results (the results class throws an error if no FDR-corrected results
    # exist).
    res_1.settings['n_perm_max_seq'] = 2
    res_2.settings['n_perm_max_seq'] = 2
    res_pruned = stats.network_fdr(settings, res_1, res_2)
    with pytest.raises(RuntimeError):
        res_pruned.get_adjacency_matrix('binary', fdr=True)
コード例 #56
0
def test_multivariate_te_init():
    """Test instance creation for MultivariateTE class."""
    # Test error on missing estimator
    settings = {
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'max_lag_sources': 7,
        'min_lag_sources': 2,
        'max_lag_target': 5}
    nw = MultivariateTE()
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=Data(), target=1)

    # Test setting of min and max lags
    settings['cmi_estimator'] = 'JidtKraskovCMI'
    data = Data()
    data.generate_mute_data(n_samples=10, n_replications=5)

    # Valid: max lag sources bigger than max lag target
    nw.analyse_single_target(settings=settings, data=data, target=1)

    # Valid: max lag sources smaller than max lag target
    settings['max_lag_sources'] = 3
    nw.analyse_single_target(settings=settings, data=data, target=1)

    # Invalid: min lag sources bigger than max lag
    settings['min_lag_sources'] = 8
    settings['max_lag_sources'] = 7
    settings['max_lag_target'] = 5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)

    # Invalid: taus bigger than lags
    settings['min_lag_sources'] = 2
    settings['max_lag_sources'] = 4
    settings['max_lag_target'] = 5
    settings['tau_sources'] = 10
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = 10
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)

    # Invalid: negative lags or taus
    settings['min_lag_sources'] = 1
    settings['max_lag_target'] = 5
    settings['max_lag_sources'] = -7
    settings['tau_target'] = 1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['max_lag_sources'] = 7
    settings['min_lag_sources'] = -4
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['min_lag_sources'] = 4
    settings['max_lag_target'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['max_lag_target'] = 5
    settings['tau_sources'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)

    # Invalid: lags or taus are no integers
    settings['tau_target'] = 1
    settings['min_lag_sources'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['min_lag_sources'] = 1
    settings['max_lag_sources'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['max_lag_sources'] = 7
    settings['tau_sources'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=data, target=1)
    settings['tau_target'] = 1

    # Invalid: sources or target is no int
    with pytest.raises(RuntimeError):  # no int
        nw.analyse_single_target(settings=settings, data=data, target=1.5)
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(settings=settings, data=data, target=-1)
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings, data=data, target=10)
    with pytest.raises(RuntimeError):  # wrong type
        nw.analyse_single_target(settings=settings, data=data, target={})
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(settings=settings, data=data, target=0,
                                 sources=-1)
    with pytest.raises(RuntimeError):   # negative
        nw.analyse_single_target(settings=settings, data=data, target=0,
                                 sources=[-1])
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings, data=data, target=0,
                                 sources=20)
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings, data=data, target=0,
                                 sources=[20])
コード例 #57
0
# Import classes
from idtxl.multivariate_te import MultivariateTE
from idtxl.data import Data
from idtxl.visualise_graph import plot_network
import matplotlib.pyplot as plt

# a) Generate test data
data = Data()
data.generate_mute_data(n_samples=1000, n_replications=5)

# b) Initialise analysis object and define settings
network_analysis = MultivariateTE()
settings = {'cmi_estimator': 'JidtGaussianCMI',
            'max_lag_sources': 5,
            'min_lag_sources': 1}

# c) Run analysis
results = network_analysis.analyse_network(settings=settings, data=data)

# d) Plot inferred network to console and via matplotlib
results.print_edge_list(weights='max_te_lag', fdr=False)
plot_network(results=results, weights='max_te_lag', fdr=False)
plt.show()
コード例 #58
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 = 'JidtKraskovCMI'

    n = 1000
    cov = 0.4
    source = [rn.normalvariate(0, 1) for r in range(n)]
    target = [sum(pair) for pair in zip(
        [cov * y for y in source],
        [(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 = np.expand_dims(np.array(source), axis=1)
    target = np.expand_dims(np.array(target), axis=1)

    data = Data(normalise=True)
    data.set_data(np.vstack((source[1:].T, target[:-1].T)), 'ps')
    settings = {
        'cmi_estimator': estimator,
        'max_lag_sources': 5,
        'min_lag_sources': 1,
        'max_lag_target': 5,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_omnibus': 21,
        'n_perm_max_seq': 21,
        }
    random_analysis = MultivariateTE()
    results = random_analysis.analyse_single_target(settings, data, 1)

    # 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.
    corr_expected = cov / (1 * np.sqrt(cov**2 + (1-cov)**2))
    expected_res = calculate_mi(corr_expected)
    estimated_res = results.get_single_target(1, fdr=False).omnibus_te
    diff = np.abs(estimated_res - 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(
               estimated_res, 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, estimated_res))