def get_outlier_nodes(data, feat="deg", threshold=1e-3): T = len(data) n = len(data[0]) feature = np.empty((T, n)) for t in range(0, T): if feat == "deg": feature[t] = data[t].sum(axis=0) elif feat == "cent": feature[t] = bct.betweenness_wei(data[t]) elif feat == "cc": feature[t] = bct.clustering_coef_wu(data[t]) elif feat == "assort": feature[t] = bct.assortativity_wei(data[t]) diff = forward_diff(forward_diff(feature)) ind_vec = diff > threshold print(ind_vec.sum(axis=None)) return ind_vec
# 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]) # network measures of interest here # global efficiency at = bct.assortativity_wei(thresh) at_s.append(at) # modularity tr = bct.transitivity_wu(thresh) tr_s.append(tr) df.at[(subject, session, task, conds[i], mask), "assortativity"] = np.trapz(ge_s, dx=0.01) df.at[(subject, session, task, conds[i], mask), "transitivity"] = np.trapz(md_s, dx=0.01) 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),
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 process(data): return bct.assortativity_wei(data)
def graph_estimates(cm, th): #dictionary for storing our results d = OrderedDict() #thresholding moved here for other matrices than MatLab matrices #removes negative weights cm = bct.threshold_absolute(cm, 0.0) cm = threshold_connected(cm, th) #for binarizing the connectivity matrices, #we work with weighted so this is turned off #bin_cm = bct.binarize(cm) #invert the connectivity for computing shortest paths cm_inv = bct.invert(cm) #modularity_und is found in modularity.py modularity_und = bct.modularity_und(cm) #the community_affiliation vector that gets input to some of the functions community_affiliation = modularity_und[0] #distance_wei and charpath is found in distance.py distance_wei = bct.distance_wei(cm_inv) charpath = bct.charpath(distance_wei[0], False, False) #clustering_coef_wu is found in clustering.py clustering_coef_wu = bct.clustering_coef_wu(cm) avg_clustering_coef_wu = np.mean(clustering_coef_wu) #assortativity_wei is found in core.py d['assortativity_wei-r'] = bct.assortativity_wei(cm, flag=0) #just taking the average of clustering_coef_wu d['avg_clustering_coef_wu:C'] = avg_clustering_coef_wu d['charpath-lambda'] = charpath[0] #d['charpath-efficiency'] = charpath[1] #d['charpath-ecc'] = charpath[2] #d['charpath-radius'] = charpath[3] #d['charpath-diameter'] = charpath[4] d['clustering_coef_wu-C'] = clustering_coef_wu d['efficiency_wei-Eglob'] = bct.efficiency_wei(cm) #d['efficiency_wei-Eloc'] = bct.efficiency_wei(cm, True) #d['modularity_und-ci'] = modularity_und[0] d['modularity_und-Q'] = modularity_und[1] d['small_worldness:S'] = compute_small_worldness(cm, avg_clustering_coef_wu, charpath[0]) #transitivity_wu can be found in clustering.py d['transitivity_wu-T'] = bct.transitivity_wu(cm) #EXAMPLES for local measures and binary measures. Comment in to use. #VECTOR MEASURES #d['betweenness_wei-BC'] = bct.betweenness_wei(cm_inv) # d['module_degree_zscore-Z'] = bct.module_degree_zscore(cm, community_affiliation) #d['degrees_und-deg'] = bct.degrees_und(cm) #d['charpath-ecc'] = charpath[2] #BINARIES # d['clustering_coef_bu-C'] = bct.clustering_coef_bu(bin_cm) # d['efficiency_bin-Eglob'] = bct.efficiency_bin(bin_cm) # d['efficiency_bin-Eloc'] = bct.efficiency_bin(bin_cm, True) # d['modularity_und_bin-ci'] = modularity_und_bin[0] # d['modularity_und_bin-Q'] = modularity_und_bin[1] # d['transitivity_bu-T'] = bct.transitivity_bu(bin_cm) # d['betweenness_bin-BC'] = bct.betweenness_bin(bin_cm) # modularity_und_bin = bct.modularity_und(bin_cm) #d['participation_coef'] = bct.participation_coef(cm, community_affiliation) ######## charpath giving problems with ecc, radius and diameter # np.seterr(invalid='ignore') return d
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 )