def test_modularity_finetune_und(): x = load_sample(thres=.4) seed = 94885236 _, q = bct.modularity_finetune_und(x, seed=seed) assert np.allclose(q, .25879794) fails = 0 for i in range(100): _, q = bct.modularity_finetune_und(x) try: assert np.allclose(q, .25, atol=0.03) except AssertionError: if fails >= 5: raise else: fails += 1 seed = 71040925 ci, oq = bct.modularity_louvain_und(x, seed=seed) _, q = bct.modularity_finetune_und(x, ci=ci, seed=seed) print(q, oq) # assert np.allclose(q, .25892588) assert np.allclose(q, .25856714) assert q - oq >= -1e6 ci, oq = bct.modularity_und(x) for i in range(100): _, q = bct.modularity_finetune_und(x, ci=ci) assert np.allclose(q, .25, atol=0.002) assert q - oq >= -1e6
def get_modularity(self, graph): """ Gets modularity louvain Uses bct package Returns communities and modularity value at highest level :param graph: Input graph via networkx :return c: communities :return q: modularity value """ c, q = bct.modularity_louvain_und(nx.adjacency_matrix(graph).toarray()) return (c, q)
def test_modularity_louvain_und(): x = load_sample(thres=.4) seed = 38429004 _, q = bct.modularity_louvain_und(x, seed=seed) assert np.allclose(q, 0.25892588) fails = 0 for i in range(100): ci, q = bct.modularity_louvain_und(x) try: assert np.allclose(q, .25, atol=0.01) except AssertionError: if fails >= 5: raise else: fails += 1 seed = 94885236 _, q = bct.modularity_finetune_und(x, seed=seed) assert np.allclose(q, .25879794)
def reorder_corr(dataframe, numberPartitions, consensusMatrixThreshold, LouvainMethod, gamma, seed): dataframe_np = dataframe.as_matrix() module_dict = bct.modularity_louvain_und(dataframe_np, gamma=1, hierarchy=False, seed=0)[0] [ignore1, ignore2, module_dict] = cb.consensus_clustering_louvain(np.asarray(dataframe), numberPartitions, consensusMatrixThreshold, LouvainMethod, gamma, seed) a = list(module_dict.values()) b = [] for i in range(len(a)): b.append(a[i][0][0]) new_order = np.concatenate(b) ordered_matrix = (dataframe_np[new_order, :])[:, new_order] return (ordered_matrix, new_order, module_dict)
ntwk = [] thresh = bct.threshold_proportional(corrmat, p, copy=True) #network measures of interest here #global efficiency ge = bct.efficiency_wei(thresh) ge_s.append(ge) #characteristic path length cp = bct.charpath(thresh) cp_s.append(cp[0]) #modularity md = bct.modularity_louvain_und(thresh) md_s.append(md[1]) df.at[(subject, session, task, conds[i], mask), 'efficiency'] = np.trapz(ge_s, dx=0.01) df.at[(subject, session, task, conds[i], mask), 'charpath'] = np.trapz(cp_s, dx=0.01) df.at[(subject, session, task, conds[i], mask), 'modularity'] = np.trapz(md_s, dx=0.01) #df.to_csv(join(sink_dir, 'resting-state_graphtheory_shen+craddock.csv'), sep=',') lab_notebook.at[(subject, session, task, conds[i], mask), 'end'] = str(datetime.datetime.now()) except Exception as e: print(e, subject, session)
craddock_thresh = bct.threshold_proportional(craddock_corrmat, p, copy=True) #network measures of interest here #global efficiency ge = bct.efficiency_wei(shen_thresh) ge_s.append(ge) ge = bct.efficiency_wei(craddock_thresh) ge_c.append(ge) #characteristic path length cp = bct.charpath(shen_thresh) cp_s.append(cp[0]) cp = bct.charpath(craddock_thresh) cp_c.append(cp[0]) #modularity md = bct.modularity_louvain_und(shen_thresh) md_s.append(md[1]) md = bct.modularity_louvain_und(craddock_thresh) md_c.append(md[1]) df.at[(int(subject), session), 'shen-efficiency'] = np.trapz(ge_s, dx=0.1) df.loc[(int(subject), session), 'shen-charpath'] = np.trapz(cp_s, dx=0.1) df.loc[(int(subject), session), 'shen-modularity'] = np.trapz(md_s, dx=0.1) df.loc[(int(subject), session), 'craddock-efficiency'] = np.trapz(ge_c, dx=0.1) df.loc[(int(subject), session), 'craddock-charpath'] = np.trapz(cp_c, dx=0.1) df.loc[(int(subject), session), 'craddock-modularity'] = np.trapz(ge_c, dx=0.1) #df.to_csv(join(sink_dir, 'resting-state_graphtheory_shen+craddock.csv'), sep=',') except Exception as e: print(e, subject, session) df.to_csv(join(sink_dir, 'resting-state_graphtheory_shen+craddock.csv'), sep=',') df.to_csv(join(sink_dir, 'resting-state_graphtheory_shen+craddock.csv'), sep=',')
def main(): parser = _build_arg_parser() args = parser.parse_args() assert_inputs_exist(parser, [args.in_length_matrix, args.in_conn_matrix]) if args.verbose: logging.basicConfig(level=logging.DEBUG) if not args.append_json: assert_outputs_exist(parser, args, args.out_json) else: logging.debug('Using --append_json, make sure to delete {} ' 'before re-launching a group analysis.'.format( args.out_json)) if args.append_json and args.overwrite: parser.error('Cannot use the append option at the same time as ' 'overwrite.\nAmbiguous behavior, consider deleting the ' 'output json file first instead.') conn_matrix = load_matrix_in_any_format(args.in_conn_matrix) len_matrix = load_matrix_in_any_format(args.in_length_matrix) if args.filtering_mask: mask_matrix = load_matrix_in_any_format(args.filtering_mask) conn_matrix *= mask_matrix len_matrix *= mask_matrix N = len_matrix.shape[0] if args.avg_node_wise: func_cast = avg_cast else: func_cast = list_cast gtm_dict = {} betweenness_centrality = bct.betweenness_wei(len_matrix) / ((N - 1) * (N - 2)) gtm_dict['betweenness_centrality'] = func_cast(betweenness_centrality) ci, gtm_dict['modularity'] = bct.modularity_louvain_und(conn_matrix, seed=0) gtm_dict['assortativity'] = bct.assortativity_wei(conn_matrix, flag=0) gtm_dict['participation'] = func_cast( bct.participation_coef_sign(conn_matrix, ci)[0]) gtm_dict['clustering'] = func_cast(bct.clustering_coef_wu(conn_matrix)) gtm_dict['nodal_strength'] = func_cast(bct.strengths_und(conn_matrix)) gtm_dict['local_efficiency'] = func_cast( bct.efficiency_wei(len_matrix, local=True)) gtm_dict['global_efficiency'] = func_cast(bct.efficiency_wei(len_matrix)) gtm_dict['density'] = func_cast(bct.density_und(conn_matrix)[0]) # Rich club always gives an error for the matrix rank and gives NaN with warnings.catch_warnings(): warnings.simplefilter("ignore") tmp_rich_club = bct.rich_club_wu(conn_matrix) gtm_dict['rich_club'] = func_cast(tmp_rich_club[~np.isnan(tmp_rich_club)]) # Path length gives an infinite distance for unconnected nodes # All of this is simply to fix that empty_connections = np.where(np.sum(len_matrix, axis=1) < 0.001)[0] if len(empty_connections): len_matrix = np.delete(len_matrix, empty_connections, axis=0) len_matrix = np.delete(len_matrix, empty_connections, axis=1) path_length_tuple = bct.distance_wei(len_matrix) gtm_dict['path_length'] = func_cast(path_length_tuple[0]) gtm_dict['edge_count'] = func_cast(path_length_tuple[1]) if not args.avg_node_wise: for i in empty_connections: gtm_dict['path_length'].insert(i, -1) gtm_dict['edge_count'].insert(i, -1) if args.small_world: gtm_dict['omega'], gtm_dict['sigma'] = omega_sigma(len_matrix) if os.path.isfile(args.out_json) and args.append_json: with open(args.out_json) as json_data: out_dict = json.load(json_data) for key in gtm_dict.keys(): if isinstance(out_dict[key], list): out_dict[key].append(gtm_dict[key]) else: out_dict[key] = [out_dict[key], gtm_dict[key]] else: out_dict = {} for key in gtm_dict.keys(): out_dict[key] = [gtm_dict[key]] with open(args.out_json, 'w') as outfile: json.dump(out_dict, outfile, indent=args.indent, sort_keys=args.sort_keys)
def calculate_modules(adj): ci, _ = bct.modularity_louvain_und(adj) ci2, _ = bct.modularity_finetune_und(adj, ci=ci) return ci2
def calc_graph_vector(filename, thresholds) : ''' This function calculates graph measures for connectivity matrix loaded from textfile and save results under the same name with additional superscript +'_GV' (in same dir filename is located) Input arguments: filename(str): name of file containing connectivity matrix (txt extension) thresholds(list): list containing thresholds of interest # Kamil Bonna, 14.08.2018 ''' #--- check inputs import os if not os.path.exists(filename): raise Exception('{} does not exist'.format(filename)) if type(thresholds) != list: raise Exception('thresholds should be a list!') import numpy as np import bct #=== inner variables N_rep_louvain = 10 # number of Louvain algorithm repetitions N_measures = 10 # number of graph measures gamma = 1 # Louvain resolution parameter #--- load matrix A_raw = np.loadtxt(filename) N = A_raw.shape[0] # number of nodes M_sat = N*(N-1)/2 # max number of connections #=== calculate output graph_measures = np.zeros([ len(thresholds), N_measures ]) # create empty output matrix for thr in range(len(thresholds)) : #--- thresholding A = bct.threshold_proportional( A_raw, p=thresholds[thr], copy=True ); A[np.nonzero(A<0)] = 0 # ensure only positive weights M_act = A[np.nonzero(A>0)].shape[0] / 2 # actual number of nonzero connections #--- calculate measures #-- mean connection strenght S = np.sum(A)/M_act #-- connection strenght std Svar = np.std(A[np.nonzero(A)]) #-- modularity [M,Q] = bct.modularity_louvain_und(A, gamma) for i in range(N_rep_louvain) : [Mt,Qt] = bct.modularity_louvain_und(A, gamma) if Qt > Q : Q = Qt M = Mt #-- participation coefficient P = np.mean(bct.participation_coef_sign(A, M)) #-- clustering C = np.mean(bct.clustering_coef_wu(A)) #-- transitivity T = bct.transitivity_wu(A) #-- assortativity Asso = bct.assortativity_wei(A) #-- global & local efficiency Eglo = bct.efficiency_wei(A) Eloc = np.mean(bct.efficiency_wei(A, local=True)) #-- mean eigenvector centralit Eig = np.mean(bct.eigenvector_centrality_und(A)) #--- write vector to matrix graph_measures[thr] = [ S, Svar, Q, P, C, T, Asso, Eglo, Eloc, Eig ] #=== save results to file np.savetxt( filename[:-4]+'_GV.txt', graph_measures )