def test_modularity_dir_bug71(): """Regression test for bug described in issue #71""" fpath = os.path.join(TEST_DIR, "failing_cases", "modularity_dir_example.csv") x = np.loadtxt(fpath, int, delimiter=',') bct.modularity_dir(x)
def community(file): """ ******* The function uses Girvan-Newman algorithm to get communities from a given network. Packages bct (Brain Connectivity Toolbox) and networkx are required. Slow (>48 hrs for one book) but accurate than community_fast() function. ******* input: file .txt file that contains the network. format: u,v,weight ******* output: communities: N*1 np.ndarray Q-metric: the measure of modularity for networks """ net = generate_netx(file) n_edges = nx.number_of_edges(net) Q_best = 0 while n_edges > 1: #Calculate the betweenness for each edge. The output is a dictionary. edge_betweenness = nx.edge_betweenness_centrality(net,weight='weight') #get the edge(s) with max edge betweenness and remove it(them) max_values = max(edge_betweenness.values()) for key,value in edge_betweenness.items(): if float(value) == max_values: net.remove_edge(key[0],key[1]) n_edges = nx.number_of_edges(net) #transform the network into a numpy array AM = nx.to_numpy_matrix(net) #utilize bct.modularity_dir() function to get community structure and calculate the Q-metric #for current network [ci,Q] = bct.modularity_dir(AM) #update the best Q value if needed if Q>Q_best: Q_best = Q ci_best = ci else: pass words = print_com('unique.txt') ci_best = [[words[i] for i in ci] for ci in ci_best] return ci_best,Q_best
# -*- coding: utf-8 -*- from dyconnmap.graphs import mutual_information import numpy as np import scipy from scipy import io import bct if __name__ == '__main__': rng = np.random.RandomState(0) a = rng.rand(30, 30) np.fill_diagonal(a, 0.0) b = rng.rand(30, 30) np.fill_diagonal(b, 0.0) # In case you get NaNs you have to modify the `gamma` paramter Ca, _ = bct.modularity_dir(a, gamma=1.0) Cb, _ = bct.modularity_dir(b, gamma=1.0) mi, nmi = mutual_information(Ca, Cb) print(mi) print(nmi)
def test_modularity_dir(): x = load_directed_sample() _, q = bct.modularity_dir(x) assert np.allclose(q, .31446049)
def test_modularity_dir_low_modularity(): x = load_directed_low_modularity_sample(thres=.67) _, q = bct.modularity_dir(x) assert np.allclose(q, .06450290)
def test_modularity_dir(): x = load_directed_sample() _, q = bct.modularity_dir(x) print(q, .32742787) assert np.allclose(q, .32742787)
def test_modularity_dir(): x = load_directed_sample() _,q = bct.modularity_dir(x) assert np.allclose(q, .31446049)