def test_modularity_finetune_und(): x = load_sample(thres=.4) seed = 94885236 _, q = bc.modularity.modularity_finetune_und(x, seed=seed) assert np.allclose(q, .25879794) fails = 0 for i in range(100): _, q = bc.modularity.modularity_finetune_und(x) try: assert np.allclose(q, .25, atol=0.03) except AssertionError: if fails >= 5: raise else: fails += 1 seed = 71040925 ci, oq = bc.modularity.modularity_louvain_und(x, seed=seed) _, q = bc.modularity.modularity_finetune_und(x, ci=ci, seed=seed) print(q, oq) # assert np.allclose(q, .25892588) assert np.allclose(q, .25856714) assert q - oq >= -1e6 ci, oq = bc.modularity.modularity_und(x) for i in range(100): _, q = bc.modularity.modularity_finetune_und(x, ci=ci) assert np.allclose(q, .25, atol=0.002) assert q - oq >= -1e6
def test_shannon_entropy(): x = load_sample(thres=0.4) ci = np.load(op.join(MAT_DIR, 'sample_partition.npy')) hpos, _ = centrality.diversity_coef_sign(x, ci) print(np.sum(hpos)) print(hpos[-1]) assert np.allclose(np.sum(hpos), 102.6402, atol=.01)
def test_breadthdist(): x = load_sample(thres=.02) r, d = bc.distance.breadthdist(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
def test_consensus(): x = load_sample(thres=.38) ci = bc.clustering.consensus_und(x, .1, reps=50) print(np.max(ci), 4) assert np.max(ci) == 4 _, q = bc.modularity.modularity_und(x, kci=ci) print(q, 0.27) assert np.allclose(q, 0.27, atol=.01)
def test_distance_wei(): x = load_sample(thres=.02) d, e = bc.distance.distance_wei(x) d[np.where(np.isinf(d))] = 0 print(np.sum(d), np.sum(e)) assert np.allclose(np.sum(d), 155650.1, atol=.01) assert np.sum(e) == 30570
def test_charpath(): x = load_sample(thres=.02) d, e = bc.distance.distance_wei(x) l, eff, ecc, radius, diameter = bc.distance.charpath(d) assert np.any(np.isinf(d)) assert not np.isnan(radius) assert not np.isnan(diameter)
def test_zi(): x = load_sample(thres=.4) ci = np.load(op.join(MAT_DIR, 'sample_partition.npy')) zi = np.load(op.join(MAT_DIR, 'sample_zi.npy')) zi_ = centrality.module_degree_zscore(x, ci) print(list(zip(zi, zi_))) assert np.allclose(zi, zi_, atol=0.05)
def test_glob_eff_bin(): x = load_sample(thres=.4) geff = bc.distance.efficiency_bin(x) y = bc.utils.binarize(x) geff2 = bc.distance.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_loc_eff_bin(): x = load_sample(thres=.4) leff = bc.distance.efficiency_bin(x, local=True) y = bc.utils.binarize(x) leff2 = bc.distance.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_pc(): x = load_sample(thres=.4) # ci,q = bc.modularity_und(x) ci = np.load(op.join(MAT_DIR, 'sample_partition.npy')) pc = np.load(op.join(MAT_DIR, 'sample_pc.npy')) pc_ = centrality.participation_coef(x, ci) print(list(zip(pc, pc_))) assert np.allclose(pc, pc_, atol=0.02)
def test_gateway(): x = load_sample(thres=.1) ci = np.load(op.join(MAT_DIR, 'sample_partition.npy')) g_pos, _ = centrality.gateway_coef_sign(x, ci) print(np.sum(g_pos), 43.4382) assert np.allclose(np.sum(g_pos), 43.4382, atol=.001) gpb, _ = centrality.gateway_coef_sign(x, ci, centrality_type='betweenness') print(np.sum(gpb), 43.4026) assert np.allclose(np.sum(gpb), 43.4026, atol=.001)
def test_reachdist(): x = load_sample(thres=.02) r, d = bc.distance.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 = bc.utils.binarize(x, copy=False) br, bd = bc.distance.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_modularity_louvain_und(): x = load_sample(thres=.4) seed = 38429004 _, q = bc.modularity.modularity_louvain_und(x, seed=seed) assert np.allclose(q, 0.25892588) fails = 0 for i in range(100): ci, q = bc.modularity.modularity_louvain_und(x) try: assert np.allclose(q, .25, atol=0.01) except AssertionError: if fails >= 5: raise else: fails += 1 seed = 94885236 _, q = bc.modularity.modularity_finetune_und(x, seed=seed) assert np.allclose(q, .25879794)
def test_community_louvain(): x = load_sample(thres=0.4) seed = 39185 ci, q = bc.modularity.community_louvain(x, seed=seed) print(q) assert np.allclose(q, 0.2583, atol=0.015)
def test_transitivity_bu(): x = bc.utils.binarize(load_sample(thres=.17), copy=False) t = bc.clustering.transitivity_bu(x) print(t, 0.42763) assert np.allclose(t, 0.42763107)
def test_cluscoef_bu(): x = bc.utils.binarize(load_sample(thres=.17), copy=False) cc = bc.clustering.clustering_coef_bu(x) print(np.sum(cc), 60.1016) assert np.allclose(np.sum(cc), 60.10160458)
def test_strengths_und(): x = load_sample() s = bc.degree.strengths_und(x) assert np.allclose(np.sum(s), 38967.38702018)
def test_transitivity_wu(): x = load_sample(thres=.23) t = bc.clustering.transitivity_wu(x) print(t, 1.32927829) assert np.allclose(t, 1.32927829)
def test_cluscoef_wu(): x = load_sample(thres=.23) cc = bc.clustering.clustering_coef_wu(x) print(np.sum(cc), 187.95878414) assert np.allclose(np.sum(cc), 187.95878414)
def test_threshold_absolute(): x = load_sample() x = bc.utils.threshold_absolute(x, 2.1) assert np.allclose(np.sum(x), 13280.17768104)
def test_glob_eff(): x = load_sample(thres=.4) geff = bc.distance.efficiency_wei(x) print(geff, 1.8784) assert np.allclose(geff, 1.8784, atol=1e-4)
def test_distance_bin(): x = bc.utils.binarize(load_sample(thres=.02), copy=False) d = bc.distance.distance_bin(x) d[np.where(np.isinf(d))] = 0 print(np.sum(d)) assert np.sum(d) == 30506 # deals with diagonals differently
def test_degrees_und(): x = load_sample() s = bc.degree.degrees_und(bc.utils.threshold_proportional(x, .26)) assert np.sum(s) == 4916
def test_invert(): x = load_sample() s = bc.utils.invert(bc.utils.threshold_proportional(x, .13)) assert np.allclose(np.sum(s), 790.43107587)
def test_normalize(): x = load_sample() s = bc.utils.normalize(bc.utils.threshold_proportional(x, .79)) assert np.allclose(np.sum(s), 3327.96285964)
def test_binarize(): x = load_sample() s = bc.utils.binarize(bc.utils.threshold_proportional(x, .41)) assert np.sum(s) == 7752
def test_link_communities(): x = load_sample(thres=0.4) M = bc.modularity.link_communities(x) assert np.max(M) == 1
def test_modularity_und(): x = load_sample(thres=.4) _, q = bc.modularity.modularity_und(x) assert np.allclose(q, 0.24097717)
def test_threshold_proportional(): x = load_sample() x = bc.utils.threshold_proportional(x, .5, copy=True) assert np.allclose(np.sum(x), 22548.51206965)
def test_threshold_proportional_nocopy(): x = load_sample() bc.utils.threshold_proportional(x, .3, copy=False) assert np.allclose(np.sum(x), 15253.75425406)