Example #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)
Example #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)
Example #3
0
def test_modularity_louvain_und_sign_seed():
    # performance is same as matlab if randomness is quashed
    x = load_signed_sample()
    seed = 90772777
    _, q = bct.modularity_louvain_und_sign(x, seed=seed)
    print(q)
    assert np.allclose(q, .46605515)
Example #4
0
def test_modularity_louvain_und_sign_seed():
    # performance is same as matlab if randomness is quashed
    x = load_signed_sample()
    seed = 90772777
    _, q = bct.modularity_louvain_und_sign(x, seed=seed)
    print(q)
    assert np.allclose(q, .46605515)
def get_consensus_matrix(network):
    import bct
    import numpy as np
    modules,q = bct.modularity_louvain_und_sign(network, qtype='smp')
    module_matrix = np.repeat(modules,repeats=network.shape[0])
    module_matrix = np.reshape(module_matrix,newshape=network.shape)
    consensus_matrix = module_matrix == module_matrix.transpose()
    return (consensus_matrix.astype('float'), modules, q)
Example #6
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
Example #7
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
Example #8
0
def cal_dynamic_graph(MTD, impose=False, threshold=False):
    '''calculate graph metrics across time(dynamic)'''
    #setup outputs
    time_points = MTD.shape[0]
    ci = np.zeros([MTD.shape[1], MTD.shape[0]])
    q = np.zeros([MTD.shape[0]])
    WMD = np.zeros([MTD.shape[1], MTD.shape[0]])
    PC = np.zeros([MTD.shape[1], MTD.shape[0]])
    WW = np.zeros([MTD.shape[1], MTD.shape[0]])
    BW = np.zeros([MTD.shape[1], MTD.shape[0]])

    #modularity
    if impose:
        ci = np.tile(
            np.loadtxt(
                '/home/despoB/kaihwang/Rest/ThaGate/ROIs/Morel_Striatum_Gordon_CI'
            ), [time_points, 1]).T

    for i, t in enumerate(range(0, time_points)):
        matrix = MTD[i, :, :]

        #need to deal with NANs because of coverage (no signal in some ROIs)
        matrix[np.isnan(matrix)] = 0

        #threshold here
        if threshold:
            matrix = bct.threshold_proportional(matrix, threshold)

        #modularity
        if impose == False:
            ci[:, i], q[i] = bct.modularity_louvain_und_sign(matrix)

        #PC
        # for now, no negative weights
        matrix[matrix < 0] = 0
        PC[:, i] = bct.participation_coef(matrix, ci[:, i])

        #WMD
        WMD[:, i] = bct.module_degree_zscore(matrix, ci[:, i])

        ## within weight
        WW[:, i] = cal_within_weight(matrix, ci[:, i])

        ## between Weight
        BW[:, i] = cal_between_weight(matrix, ci[:, i])

        # cal q using impsose CI partition
        if impose:
            q[i] = cal_modularity_w_imposed_community(matrix, ci[:, i])

    return ci, q, PC, WMD, WW, BW
Example #9
0
def cal_sFC_graph(subject, sequence, roi, impose=False, threshold=1.0):
    ''' load TS and run static FC'''
    ts_path = '/home/despoB/kaihwang/Rest/ThaGate/NotBackedUp/'
    fn = ts_path + str(subject) + '_%s_%s_000.netts' % (roi, sequence)
    ts = np.loadtxt(fn)

    matrix = np.corrcoef(ts)
    matrix[np.isnan(matrix)] = 0

    matrix = bct.threshold_proportional(matrix, threshold)

    num_iter = 200
    consensus = np.zeros((num_iter, matrix.shape[0], matrix.shape[1]))

    for i in np.arange(0, num_iter):
        ci, _ = bct.modularity_louvain_und_sign(matrix, qtype='sta')
        consensus[i, :, :] = community_matrix(ci)

    mean_matrix = np.nanmean(consensus, axis=0)
    mean_matrix[np.isnan(mean_matrix)] = 0
    CI, Q = bct.modularity_louvain_und_sign(mean_matrix, qtype='sta')

    #no negative weights
    matrix[matrix < 0] = 0

    PC = bct.participation_coef(matrix, CI)

    #WMD
    WMD = bct.module_degree_zscore(matrix, CI)

    ## within weight
    WW = cal_within_weight(matrix, CI)

    ## between Weight
    BW = cal_between_weight(matrix, CI)

    return CI, Q, PC, WMD, WW, BW
Example #10
0
def get_consensus_matrix(network):
    """
    #================================
    # Helper function for consensus module assignment
    #================================

    inputs:
    network: numpy array of an adjacency matrix
    """

    import bct
    import numpy as np
    modules, q = bct.modularity_louvain_und_sign(network, qtype='smp')
    module_matrix = np.repeat(modules, repeats=network.shape[0])
    module_matrix = np.reshape(module_matrix, newshape=network.shape)
    consensus_matrix = module_matrix == module_matrix.transpose()
    return (consensus_matrix.astype('float'), modules, q)
Example #11
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
Example #12
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
Example #13
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
Example #14
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
## weighted hub metrics
#degs = a.G.degree(weight='weight')
#extras.writeResults(degs, "degree_wt", ofb, append=appVal)
#
#betCent = mbt.nx.centrality.betweenness_centrality(a.G, weight='distance')
#extras.writeResults(betCent, "betCent_wt", ofb, append=appVal)
#
#closeCent = mbt.nx.centrality.closeness_centrality(a.G, distance='distance')
#extras.writeResults(closeCent, "closeCent_wt", ofb, append=appVal)
#
#eigCent = mbt.nx.centrality.eigenvector_centrality_numpy(a.G)
#extras.writeResults(eigCent, "eigCentNP_wt", ofb, append=appVal)
#del(eigCent)
#
## weighted modularity metrics
ci = bct.modularity_louvain_und_sign(a.bctmat)
Q = ci[1]
ciN = a.assignbctResult(ci[0])
extras.writeResults(Q, "Q_wt", ofb, append=appVal)
extras.writeResults(ciN, "ci_wt", ofb, append=appVal)  

nM = len(np.unique(ci[0]))
extras.writeResults(nM, "nM_wt", ofb, append=appVal)
del(nM)

wmd = extras.withinModuleDegree(a.G, ciN, weight='weight')
extras.writeResults(wmd, "wmd_wt", ofb, append=appVal)
del wmd

#pl = mbt.nx.average_shortest_path_length(a.G, weight="distance")
#extras.writeResults(pl, "pl_wt", ofb, append=appVal)
Example #16
0
        left_matrix = matrix[0:200, 0:200]

        num_iter = 200
        consensus = np.zeros((200, matrix.shape[0], matrix.shape[1]))
        left_consensus = np.zeros(
            (200, left_matrix.shape[0], left_matrix.shape[1]))
        right_consensus = np.zeros(
            (200, right_matrix.shape[0], right_matrix.shape[1]))

        qs = np.zeros(200)
        left_qs = np.zeros(200)
        right_qs = np.zeros(200)

        for i in np.arange(0, num_iter):

            ci, qs[i] = bct.modularity_louvain_und_sign(matrix, qtype='sta')
            consensus[i, :, :] = community_matrix(ci)

            ci, left_qs[i] = bct.modularity_louvain_und_sign(left_matrix,
                                                             qtype='sta')
            left_consensus[i, :, :] = community_matrix(ci)

            ci, right_qs[i] = bct.modularity_louvain_und_sign(right_matrix,
                                                              qtype='sta')
            right_consensus[i, :, :] = community_matrix(ci)

        mean_matrix = np.mean(consensus, axis=0)
        mean_matrix[np.isnan(mean_matrix)] = 0

        mean_left_matrix = np.mean(left_consensus, axis=0)
        mean_left_matrix[np.isnan(mean_left_matrix)] = 0
Example #17
0
		matrix = matrix[2:,:]
		#note, roi 1-200 is left, 201-400 is right
		right_matrix = matrix[200:,200:]
		left_matrix = matrix[0:200,0:200] 

		num_iter = 200
		consensus = np.zeros((200, matrix.shape[0], matrix.shape[1]))
		left_consensus = np.zeros((200, left_matrix.shape[0], left_matrix.shape[1]))
		right_consensus = np.zeros((200, right_matrix.shape[0], right_matrix.shape[1]))

		qs = np.zeros(200)
		left_qs = np.zeros(200)
		right_qs = np.zeros(200)

		for i in np.arange(0,num_iter):
			ci, qs[i] =bct.modularity_louvain_und_sign(matrix, qtype='sta')
			consensus[i, :,:] = community_matrix(ci)

			ci, left_qs[i] =bct.modularity_louvain_und_sign(left_matrix, qtype='sta')
			left_consensus[i, :,:] = community_matrix(ci)

			ci, right_qs[i] =bct.modularity_louvain_und_sign(right_matrix, qtype='sta')
			right_consensus[i, :,:] = community_matrix(ci)		


		#mean_matrix = np.mean(consensus, axis=0)	
		#mean_matrix[np.isnan(mean_matrix)]=0

		#mean_left_matrix = np.mean(left_consensus, axis=0)	
		#mean_left_matrix[np.isnan(mean_left_matrix)]=0