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': 21, 'max_lag_sources': 5, 'min_lag_sources': 4 } nw_0 = MultivariateMI() # Test all to all analysis results = nw_0.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_0.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_0.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 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_min_stat': 21, 'n_perm_max_seq': 21, 'n_perm_omnibus': 21, 'max_lag_sources': 5, 'min_lag_sources': 4} nw_0 = MultivariateMI() # Test all to all analysis results = nw_0.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_0.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_0.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 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_permute_time(): """Create surrogates by permuting data in time instead of over replic.""" # Test if perm type is set to default default = 'random' 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': 21, 'max_lag_sources': 5, 'min_lag_sources': 4, 'permute_in_time': True} nw_0 = MultivariateMI() results = nw_0.analyse_network( settings, data, targets='all', sources='all') assert results.settings.perm_type == default, ( 'Perm type was not set to default.')
def analyze_data(ROI_nodes, file_to_read, counter, analysis): start_time = time.time() #start timer n_nodes = len(ROI_nodes) raw_data = set_up_data( ROI_nodes, file_to_read) #parse tsv file and convert to formatted numpy array data = Data(raw_data, dim_order='ps') #convert to Data type for use with IDTXL # b) Initialise analysis object and define settings if analysis == 1: network_analysis = MultivariateMI() else: network_analysis = MultivariateTE() settings = { 'cmi_estimator': 'JidtGaussianCMI', 'max_lag_sources': 5, 'min_lag_sources': 1 } results = network_analysis.analyse_network(settings=settings, data=data) time_elapsed = (time.time()) - start_time adj_matrix = results.get_adjacency_matrix(weights='max_te_lag', fdr=False) # adj_matrix.print_matrix() # print(adj_matrix._weight_matrix) all_adj_matrix.append(adj_matrix._weight_matrix) # print(type(all_adj_matrix)) # print(type(all_adj_matrix[0])) if type == 1: file_name = mMI_pickle_path + file_to_read[:-4] + " mMI - " + str(n_nodes) + \ " ROIs results (min lag 1, max lag 5) pickled.p" #create pickle file name based on analyzed file's name else: file_name = mTE_pickle_path + file_to_read[:-4] + " mTE - " + str(n_nodes) + \ " ROIs results (min lag 1, max lag 1) pickled.p" pickle.dump(results, open(file_name, 'wb')) #pickle results to a file #results = pickle.load(open('results.p', 'rb')) #open the pickled file return (time_elapsed)
def test_permute_time(): """Create surrogates by permuting data in time instead of over replic.""" # Test if perm type is set to default default = 'random' 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': 21, 'max_lag_sources': 5, 'min_lag_sources': 4, 'permute_in_time': True } nw_0 = MultivariateMI() results = nw_0.analyse_network(settings, data, targets='all', sources='all') assert results.settings.perm_type == default, ( 'Perm type was not set to default.')
def test_return_local_values(): """Test estimation of local values.""" max_lag = 5 data = Data() data.generate_mute_data(500, 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 = 1 mi = MultivariateMI() results = mi.analyse_network(settings, data, targets=[target]) # Test if any sources were inferred. If not, return (this may happen # sometimes due to too few samples, however, a higher no. samples is not # feasible for a unit test). if results.get_single_target(target, fdr=False)['mi'] is None: return lmi = results.get_single_target(target, fdr=False)['mi'] n_sources = len(results.get_target_sources(target, fdr=False)) assert type(lmi) is np.ndarray, ( 'LMI estimation did not return an array of values: {0}'.format(lmi)) assert lmi.shape[0] == n_sources, ( 'Wrong dim (no. sources) in LMI estimate: {0}'.format(lmi.shape)) assert lmi.shape[1] == data.n_realisations_samples( (0, max_lag)), ('Wrong dim (no. samples) in LMI estimate: {0}'.format( lmi.shape)) assert lmi.shape[2] == data.n_replications, ( 'Wrong dim (no. replications) in LMI estimate: {0}'.format(lmi.shape)) # Test for correctnes of single link MI estimation by comparing it to the # omnibus MI. In this case (single source), the two should be the same. # Skip assertion if more than one source was inferred (this happens # sometime due to random data and low no. permutations for statistical # testing in unit tests). settings['local_values'] = False results_avg = mi.analyse_network(settings, data, targets=[target]) if results_avg.get_single_target(target, fdr=False)['mi'] is None: return mi_single_link = results_avg.get_single_target(target, fdr=False)['mi'] mi_omnibus = results_avg.get_single_target(target, fdr=False)['omnibus_mi'] sources_local = results.get_target_sources(target, fdr=False) sources_avg = results_avg.get_target_sources(target, fdr=False) if len(sources_avg) == 1: print('Compare single link and omnibus MI.') assert np.isclose(mi_single_link, mi_omnibus, rtol=0.00005), ( 'Single link MI ({0:.6f}) is not equal to omnibus information ' '({1:.6f}).'.format(mi_single_link[0], mi_omnibus)) # Check if average and mean local values are the same. Test each source # separately. Inferred sources may differ between the two calls to # analyse_network() due to low number of surrogates used in unit testing. for s in list(set(sources_avg).intersection(sources_local)): print('Compare average and local values.') i1 = np.where(sources_avg == s)[0][0] i2 = np.where(sources_local == s)[0][0] assert np.isclose( mi_single_link[i1], np.mean(lmi[i2, :, :]), rtol=0.00005), ( 'Single link average MI ({0:.6f}) and mean LMI ({1:.6f}) ' ' deviate.'.format(mi_single_link, np.mean(lmi)))
# Import classes from idtxl.multivariate_mi import MultivariateMI 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 = MultivariateMI() 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_JidtKraskovCMI_MMI_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 = MultivariateMI() network_analysis2 = MultivariateMI() network_analysis3 = MultivariateMI() network_analysis4 = MultivariateMI() # 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 = {'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, 'write_ckp': True, 'filename_ckp': filename_ckp1} # Settings resuming from checkpoint. settings3 = {'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, 'write_ckp': True, '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]) 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 = MultivariateMI() 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_return_local_values(): """Test estimation of local values.""" max_lag = 5 data = Data() data.generate_mute_data(500, 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 = 1 mi = MultivariateMI() results = mi.analyse_network(settings, data, targets=[target]) # Test if any sources were inferred. If not, return (this may happen # sometimes due to too few samples, however, a higher no. samples is not # feasible for a unit test). if results.get_single_target(target, fdr=False)['mi'] is None: return lmi = results.get_single_target(target, fdr=False)['mi'] n_sources = len(results.get_target_sources(target, fdr=False)) assert type(lmi) is np.ndarray, ( 'LMI estimation did not return an array of values: {0}'.format( lmi)) assert lmi.shape[0] == n_sources, ( 'Wrong dim (no. sources) in LMI estimate: {0}'.format(lmi.shape)) assert lmi.shape[1] == data.n_realisations_samples((0, max_lag)), ( 'Wrong dim (no. samples) in LMI estimate: {0}'.format(lmi.shape)) assert lmi.shape[2] == data.n_replications, ( 'Wrong dim (no. replications) in LMI estimate: {0}'.format(lmi.shape)) # Test for correctnes of single link MI estimation by comparing it to the # omnibus MI. In this case (single source), the two should be the same. # Skip assertion if more than one source was inferred (this happens # sometime due to random data and low no. permutations for statistical # testing in unit tests). settings['local_values'] = False results_avg = mi.analyse_network(settings, data, targets=[target]) if results_avg.get_single_target(target, fdr=False)['mi'] is None: return mi_single_link = results_avg.get_single_target(target, fdr=False)['mi'] mi_omnibus = results_avg.get_single_target(target, fdr=False)['omnibus_mi'] sources_local = results.get_target_sources(target, fdr=False) sources_avg = results_avg.get_target_sources(target, fdr=False) if len(sources_avg) == 1: print('Compare single link and omnibus MI.') assert np.isclose(mi_single_link, mi_omnibus, rtol=0.00005), ( 'Single link MI ({0:.6f}) is not equal to omnibus information ' '({1:.6f}).'.format(mi_single_link[0], mi_omnibus)) # Check if average and mean local values are the same. Test each source # separately. Inferred sources may differ between the two calls to # analyse_network() due to low number of surrogates used in unit testing. for s in list(set(sources_avg).intersection(sources_local)): print('Compare average and local values.') i1 = np.where(sources_avg == s)[0][0] i2 = np.where(sources_local == s)[0][0] assert np.isclose(mi_single_link[i1], np.mean(lmi[i2, :, :]), rtol=0.00005), ( 'Single link average MI ({0:.6f}) and mean LMI ({1:.6f}) ' ' deviate.'.format(mi_single_link, np.mean(lmi)))