def iterate_modularity_partition(subject, iter): ''' Function to run iterations of Louvain modularity detection, return the one with the highest modularity (Q) usage: ci, q = iterate_modularity_partition(subject, iter) ---- Parameters ---- subject: subject number, will look for its correlation corrmat file with a hard coded path... (should prob change that) iter: int number indicating number of iterations ---- Returns ---- ci : community partition q : modularity ''' fn = '/home/despoB/kaihwang/Rest/AdjMatrices/t%s_Full_WashU333_corrmat' % subject AveMat = np.loadtxt(fn) graph = nx.from_numpy_matrix( bct.binarize(bct.threshold_proportional(AveMat, 0.05))) q = 0 for i in xrange(0, iter): print(i) louvain = weighted_modularity.LouvainCommunityDetection(graph) weighted_partitions = louvain.run() if weighted_partitions[0].modularity() > q: q = weighted_partitions[0].modularity() weighted_partition = weighted_partitions[0] ci = convert_partition_dict_to_array( convert_partition_to_dict(weighted_partition.communities), len(AveMat)) return ci, q
def test_loc_eff_bin(): x = load_sample(thres=.4) leff = bct.efficiency_bin(x, local=True) y = bct.binarize(x) leff2 = bct.efficiency_bin(y, local=True) print(np.sum(leff), np.sum(leff2), 105.5111) assert np.allclose(np.sum(leff), 105.5111, atol=0.1) assert np.allclose(np.sum(leff2), 105.5111, atol=0.1)
def test_glob_eff_bin(): x = load_sample(thres=.4) geff = bct.efficiency_bin(x) y = bct.binarize(x) geff2 = bct.efficiency_bin(y) print(geff, geff2, 0.6999) assert np.allclose(geff, 0.6999, atol=1e-4) assert np.allclose(geff2, 0.6999, atol=1e-4)
def test_reachdist(): x = load_sample(thres=.02) r, d = bct.reachdist(x) d[np.where(np.isinf(d))] = 0 print(np.sum(r), np.sum(d)) assert np.sum(r) == 5804 assert np.sum(d) == 30762 bx = bct.binarize(x, copy=False) br, bd = bct.reachdist(bx) bd[np.where(np.isinf(bd))] = 0 print(np.sum(br), np.sum(bd)) assert np.sum(br) == 5804 assert np.sum(bd) == 30762
def test_binarize(): x = load_sample() s = bct.binarize(bct.threshold_proportional(x, .41)) assert np.sum(s) == 7752
def test_transitivity_bu(): x = bct.binarize(load_sample(thres=.17), copy=False) t = bct.transitivity_bu(x) assert np.allclose(t, 0.42763107)
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)
10 34 14 34 15 34 16 34 19 34 20 34 21 34 23 34 24 34 27 34 28 34 29 34 30 34 31 34 32 34 33 34 """.strip() arr = np.zeros((34, 34), dtype=np.uint8) for row in s.split('\n'): first, second = row.split(' ') arr[int(first) - 1, int(second) - 1] += 1 arr = bct.binarize(arr + arr.T) np.random.seed(1991) eff = bct.efficiency_bin(arr) mod = bct.modularity_und(arr) rand = bct.randmio_und_connected(arr, 5)
def load_binary_directed_low_modularity_sample(thres=.35): return bct.binarize(load_directed_low_modularity_sample(thres=thres))
def load_binary_directed_sample(thres=.35): return bct.binarize(load_directed_sample(thres=thres))
def load_binary_sample(thres=.35): return bct.binarize(load_sample(thres=thres), copy=False)
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