def generate_gauss_data(n_replications=1, discrete=False): settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) covariance_1 = 0.4 covariance_2 = 0.3 n = 10000 delay = 1 if discrete: d = np.zeros((3, n - 2*delay, n_replications), dtype=int) else: d = np.zeros((3, n - 2*delay, n_replications)) for r in range(n_replications): proc_1 = np.random.normal(0, 1, size=n) proc_2 = (covariance_1 * proc_1 + (1 - covariance_1) * np.random.normal(0, 1, size=n)) proc_3 = (covariance_2 * proc_2 + (1 - covariance_2) * np.random.normal(0, 1, size=n)) proc_1 = proc_1[(2*delay):] proc_2 = proc_2[delay:-delay] proc_3 = proc_3[:-(2*delay)] if discrete: # discretise data proc_1_dis, proc_2_dis = est._discretise_vars( var1=proc_1, var2=proc_2) proc_1_dis, proc_3_dis = est._discretise_vars( var1=proc_1, var2=proc_3) d[0, :, r] = proc_1_dis d[1, :, r] = proc_2_dis d[2, :, r] = proc_3_dis else: d[0, :, r] = proc_1 d[1, :, r] = proc_2 d[2, :, r] = proc_3 return d
def test_analytical_surrogates(): # Test generation of analytical surrogates. # Generate data and discretise it such that we can use analytical # surrogates. expected_mi, source1, source2, target = _get_gauss_data(covariance=0.4) settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) source_dis, target_dis = est._discretise_vars(var1=source1, var2=target) data = Data(np.hstack((source_dis, target_dis)), dim_order='sp', normalise=False) settings = { 'cmi_estimator': 'JidtDiscreteCMI', 'n_discrete_bins': 5, # alphabet size of the variables analysed 'n_perm_max_stat': 100, 'n_perm_min_stat': 21, 'n_perm_omnibus': 21, 'n_perm_max_seq': 21, 'max_lag_sources': 5, 'min_lag_sources': 1, 'max_lag_target': 5 } nw = MultivariateTE() res = nw.analyse_single_target(settings, data, target=1) # Check if generation of analytical surrogates is documented in the # settings. assert res.settings.analytical_surrogates, ( 'Surrogates were not created analytically.')
def generate_gauss_data(n_replications=1, discrete=False): settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) covariance_1 = 0.4 covariance_2 = 0.3 n = 10000 delay = 1 if discrete: d = np.zeros((3, n - 2 * delay, n_replications), dtype=int) else: d = np.zeros((3, n - 2 * delay, n_replications)) for r in range(n_replications): proc_1 = np.random.normal(0, 1, size=n) proc_2 = (covariance_1 * proc_1 + (1 - covariance_1) * np.random.normal(0, 1, size=n)) proc_3 = (covariance_2 * proc_2 + (1 - covariance_2) * np.random.normal(0, 1, size=n)) proc_1 = proc_1[(2 * delay):] proc_2 = proc_2[delay:-delay] proc_3 = proc_3[:-(2 * delay)] if discrete: # discretise data proc_1_dis, proc_2_dis = est._discretise_vars(var1=proc_1, var2=proc_2) proc_1_dis, proc_3_dis = est._discretise_vars(var1=proc_1, var2=proc_3) d[0, :, r] = proc_1_dis d[1, :, r] = proc_2_dis d[2, :, r] = proc_3_dis else: d[0, :, r] = proc_1 d[1, :, r] = proc_2 d[2, :, r] = proc_3 return d
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_analytical_surrogates(): # Generate discrete test data. covariance = 0.4 n = 10000 delay = 1 source = np.random.normal(0, 1, size=n) target = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay:] target = target[:-delay] settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) source_dis, target_dis = est._discretise_vars(var1=source, var2=target) data = Data(np.vstack((source_dis, target_dis)), dim_order='ps', normalise=False) settings = { 'cmi_estimator': 'JidtDiscreteCMI', 'n_discrete_bins': 5, # alphabet size of the variables analysed 'n_perm_max_stat': 100, 'n_perm_min_stat': 21, 'n_perm_omnibus': 21, 'n_perm_max_seq': 21, 'max_lag_sources': 5, 'min_lag_sources': 1, 'max_lag_target': 5 } nw = MultivariateTE() res = nw.analyse_single_target(settings, data, target=1) assert res.settings.analytical_surrogates, ( 'Surrogates were not created analytically.')
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_plot_network(): """Test results class for multivariate TE network inference.""" covariance = 0.4 n = 10000 delay = 1 normalisation = False source = np.random.normal(0, 1, size=n) target_1 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_2 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay:] target_1 = target_1[:-delay] target_2 = target_2[:-delay] # Discretise data for speed settings_dis = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings_dis) source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1) source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2) data = Data(np.vstack((source_dis, target_1_dis, target_2_dis)), dim_order='ps', normalise=normalisation) settings = { 'cmi_estimator': 'JidtDiscreteCMI', 'discretise_method': 'none', 'n_discrete_bins': 5, # alphabet size of the variables analysed 'n_perm_max_stat': 21, 'n_perm_omnibus': 30, 'n_perm_max_seq': 30, 'min_lag_sources': 1, 'max_lag_sources': 2, 'max_lag_target': 1, 'alpha_fdr': 0.5 } nw = MultivariateTE() # Analyse a single target and the whole network res_single = nw.analyse_single_target(settings=settings, data=data, target=1) res_network = nw.analyse_network(settings=settings, data=data) graph, fig = plot_network(res_single, 'max_te_lag', fdr=False) plt.close(fig) graph, fig = plot_network(res_network, 'max_te_lag', fdr=False) plt.close(fig) for sign_sources in [True, False]: graph, fig = plot_selected_vars(res_network, target=1, sign_sources=True, fdr=False) plt.close(fig)
def test_one_two_dim_input_discrete(): """Test one- and two-dimensional input for discrete estimators.""" expected_mi, src_one, s, target_one = _get_gauss_data(expand=False, seed=SEED) src_two = np.expand_dims(src_one, axis=1) target_two = np.expand_dims(target_one, axis=1) ar_src_one, s = _get_ar_data(expand=False, seed=SEED) ar_src_two = np.expand_dims(ar_src_one, axis=1) settings = { 'discretise_method': 'equal', 'n_discrete_bins': 4, 'history_target': 1, 'history': 2 } # MI mi_estimator = JidtDiscreteMI(settings=settings) mi_cor_one = mi_estimator.estimate(src_one, target_one) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteMI', 'MI', 0.08) # More variability here mi_cor_two = mi_estimator.estimate(src_two, target_two) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteMI', 'MI', 0.08) # More variability here _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'MI') # CMI cmi_estimator = JidtDiscreteCMI(settings=settings) mi_cor_one = cmi_estimator.estimate(src_one, target_one) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteCMI', 'CMI', 0.08) # More variability here mi_cor_two = cmi_estimator.estimate(src_two, target_two) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteCMI', 'CMI', 0.08) # More variability here _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'CMI') # TE te_estimator = JidtDiscreteTE(settings=settings) mi_cor_one = te_estimator.estimate(src_one[1:], target_one[:-1]) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteTE', 'TE', 0.08) # More variability here mi_cor_two = te_estimator.estimate(src_one[1:], target_one[:-1]) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteTE', 'TE', 0.08) # More variability here _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'TE') # AIS ais_estimator = JidtDiscreteAIS(settings=settings) mi_cor_one = ais_estimator.estimate(ar_src_one) mi_cor_two = ais_estimator.estimate(ar_src_two) _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteAIS one dim', 'JidtDiscreteAIS two dim', 'AIS (AR process)')
def _generate_gauss_data(covariance=0.4, n=10000, delay=1, normalise=False): # Generate two coupled Gaussian time series source = np.random.normal(0, 1, size=n) target = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay:] target = target[:-delay] # Discretise data for speed settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) source_dis, target_dis = est._discretise_vars(var1=source, var2=target) return Data(np.vstack((source_dis, target_dis)), dim_order='ps', normalise=normalise)
def _generate_gauss_data(covariance=0.4, n=10000, delay=1, normalise=False): # Generate two coupled Gaussian time series source = np.random.normal(0, 1, size=n) target = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay:] target = target[:-delay] # Discretise data for speed settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) source_dis, target_dis = est._discretise_vars(var1=source, var2=target) return Data(np.vstack((source_dis, target_dis)), dim_order='ps', normalise=normalise)
def test_cmi_gauss_data_no_cond(): """Test estimators on correlated Gauss data without a conditional. The estimators should return the MI if no conditional variable is provided. Note that the calculation is based on a random variable (because the generated data is a set of random variables) - the result will be of the order of what we expect, but not exactly equal to it; in fact, there will be a large variance around it. """ expected_mi, source1, source2, target = _get_gauss_data() # Test Kraskov mi_estimator = JidtKraskovCMI(settings={}) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtKraskovCMI', 'CMI (no cond.)') _assert_result(mi_uncor, 0, 'JidtKraskovCMI', 'CMI (uncorr., no cond.)') # Test Gaussian mi_estimator = JidtGaussianCMI(settings={}) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtGaussianCMI', 'CMI (no cond.)') _assert_result(mi_uncor, 0, 'JidtGaussianCMI', 'CMI (uncorr., no cond.)') # Test Discrete settings = {'discretise_method': 'equal', 'num_discrete_bins': 5} mi_estimator = JidtDiscreteCMI(settings=settings) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtDiscreteCMI', 'CMI (no cond.)') _assert_result(mi_uncor, 0, 'JidtDiscreteCMI', 'CMI (uncorr., no cond.)')
def test_cmi_gauss_data(): """Test CMI estimation on two sets of Gaussian random data. The first test is on correlated variables, the second on uncorrelated variables. Note that the calculation is based on a random variable (because the generated data is a set of random variables) - the result will be of the order of what we expect, but not exactly equal to it; in fact, there will be a large variance around it. """ expected_mi, source1, source2, target = _get_gauss_data() # Test Kraskov mi_estimator = JidtKraskovCMI(settings={}) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtKraskovCMI', 'CMI (corr.)') _assert_result(mi_uncor, 0, 'JidtKraskovCMI', 'CMI (uncorr.)') # Test Gaussian mi_estimator = JidtGaussianCMI(settings={}) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtGaussianCMI', 'CMI (corr.)') _assert_result(mi_uncor, 0, 'JidtGaussianCMI', 'CMI (uncorr.)') # Test Discrete settings = {'discretise_method': 'equal', 'num_discrete_bins': 5} mi_estimator = JidtDiscreteCMI(settings=settings) mi_cor = mi_estimator.estimate(source1, target) mi_uncor = mi_estimator.estimate(source2, target) _assert_result(mi_cor, expected_mi, 'JidtDiscreteCMI', 'CMI (corr.)') _assert_result(mi_uncor, 0, 'JidtDiscreteCMI', 'CMI (uncorr.)')
def test_one_two_dim_input_discrete(): """Test one- and two-dimensional input for discrete estimators.""" expected_mi, src_one, s, target_one = _get_gauss_data(expand=False) src_two = np.expand_dims(src_one, axis=1) target_two = np.expand_dims(target_one, axis=1) ar_src_one, s = _get_ar_data(expand=False) ar_src_two = np.expand_dims(ar_src_one, axis=1) settings = {'discretise_method': 'equal', 'n_discrete_bins': 4, 'history_target': 1, 'history': 2} # MI mi_estimator = JidtDiscreteMI(settings=settings) mi_cor_one = mi_estimator.estimate(src_one, target_one) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteMI', 'MI') mi_cor_two = mi_estimator.estimate(src_two, target_two) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteMI', 'MI') _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'MI') # CMI cmi_estimator = JidtDiscreteCMI(settings=settings) mi_cor_one = cmi_estimator.estimate(src_one, target_one) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteCMI', 'CMI') mi_cor_two = cmi_estimator.estimate(src_two, target_two) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteCMI', 'CMI') _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'CMI') # TE te_estimator = JidtDiscreteTE(settings=settings) mi_cor_one = te_estimator.estimate(src_one[1:], target_one[:-1]) _assert_result(mi_cor_one, expected_mi, 'JidtDiscreteTE', 'TE') mi_cor_two = te_estimator.estimate(src_one[1:], target_one[:-1]) _assert_result(mi_cor_two, expected_mi, 'JidtDiscreteTE', 'TE') _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteMI one dim', 'JidtDiscreteMI two dim', 'TE') # AIS ais_estimator = JidtDiscreteAIS(settings=settings) mi_cor_one = ais_estimator.estimate(ar_src_one) mi_cor_two = ais_estimator.estimate(ar_src_two) _compare_result(mi_cor_one, mi_cor_two, 'JidtDiscreteAIS one dim', 'JidtDiscreteAIS two dim', 'AIS (AR process)')
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))
def test_results_network_inference(): """Test results class for multivariate TE network inference.""" covariance = 0.4 n = 10000 delay = 1 normalisation = False source = np.random.normal(0, 1, size=n) target_1 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_2 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) corr_expected = covariance / ( 1 * np.sqrt(covariance**2 + (1-covariance)**2)) expected_mi = calculate_mi(corr_expected) source = source[delay:] target_1 = target_1[:-delay] target_2 = target_2[:-delay] # Discretise data for speed settings_dis = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings_dis) source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1) source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2) data = Data(np.vstack((source_dis, target_1_dis, target_2_dis)), dim_order='ps', normalise=normalisation) nw = MultivariateTE() # TE - single target res_single_multi_te = nw.analyse_single_target( settings=settings, data=data, target=1) # TE whole network res_network_multi_te = nw.analyse_network(settings=settings, data=data) nw = BivariateTE() # TE - single target res_single_biv_te = nw.analyse_single_target( settings=settings, data=data, target=1) # TE whole network res_network_biv_te = nw.analyse_network(settings=settings, data=data) nw = MultivariateMI() # TE - single target res_single_multi_mi = nw.analyse_single_target( settings=settings, data=data, target=1) # TE whole network res_network_multi_mi = nw.analyse_network(settings=settings, data=data) nw = BivariateMI() # TE - single target res_single_biv_mi = nw.analyse_single_target( settings=settings, data=data, target=1) # TE whole network res_network_biv_mi = nw.analyse_network(settings=settings, data=data) res_te = [res_single_multi_te, res_network_multi_te, res_single_biv_te, res_network_biv_te] res_mi = [res_single_multi_mi, res_network_multi_mi, res_single_biv_mi, res_network_biv_mi] res_all = res_te + res_mi # Check estimated values for res in res_te: est_te = res._single_target[1].omnibus_te assert np.isclose(est_te, expected_mi, atol=0.05), ( 'Estimated TE for discrete variables is not correct. Expected: ' '{0}, Actual results: {1}.'.format(expected_mi, est_te)) for res in res_mi: est_mi = res._single_target[1].omnibus_mi assert np.isclose(est_mi, expected_mi, atol=0.05), ( 'Estimated TE for discrete variables is not correct. Expected: ' '{0}, Actual results: {1}.'.format(expected_mi, est_mi)) est_te = res_network_multi_te._single_target[2].omnibus_te assert np.isclose(est_te, expected_mi, atol=0.05), ( 'Estimated TE for discrete variables is not correct. Expected: {0}, ' 'Actual results: {1}.'.format(expected_mi, est_te)) est_mi = res_network_multi_mi._single_target[2].omnibus_mi assert np.isclose(est_mi, expected_mi, atol=0.05), ( 'Estimated TE for discrete variables is not correct. Expected: {0}, ' 'Actual results: {1}.'.format(expected_mi, est_mi)) # Check data parameters in results objects n_nodes = 3 n_realisations = n - delay - max( settings['max_lag_sources'], settings['max_lag_target']) for res in res_all: assert res.data_properties.n_nodes == n_nodes, 'Incorrect no. nodes.' assert res.data_properties.n_nodes == n_nodes, 'Incorrect no. nodes.' assert res.data_properties.n_realisations == n_realisations, ( 'Incorrect no. realisations.') assert res.data_properties.n_realisations == n_realisations, ( 'Incorrect no. realisations.') assert res.data_properties.normalised == normalisation, ( 'Incorrect value for data normalisation.') assert res.data_properties.normalised == normalisation, ( 'Incorrect value for data normalisation.') adj_matrix = res.get_adjacency_matrix('binary', fdr=False) assert adj_matrix.shape[0] == n_nodes, ( 'Incorrect number of rows in adjacency matrix.') assert adj_matrix.shape[1] == n_nodes, ( 'Incorrect number of columns in adjacency matrix.') assert adj_matrix.shape[0] == n_nodes, ( 'Incorrect number of rows in adjacency matrix.') assert adj_matrix.shape[1] == n_nodes, ( 'Incorrect number of columns in adjacency matrix.')
from idtxl.data import Data from idtxl.multivariate_te import MultivariateTE # 1 Use core esimtators with data discretisation. Generate Gaussian test data # and call JIDT Discrete estimators using the build-in discretization. n = 1000 covariance = 0.4 corr_expected = covariance / (1 * np.sqrt(covariance**2 + (1 - covariance)**2)) expected_mi = calculate_mi(corr_expected) source_cor = np.random.normal(0, 1, size=n) # correlated src source_uncor = np.random.normal(0, 1, size=n) # uncorrelated src target = (covariance * source_cor + (1 - covariance) * np.random.normal(0, 1, size=n)) settings = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings) cmi = est.estimate(source_cor, target, source_uncor) print('Estimated CMI: {0:.5f}, expected CMI: {1:.5f}'.format(cmi, expected_mi)) settings['history_target'] = 1 est = JidtDiscreteTE(settings) te = est.estimate(source_cor[1:n], target[0:n - 1]) print('Estimated TE: {0:.5f}, expected TE: {1:.5f}'.format(te, expected_mi)) # 2 Use network inference algorithms on discrete data. n_procs = 5 alphabet_size = 5 data = Data(np.random.randint(0, alphabet_size, size=(n, n_procs)), dim_order='sp', normalise=False) # don't normalize discrete data # Initialise analysis object and define settings
def test_delay_reconstruction(): """Test the reconstruction of information transfer delays from results.""" covariance = 0.4 corr_expected = covariance / ( 1 * np.sqrt(covariance**2 + (1-covariance)**2)) expected_mi = calculate_mi(corr_expected) n = 10000 delay_1 = 1 delay_2 = 3 delay_3 = 5 normalisation = False source = np.random.normal(0, 1, size=n) target_1 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_2 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_3 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay_3:] target_1 = target_1[(delay_3-delay_1):-delay_1] target_2 = target_2[(delay_3-delay_2):-delay_2] target_3 = target_3[:-delay_3] # Discretise data for speed settings_dis = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings_dis) source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1) source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2) source_dis, target_3_dis = est._discretise_vars(var1=source, var2=target_3) data = Data( np.vstack((source_dis, target_1_dis, target_2_dis, target_3_dis)), dim_order='ps', normalise=normalisation) nw = MultivariateTE() settings = { 'cmi_estimator': 'JidtDiscreteCMI', 'discretise_method': 'none', 'n_discrete_bins': 5, # alphabet size of the variables analysed 'n_perm_max_stat': 21, 'n_perm_omnibus': 30, 'n_perm_max_seq': 30, 'min_lag_sources': 1, 'max_lag_sources': delay_3 + 1, 'max_lag_target': 1} res_network = nw.analyse_single_target( settings=settings, data=data, target=1) res_network.combine_results(nw.analyse_single_target( settings=settings, data=data, target=2)) res_network.combine_results(nw.analyse_single_target( settings=settings, data=data, target=3)) adj_mat = res_network.get_adjacency_matrix('max_te_lag', fdr=False) print(adj_mat) assert adj_mat[0, 1] == delay_1, ('Estimate for delay 1 is not correct.') assert adj_mat[0, 2] == delay_2, ('Estimate for delay 2 is not correct.') assert adj_mat[0, 3] == delay_3, ('Estimate for delay 3 is not correct.') for target in range(1, 4): est_mi = res_network._single_target[target].omnibus_te assert np.isclose(est_mi, expected_mi, atol=0.05), ( 'Estimated TE for target {0} is not correct. Expected: {1}, ' 'Actual results: {2}.'.format(target, expected_mi, est_mi))
def test_delay_reconstruction(): """Test the reconstruction of information transfer delays from results.""" covariance = 0.4 corr_expected = covariance / (1 * np.sqrt(covariance**2 + (1 - covariance)**2)) expected_mi = calculate_mi(corr_expected) n = 10000 delay_1 = 1 delay_2 = 3 delay_3 = 5 normalisation = False source = np.random.normal(0, 1, size=n) target_1 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_2 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) target_3 = (covariance * source + (1 - covariance) * np.random.normal(0, 1, size=n)) source = source[delay_3:] target_1 = target_1[(delay_3 - delay_1):-delay_1] target_2 = target_2[(delay_3 - delay_2):-delay_2] target_3 = target_3[:-delay_3] # Discretise data for speed settings_dis = {'discretise_method': 'equal', 'n_discrete_bins': 5} est = JidtDiscreteCMI(settings_dis) source_dis, target_1_dis = est._discretise_vars(var1=source, var2=target_1) source_dis, target_2_dis = est._discretise_vars(var1=source, var2=target_2) source_dis, target_3_dis = est._discretise_vars(var1=source, var2=target_3) data = Data(np.vstack( (source_dis, target_1_dis, target_2_dis, target_3_dis)), dim_order='ps', normalise=normalisation) nw = MultivariateTE() settings = { 'cmi_estimator': 'JidtDiscreteCMI', 'discretise_method': 'none', 'n_discrete_bins': 5, # alphabet size of the variables analysed 'n_perm_max_stat': 21, 'n_perm_omnibus': 30, 'n_perm_max_seq': 30, 'min_lag_sources': 1, 'max_lag_sources': delay_3 + 1, 'max_lag_target': 1 } res_network = nw.analyse_single_target(settings=settings, data=data, target=1) res_network.combine_results( nw.analyse_single_target(settings=settings, data=data, target=2)) res_network.combine_results( nw.analyse_single_target(settings=settings, data=data, target=3)) adj_mat = res_network.get_adjacency_matrix('max_te_lag', fdr=False) adj_mat.print_matrix() assert adj_mat._weight_matrix[0, 1] == delay_1, ( 'Estimate for delay 1 is not correct.') assert adj_mat._weight_matrix[0, 2] == delay_2, ( 'Estimate for delay 2 is not correct.') assert adj_mat._weight_matrix[0, 3] == delay_3, ( 'Estimate for delay 3 is not correct.') for target in range(1, 4): est_mi = res_network._single_target[target].omnibus_te assert np.isclose(est_mi, expected_mi, atol=0.05), ( 'Estimated TE for target {0} is not correct. Expected: {1}, ' 'Actual results: {2}.'.format(target, expected_mi, est_mi))
def test_invalid_settings_input(): """Test handling of wrong inputs for settings dictionary.""" # Wrong input type for settings dict. with pytest.raises(TypeError): JidtDiscreteMI(settings=1) with pytest.raises(TypeError): JidtDiscreteCMI(settings=1) with pytest.raises(TypeError): JidtDiscreteAIS(settings=1) with pytest.raises(TypeError): JidtDiscreteTE(settings=1) with pytest.raises(TypeError): JidtGaussianMI(settings=1) with pytest.raises(TypeError): JidtGaussianCMI(settings=1) with pytest.raises(TypeError): JidtGaussianAIS(settings=1) with pytest.raises(TypeError): JidtGaussianTE(settings=1) with pytest.raises(TypeError): JidtKraskovMI(settings=1) with pytest.raises(TypeError): JidtKraskovCMI(settings=1) with pytest.raises(TypeError): JidtKraskovAIS(settings=1) with pytest.raises(TypeError): JidtKraskovTE(settings=1) # Test if settings dict is initialised correctly. e = JidtDiscreteMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' e = JidtDiscreteCMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' e = JidtGaussianMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' e = JidtGaussianCMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' e = JidtKraskovMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' e = JidtKraskovCMI() assert type( e.settings) is dict, 'Did not initialise settings as dictionary.' # History parameter missing for AIS and TE estimation. with pytest.raises(RuntimeError): JidtDiscreteAIS(settings={}) with pytest.raises(RuntimeError): JidtDiscreteTE(settings={}) with pytest.raises(RuntimeError): JidtGaussianAIS(settings={}) with pytest.raises(RuntimeError): JidtGaussianTE(settings={}) with pytest.raises(RuntimeError): JidtKraskovAIS(settings={}) with pytest.raises(RuntimeError): JidtKraskovTE(settings={})
def test_local_values(): """Test estimation of local values and their return type.""" expected_mi, source, s, target = _get_gauss_data(expand=False) ar_proc, s = _get_ar_data(expand=False) settings = { 'discretise_method': 'equal', 'n_discrete_bins': 4, 'history_target': 1, 'history': 2, 'local_values': True } # MI - Discrete mi_estimator = JidtDiscreteMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteMI', 'MI', 0.08) # More variability here assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Gaussian mi_estimator = JidtGaussianMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Kraskov mi_estimator = JidtKraskovMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # CMI - Discrete cmi_estimator = JidtDiscreteCMI(settings=settings) mi = cmi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteCMI', 'CMI', 0.08) # More variability here assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Gaussian mi_estimator = JidtGaussianCMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianCMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Kraskov mi_estimator = JidtKraskovCMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovCMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Discrete te_estimator = JidtDiscreteTE(settings=settings) mi = te_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteTE', 'TE', 0.08) # More variability here assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Gaussian mi_estimator = JidtGaussianTE(settings=settings) mi = mi_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianTE', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Kraskov mi_estimator = JidtKraskovTE(settings=settings) mi = mi_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovTE', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # AIS - Kraskov ais_estimator = JidtKraskovAIS(settings=settings) mi_k = ais_estimator.estimate(ar_proc) assert type(mi_k) is np.ndarray, 'Local values are not a numpy array.' # AIS - Discrete ais_estimator = JidtDiscreteAIS(settings=settings) mi_d = ais_estimator.estimate(ar_proc) assert type(mi_d) is np.ndarray, 'Local values are not a numpy array.' # TODO should we compare these? # _compare_result(np.mean(mi_k), np.mean(mi_d), # 'JidtKraskovAIS', 'JidtDiscreteAIS', 'AIS (AR process)') # AIS - Gaussian ais_estimator = JidtGaussianAIS(settings=settings) mi_g = ais_estimator.estimate(ar_proc) assert type(mi_g) is np.ndarray, 'Local values are not a numpy array.' _compare_result(np.mean(mi_k), np.mean(mi_g), 'JidtKraskovAIS', 'JidtGaussianAIS', 'AIS (AR process)')
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.')
from idtxl.estimators_pid import SydneyPID, TartuPID from idtxl.idtxl_utils import calculate_mi # Generate Gaussian test data n = 10000 covariance = 0.4 corr_expected = covariance / (1 * np.sqrt(covariance**2 + (1-covariance)**2)) expected_mi = calculate_mi(corr_expected) source_cor = np.random.normal(0, 1, size=n) # correlated src source_uncor = np.random.normal(0, 1, size=n) # uncorrelated src target = (covariance * source_cor + (1 - covariance) * np.random.normal(0, 1, size=n)) # JIDT Discrete estimators settings = {'discretise_method': 'equal', 'alph1': 5, 'alph2': 5} est = JidtDiscreteCMI(settings) cmi = est.estimate(source_cor, target, source_uncor) print('Estimated CMI: {0:.5f}, expected CMI: {1:.5f}'.format(cmi, expected_mi)) est = JidtDiscreteMI(settings) mi = est.estimate(source_cor, target) print('Estimated MI: {0:.5f}, expected MI: {1:.5f}'.format(mi, expected_mi)) settings['history_target'] = 1 est = JidtDiscreteTE(settings) te = est.estimate(source_cor[1:n], target[0:n - 1]) print('Estimated TE: {0:.5f}, expected TE: {1:.5f}'.format(te, expected_mi)) settings['history'] = 1 est = JidtDiscreteAIS(settings) ais = est.estimate(target) print('Estimated AIS: {0:.5f}, expected AIS: ~0'.format(ais)) # JIDT Kraskov estimators
def test_local_values(): """Test estimation of local values and their return type.""" expected_mi, source, s, target = _get_gauss_data(expand=False) ar_proc, s = _get_ar_data(expand=False) settings = {'discretise_method': 'equal', 'n_discrete_bins': 4, 'history_target': 1, 'history': 2, 'local_values': True} # MI - Discrete mi_estimator = JidtDiscreteMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Gaussian mi_estimator = JidtGaussianMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Kraskov mi_estimator = JidtKraskovMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # CMI - Discrete cmi_estimator = JidtDiscreteCMI(settings=settings) mi = cmi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteCMI', 'CMI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Gaussian mi_estimator = JidtGaussianCMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianCMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # MI - Kraskov mi_estimator = JidtKraskovCMI(settings=settings) mi = mi_estimator.estimate(source, target) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovCMI', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Discrete te_estimator = JidtDiscreteTE(settings=settings) mi = te_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtDiscreteTE', 'TE') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Gaussian mi_estimator = JidtGaussianTE(settings=settings) mi = mi_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtGaussianTE', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # TE - Kraskov mi_estimator = JidtKraskovTE(settings=settings) mi = mi_estimator.estimate(source[1:], target[:-1]) _assert_result(np.mean(mi), expected_mi, 'JidtKraskovTE', 'MI') assert type(mi) is np.ndarray, 'Local values are not a numpy array.' # AIS - Kraskov ais_estimator = JidtKraskovAIS(settings=settings) mi_k = ais_estimator.estimate(ar_proc) assert type(mi_k) is np.ndarray, 'Local values are not a numpy array.' # AIS - Discrete ais_estimator = JidtDiscreteAIS(settings=settings) mi_d = ais_estimator.estimate(ar_proc) assert type(mi_d) is np.ndarray, 'Local values are not a numpy array.' # TODO should we compare these? # _compare_result(np.mean(mi_k), np.mean(mi_d), # 'JidtKraskovAIS', 'JidtDiscreteAIS', 'AIS (AR process)') # AIS - Gaussian ais_estimator = JidtGaussianAIS(settings=settings) mi_g = ais_estimator.estimate(ar_proc) assert type(mi_g) is np.ndarray, 'Local values are not a numpy array.' _compare_result(np.mean(mi_k), np.mean(mi_g), 'JidtKraskovAIS', 'JidtGaussianAIS', 'AIS (AR process)')