Ejemplo n.º 1
0
def cross_modularity(a1, a2):
    # There are some problems with my code
    ma, _ = bct.modularity_louvain_und_sign(a1)
    mb, _ = bct.modularity_louvain_und_sign(a2)

    ma, qa = bct.modularity_finetune_und_sign(a1, ci=ma)
    mb, qb = bct.modularity_finetune_und_sign(a2, ci=mb)

    _, qab = bct.modularity_und_sign(a1, mb)
    _, qba = bct.modularity_und_sign(a2, ma)

    return (qab + qba) / (qa + qb)
Ejemplo n.º 2
0
def cross_modularity(a1, a2):
    # There are some problems with my code
    ma, _ = bct.modularity_louvain_und_sign(a1)
    mb, _ = bct.modularity_louvain_und_sign(a2)

    ma, qa = bct.modularity_finetune_und_sign(a1, ci=ma)
    mb, qb = bct.modularity_finetune_und_sign(a2, ci=mb)

    _, qab = bct.modularity_und_sign(a1, mb)
    _, qba = bct.modularity_und_sign(a2, ma)

    return (qab + qba) / (qa + qb)
Ejemplo n.º 3
0
def test_modularity_finetune_und_sign_actually_finetune():
    x = load_signed_sample()
    seed = 34908314
    ci, oq = bct.modularity_louvain_und_sign(x, seed=seed)
    _, q = bct.modularity_finetune_und_sign(x, seed=seed, ci=ci)
    print(q)
    assert np.allclose(q, .47282924)
    assert q >= oq

    seed = 88215881
    np.random.seed(seed)
    randomized_sample = np.random.random(size=(len(x), len(x)))
    randomized_sample = randomized_sample + randomized_sample.T
    x[np.where(bct.threshold_proportional(randomized_sample, .2))] = 0

    ci, oq = bct.modularity_louvain_und_sign(x, seed=seed)
    print(oq)
    assert np.allclose(oq, .45254522)
    for i in range(100):
        _, q = bct.modularity_finetune_und_sign(x, ci=ci)
        assert q >= oq
Ejemplo n.º 4
0
def test_modularity_finetune_und_sign_actually_finetune():
    x = load_signed_sample()
    seed = 34908314
    ci, oq = bct.modularity_louvain_und_sign(x, seed=seed)
    _, q = bct.modularity_finetune_und_sign(x, seed=seed, ci=ci)
    print(q)
    assert np.allclose(q, .47282924)
    assert q >= oq

    seed = 88215881
    np.random.seed(seed)
    randomized_sample = np.random.random(size=(len(x), len(x)))
    randomized_sample = randomized_sample + randomized_sample.T
    x[np.where(bct.threshold_proportional(randomized_sample, .2))] = 0

    ci, oq = bct.modularity_louvain_und_sign(x, seed=seed)
    print(oq)
    assert np.allclose(oq, .45254522)
    for i in range(100):
        _, q = bct.modularity_finetune_und_sign(x, ci=ci)
        assert q >= oq
Ejemplo n.º 5
0
def sample_degenerate_partitions(w, probtune_cap=.10, modularity_cutoff=.95):
    ntries = 0
    while True:
        init_ci, _ = bct.modularity_louvain_und_sign(w)
        seed_ci, seed_q = bct.modularity_finetune_und_sign(w, ci=init_ci)

        p = (np.random.random() * probtune_cap)
        ci, q = bct.modularity_probtune_und_sign(w, ci=seed_ci, p=p)
        if q > (seed_q * modularity_cutoff):
            print ('found a degenerate partition after %i tries with probtune '
                   'parameter %.3f: %.5f %.5f' % (ntries, p, q, seed_q))
            ntries = 0
            yield ci, q
        else:
            # print 'failed to find degenerate partition, trying again',q,
            # seed_q
            ntries += 1
Ejemplo n.º 6
0
def sample_degenerate_partitions(w, probtune_cap=.10, modularity_cutoff=.95):
    ntries = 0
    while True:
        init_ci, _ = bct.modularity_louvain_und_sign(w)
        seed_ci, seed_q = bct.modularity_finetune_und_sign(w, ci=init_ci)

        p = (np.random.random() * probtune_cap)
        ci, q = bct.modularity_probtune_und_sign(w, ci=seed_ci, p=p)
        if q > (seed_q * modularity_cutoff):
            print(
                'found a degenerate partition after %i tries with probtune '
                'parameter %.3f: %.5f %.5f' % (ntries, p, q, seed_q))
            ntries = 0
            yield ci, q
        else:
            # print 'failed to find degenerate partition, trying again',q,
            # seed_q
            ntries += 1
Ejemplo n.º 7
0
def test_modularity_probtune_und_sign():
	x = load_signed_sample()
	seed = 59468096
	ci,q = bct.modularity_probtune_und_sign(x, seed=seed)
	print q
	assert np.allclose(q, .07885327)

	seed = 1742447
	ci,_ = bct.modularity_louvain_und_sign(x, seed=seed)
	_,oq = bct.modularity_finetune_und_sign(x, seed=seed, ci=ci)
	
	for i in np.arange(.05, .5, .02):
		fails=0
		for j in xrange(100):
			_,q = bct.modularity_probtune_und_sign(x, ci=ci, p=i)
			try:
				assert q < oq
			except AssertionError:
				if fails > 5: raise
				else: fails+=1
Ejemplo n.º 8
0
def test_modularity_probtune_und_sign():
    x = load_signed_sample()
    seed = 59468096
    ci, q = bct.modularity_probtune_und_sign(x, seed=seed)
    print(q)
    assert np.allclose(q, .07885327)

    seed = 1742447
    ci, _ = bct.modularity_louvain_und_sign(x, seed=seed)
    _, oq = bct.modularity_finetune_und_sign(x, seed=seed, ci=ci)

    for i in np.arange(.05, .5, .02):
        fails = 0
        for j in range(100):
            _, q = bct.modularity_probtune_und_sign(x, ci=ci, p=i)
            try:
                assert q < oq
            except AssertionError:
                if fails > 5:
                    raise
                else:
                    fails += 1