def test_add_conditional_manually(): """Enforce the conditioning on additional variables.""" settings = { 'cmi_estimator': 'JidtKraskovCMI', 'max_lag_sources': 5, 'min_lag_sources': 3 } nw = BivariateMI() data = Data(seed=SEED) 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_bivariate_mi_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_sources': 5, 'min_lag_sources': 4 } target = 0 data = Data(normalise=False, seed=SEED) 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 = BivariateMI() 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, settings['max_lag_sources'])) assert (nw._current_value_realisations[:, 0] == data.data[target, -1, :]).all()
def test_bivariate_mi_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_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 = BivariateMI() 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, settings['max_lag_sources'])) assert (nw._current_value_realisations[:, 0] == data.data[target, -1, :]).all()
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} nw_1 = BivariateMI() 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_faes_method(): """Check if the Faes method is working.""" settings = {'cmi_estimator': 'JidtKraskovCMI', 'add_conditionals': 'faes', 'max_lag_sources': 5, 'min_lag_sources': 3} nw_1 = BivariateMI() 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(): """Enforce the conditioning on additional variables.""" settings = {'cmi_estimator': 'JidtKraskovCMI', 'max_lag_sources': 5, 'min_lag_sources': 3} nw = BivariateMI() 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.')