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 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 min_hop_count(CIJ): mhc = bct.distance_bin(CIJ) mhc[mhc == 0] = np.Inf return mhc
def test_distance_bin(): x = bct.binarize(load_sample(thres=.02), copy=False) d = bct.distance_bin(x) d[np.where(np.isinf(d))] = 0 print(np.sum(d)) assert np.sum(d) == 30506 # deals with diagonals differently