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 compute(self): cc = bct.clustering_coef_bu(self.binarized) self.stats['CluseringCoefficient'] = [v for v in cc] average = statistics.mean(self.stats['CluseringCoefficient']) print("Average Clustering Coefficient: " + str(average) + "\n") return self.stats
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_bu(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_bu(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 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
def test_cluscoef_bu(): x = bct.binarize(load_sample(thres=.17), copy=False) cc = bct.clustering_coef_bu(x) assert np.allclose(np.sum(cc), 60.10160458)