Пример #1
0
    def test_reciprocity(self):

        G = nx.DiGraph()
        G.add_nodes_from(range(4))
        G.add_edge(0, 2)
        G.add_edge(2, 0)
        G.add_edge(1, 2)
        G.add_edge(2, 1)
        G.add_edge(2, 3)
        G.add_edge(3, 1)

        reciprocity_correct = 1 / 3

        self.assertAlmostEqual(metrics_bd.reciprocity(G), reciprocity_correct)
Пример #2
0
        rs = {'LS': LS}

        for key, algo in algos.items():
            print(key)
            rs[key] = np.nan * np.zeros((len(LS), N_REPEATS), dtype=float)

            for l_ctr, l in enumerate(LS):
                print(l)
                for repeat in range(N_REPEATS):
                    print(repeat)
                    g = algo(bc.num_brain_nodes,
                             bc.num_brain_edges_directed,
                             L=l,
                             gamma=1,
                             brain_size=BRAIN_SIZE)[0]
                    rs[key][l_ctr, repeat] = metrics.reciprocity(g)

            rs['rand'] = np.nan * np.zeros((N_REPEATS, ), dtype=float)

            for repeat in range(N_REPEATS):
                g_rand = random_directed_deg_seq(in_sequence=brain_in_deg,
                                                 out_sequence=brain_out_deg,
                                                 simplify=True)[0]
                rs['rand'][repeat] = metrics.reciprocity(g_rand)

        np.save(RECIPROCITY_FILE_NAME, np.array([rs]))
    else:
        print('Loading previous reciprocity calculations...')
        rs = np.load(RECIPROCITY_FILE_NAME)[0]

    print('Crunching numbers...')
# Barabasi-Albert
G_BA = nx.barabasi_albert_graph(n_nodes, int(round(brain_degree_mean / 2.)))
BA_degree = nx.degree(G_BA).values()
BA_clustering = nx.clustering(G_BA).values()


# Loop through model graphs with different gamma
model_degrees = [None for gamma in GAMMAS]
model_clusterings = [None for gamma in GAMMAS]

for gamma_idx, gamma in enumerate(GAMMAS):
    L = LS[gamma_idx]
    print 'Generating model graph for gamma = %.2f' % gamma
    G_model_directed, _, _ = rg(n_nodes, n_edges, gamma=gamma, L=L)
    print reciprocity(G_model_directed)
    G_model = G_model_directed.to_undirected()
    model_degree = nx.degree(G_model).values()
    model_clustering = nx.clustering(G_model).values()

    # Store examples
    model_degrees[gamma_idx] = model_degree
    model_clusterings[gamma_idx] = model_clustering

# Make 8 clustering vs. degree plots
fig, axs = plt.subplots(2, 4, facecolor=FACECOLOR, tight_layout=True)

# Brain
axs[BRAIN_POS].scatter(brain_degree, brain_clustering, color=BRAIN_COLOR)

# Standard random graphs
Пример #4
0
from numpy import concatenate as cc

from extract.brain_graph import binary_directed as brain_graph
from random_graph.binary_directed import biophysical_reverse_outdegree_reciprocal as model_graph
from metrics.binary_directed import reciprocity
from network_plot.change_settings import set_all_text_fontsizes as set_fontsize

from brain_constants import *

# PARAMETERS
NER = 100 # number of ER-directed graphs to generate
NMODEL = 100

# load brain graph, adjacency matrix, and labels
Gbrain, Abrain, labels = brain_graph()
reciprocity_brain = reciprocity(G)

# calculate reciprocity for several ER-directed graphs
reciprocity_ER = np.zeros((NER,), dtype=float)
for ctr in range(NER):
    GER = nx.erdos_renyi_graph(num_brain_nodes, p_brain_edge_directed, 
                               directed=True)
    reciprocity_ER[ctr] = reciprocity(GER)

# calculate reciprocity for several model graphs
reciprocity_model = np.zeros((NMODEL,), dtype=float)
for ctr in range(NMODEL):
    print ctr
    Gmodel = model_graph(N=num_brain_nodes, 
                         N_edges=num_brain_edges_directed, 
                         gamma=1., reciprocity=7.)
Пример #5
0
        print('Looping through construction of models for reciprocity...')
        algos = {'sg': source_growth, 'ta': target_attraction}

        rs = {'LS': LS}

        for key, algo in algos.items():
            print(key)
            rs[key] = np.nan * np.zeros((len(LS), N_REPEATS), dtype=float)

            for l_ctr, l in enumerate(LS):
                print(l)
                for repeat in range(N_REPEATS):
                    print(repeat)
                    g = algo(bc.num_brain_nodes, bc.num_brain_edges_directed,
                             L=l, gamma=1, brain_size=BRAIN_SIZE)[0]
                    rs[key][l_ctr, repeat] = metrics.reciprocity(g)

            rs['rand'] = np.nan * np.zeros((N_REPEATS,), dtype=float)

            for repeat in range(N_REPEATS):
                g_rand = random_directed_deg_seq(in_sequence=brain_in_deg,
                                                 out_sequence=brain_out_deg,
                                                 simplify=True)[0]
                rs['rand'][repeat] = metrics.reciprocity(g_rand)

        np.save(RECIPROCITY_FILE_NAME, np.array([rs]))
    else:
        print('Loading previous reciprocity calculations...')
        rs = np.load(RECIPROCITY_FILE_NAME)[0]

    print('Crunching numbers...')