def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 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_mi': 21,
        'max_lag': max_lag,
        'tau': 1
    }
    data = Data()
    data.generate_mute_data(100, 3)
    ais = ActiveInformationStorage()
    processes = [1, 2]
    results = ais.analyse_network(settings, data, processes)

    for p in processes:
        lais = results.get_single_process(p, fdr=False)['ais']
        if lais is np.nan:
            continue
        assert type(lais) is np.ndarray, (
            'LAIS estimation did not return an array of values: {0}'.format(
                lais))
        assert lais.shape[0] == data.n_replications, (
            'Wrong dim (no. replications) in LAIS estimate: {0}'.format(
                lais.shape))
        assert lais.shape[1] == data.n_realisations_samples((0, max_lag)), (
            'Wrong dim (no. samples) in LAIS estimate: {0}'.format(lais.shape))
def test_return_local_values():
    """Test estimation of local values."""
    max_lag = 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_mi': 21,
        'max_lag': max_lag,
        'tau': 1}
    data = Data()
    data.generate_mute_data(100, 3)
    ais = ActiveInformationStorage()
    processes = [1, 2]
    results = ais.analyse_network(settings, data, processes)

    for p in processes:
        lais = results.get_single_process(p, fdr=False)['ais']
        if lais is np.nan:
            continue
        assert type(lais) is np.ndarray, (
            'LAIS estimation did not return an array of values: {0}'.format(
                lais))
        assert lais.shape[0] == data.n_replications, (
            'Wrong dim (no. replications) in LAIS estimate: {0}'.format(
                lais.shape))
        assert lais.shape[1] == data.n_realisations_samples((0, max_lag)), (
            'Wrong dim (no. samples) in LAIS estimate: {0}'.format(lais.shape))
def test_discrete_input():
    """Test AIS estimation from discrete data."""
    # Generate AR data
    order = 1
    n = 10000 - order
    self_coupling = 0.5
    process = np.zeros(n + order)
    process[0:order] = np.random.normal(size=(order))
    for n in range(order, n + order):
        process[n] = self_coupling * process[n - 1] + np.random.normal()

    # Discretise data
    settings = {'discretise_method': 'equal', 'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings)
    process_dis, temp = est._discretise_vars(var1=process, var2=process)
    data = Data(process_dis, dim_order='s', normalise=False)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 2
    }
    nw = ActiveInformationStorage()
    nw.analyse_single_process(settings=settings, data=data, process=0)
def test_discrete_input():
    """Test AIS estimation from discrete data."""
    # Generate AR data
    order = 1
    n = 10000 - order
    self_coupling = 0.5
    process = np.zeros(n + order)
    process[0:order] = np.random.normal(size=(order))
    for n in range(order, n + order):
        process[n] = self_coupling * process[n - 1] + np.random.normal()

    # Discretise data
    settings = {'discretise_method': 'equal',
                'n_discrete_bins': 5}
    est = JidtDiscreteCMI(settings)
    process_dis, temp = est._discretise_vars(var1=process, var2=process)
    data = Data(process_dis, dim_order='s', normalise=False)
    settings = {
        'cmi_estimator': 'JidtDiscreteCMI',
        'discretise_method': 'none',
        'n_discrete_bins': 5,  # alphabet size of the variables
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 2}
    nw = ActiveInformationStorage()
    nw.analyse_single_process(settings=settings, data=data, process=0)
Example #5
0
def test_ais_fdr():
    settings = {'n_perm_max_seq': 1000, 'n_perm_mi': 1000}
    process_0 = {
        'selected_vars': [(0, 1), (0, 2), (0, 3)],
        'ais_pval': 0.0001,
        'ais_sign': True}
    process_1 = {
        'selected_vars': [(1, 0), (1, 1), (1, 2)],
        'ais_pval': 0.031,
        'ais_sign': True}
    process_2 = {
        'selected_vars': [],
        'ais_pval': 0.41,
        'ais_sign': False}
    res_1 = ResultsSingleProcessAnalysis(
        n_nodes=3, n_realisations=1000, normalised=True)
    res_1._add_single_result(process=0, settings=settings, results=process_0)
    res_1._add_single_result(process=1, settings=settings, results=process_1)
    res_2 = ResultsSingleProcessAnalysis(
        n_nodes=3, n_realisations=1000, normalised=True)
    res_2._add_single_result(process=2, settings=settings, results=process_2)

    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'alpha_fdr': 0.05,
        'max_lag': 3}
    data = Data()
    data.generate_mute_data(n_samples=100, n_replications=3)
    analysis_setup = ActiveInformationStorage()
    analysis_setup._initialise(settings=settings, data=data, process=1)
    res_pruned = stats.ais_fdr(settings, res_1, res_2)
    assert (not res_pruned._single_process[2].selected_vars_sources), (
        'Process 2 has not been pruned from results.')

    alpha_fdr = res_pruned.settings.alpha_fdr
    for k in res_pruned.processes_analysed:
        if not res_pruned._single_process[k]['ais_sign']:
            assert (res_pruned._single_process[k]['ais_pval'] > alpha_fdr), (
                'P-value of non-sign. AIS is not 1.')
            assert (not res_pruned._single_process[k]['selected_vars']), (
                'List of significant past variables is not empty')
        else:
            assert (res_pruned._single_process[k]['ais_pval'] < 1), (
                'P-value of sign. AIS is not smaller 1.')
            assert (res_pruned._single_process[k]['selected_vars']), (
                'List of significant past variables is empty')

    # Test function call for single result
    res_pruned = stats.ais_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_mi'] = 2
    res_2.settings['n_perm_mi'] = 2
    res_pruned = stats.ais_fdr(settings, res_1, res_2)
    with pytest.raises(RuntimeError):
        res_pruned.get_significant_processes(fdr=True)
def test_add_conditional_manually():
    """Enforce the conditioning on additional variables."""
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'max_lag': 5,
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21
    }
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()

    # Add a conditional with a lag bigger than the max_lag requested above
    settings['add_conditionals'] = (8, 0)
    with pytest.raises(IndexError):
        ais.analyse_single_process(settings=settings, data=data, process=0)

    # Add valid conditionals and test if they were added
    settings['add_conditionals'] = [(0, 1), (1, 3)]
    ais._initialise(settings, data, 0)
    # Get list of conditionals after intialisation and convert absolute samples
    # back to lags for comparison.
    cond_list = ais._idx_to_lag(ais.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_compare_jidt_open_cl_estimator():
    """Compare results from OpenCl and JIDT estimators for AIS calculation."""
    data = Data()
    data.generate_mute_data(1000, 2)
    settings = {
        'cmi_estimator': 'OpenCLKraskovCMI',
        'alpha_mi': 0.05,
        'tail_mi': 'one_bigger',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 5,
        'tau': 1
    }
    processes = [2, 3]
    network_analysis = ActiveInformationStorage()
    res_opencl = network_analysis.analyse_network(settings, data, processes)
    settings['cmi_estimator'] = 'JidtKraskovCMI'
    res_jidt = network_analysis.analyse_network(settings, data, processes)
    # Note that I require equality up to three digits. Results become more
    # exact for bigger data sizes, but this takes too long for a unit test.
    ais_opencl_2 = res_opencl._single_process[2].ais
    ais_jidt_2 = res_jidt._single_process[2].ais
    ais_opencl_3 = res_opencl._single_process[3].ais
    ais_jidt_3 = res_jidt._single_process[3].ais
    print('AIS for MUTE data proc 2 - opencl: {0} and jidt: {1}'.format(
        ais_opencl_2, ais_jidt_2))
    print('AIS for MUTE data proc 3 - opencl: {0} and jidt: {1}'.format(
        ais_opencl_3, ais_jidt_3))
    if not (ais_opencl_2 is np.nan or ais_jidt_2 is np.nan):
        assert (ais_opencl_2 - ais_jidt_2) < 0.05, (
            'AIS results differ between OpenCl and JIDT estimator.')
    else:
        assert ais_opencl_2 is ais_jidt_2, (
            'AIS results differ between OpenCl and JIDT estimator.')
    if not (ais_opencl_3 is np.nan or ais_jidt_3 is np.nan):
        assert (ais_opencl_3 - ais_jidt_3) < 0.05, (
            'AIS results differ between OpenCl and JIDT estimator.')
    else:
        assert ais_opencl_3 is ais_jidt_3, (
            'AIS results differ between OpenCl and JIDT estimator.')
def test_compare_jidt_open_cl_estimator():
    """Compare results from OpenCl and JIDT estimators for AIS calculation."""
    data = Data()
    data.generate_mute_data(1000, 2)
    settings = {
        'cmi_estimator': 'OpenCLKraskovCMI',
        'alpha_mi': 0.05,
        'tail_mi': 'one_bigger',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 5,
        'tau': 1
        }
    processes = [2, 3]
    network_analysis = ActiveInformationStorage()
    res_opencl = network_analysis.analyse_network(settings, data, processes)
    settings['cmi_estimator'] = 'JidtKraskovCMI'
    res_jidt = network_analysis.analyse_network(settings, data, processes)
    # Note that I require equality up to three digits. Results become more
    # exact for bigger data sizes, but this takes too long for a unit test.
    ais_opencl_2 = res_opencl._single_process[2].ais
    ais_jidt_2 = res_jidt._single_process[2].ais
    ais_opencl_3 = res_opencl._single_process[3].ais
    ais_jidt_3 = res_jidt._single_process[3].ais
    print('AIS for MUTE data proc 2 - opencl: {0} and jidt: {1}'.format(
        ais_opencl_2, ais_jidt_2))
    print('AIS for MUTE data proc 3 - opencl: {0} and jidt: {1}'.format(
        ais_opencl_3, ais_jidt_3))
    if not (ais_opencl_2 is np.nan or ais_jidt_2 is np.nan):
        assert (ais_opencl_2 - ais_jidt_2) < 0.05, (
            'AIS results differ between OpenCl and JIDT estimator.')
    else:
        assert ais_opencl_2 is ais_jidt_2, (
            'AIS results differ between OpenCl and JIDT estimator.')
    if not (ais_opencl_3 is np.nan or ais_jidt_3 is np.nan):
        assert (ais_opencl_3 - ais_jidt_3) < 0.05, (
            'AIS results differ between OpenCl and JIDT estimator.')
    else:
        assert ais_opencl_3 is ais_jidt_3, (
            'AIS results differ between OpenCl and JIDT estimator.')
def test_active_information_storage_opencl():
    """Test AIS estimation in MuTE example network."""
    data = Data()
    data.generate_mute_data(1000, 5)
    settings = {
        'cmi_estimator': 'OpenCLKraskovCMI',
        'max_lag': 5,
        'tau': 1,
        'n_perm_mi': 22,
        'alpha_mi': 0.05,
        'tail_mi': 'one',
    }
    processes = [1, 2, 3]
    network_analysis = ActiveInformationStorage()
    results = network_analysis.analyse_network(settings, data, processes)
    print('AIS for MUTE data proc 1: {0}'.format(
        results.get_single_process(1, fdr=False)['ais']))
    print('AIS for MUTE data proc 2: {0}'.format(
        results.get_single_process(2, fdr=False)['ais']))
    print('AIS for MUTE data proc 3: {0}'.format(
        results.get_single_process(3, fdr=False)['ais']))
def test_active_information_storage_opencl():
    """Test AIS estimation in MuTE example network."""
    data = Data()
    data.generate_mute_data(1000, 5)
    settings = {
        'cmi_estimator': 'OpenCLKraskovCMI',
        'max_lag': 5,
        'tau': 1,
        'n_perm_mi': 22,
        'alpha_mi': 0.05,
        'tail_mi': 'one',
        }
    processes = [1, 2, 3]
    network_analysis = ActiveInformationStorage()
    results = network_analysis.analyse_network(settings, data, processes)
    print('AIS for MUTE data proc 1: {0}'.format(
        results.get_single_process(1, fdr=False)['ais']))
    print('AIS for MUTE data proc 2: {0}'.format(
        results.get_single_process(2, fdr=False)['ais']))
    print('AIS for MUTE data proc 3: {0}'.format(
        results.get_single_process(3, fdr=False)['ais']))
def test_add_conditional_manually():
    """Enforce the conditioning on additional variables."""
    settings = {'cmi_estimator': 'JidtKraskovCMI',
                'max_lag': 5,
                'n_perm_max_stat': 21,
                'n_perm_min_stat': 21,
                'n_perm_mi': 21}
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()

    # Add a conditional with a lag bigger than the max_lag requested above
    settings['add_conditionals'] = (8, 0)
    with pytest.raises(IndexError):
        ais.analyse_single_process(settings=settings, data=data, process=0)

    # Add valid conditionals and test if they were added
    settings['add_conditionals'] = [(0, 1), (1, 3)]
    ais._initialise(settings, data, 0)
    # Get list of conditionals after intialisation and convert absolute samples
    # back to lags for comparison.
    cond_list = ais._idx_to_lag(ais.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_single_source_storage_gaussian():
    n = 1000
    proc_1 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    proc_2 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    # Cast everything to numpy so the idtxl estimator understands it.
    dat = Data(np.array([proc_1, proc_2]), dim_order='ps')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_mi': 50,
        'alpha_mi': 0.05,
        'tail_mi': 'one_bigger',
        'n_perm_max_stat': 21,
        'max_lag': 5,
        'tau': 1
        }
    processes = [1]
    network_analysis = ActiveInformationStorage()
    res = network_analysis.analyse_network(settings, dat, processes)
    print('AIS for random normal data without memory (expected is NaN): '
          '{0}'.format(res[1]['ais']))
    assert res[1]['ais'] is np.nan, ('Estimator did not return nan for '
                                     'memoryless data.')
def test_single_source_storage_gaussian():
    n = 1000
    proc_1 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    proc_2 = [rn.normalvariate(0, 1) for r in range(n)]  # correlated src
    # Cast everything to numpy so the idtxl estimator understands it.
    data = Data(np.array([proc_1, proc_2]), dim_order='ps')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'alpha_mi': 0.05,
        'tail_mi': 'one_bigger',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 5,
        'tau': 1
        }
    processes = [1]
    network_analysis = ActiveInformationStorage()
    results = network_analysis.analyse_network(settings, data, processes)
    print('AIS for random normal data without memory (expected is NaN): '
          '{0}'.format(results._single_process[1].ais))
    assert results._single_process[1].ais is np.nan, (
        'Estimator did not return nan for memoryless data.')
Example #14
0
def test_single_source_storage_gaussian():
    n = 1000
    np.random.seed(SEED)
    proc_1 = np.random.normal(0, 1, size=n)
    proc_2 = np.random.normal(0, 1, size=n)
    # Cast everything to numpy so the idtxl estimator understands it.
    data = Data(np.array([proc_1, proc_2]), dim_order='ps')
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'alpha_mi': 0.05,
        'tail_mi': 'one_bigger',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 5,
        'tau': 1
    }
    processes = [1]
    network_analysis = ActiveInformationStorage()
    results = network_analysis.analyse_network(settings, data, processes)
    print('AIS for random normal data without memory (expected is NaN): '
          '{0}'.format(results._single_process[1].ais))
    assert results._single_process[1].ais is np.nan, (
        'Estimator did not return nan for memoryless data.')
def test_analyse_network():
    """Test AIS estimation for the whole network."""
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'max_lag': 5,
        'tau': 1}
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()
    # Test analysis of 'all' processes
    r = ais.analyse_network(settings, data)
    k = list(r.keys())
    assert all(np.array(k) == np.arange(data.n_processes)), (
                'Network analysis did not run on all targets.')
    # Test check for correct definition of processes
    with pytest.raises(ValueError):  # no list
        ais.analyse_network(settings, data=data, processes={})
    with pytest.raises(ValueError):  # no list of ints
        ais.analyse_network(settings, data=data, processes=[1.5, 0.7])
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)
    # Test if candidates that are added manually to the conditioning set are
    # removed from the candidate set.
    nw = ActiveInformationStorage()
    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))
def test_analyse_network():
    """Test AIS estimation for the whole network."""
    settings = {
        'cmi_estimator': 'JidtKraskovCMI',
        'n_perm_max_stat': 21,
        'n_perm_min_stat': 21,
        'n_perm_mi': 21,
        'max_lag': 5,
        'tau': 1}
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()
    # Test analysis of 'all' processes
    results = ais.analyse_network(settings, data)
    k = results.processes_analysed
    assert all(np.array(k) == np.arange(data.n_processes)), (
                'Network analysis did not run on all targets.')
    # Test check for correct definition of processes
    with pytest.raises(ValueError):  # no list
        ais.analyse_network(settings, data=data, processes={})
    with pytest.raises(ValueError):  # no list of ints
        ais.analyse_network(settings, data=data, processes=[1.5, 0.7])
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)
    # Test if candidates that are added manually to the conditioning set are
    # removed from the candidate set.
    nw = ActiveInformationStorage()
    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).'
Example #19
0
    def active_info_storage(self,
                            res_states=None,
                            compute_res_states=False,
                            inputs=None,
                            init_state=None):
        """
        Computes Active Information Storage of the ESN using 'IDTxl' library

        Args:
          res_states: the reservoir states to be used in computing AIS, shape [<timeserieslength> x self.res_units]
          compute_res_states: if True, then res_states are computed for the given 'inputs' and 'init_state'
          inputs: shape [<timeserieslength> x self.in_units]
          init_state: shape [1 x self.res_units]

        Returns:
          A tuple of (AIS_results, AIS value for the network)
        
        """

        total_AIS = 0
        targets_list = []

        for n in range(self.res_units):
            sources = []
            for i in range(self.res_units):
                if self.sparse_mask[i][n] == 1.0:
                    if i != n:
                        sources.append(i)

            if len(sources) != 0:
                targets_list.append(n)

        if compute_res_states:
            res_states = self.res_states(inputs=inputs,
                                         init_state=init_state)[0]

        # length of discarded initial transient
        init_transient = 999

        # Dataset for IDTxl MultivariateTE()
        d = Data(res_states[init_transient:, :], dim_order='sp')

        # Settings for ActiveInformationStorage() computation
        settings = {
            'cmi_estimator': 'JidtKraskovCMI',
            'max_lag': 3,
            'n_perm_max_stat': 200,
            'n_perm_min_stat': 200,
            'n_perm_mi': 400,
            'verbose': False
        }

        AIS_results = ActiveInformationStorage().analyse_network(
            settings=settings, data=d, processes=targets_list)
        for i in targets_list:

            if AIS_results.get_single_process(process=i,
                                              fdr=False).ais != None:
                total_AIS += AIS_results.get_single_process(process=i,
                                                            fdr=False).ais

        avg_AIS = total_AIS / len(targets_list)

        return AIS_results, avg_AIS
def test_ActiveInformationStorage_init():
    """Test instance creation for ActiveInformationStorage class."""
    # Test error on missing estimator
    settings = {'max_lag': 5}
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)

    # Test tau larger than maximum lag
    settings['cmi_estimator'] = 'JidtKraskovCMI'
    settings['tau'] = 10
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)
    # Test negative tau and maximum lag
    settings['tau'] = -10
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)
    settings['tau'] = 1
    settings['max_lag'] = -5
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)

    # Invalid: process is not an int
    settings['max_lag'] = 5
    with pytest.raises(RuntimeError):  # no int
        ais.analyse_single_process(settings, data, process=1.5)
    with pytest.raises(RuntimeError):  # negative
        ais.analyse_single_process(settings, data, process=-1)
    with pytest.raises(RuntimeError):  # not in data
        ais.analyse_single_process(settings, data, process=10)
    with pytest.raises(RuntimeError):  # wrong type
        ais.analyse_single_process(settings, data, process={})
# 这个例子其实就是对于单个的随机过程来进行分析的,就是看看$X_n^{(k)}$对$X_{n+1}$的贡献,需要提前设置所要考察的最大的k值。

# In[30]:

# %load ./IDTxl/demos/demo_active_information_storage.py
# Import classes
from idtxl.active_information_storage import ActiveInformationStorage
from idtxl.data import Data

# 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 = ActiveInformationStorage()
settings = {'cmi_estimator': 'JidtGaussianCMI', 'max_lag': 5}

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

# d) Plot list of processes with significant AIS to console
print(results.get_significant_processes(fdr=False))

# ### Partial Information Decomposition

# 下面这个是一个异或问题的例子,输入是两个变量,输出是一个变量

# In[32]:

# # %load ./IDTxl/demos/demo_partial_information_decomposition.py
def test_ActiveInformationStorage_init():
    """Test instance creation for ActiveInformationStorage class."""
    # Test error on missing estimator
    settings = {'max_lag': 5}
    data = Data()
    data.generate_mute_data(10, 3)
    ais = ActiveInformationStorage()
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)

    # Test tau larger than maximum lag
    settings['cmi_estimator'] = 'JidtKraskovCMI'
    settings['tau'] = 10
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)
    # Test negative tau and maximum lag
    settings['tau'] = -10
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)
    settings['tau'] = 1
    settings['max_lag'] = -5
    with pytest.raises(RuntimeError):
        ais.analyse_single_process(settings, data, process=0)

    # Invalid: process is not an int
    settings['max_lag'] = 5
    with pytest.raises(RuntimeError):  # no int
        ais.analyse_single_process(settings, data, process=1.5)
    with pytest.raises(RuntimeError):  # negative
        ais.analyse_single_process(settings, data, process=-1)
    with pytest.raises(RuntimeError):  # not in data
        ais.analyse_single_process(settings, data, process=10)
    with pytest.raises(RuntimeError):  # wrong type
        ais.analyse_single_process(settings, data, process={})

    # Force conditionals
    settings['add_conditionals'] = [(0, 1), (1, 3)]
    settings['n_perm_max_stat'] = 21
    settings['n_perm_min_stat'] = 21
    res = ais.analyse_single_process(settings, data, process=0)
# Import classes
from idtxl.active_information_storage import ActiveInformationStorage
from idtxl.data import Data

# 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 = ActiveInformationStorage()
settings = {'cmi_estimator':  'JidtGaussianCMI',
            'max_lag': 5}

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

# d) Plot list of processes with significant AIS to console
print(results.get_significant_processes(fdr=False))
# Import classes
from idtxl.active_information_storage import ActiveInformationStorage
from idtxl.data import Data

# 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 = ActiveInformationStorage()
settings = {'cmi_estimator': 'JidtGaussianCMI', 'max_lag': 5}

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

# d) Plot list of processes with significant AIS to console
print(results.get_significant_processes(fdr=False))