def test_bivariate_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
    dat = Data(normalise=False)
    n_repl = 10
    n_procs = 2
    n_points = n_procs * (settings['max_lag_sources'] + 1) * n_repl
    dat.set_data(
        np.arange(n_points).reshape(n_procs, settings['max_lag_sources'] + 1,
                                    n_repl), 'psr')
    nw_0 = BivariateTE()
    nw_0._initialise(settings, dat, '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] == dat.data[target,
                                                               -1, :]).all()
def test_bivariate_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 = BivariateTE()
    nw._initialise(settings, data, 'all', target)
    assert (not nw.selected_vars_full)
    assert (not nw.selected_vars_sources)
    assert (not nw.selected_vars_target)
    assert ((nw._replication_index == np.arange(n_repl)).all())
    assert (nw._current_value == (target, max(
           settings['max_lag_sources'], settings['max_lag_target'])))
    assert (nw._current_value_realisations[:, 0] ==
            data.data[target, -1, :]).all()
Exemple #3
0
def test_discrete_input():
    """Test bivariate 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 = BivariateTE()
    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))
Exemple #4
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': 30,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5
    }
    nw = BivariateTE()

    # Test all to all analysis
    results = nw.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 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.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 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.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))
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_max_seq': 21,
        'n_perm_omnibus': 30,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5}
    nw = BivariateTE()

    # Test all to all analysis
    results = nw.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 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.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 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.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))
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 = BivariateTE()
    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).'
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 = BivariateTE()
    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).'
Exemple #8
0
def test_gauss_data():
    """Test bivariate TE estimation from correlated Gaussians."""
    # Generate data and add a delay one one sample.
    expected_mi, source, source_uncorr, target = _get_gauss_data(seed=SEED)
    source = source[1:]
    source_uncorr = source_uncorr[1:]
    target = target[:-1]
    data = Data(np.hstack((source, source_uncorr, target)),
                dim_order='sp',
                normalise=False,
                seed=SEED)
    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': 1,
        'min_lag_sources': 1,
        'max_lag_target': 1
    }
    nw = BivariateTE()
    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 BivarateTE() estimate to JIDT estimate.
    est = JidtKraskovTE({
        'history_target': 1,
        'history_source': 1,
        'source_target_delay': 1,
        'normalise': False
    })
    jidt_cmi = est.estimate(source=source, target=target)
    print('Estimated TE: {0:0.6f}, estimated TE 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 TE {0:0.6f} differs from JIDT estimate {1:0.6f} (expected: '
        'TE {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))
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 = BivariateTE()
    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.')
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 = BivariateTE()
    dat = Data()
    dat.generate_mute_data()
    sources = [1, 2, 3]
    target = 0
    with pytest.raises(IndexError):
        nw_1._initialise(settings, dat, sources, target)
def getAnalysisClass(methodname):
    # Initialise analysis object
    if methodname == "BivariateMI": return BivariateMI()
    elif methodname == "MultivariateMI": return MultivariateMI()
    elif methodname == "BivariateTE": return BivariateTE()
    elif methodname == "MultivariateTE": return MultivariateTE()
    else:
        raise ValueError("Unexpected method", methodname)
Exemple #12
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 = BivariateTE()
    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.analyse_single_target(settings=settings, data=data, 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.')
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 = BivariateTE()
    dat = Data()
    dat.generate_mute_data()
    sources = [1, 2, 3]
    target = 0
    nw_1._initialise(settings, dat, 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.')
Exemple #14
0
def test_gauss_data():
    """Test bivariate 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')
    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,
        'max_lag_target': 1
    }
    nw = BivariateTE()
    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 BivarateTE() estimate to JIDT estimate.
    current_value = (2, 2)
    source_vars = results.get_single_target(2, False)['selected_vars_sources']
    target_vars = results.get_single_target(2, False)['selected_vars_target']
    var1 = data.get_realisations(current_value, source_vars)[0]
    var2 = data.get_realisations(current_value, [current_value])[0]
    cond = data.get_realisations(current_value, target_vars)[0]
    est = JidtKraskovCMI({})
    jidt_cmi = est.estimate(var1=var1, var2=var2, conditional=cond)
    print('Estimated TE: {0:0.6f}, estimated TE 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 TE {0:0.6f} differs from JIDT estimate {1:0.6f} (expected: '
        'TE {2:0.6f}).'.format(te, jidt_cmi, expected_mi))
Exemple #15
0
def test_mute_data():
    """Test estimation from MuTE data."""
    max_lag = 5
    data = Data()
    data.generate_mute_data(200, 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': max_lag,
        'min_lag_sources': 1,
        'add_conditionals': [(1, 3), (1, 2)],
        'max_lag_target': max_lag
    }
    target = 2
    te = BivariateTE()
    te.analyse_network(settings, data, targets=[target])
def test_gauss_data():
    """Test bivariate 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')
    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,
        'max_lag_target': 1}
    nw = BivariateTE()
    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 BivarateTE() estimate to JIDT estimate.
    current_value = (2, 2)
    source_vars = results.get_single_target(2, False)['selected_vars_sources']
    target_vars = results.get_single_target(2, False)['selected_vars_target']
    var1 = data.get_realisations(current_value, source_vars)[0]
    var2 = data.get_realisations(current_value, [current_value])[0]
    cond = data.get_realisations(current_value, target_vars)[0]
    est = JidtKraskovCMI({})
    jidt_cmi = est.estimate(var1=var1, var2=var2, conditional=cond)
    print('Estimated TE: {0:0.6f}, estimated TE 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 TE {0:0.6f} differs from JIDT estimate {1:0.6f} (expected: '
        'TE {2:0.6f}).'.format(te, jidt_cmi, expected_mi))
Exemple #17
0
def test_compare_bivariate_and_multivariate_te():
    """Compare bivariate to multivariate TE estimation."""
    expected_mi, source, source_uncorr, target = _get_gauss_data(seed=SEED)
    source = source[1:]
    source_uncorr = source_uncorr[1:]
    target = target[:-1]
    data = Data(np.hstack((source, source_uncorr, target)),
                dim_order='sp',
                normalise=False,
                seed=SEED)
    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': 1,
        'min_lag_sources': 1,
        'max_lag_target': 1
    }
    nw_bivar = BivariateTE()
    results = nw_bivar.analyse_single_target(settings,
                                             data,
                                             target=2,
                                             sources=[0, 1])
    te_bivar = results.get_single_target(2, fdr=False)['te'][0]

    nw_multivar = MultivariateTE()
    results = nw_multivar.analyse_single_target(settings,
                                                data,
                                                target=2,
                                                sources=[0, 1])
    te_multivar = results.get_single_target(2, fdr=False)['te'][0]

    print('Estimated TE: {0:0.6f}, estimated TE using multivariate algorithm: '
          '{1:0.6f} (expected: ~ {2:0.6f}).'.format(te_bivar, te_multivar,
                                                    expected_mi))
    assert np.isclose(te_bivar, te_multivar, atol=0.005), (
        'Estimated TE {0:0.6f} differs from multivariate estimate {1:0.6f} '
        '(expected: TE {2:0.6f}).'.format(te_bivar, te_multivar, expected_mi))
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 = BivariateTE()
    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.analyse_single_target(settings=settings, data=data, 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.')
Exemple #19
0
def test_discrete_input():
    """Test bivariate 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 = BivariateTE()
    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))
def test_discrete_input():
    """Test bivariate 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 = BivariateTE()
    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))
Exemple #21
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'))
Exemple #22
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'))
Exemple #23
0
def test_define_candidates():
    """Test candidate definition from a list of procs and a list of samples."""
    target = 1
    tau_sources = 3
    max_lag_sources = 10
    current_val = (target, 10)
    procs = [target]
    samples = np.arange(current_val[1] - 1, current_val[1] - max_lag_sources,
                        -tau_sources)
    # Test if candidates that are added manually to the conditioning set are
    # removed from the candidate set.
    nw = BivariateTE()
    nw.current_value = current_val
    settings = [{
        'add_conditionals': None
    }, {
        'add_conditionals': (2, 3)
    }, {
        'add_conditionals': [(2, 3), (4, 1)]
    }, {
        'add_conditionals': [(1, 9)]
    }, {
        'add_conditionals': [(1, 9), (2, 3), (4, 1)]
    }]
    for s in settings:
        nw.settings = s
        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).'
        if s['add_conditionals'] is not None:
            if type(s['add_conditionals']) is tuple:
                cond_ind = nw._lag_to_idx([s['add_conditionals']])
            else:
                cond_ind = nw._lag_to_idx(s['add_conditionals'])
            for c in cond_ind:
                assert c not in candidates, (
                    'Sample added erronously to candidates: {}.'.format(c))
Exemple #24
0
def test_define_candidates():
    """Test candidate definition from a list of procs and a list of samples."""
    target = 1
    tau_sources = 3
    max_lag_sources = 10
    current_val = (target, 10)
    procs = [target]
    samples = np.arange(current_val[1] - 1, current_val[1] - max_lag_sources,
                        -tau_sources)
    # Test if candidates that are added manually to the conditioning set are
    # removed from the candidate set.
    nw = BivariateTE()
    settings = [{
        'add_conditionals': None
    }, {
        'add_conditionals': (2, 3)
    }, {
        'add_conditionals': [(2, 3), (4, 1)]
    }]
    for s in settings:
        nw.settings = s
        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).'

    settings = [{
        'add_conditionals': [(1, 9)]
    }, {
        'add_conditionals': [(1, 9), (2, 3), (4, 1)]
    }]
    for s in settings:
        nw.settings = s
        candidates = nw._define_candidates(procs, samples)
    assert (1, 9) not 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).'
def test_bivariate_te_init():
    """Test instance creation for BivariateTE class."""
    # Test error on missing estimator
    settings = {
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'max_lag_sources': 7,
        'min_lag_sources': 2,
        'max_lag_target': 5
    }
    nw = BivariateTE()
    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'
    dat = Data()
    dat.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=dat, target=1)

    # Valid: max lag sources smaller than max lag target
    settings['max_lag_sources'] = 3
    nw.analyse_single_target(settings=settings, data=dat, 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=dat, 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=dat, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = 10
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, 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=dat, target=1)
    settings['max_lag_sources'] = 7
    settings['min_lag_sources'] = -4
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, target=1)
    settings['min_lag_sources'] = 4
    settings['max_lag_target'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, target=1)
    settings['max_lag_target'] = 5
    settings['tau_sources'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = -1
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, 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=dat, target=1)
    settings['min_lag_sources'] = 1
    settings['max_lag_sources'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, target=1)
    settings['max_lag_sources'] = 7
    settings['tau_sources'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, target=1)
    settings['tau_sources'] = 1
    settings['tau_target'] = 1.5
    with pytest.raises(RuntimeError):
        nw.analyse_single_target(settings=settings, data=dat, 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=dat, target=1.5)
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(settings=settings, data=dat, target=-1)
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings, data=dat, target=10)
    with pytest.raises(RuntimeError):  # wrong type
        nw.analyse_single_target(settings=settings, data=dat, target={})
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(settings=settings,
                                 data=dat,
                                 target=0,
                                 sources=-1)
    with pytest.raises(RuntimeError):  # negative
        nw.analyse_single_target(settings=settings,
                                 data=dat,
                                 target=0,
                                 sources=[-1])
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings,
                                 data=dat,
                                 target=0,
                                 sources=20)
    with pytest.raises(RuntimeError):  # not in data
        nw.analyse_single_target(settings=settings,
                                 data=dat,
                                 target=0,
                                 sources=[20])

    # Force conditionals
    settings['add_conditionals'] = [(0, 1), (1, 3)]
    nw.analyse_single_target(settings=settings, data=dat, target=0)
    settings['add_conditionals'] = (8, 0)
    with pytest.raises(IndexError):
        nw.analyse_single_target(settings=settings, data=dat, target=0)
Exemple #26
0
def infer_network(network_inference,
                  time_series,
                  parallel_target_analysis=False):
    # Define parameter options dictionaries
    network_inference_algorithms = pd.DataFrame()
    network_inference_algorithms['Description'] = pd.Series({
        'bMI_greedy':
        'Bivariate Mutual Information via greedy algorithm',
        'bTE_greedy':
        'Bivariate Transfer Entropy via greedy algorithm',
        'mMI_greedy':
        'Multivariate Mutual Information via greedy algorithm',
        'mTE_greedy':
        'Multivariate Transfer Entropy via greedy algorithm',
        'cross_corr':
        'Cross-correlation thresholding algorithm'
    })
    network_inference_algorithms['Required parameters'] = pd.Series({
        'bMI_greedy': [
            'min_lag_sources', 'max_lag_sources', 'tau_sources', 'tau_target',
            'cmi_estimator', 'z_standardise', 'permute_in_time',
            'n_perm_max_stat', 'n_perm_min_stat', 'n_perm_omnibus',
            'n_perm_max_seq', 'fdr_correction', 'p_value'
            # 'alpha_max_stats',
            # 'alpha_min_stats',
            # 'alpha_omnibus',
            # 'alpha_max_seq',
            # 'alpha_fdr'
        ],
        'bTE_greedy': [
            'min_lag_sources', 'max_lag_sources', 'tau_sources',
            'max_lag_target', 'tau_target', 'cmi_estimator', 'z_standardise',
            'permute_in_time', 'n_perm_max_stat', 'n_perm_min_stat',
            'n_perm_omnibus', 'n_perm_max_seq', 'fdr_correction', 'p_value'
            # 'alpha_max_stats',
            # 'alpha_min_stats',
            # 'alpha_omnibus',
            # 'alpha_max_seq',
            # 'alpha_fdr'
        ],
        'mMI_greedy': [
            'min_lag_sources', 'max_lag_sources', 'tau_sources', 'tau_target',
            'cmi_estimator', 'z_standardise', 'permute_in_time',
            'n_perm_max_stat', 'n_perm_min_stat', 'n_perm_omnibus',
            'n_perm_max_seq', 'fdr_correction', 'p_value'
            # 'alpha_max_stats',
            # 'alpha_min_stats',
            # 'alpha_omnibus',
            # 'alpha_max_seq',
            # 'alpha_fdr'
        ],
        'mTE_greedy': [
            'min_lag_sources', 'max_lag_sources', 'tau_sources',
            'max_lag_target', 'tau_target', 'cmi_estimator', 'z_standardise',
            'permute_in_time', 'n_perm_max_stat', 'n_perm_min_stat',
            'n_perm_omnibus', 'n_perm_max_seq', 'fdr_correction', 'p_value'
            # 'alpha_max_stats',
            # 'alpha_min_stats',
            # 'alpha_omnibus',
            # 'alpha_max_seq',
            # 'alpha_fdr'
        ],
        'cross_corr': ['min_lag_sources', 'max_lag_sources']
    })
    try:
        # Ensure that a network inference algorithm has been specified
        if 'algorithm' not in network_inference:
            raise ParameterMissing('algorithm')
        # Ensure that the provided algorithm is implemented
        if network_inference.algorithm not in network_inference_algorithms.index:
            raise ParameterValue(network_inference.algorithm)
        # Ensure that all the parameters required by the algorithm have been provided
        par_required = network_inference_algorithms['Required parameters'][
            network_inference.algorithm]
        for par in par_required:
            if par not in network_inference:
                raise ParameterMissing(par)

    except ParameterMissing as e:
        print(e.msg, e.par_names)
        raise
    except ParameterValue as e:
        print(e.msg, e.par_value)
        raise

    else:
        nodes_n = np.shape(time_series)[0]

        can_be_z_standardised = True
        if network_inference.z_standardise:
            # Check if data can be normalised per process (assuming the
            # first dimension represents processes, as in the rest of the code)
            can_be_z_standardised = np.all(np.std(time_series, axis=1) > 0)
            if not can_be_z_standardised:
                print('Time series can not be z-standardised')

        if len(time_series.shape) == 2:
            dim_order = 'ps'
        else:
            dim_order = 'psr'

        # initialise an empty data object
        dat = Data()

        # Load time series
        dat = Data(time_series,
                   dim_order=dim_order,
                   normalise=(network_inference.z_standardise
                              & can_be_z_standardised))

        algorithm = network_inference.algorithm
        if algorithm in [
                'bMI_greedy', 'mMI_greedy', 'bTE_greedy', 'mTE_greedy'
        ]:
            # Set analysis options
            if algorithm == 'bMI_greedy':
                network_analysis = BivariateMI()
            if algorithm == 'mMI_greedy':
                network_analysis = MultivariateMI()
            if algorithm == 'bTE_greedy':
                network_analysis = BivariateTE()
            if algorithm == 'mTE_greedy':
                network_analysis = MultivariateTE()

            settings = {
                'min_lag_sources': network_inference.min_lag_sources,
                'max_lag_sources': network_inference.max_lag_sources,
                'tau_sources': network_inference.tau_sources,
                'max_lag_target': network_inference.max_lag_target,
                'tau_target': network_inference.tau_target,
                'cmi_estimator': network_inference.cmi_estimator,
                'kraskov_k': network_inference.kraskov_k,
                'num_threads': network_inference.jidt_threads_n,
                'permute_in_time': network_inference.permute_in_time,
                'n_perm_max_stat': network_inference.n_perm_max_stat,
                'n_perm_min_stat': network_inference.n_perm_min_stat,
                'n_perm_omnibus': network_inference.n_perm_omnibus,
                'n_perm_max_seq': network_inference.n_perm_max_seq,
                'fdr_correction': network_inference.fdr_correction,
                'alpha_max_stat': network_inference.p_value,
                'alpha_min_stat': network_inference.p_value,
                'alpha_omnibus': network_inference.p_value,
                'alpha_max_seq': network_inference.p_value,
                'alpha_fdr': network_inference.p_value
            }

            # # Add optional settings
            # optional_settings_keys = {
            #     'config.debug',
            #     'config.max_mem_frac'
            # }

            # for key in optional_settings_keys:
            #     if traj.f_contains(key, shortcuts=True):
            #         key_last = key.rpartition('.')[-1]
            #         settings[key_last] = traj[key]
            #         print('Using optional setting \'{0}\'={1}'.format(
            #             key_last,
            #             traj[key])
            #         )

            if parallel_target_analysis:
                # Use SCOOP to create a generator of map results, each
                # correspinding to one map iteration
                res_iterator = futures.map_as_completed(
                    network_analysis.analyse_single_target,
                    itertools.repeat(settings, nodes_n),
                    itertools.repeat(dat, nodes_n), list(range(nodes_n)))
                # Run analysis
                res_list = list(res_iterator)
                if settings['fdr_correction']:
                    res = network_fdr({'alpha_fdr': settings['alpha_fdr']},
                                      *res_list)
                else:
                    res = res_list[0]
                    res.combine_results(*res_list[1:])
            else:
                # Run analysis
                res = network_analysis.analyse_network(settings=settings,
                                                       data=dat)
            return res

        else:
            raise ParameterValue(
                algorithm,
                msg='Network inference algorithm not yet implemented')
Exemple #27
0
def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 5
    data = Data()
    data.generate_mute_data(200, 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 = 2
    te = BivariateTE()
    results_local = te.analyse_network(settings, data, targets=[target])

    lte = results_local.get_single_target(target, fdr=False)['te']
    n_sources = len(results_local.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
    # TE between single variables and the target. For this test case where we
    # find only one significant past variable per source, the two should be the
    # same. Also compare single link average TE to mean local TE for each
    # link.
    settings['local_values'] = False
    results_avg = te.analyse_network(settings, data, targets=[target])
    te_single_link = results_avg.get_single_target(target, fdr=False)['te']
    te_selected_sources = results_avg.get_single_target(
        target, fdr=False)['selected_sources_te']
    sources_local = results_local.get_target_sources(target, fdr=False)
    sources_avg = results_avg.get_target_sources(target, fdr=False)
    assert np.isclose(te_single_link, te_selected_sources, atol=0.005).all(), (
        'Single link average TE {0} and single source TE {1} deviate.'.format(
            te_single_link, te_selected_sources))
    # Check if average and local values are the same. Make sure target pasts
    # are the same and test each source separately. Inferred source and target
    # may differ between the two calls to analyse_network() due to random data
    # and low number of surrogates used in unit testing. Different no. inferred
    # past variables will also lead to differences in estimates.
    if (results_avg.get_single_target(
            target,
            fdr=False).selected_vars_target == results_local.get_single_target(
                target, fdr=False).selected_vars_target):
        print('Compare average and local values.')
        for s in list(set(sources_avg).intersection(sources_local)):
            i1 = np.where(sources_avg == s)[0][0]
            i2 = np.where(sources_local == s)[0][0]
            assert np.isclose(
                te_single_link[i1], np.mean(lte[i2, :, :]), atol=0.005
            ), ('Single link average TE {0:.6f} and mean LTE {1:.6f} deviate for '
                'source {2}.'.format(te_single_link[i1],
                                     np.mean(lte[i2, :, :]), s))
            assert np.isclose(
                te_single_link[i1], te_selected_sources[i1], atol=0.005
            ), ('Single link average TE {0:.6f} and single source TE {1:.6f} '
                'deviate.'.format(te_single_link[i1], te_selected_sources[i1]))
def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 5
    data = Data()
    data.generate_mute_data(200, 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 = 2
    te = BivariateTE()
    results_local = te.analyse_network(settings, data, targets=[target])

    lte = results_local.get_single_target(target, fdr=False)['te']
    n_sources = len(results_local.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
    # TE between single variables and the target. For this test case where we
    # find only one significant past variable per source, the two should be the
    # same. Also compare single link average TE to mean local TE for each
    # link.
    settings['local_values'] = False
    results_avg = te.analyse_network(settings, data, targets=[target])
    te_single_link = results_avg.get_single_target(target, fdr=False)['te']
    te_selected_sources = results_avg.get_single_target(
        target, fdr=False)['selected_sources_te']
    sources_local = results_local.get_target_sources(target, fdr=False)
    sources_avg = results_avg.get_target_sources(target, fdr=False)
    assert np.isclose(te_single_link, te_selected_sources, atol=0.005).all(), (
        'Single link average TE {0} and single source TE {1} deviate.'.format(
                te_single_link, te_selected_sources))
    # Check if average and local values are the same. Make sure target pasts
    # are the same and test each source separately. Inferred source and target
    # may differ between the two calls to analyse_network() due to random data
    # and low number of surrogates used in unit testing. Different no. inferred
    # past variables will also lead to differences in estimates.
    if (results_avg.get_single_target(target, fdr=False).selected_vars_target ==
            results_local.get_single_target(target, fdr=False).selected_vars_target):
        print('Compare average and local values.')
        for s in list(set(sources_avg).intersection(sources_local)):
            i1 = np.where(sources_avg == s)[0][0]
            i2 = np.where(sources_local == s)[0][0]
            assert np.isclose(te_single_link[i1], np.mean(lte[i2, :, :]), atol=0.005), (
                'Single link average TE {0:.6f} and mean LTE {1:.6f} deviate for '
                'source {2}.'.format(
                    te_single_link[i1], np.mean(lte[i2, :, :]), s))
            assert np.isclose(te_single_link[i1], te_selected_sources[i1], atol=0.005), (
                'Single link average TE {0:.6f} and single source TE {1:.6f} '
                'deviate.'.format(te_single_link[i1], te_selected_sources[i1]))
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 = BivariateTE()
    nw.settings = {'verbose': True}
    # Add list of sources.
    sources = [1, 2, 3]
    nw._check_source_set(sources, data.n_processes)
    assert nw.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.target = 0
    with pytest.raises(RuntimeError):
        nw._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._check_source_set(sources, data.n_processes)
    assert (type(nw.source_set) is list)

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

    # Test invalid inputs.
    with pytest.raises(RuntimeError):   # sources greater than no. procs
        nw._check_source_set(8, data.n_processes)
    with pytest.raises(RuntimeError):  # negative value as source
        nw._check_source_set(-3, data.n_processes)
data += np.random.normal(0, 0.1, N_NODE * N_DATA).reshape((N_NODE, N_DATA))

#######################
# Run IDTxl
#######################
# a) Convert data to ITDxl format
dataIDTxl = Data(data, dim_order='ps')

# b) Initialise analysis object and define settings
N_METHOD = 3
title_lst = [
    "BivariateMI for shift-based data", "BivariateTE for shift-based data",
    "MultivariateTE for shift-based data"
]
method_lst = ['BivariateMI', 'BivariateTE', 'MultivariateTE']
network_analysis_lst = [BivariateMI(), BivariateTE(), MultivariateTE()]

settings = {
    'cmi_estimator': 'JidtGaussianCMI',
    'max_lag_sources': LAG_MAX,
    'min_lag_sources': LAG_MIN
}

# c) Run analysis
results_lst = [
    net_analysis.analyse_network(settings=settings, data=dataIDTxl)
    for net_analysis in network_analysis_lst
]

#######################
# Convert results to matrices and plot them
Exemple #31
0
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._edge_matrix.shape[0] == n_nodes, (
            'Incorrect number of rows in adjacency matrix.')
        assert adj_matrix._edge_matrix.shape[1] == n_nodes, (
            'Incorrect number of columns in adjacency matrix.')
        assert adj_matrix._edge_matrix.shape[0] == n_nodes, (
            'Incorrect number of rows in adjacency matrix.')
        assert adj_matrix._edge_matrix.shape[1] == n_nodes, (
            'Incorrect number of columns in adjacency matrix.')
           dim_order=dim_order,
           normalise=(network_inference.z_standardise & can_be_z_standardised))

network_inference = traj.parameters.network_inference

print('network_inference.p_value = {0}\n'.format(network_inference.p_value))

algorithm = network_inference.algorithm
if algorithm in ['bMI', 'mMI_greedy', 'bTE_greedy', 'mTE_greedy']:
    # Set analysis options
    if algorithm == 'bMI':
        network_analysis = BivariateMI()
    if algorithm == 'mMI_greedy':
        network_analysis = MultivariateMI()
    if algorithm == 'bTE_greedy':
        network_analysis = BivariateTE()
    if algorithm == 'mTE_greedy':
        network_analysis = MultivariateTE()

    #settings['add_conditionals'] = [(35, 3), (26, 1), (17, 5), (25, 4)]

    # Add optional settings
    optional_settings_keys = {'config.debug', 'config.max_mem_frac'}

    for key in optional_settings_keys:
        if traj.f_contains(key, shortcuts=True):
            key_last = key.rpartition('.')[-1]
            settings[key_last] = traj[key]
            print('Using optional setting \'{0}\'={1}\n'.format(
                key_last, traj[key]))
def test_bivariate_te_init():
    """Test instance creation for BivariateTE class."""
    # Test error on missing estimator
    settings = {
        'n_perm_max_stat': 21,
        'n_perm_omnibus': 30,
        'n_perm_max_seq': 30,
        'max_lag_sources': 7,
        'min_lag_sources': 2,
        'max_lag_target': 5}
    nw = BivariateTE()
    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])
# Import classes
from idtxl.bivariate_te import BivariateTE
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 = BivariateTE()
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()
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.
    """
    dat = Data()
    dat.generate_mute_data(100, 5)
    nw_0 = BivariateTE()
    nw_0.settings = {'verbose': True}
    # Add list of sources.
    sources = [1, 2, 3]
    nw_0._check_source_set(sources, dat.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=dat.n_processes)

    # Test if a single source, no list is added correctly.
    sources = 1
    nw_0._check_source_set(sources, dat.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', dat.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, dat.n_processes)
    with pytest.raises(RuntimeError):  # negative value as source
        nw_0._check_source_set(-3, dat.n_processes)
Exemple #36
0
def test_JidtKraskovCMI_BTE_checkpoint():
    """ run test without checkpointing, with checkpointing without resume and checkpointing with resume to compare the results"""
    filename_ckp1 = os.path.join(
        os.path.dirname(__file__), 'data', 'run_checkpoint')
    filename_ckp2 = os.path.join(
        os.path.dirname(__file__), 'data', 'resume_checkpoint')

    """Test running analysis without any checkpoint setting."""
    # Generate test data
    data = Data(seed=SEED)
    data.generate_mute_data(n_samples=N_SAMPLES, n_replications=1)

    # Initialise analysis object and define settings
    network_analysis1 = BivariateTE()
    network_analysis2 = BivariateTE()
    network_analysis3 = BivariateTE()
    network_analysis4 = BivariateTE()

    # Settings without checkpointing.
    settings1 = {'cmi_estimator': 'JidtKraskovCMI',
                 'n_perm_max_stat': N_PERM,
                 'n_perm_min_stat': N_PERM,
                 'n_perm_max_seq': N_PERM,
                 'n_perm_omnibus': N_PERM,
                 'max_lag_sources': 3,
                 'min_lag_sources': 2,
                 'noise_level': 0
                 }

    # Settings with checkpointing.
    settings2 = settings1
    settings2['write_ckp'] = True
    settings2['filename_ckp'] = filename_ckp1

    # Settings resuming from checkpoint.
    settings3 = settings1
    settings3['write_ckp'] = True
    settings3['filename_ckp'] = filename_ckp2

    # Setting sources and targets for the analysis
    sources = [0, 1]
    targets = [2, 3]
    targets2 = [3]

    # Starting Analysis of the Network
    # results of a network analysis without checkpointing
    results1 = network_analysis1.analyse_network(
        settings=settings1, data=data, targets=targets, sources=sources)

    # results of a network analysis with checkpointing
    results2 = network_analysis2.analyse_network(
        settings=settings2, data=data, targets=targets, sources=sources)

    # Creating a checkpoint similar to the above settings where the targets of
    # of the first source have been already analyzed
    with open(filename_ckp1 + ".ckp", "r+") as f:
        tarsource = f.readlines()
    tarsource = tarsource[8]
    tarsource = (tarsource[:-1])
    print(tarsource)

    network_analysis3._set_checkpointing_defaults(
        settings3, data, sources, targets)

    with open(filename_ckp2 + ".ckp") as f:
        lines = f.read().splitlines()
    lines[8] = tarsource
    with open(filename_ckp2 + ".ckp", 'w') as f:
        f.write('\n'.join(lines))
    print(lines)

    # Resume analysis.
    network_analysis_res = BivariateMI()
    data, settings, targets, sources = network_analysis_res.resume_checkpoint(
        filename_ckp2)

    # results of a network analysis resuming from checkpoint
    results3 = network_analysis_res.analyse_network(
        settings=settings3, data=data, targets=targets, sources=sources)

    # results of a network analysis without checkpointing but other targets/sources
    results4 = network_analysis4.analyse_network(
        settings=settings1, data=data, targets=targets2, sources=sources)

    adj_matrix1 = results1.get_adjacency_matrix(weights='binary', fdr=False)
    adj_matrix2 = results2.get_adjacency_matrix(weights='binary', fdr=False)
    adj_matrix3 = results3.get_adjacency_matrix(weights='binary', fdr=False)
    adj_matrix4 = results4.get_adjacency_matrix(weights='binary', fdr=False)

    result1 = adj_matrix1.get_edge_list()
    result2 = adj_matrix2.get_edge_list()
    result3 = adj_matrix3.get_edge_list()
    result4 = adj_matrix4.get_edge_list()

    print("Printing results:")
    print("Result 1: without checkpoint")
    print(result1)
    print("Result 2: with checkpoint")
    print(result2)
    print("Result 3: resuming from checkpoint")
    print(result3)
    print("Result 4: without checkpoint and different targets and sources")
    print(result4)

    print("Comparing the results:")
    assert np.array_equal(result1, result2), 'Result 1 and 2 not equal!'
    assert np.array_equal(result1, result3), 'Result 1 and 3 not equal!'
    assert not np.array_equal(result1, result4), 'Result 1 and 4 equal, expected to be different!'

    cmp1 = list(set(result1).intersection(result2))
    cmp2 = list(set(result1).intersection(result3))
    cmp3 = list(set(result1).intersection(result4))
    len1 = len(result1)
    assert len(cmp1) == len1 and len(cmp2) == len1 and len(cmp3) != len1, (
        "Discrete MultivariateMI Running with checkpoints does not give the expected results")

    print("Final")
    print("Elements of comparison between result 1 and 2")
    print(cmp1)
    print("Elements of comparison between result 1 and 3")
    print(cmp2)
    print("Elements of comparison between result 1 and 4")
    print(cmp3)

    _clear_ckp(filename_ckp1)
    _clear_ckp(filename_ckp2)
def test_analyse_network():
    """Test method for full network analysis."""
    n_processes = 5  # the MuTE network has 5 nodes
    dat = Data()
    dat.generate_mute_data(10, 5)
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_max_seq': 21,
        'n_perm_omnibus': 30,
        'max_lag_sources': 5,
        'min_lag_sources': 4,
        'max_lag_target': 5
    }
    nw_0 = BivariateTE()

    # Test all to all analysis
    r = nw_0.analyse_network(settings, dat, targets='all', sources='all')
    try:
        del r['fdr_corrected']
    except:
        pass
    k = list(r.keys())
    sources = np.arange(n_processes)
    assert all(np.array(k) == np.arange(n_processes)), (
        'Network analysis did not run on all targets.')
    for t in r.keys():
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(r[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]
    r = nw_0.analyse_network(settings, dat, targets=target_list, sources='all')
    try:
        del r['fdr_corrected']
    except:
        pass
    k = list(r.keys())
    assert all(np.array(k) == np.array(target_list)), (
        'Network analysis did not run on correct subset of targets.')
    for t in r.keys():
        s = np.array(list(set(sources) - set([t])))
        assert all(np.array(r[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]
    r = nw_0.analyse_network(settings,
                             dat,
                             targets=target_list,
                             sources=source_list)
    try:
        del r['fdr_corrected']
    except:
        pass
    k = list(r.keys())
    assert all(np.array(k) == np.array(target_list)), (
        'Network analysis did not run for all targets.')
    for t in r.keys():
        assert all(r[t]['sources_tested'] == np.array(source_list)), (
            'Network analysis did not run on the correct subset of sources '
            'for target {0}'.format(t))
# Import classes
from idtxl.bivariate_te import BivariateTE
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 = BivariateTE()
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()