Esempio n. 1
0
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)
Esempio n. 2
0
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 calculate_path_and_efficiency_bin(G):
    """
    Return the following network metrics for your graph. Uses brain connectivity tool-box.
    Input:
        G (np.array): binarized matrix, symmetric
    Outputs:
        Eglob: Global efficiency
        average_Elocal: mean Local Efficiency
        char_path: Average shortest path length
        clustering: Average Clustering coefficient
    
    """
    if not np.allclose(G, G.T):
        raise bct.BCTParamError('Not undirected')

    average_shortest_path, Eglob, _, _, _ = bct.charpath(bct.distance_bin(G))
    average_clustering = np.mean(abs(bct.clustering_coef_bu(G)))
    average_Elocal = np.mean(bct.efficiency_bin(G, 1))

    return Eglob, average_Elocal, average_clustering, average_shortest_path
Esempio n. 4
0
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)
Esempio n. 5
0
    return np.where(CIJ[i] > 0)[0]


def min_hop_count(CIJ):
    mhc = bct.distance_bin(CIJ)
    mhc[mhc == 0] = np.Inf

    return mhc


def network_of_neighbours(CIJ, i):
    neighbours = np.where(CIJ[i] > 0)[0]
    return CIJ[neighbours][:, neighbours]


N = int(sys.argv[1])
k = int(sys.argv[2])
p = float(sys.argv[3])

net = NetworkWattsStrogatz(N, k, p)

print("BrainConnectivityToolbox...\n")
print("\tEff[Global] = " + str(bct.efficiency_bin(net)))
print("\tEff[Local]  = " + str(bct.efficiency_bin(net, local=True)))

print("\n\nHomebrew...\n")
print("\tEff[Global] = " + str(global_eff(net)))
print("\tEff[Local]  = " + str(local_eff(net)))

PlotConnectivity(net)
Esempio n. 6
0
def threshold_global_cost_efficiency(mtx, iterations):
    """ Threshold a graph based on the Global Efficiency - Cost formula.

    .. [Basset2009] Bassett, D. S., Bullmore, E. T., Meyer-Lindenberg, A., Apud, J. A., Weinberger, D. R., & Coppola, R. (2009). Cognitive fitness of cost-efficient brain functional networks. Proceedings of the National Academy of Sciences, 106(28), 11747-11752.



    Parameters
    ----------
    mtx : array-like, shape(N, N)
        Symmetric, weighted and undirected connectivity matrix.

    iterations : int
        Number of steps, as a resolution when search for optima.


    Returns
    -------
    binary_mtx : array-like, shape(N, N)
        A binary mask matrix.

    threshold : float
        The threshold that maximizes the global cost efficiency.

    global_cost_eff_max : float
        Global cost efficiency.

    efficiency : float
        Global efficiency.

    cost_max : float
        Cost of the network at the maximum global cost efficiency
    """
    binary_mtx = np.zeros_like(mtx, dtype=np.int32)

    step = 1.0 / iterations

    thresholds = np.arange(0, 1 + step, step)

    N, _ = np.shape(mtx)

    num_connections = (N * (N - 1)) / 2.0

    global_cost_eff = np.zeros((iterations, 1))

    cost = np.zeros((iterations, 1))

    for i in range(iterations):
        tmp_binary = np.zeros_like(binary_mtx)

        for k in range(N):
            for l in range(k + 1, N):
                if mtx[k, l] > thresholds[i]:
                    tmp_binary[k, l] = 1
                    tmp_binary[l, k] = 1

        global_eff = bct.efficiency_bin(tmp_binary)

        degree = bct.degrees_und(tmp_binary)
        total_degree = np.sum(degree)

        cost[i] = (0.5 * total_degree) / num_connections
        global_cost_eff[i] = global_eff - cost[i]

    indx_max = np.argmax(global_cost_eff)
    threshold = thresholds[indx_max]

    for k in range(N):
        for l in range(k + 1, N):
            if mtx[k, l] >= threshold:
                binary_mtx[k, l] = 1
                binary_mtx[l, k] = 1

    cost_max = cost[indx_max]
    global_cost_eff_max = global_cost_eff[indx_max]
    efficiency = bct.efficiency_bin(binary_mtx)

    # import matplotlib.pyplot as plt
    # plt.figure()
    # plt.plot(cost, global_cost_eff)
    # plt.plot(cost_max, global_cost_eff_max, 'b*', label='Max Global Cost Efficiency')
    # plt.title('Economical small-world network at max Global Cost Efficiency')
    # plt.xlabel('Cost')
    # plt.ylabel('Global Cost Efficiency')
    # plt.legend()
    # plt.show()

    return binary_mtx, threshold, global_cost_eff_max, efficiency, cost_max