当前位置: 首页>>代码示例>>Python>>正文


Python Data.get_realisations方法代码示例

本文整理汇总了Python中idtxl.data.Data.get_realisations方法的典型用法代码示例。如果您正苦于以下问题:Python Data.get_realisations方法的具体用法?Python Data.get_realisations怎么用?Python Data.get_realisations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在idtxl.data.Data的用法示例。


在下文中一共展示了Data.get_realisations方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_data_type

# 需要导入模块: from idtxl.data import Data [as 别名]
# 或者: from idtxl.data.Data import get_realisations [as 别名]
def test_data_type():
    """Test if data class always returns the correct data type."""
    # Change data type for the same object instance.
    d_int = np.random.randint(0, 10, size=(3, 50))
    orig_type = type(d_int[0][0])
    data = Data(d_int, dim_order='ps', normalise=False)
    # The concrete type depends on the platform:
    # https://mail.scipy.org/pipermail/numpy-discussion/2011-November/059261.html
    # Hence, compare against the type automatically assigned by Python or
    # against np.integer
    assert data.data_type is orig_type, 'Data type did not change.'
    assert issubclass(type(data.data[0, 0, 0]), np.integer), (
        'Data type is not an int.')
    d_float = np.random.randn(3, 50)
    data.set_data(d_float, dim_order='ps')
    assert data.data_type is np.float64, 'Data type did not change.'
    assert issubclass(type(data.data[0, 0, 0]), np.float), (
        'Data type is not a float.')

    # Check if data returned by the object have the correct type.
    d_int = np.random.randint(0, 10, size=(3, 50, 5))
    data = Data(d_int, dim_order='psr', normalise=False)
    real = data.get_realisations((0, 5), [(1, 1), (1, 3)])[0]
    assert issubclass(type(real[0, 0]), np.integer), (
        'Realisations type is not an int.')
    sl = data._get_data_slice(0)[0]
    assert issubclass(type(sl[0, 0]), np.integer), (
        'Data slice type is not an int.')
    settings = {'perm_type': 'random'}
    sl_perm = data.slice_permute_samples(0, settings)[0]
    assert issubclass(type(sl_perm[0, 0]), np.integer), (
        'Permuted data slice type is not an int.')
    samples = data.permute_samples((0, 5), [(1, 1), (1, 3)], settings)[0]
    assert issubclass(type(samples[0, 0]), np.integer), (
        'Permuted samples type is not an int.')
开发者ID:SimonStreicher,项目名称:IDTxl,代码行数:37,代码来源:test_data.py

示例2: test_gauss_data

# 需要导入模块: from idtxl.data import Data [as 别名]
# 或者: from idtxl.data.Data import get_realisations [as 别名]
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))
开发者ID:SimonStreicher,项目名称:IDTxl,代码行数:43,代码来源:test_bivariate_te.py

示例3: test_get_realisations

# 需要导入模块: from idtxl.data import Data [as 别名]
# 或者: from idtxl.data.Data import get_realisations [as 别名]
def test_get_realisations():
    """Test low-level function for data retrieval."""
    data = Data()
    data.generate_mute_data()
    idx_list = [(0, 4), (0, 6)]
    current_value = (0, 3)
    with pytest.raises(RuntimeError):
        data.get_realisations(current_value, idx_list)

    # Test retrieved data for one/two replications in time (i.e., the current
    # value is equal to the last sample)
    n = 7
    d = Data(np.arange(n + 1), 's', normalise=False)
    current_value = (0, n)
    realisations = d.get_realisations(current_value, [(0, 1)])[0]
    assert (realisations[0][0] == 1)
    assert (realisations.shape == (1, 1))
    d = Data(np.arange(n + 2), 's', normalise=False)
    current_value = (0, n)
    realisations = d.get_realisations(current_value, [(0, 1)])[0]
    assert (realisations[0][0] == 1)
    assert (realisations[1][0] == 2)
    assert (realisations.shape == (2, 1))
    n_realisations = 2
    data = np.arange(10).reshape(n_realisations, 5)
    d = Data(data, 'rs', normalise=False)
    current_value = (0, 1)
    realisations, ind = d.get_realisations(current_value, [(0, 0)])
    for r in range(n_realisations):
        assert (data[r, :-1] == np.squeeze(realisations[ind == r])).all()

    # Test retrieval of realisations of the current value.
    n = 7
    d = Data(np.arange(n), 's', normalise=False)
    current_value = (0, n - 1)
    realisations = d.get_realisations(current_value, [current_value])[0]
开发者ID:SimonStreicher,项目名称:IDTxl,代码行数:38,代码来源:test_data.py

示例4: test_calculate_single_link

# 需要导入模块: from idtxl.data import Data [as 别名]
# 或者: from idtxl.data.Data import get_realisations [as 别名]
def test_calculate_single_link():
    """Test calculation of single link (conditional) MI and TE."""

    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')

    n = NetworkAnalysis()
    n._cmi_estimator = JidtKraskovCMI(settings={})
    n.settings = {
        'local_values': False
    }
    current_value = (2, 1)

    # Test single link estimation for a single and multiple sources for
    # cases: no target vars, source vars/no source vars (tests if the
    # conditioning set is built correctly for conditioning='full').
    source_realisations = data.get_realisations(current_value, [(0, 0)])[0]
    current_value_realisations = data.get_realisations(
        current_value, [current_value])[0]
    expected_mi = n._cmi_estimator.estimate(
        current_value_realisations, source_realisations)
    # cond. on second source
    cond_realisations = data.get_realisations(current_value, [(1, 0)])[0]
    expected_mi_cond1 = n._cmi_estimator.estimate(
        current_value_realisations, source_realisations, cond_realisations)

    for sources in ['all', [0]]:
        for conditioning in ['full', 'target', 'none']:
            for source_vars in [[(0, 0)], [(0, 0), (1, 0)]]:
                mi = n._calculate_single_link(
                    data, current_value, source_vars, target_vars=None,
                    sources=sources, conditioning=conditioning)
                if mi.shape[0] > 1:  # array for source='all'
                    mi = mi[0]

                if source_vars == [(0, 0)]:  # no conditioning
                    assert np.isclose(mi, expected_mi, rtol=0.05), (
                        'Estimated single-link MI ({0}) differs from expected '
                        'MI ({1}).'.format(mi, expected_mi))
                else:
                    if conditioning == 'full':  # cond. on second source
                        assert np.isclose(mi, expected_mi_cond1, rtol=0.05), (
                            'Estimated single-link MI ({0}) differs from '
                            'expected MI ({1}).'.format(mi, expected_mi_cond1))
                    else:  # no conditioning
                        assert np.isclose(mi, expected_mi, rtol=0.05), (
                            'Estimated single-link MI ({0}) differs from '
                            'expected MI ({1}).'.format(mi, expected_mi))

        # Test single link estimation for a single and multiple sources for
        # cases: target vars/no target vars, source vars (tests if the
        # conditioning set is built correctly for conditioning='full').
        cond_realisations = np.hstack((  # cond. on second source and target
            data.get_realisations(current_value, [(1, 0)])[0],
            data.get_realisations(current_value, [(2, 0)])[0]
            ))
        expected_mi_cond2 = n._cmi_estimator.estimate(
            current_value_realisations, source_realisations, cond_realisations)
        # cond. on target
        cond_realisations = data.get_realisations(current_value, [(2, 0)])[0]
        expected_mi_cond3 = n._cmi_estimator.estimate(
            current_value_realisations, source_realisations, cond_realisations)

        for target_vars in [None, [(2, 0)]]:
            for conditioning in ['full', 'target', 'none']:
                mi = n._calculate_single_link(
                    data, current_value, source_vars=[(0, 0), (1, 0)],
                    target_vars=target_vars, sources=sources,
                    conditioning=conditioning)
                if mi.shape[0] > 1:  # array for source='all'
                    mi = mi[0]

                if conditioning == 'none':  # no conditioning
                    assert np.isclose(mi, expected_mi, rtol=0.05), (
                        'Estimated single-link MI ({0}) differs from expected '
                        'MI ({1}).'.format(mi, expected_mi))
                else:
                    # target only
                    if target_vars is not None and conditioning == 'target':
                        assert np.isclose(mi, expected_mi_cond3, rtol=0.05), (
                            'Estimated single-link MI ({0}) differs from '
                            'expected MI ({1}).'.format(mi, expected_mi_cond3))
                    # target and 2nd source
                    if target_vars is not None and conditioning == 'full':
                        assert np.isclose(mi, expected_mi_cond2, rtol=0.05), (
                            'Estimated single-link MI ({0}) differs from '
                            'expected MI ({1}).'.format(mi, expected_mi_cond2))
                    # target is None, condition on second target
                    else:
                        if conditioning == 'full':
                            assert np.isclose(mi, expected_mi_cond1, rtol=0.05), (
                                'Estimated single-link MI ({0}) differs from expected '
                                'MI ({1}).'.format(mi, expected_mi_cond1))

    # Test requested sources not in source vars
    with pytest.raises(RuntimeError):
        mi = n._calculate_single_link(
#.........这里部分代码省略.........
开发者ID:SimonStreicher,项目名称:IDTxl,代码行数:103,代码来源:test_network_analysis.py


注:本文中的idtxl.data.Data.get_realisations方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。