def compute_small_worldness(cm, cc, cpl): #randmio_und_connected can be found in reference.py #second argument is number of iterations #construct a random network for comparison with our real network rand_network = bct.randmio_und_connected(cm,5)[0] #clustering_coef_wu is found in clustering.py #make sure that C_rand is non-zero, so we avoid division with zero #could probably be made more correct by taking the average of #some number of random networks. #we did not do this to keep run time at a minimum C_rand = np.mean(bct.clustering_coef_wu(rand_network)) while C_rand == 0.0: rand_network = bct.randmio_und_connected(cm,5)[0] C_rand = np.mean(bct.clustering_coef_wu(rand_network)) #invert can be found in other.py rand_inv = bct.invert(rand_network) #distance_wei and charpath can be found in distance.py distance_rand = bct.distance_wei(rand_inv) charpath_rand = bct.charpath(distance_rand[0]) #compute the small worldness index according to Rubinov C = cc L = cpl L_rand = charpath_rand[0] Ctemp = C/C_rand Ltemp = L/L_rand S_W = Ctemp / Ltemp return S_W
def SmallWorldIndex(CIJ): """ Computes the small-world index of the graph with connection matrix CIJ. Self-connections are ignored, as they are cyclic paths. Inputs: CIJ -- Graph connectivity matrix. Must be binary (0 or 1) and undirected. """ N = len(CIJ) K = np.sum(np.sum(CIJ)) / len(CIJ) # average degree # Clustering coefficient CC = np.mean(clustering_coef_bu(CIJ)) # Distance matrix [RR, DD] = breadthdist(CIJ) # Note: the charpath implementation of bctpy is not very robust. Expect # some warnings. From the output of charpath use only the characteristic # path length PL = charpath(DD, include_diagonal=False)[0] # Calculate small-world index CCs = CC / (K / N) PLs = PL / (np.log(N) / np.log(K)) SWI = CCs / PLs return (SWI)
def SmallWorldIndex(CIJ): """ Computes the small-world index of the graph with connection matrix CIJ. Self-connections are ignored, as they are cyclic paths. Inputs: CIJ -- Graph connectivity matrix. Must be binary (0 or 1) and undirected. """ N = len(CIJ) K = np.sum(np.sum(CIJ))/len(CIJ) # average degree # Clustering coefficient CC = np.mean(clustering_coef_bu(CIJ)) # Distance matrix [RR, DD] = breadthdist(CIJ) # Note: the charpath implementation of bctpy is not very robust. Expect # some warnings. From the output of charpath use only the characteristic # path length PL = charpath(DD, include_diagonal=False)[0] # Calculate small-world index CCs = CC/(K/N) PLs = PL/(np.log(N)/np.log(K)) SWI = CCs/PLs return(SWI)
def test_charpath(): x = load_sample(thres=.02) d, e = bct.distance_wei(x) l, eff, ecc, radius, diameter = bct.charpath(d) assert np.any(np.isinf(d)) assert not np.isnan(radius) assert not np.isnan(diameter)
def get_network_measures(fname_connectivity): C = pd.read_table(fname_connectivity, header=None, dtype=object) #cleaning up structural data C = C.drop([0, 1], axis=1) C = C.drop([0], axis=0) C = C.iloc[:, :-1] #C_electrode_names = np.array([e[-4:] for e in np.array(C.iloc[0])]) C = np.array(C.iloc[1:, :]).astype( 'float64') #finally turn into numpy array #binarize connectivity matrix C_binarize = bct.weight_conversion(C, "binarize") #Calculate Network Measures: # 1. Density density = bct.density_und(C_binarize)[0] # 2. Degree degree_mean = np.mean(bct.degrees_und(C_binarize)) # 3. Clustering Coefficient clustering_coefficient = bct.clustering_coef_bu(C_binarize) clustering_coefficient_mean = np.mean(clustering_coefficient) # 4. characteristic path length (i.e. average shortest path length) #Get distance C_dist = bct.distance_bin(C_binarize) #If there are any disjointed nodes set them equal to the largest non-Inf length C_dist_max = np.nanmax( C_dist[C_dist != np.inf]) #find the max length (that's not infinity) C_dist[np.where(C_dist == np.inf )] = C_dist_max #find the inifnities, and replace with max characteristic_path_length = bct.charpath(C_dist)[0] # 5. Small Worldness Cr = degree_mean / len(C_binarize) Lr = np.log10(len(C_binarize)) / np.log10(degree_mean) gamma = clustering_coefficient_mean / Cr lamb = characteristic_path_length / Lr sigma = gamma / lamb small_worldness = sigma network_measures = np.zeros(shape=(1, 5)) network_measures[0, :] = [ density, degree_mean, clustering_coefficient_mean, characteristic_path_length, small_worldness ] colLabels = [ "Density", "degree_mean", "clustering_coefficient_mean", "characteristic_path_length", "small_worldness" ] network_measures_df = pd.DataFrame(network_measures, columns=colLabels) return network_measures_df
def small_world_wu(W): ''' An implementation of small worldness. Returned is the coefficient cc/lambda, the ratio of the clustering coefficient to the characteristic path length. This ratio is >>1 for small world networks. inputs: W weighted undirected connectivity matrix output: s small world coefficient ''' cc = clustering_coef_wu(W) _, dists = breadthdist(W) _lambda, _, _, _, _ = charpath(dists) return np.mean(cc) / _lambda
def get_network_measures(ifname_connectivity): C = np.array(pd.DataFrame(loadmat(ifname_connectivity)['connectivity'])) #binarize connectivity matrix C_binarize = bct.weight_conversion(C, "binarize") #Calculate Network Measures: # 1. Density density = bct.density_und(C_binarize)[0] # 2. Degree degree_mean = np.mean(bct.degrees_und(C_binarize)) # 3. Clustering Coefficient clustering_coefficient = bct.clustering_coef_bu(C_binarize) clustering_coefficient_mean = np.mean(clustering_coefficient) # 4. characteristic path length (i.e. average shortest path length) #Get distance C_dist = bct.distance_bin(C_binarize) #If there are any disjointed nodes set them equal to the largest non-Inf length C_dist_max = np.nanmax( C_dist[C_dist != np.inf]) #find the max length (that's not infinity) C_dist[np.where(C_dist == np.inf )] = C_dist_max #find the inifnities, and replace with max characteristic_path_length = bct.charpath(C_dist)[0] # 5. Small Worldness Cr = degree_mean / len(C_binarize) Lr = np.log10(len(C_binarize)) / np.log10(degree_mean) gamma = clustering_coefficient_mean / Cr lamb = characteristic_path_length / Lr sigma = gamma / lamb small_worldness = sigma network_measures = np.zeros(shape=(1, 5)) network_measures[0, :] = [ density, degree_mean, clustering_coefficient_mean, characteristic_path_length, small_worldness ] colLabels = [ "Density", "degree_mean", "clustering_coefficient_mean", "characteristic_path_length", "small_worldness" ] network_measures_df = pd.DataFrame(network_measures, columns=colLabels) return network_measures_df
def extract_epoch_graph_features(W): import bct L = bct.weight_conversion(W, "lengths") L[W == 0] = np.inf D, _ = bct.distance_wei(L) l, eff, ecc, radius, diameter = bct.charpath(D, include_infinite=False) return [ bct.clustering_coef_wu(W), bct.efficiency_wei(W, local=True), bct.betweenness_wei(L), ecc, [l, eff, radius, diameter], ]
def calculate_path_and_efficiency_bin(G): """ Return the following network metrics for your graph. Uses brain connectivity tool-box. Input: G (np.array): binarized matrix, symmetric Outputs: Eglob: Global efficiency average_Elocal: mean Local Efficiency char_path: Average shortest path length clustering: Average Clustering coefficient """ if not np.allclose(G, G.T): raise bct.BCTParamError('Not undirected') average_shortest_path, Eglob, _, _, _ = bct.charpath(bct.distance_bin(G)) average_clustering = np.mean(abs(bct.clustering_coef_bu(G))) average_Elocal = np.mean(bct.efficiency_bin(G, 1)) return Eglob, average_Elocal, average_clustering, average_shortest_path
ge_s = [] cp_s = [] md_s = [] for p in np.arange(kappa_upper, kappa_lower, 0.01): 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],
for subject in subjects: cls = np.load(source_folder + "graph_data/%s_classic_pow_pln.npy" % subject).item() pln = np.load(source_folder + "graph_data/%s_plan_pow_pln.npy" % subject).item() cls_all.append(cls) pln_all.append(pln) for k, band in enumerate(bands.keys()): data_cls = [] for j in range(len(cls_all)): tmp = cls_all[j][band] data_cls.append( np.asarray([bct.charpath(g) for g in tmp]).mean(axis=0)) data_pln = [] for j in range(len(pln_all)): tmp = pln_all[j][band] data_pln.append( np.asarray([bct.charpath(g) for g in tmp]).mean(axis=0)) data_cls = np.asarray(data_cls) data_pln = np.asarray(data_pln) X = np.vstack([data_cls, data_pln]) y = np.concatenate([np.zeros(len(data_cls)), np.ones(len(data_pln))]) cv = StratifiedKFold(y, n_folds=6, shuffle=True) cv_params = {
for toi in tois: cls_all = [] pln_all = [] for subject in subjects: cls = np.load(source_folder + "graph_data/%s_classic_corr_%s_orth.npy" % (subject, toi)) pln = np.load(source_folder + "graph_data/%s_plan_corr_%s_orth.npy" % (subject, toi)) cls_all.append(cls.mean(axis=0)) pln_all.append(pln.mean(axis=0)) data_cls = [bct.charpath(g) for g in cls_all] data_pln = [bct.charpath(g) for g in pln_all] # calc global efficiency cls_ge = np.asarray([g[1] for g in data_cls]) pln_ge = np.asarray([g[1] for g in data_pln]) # calc lambda cls_lambda = np.asarray([g[0] for g in data_cls]) pln_lambda = np.asarray([g[0] for g in data_pln]) # calc the diameter of the graph cls_dia = np.asarray([g[4] for g in data_cls]) pln_dia = np.asarray([g[4] for g in data_pln]) ge_data = pd.DataFrame()
cp_c = [] md_s = [] md_c = [] for p in np.arange(0.1, 1, 0.1): ntwk = [] shen_thresh = bct.threshold_proportional(shen_corrmat, p, copy=True) 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)
def compute(self): distance_matrix = bct.breadthdist(self.binarized)[1] metrics = bct.charpath(distance_matrix) print("Average Path Length: " + str(metrics[0])) print("Network Diameter: " + str(metrics[4]))
conditions = ['classic', "plan"] tois = ["pln", "pre-press", "post-press"] for subject in subjects: print("Working on subject: %s" % subject) for toi in tois: for condition in conditions: data_all = [] pln_all = [] # noinspection PyAssignmentToLoopOrWithParameter data = np.load(source_folder + "graph_data/%s_%s_corr_%s_orth.npy" % (subject, condition, toi)) graph_data = [bct.charpath(g) for g in data] # calc global efficiency data_ge = np.asarray([g[1] for g in graph_data]) # calc lambda data_lambda = np.asarray([g[0] for g in graph_data]) # calc the diameter of the graph data_dia = np.asarray([g[4] for g in graph_data]) ge_data = pd.DataFrame() lambda_data = pd.DataFrame() dia_data = pd.DataFrame() ge_data["ge"] = data_ge
for subject in subjects: cls = np.load(source_folder + "graph_data/%s_classic_pow_pln.npy" % subject).item() pln = np.load(source_folder + "graph_data/%s_plan_pow_pln.npy" % subject).item() cls_all.append(cls) pln_all.append(pln) for k, band in enumerate(bands.keys()): data_cls = [] for j in range(len(cls_all)): tmp = cls_all[j][band] data_cls.append(np.asarray([bct.charpath(g) for g in tmp]).mean(axis=0)) data_pln = [] for j in range(len(pln_all)): tmp = pln_all[j][band] data_pln.append(np.asarray([bct.charpath(g) for g in tmp]).mean(axis=0)) data_cls = np.asarray(data_cls) data_pln = np.asarray(data_pln) X = np.vstack([data_cls, data_pln]) y = np.concatenate([np.zeros(len(data_cls)), np.ones(len(data_pln))]) cv = StratifiedKFold(y, n_folds=6, shuffle=True)
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