def test_all_random_graphs_yield_correct_number_of_nodes_and_edges(self):

        G, A, D = random_graph.target_attraction(N=426, N_edges=2000)
        self.assertEqual(len(G.nodes()), 426)
        self.assertEqual(len(G.edges()), 2000)

        G, A, D = random_graph.source_growth(N=426, N_edges=2000)
        self.assertEqual(len(G.nodes()), 426)
        self.assertEqual(len(G.edges()), 2000)
Example #2
0
    def test_all_random_graphs_yield_correct_number_of_nodes_and_edges(self):

        G, A, D = random_graph.target_attraction(N=426, N_edges=2000)
        self.assertEqual(len(G.nodes()), 426)
        self.assertEqual(len(G.edges()), 2000)

        G, A, D = random_graph.source_growth(N=426, N_edges=2000)
        self.assertEqual(len(G.nodes()), 426)
        self.assertEqual(len(G.edges()), 2000)
Example #3
0
def construct_graph_list_und(graphs_to_const):
    """Construct and return a list of graphs so graph construction is easily
    repeatable.

    Can handle: Random, Small-world, Scale-free, SGPA, SGPA-random"""

    graph_list = []

    # Always construct and add Allen Institute mouse brain to list
    G_brain = brain_graph.binary_undirected()[0]
    graph_list.append(G_brain)

    # Calculate degree & clustering coefficient distribution
    n_nodes = G_brain.order()

    brain_degree = nx.degree(G_brain).values()
    brain_degree_mean = np.mean(brain_degree)

    # Construct degree controlled random
    if 'Random' in graphs_to_const:
        G_RAND = und_graphs.random_simple_deg_seq(sequence=brain_degree,
                                                  brain_size=BRAIN_SIZE,
                                                  tries=1000)[0]
        graph_list.append(G_RAND)

    # Construct small-world graph
    if 'Small-world' in graphs_to_const:
        graph_list.append(
            nx.watts_strogatz_graph(n_nodes, int(round(brain_degree_mean)),
                                    SW_REWIRE_PROB))

    # Construct scale-free graph
    if 'Scale-free' in graphs_to_const:
        graph_list.append(
            nx.barabasi_albert_graph(n_nodes,
                                     int(round(brain_degree_mean / 2.))))

    # Construct SGPA graph
    if 'SGPA' in graphs_to_const:
        G_SGPA = source_growth(bc.num_brain_nodes,
                               bc.num_brain_edges_directed,
                               L=LENGTH_SCALE)[0]
        graph_list.append(G_SGPA.to_undirected())

        # Construct degree-controlled SGPA graph
        if 'SGPA-random' in graphs_to_const:
            SGPA_degree = nx.degree(G_SGPA).values()
            G_SGPA_RAND = und_graphs.random_simple_deg_seq(
                sequence=SGPA_degree, brain_size=BRAIN_SIZE, tries=1000)[0]
            graph_list.append(G_SGPA_RAND)

    # Error check that we created correct number of graphs
    if len(graph_list) != len(graphs_to_const):
        raise RuntimeError('Graph list/names don\'t match')

    return graph_list
def construct_graph_list_und(graphs_to_const):
    """Construct and return a list of graphs so graph construction is easily
    repeatable.

    Can handle: Random, Small-world, Scale-free, SGPA, SGPA-random"""

    graph_list = []

    # Always construct and add Allen Institute mouse brain to list
    G_brain = brain_graph.binary_undirected()[0]
    graph_list.append(G_brain)

    # Calculate degree & clustering coefficient distribution
    n_nodes = G_brain.order()

    brain_degree = nx.degree(G_brain).values()
    brain_degree_mean = np.mean(brain_degree)

    # Construct degree controlled random
    if 'Random' in graphs_to_const:
        G_RAND = und_graphs.random_simple_deg_seq(
            sequence=brain_degree, brain_size=BRAIN_SIZE, tries=1000)[0]
        graph_list.append(G_RAND)

    # Construct small-world graph
    if 'Small-world' in graphs_to_const:
        graph_list.append(nx.watts_strogatz_graph(
            n_nodes, int(round(brain_degree_mean)), SW_REWIRE_PROB))

    # Construct scale-free graph
    if 'Scale-free' in graphs_to_const:
        graph_list.append(nx.barabasi_albert_graph(
            n_nodes, int(round(brain_degree_mean / 2.))))

    # Construct SGPA graph
    if 'SGPA' in graphs_to_const:
        G_SGPA = source_growth(bc.num_brain_nodes, bc.num_brain_edges_directed,
                               L=LENGTH_SCALE)[0]
        graph_list.append(G_SGPA.to_undirected())

        # Construct degree-controlled SGPA graph
        if 'SGPA-random' in graphs_to_const:
            SGPA_degree = nx.degree(G_SGPA).values()
            G_SGPA_RAND = und_graphs.random_simple_deg_seq(
                sequence=SGPA_degree, brain_size=BRAIN_SIZE, tries=1000)[0]
            graph_list.append(G_SGPA_RAND)

    # Error check that we created correct number of graphs
    if len(graph_list) != len(graphs_to_const):
        raise RuntimeError('Graph list/names don\'t match')

    return graph_list
Example #5
0
###############################################
if not os.path.isfile(TEMP_FILE_NAME):
    # load brain graph
    print('Loading brain graph...')
    g_brain, a_brain, labels = brain_graph.binary_directed()
    brain_in_deg = g_brain.in_degree().values()
    brain_out_deg = g_brain.out_degree().values()
    # load distance matrix
    d_brain = load_brain_dist_matrix(labels, in_mm=True)

    # make two SG graphs and two TA graphs (each one with either L=0.725 or 0)
    print('Making example models...')
    g_sg_l_inf, a_sg_l_inf, d_sg_l_inf = source_growth(
        N=bc.num_brain_nodes,
        N_edges=bc.num_brain_edges_directed,
        L=np.inf,
        gamma=1,
        brain_size=BRAIN_SIZE)
    g_sg_l_0725, a_sg_l_0725, d_sg_l_0725 = source_growth(
        N=bc.num_brain_nodes,
        N_edges=bc.num_brain_edges_directed,
        L=L,
        gamma=1,
        brain_size=BRAIN_SIZE)
    g_ta_l_inf, a_ta_l_inf, d_ta_l_inf = target_attraction(
        N=bc.num_brain_nodes,
        N_edges=bc.num_brain_edges_directed,
        L=np.inf,
        gamma=1,
        brain_size=BRAIN_SIZE)
    g_ta_l_0725, a_ta_l_0725, d_ta_l_0725 = target_attraction(
Example #6
0
ALPHA = 0.5

L = 0.725
BRAIN_SIZE = [7., 7., 7.]

######################################
# Create graphs and calculate metrics
######################################

# create attachment and growth models
G_attachment = target_attraction(N=bc.num_brain_nodes,
                                 N_edges=bc.num_brain_edges_directed, L=L,
                                 gamma=1., brain_size=BRAIN_SIZE)[0]

G_growth = source_growth(N=bc.num_brain_nodes,
                         N_edges=bc.num_brain_edges_directed, L=L, gamma=1.,
                         brain_size=BRAIN_SIZE)[0]

# Get in- & out-degree
indeg_attachment = np.array([G_attachment.in_degree()[node]
                             for node in G_attachment])
outdeg_attachment = np.array([G_attachment.out_degree()[node]
                              for node in G_attachment])
deg_attachment = indeg_attachment + outdeg_attachment

indeg_growth = np.array([G_growth.in_degree()[node] for node in G_growth])
outdeg_growth = np.array([G_growth.out_degree()[node] for node in G_growth])
deg_growth = indeg_growth + outdeg_growth

# Calculate proportion in degree
percent_indeg_attachment = indeg_attachment / deg_attachment.astype(float)
Example #7
0
L = np.inf
BRAIN_SIZE = [7., 7., 7.]

# create attachment and growth models
Gattachment, _, _ = target_attraction(
    N=bc.num_brain_nodes,
    N_edges=bc.num_brain_edges_directed,
    L=L,
    gamma=1.,
    brain_size=BRAIN_SIZE,
)

Ggrowth, _, _ = source_growth(
    N=bc.num_brain_nodes,
    N_edges=bc.num_brain_edges_directed,
    L=L,
    gamma=1.,
    brain_size=BRAIN_SIZE,
)

# Get in- & out-degree
indeg_attachment = np.array(
    [Gattachment.in_degree()[node] for node in Gattachment])
outdeg_attachment = np.array(
    [Gattachment.out_degree()[node] for node in Gattachment])
deg_attachment = indeg_attachment + outdeg_attachment

indeg_growth = np.array([Ggrowth.in_degree()[node] for node in Ggrowth])
outdeg_growth = np.array([Ggrowth.out_degree()[node] for node in Ggrowth])
deg_growth = indeg_growth + outdeg_growth
Example #8
0
RECIPROCITY_FILE_NAME = 'reciprocity.npy'

###############################################
if not os.path.isfile(TEMP_FILE_NAME):
    # load brain graph
    print('Loading brain graph...')
    g_brain, a_brain, labels = brain_graph.binary_directed()
    brain_in_deg = g_brain.in_degree().values()
    brain_out_deg = g_brain.out_degree().values()
    # load distance matrix
    d_brain = load_brain_dist_matrix(labels, in_mm=True)

    # make two SG graphs and two TA graphs (each one with either L=0.725 or 0)
    print('Making example models...')
    g_sg_l_inf, a_sg_l_inf, d_sg_l_inf = source_growth(
        N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf,
        gamma=1, brain_size=BRAIN_SIZE)
    g_sg_l_0725, a_sg_l_0725, d_sg_l_0725 = source_growth(
        N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L,
        gamma=1, brain_size=BRAIN_SIZE)
    g_ta_l_inf, a_ta_l_inf, d_ta_l_inf = target_attraction(
        N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf,
        gamma=1, brain_size=BRAIN_SIZE)
    g_ta_l_0725, a_ta_l_0725, d_ta_l_0725 = target_attraction(
        N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L,
        gamma=1, brain_size=BRAIN_SIZE)

    # make graphs and calculate and save reciprocities if not done yet
    if not os.path.isfile(RECIPROCITY_FILE_NAME):
        print('Looping through construction of models for reciprocity...')
        algos = {'sg': source_growth, 'ta': target_attraction}
Example #9
0
# Calculate degree & clustering coefficient distribution
brain_degree = nx.degree(G_brain).values()
brain_clustering = nx.clustering(G_brain).values()
brain_degree_mean = np.mean(brain_degree)

# Calculate metrics specially because no repeats can be done on brain
brain_metrics = calc_metrics(G_brain, metrics)
for met_i, bm in enumerate(metrics):
    met_arr[graph_names.index('Connectome'), :, met_i] = bm(G_brain)

print 'Running metric table with %d repeats\n' % repeats
for rep in np.arange(repeats):
    # SGPA model
    if 'SGPA' in graph_names:
        G_SGPA = source_growth(bc.num_brain_nodes, L=LENGTH_SCALE)[0]
        met_arr[graph_names.index('SGPA'), rep, :] = \
            calc_metrics(G_SGPA.to_undirected(), metrics)

    # Random Configuration model (random with fixed degree sequence)
    if 'Random' in graph_names:
        G_CM = binary_undirected.random_simple_deg_seq(
            sequence=brain_degree, brain_size=BRAIN_SIZE, tries=100)[0]
        met_arr[graph_names.index('Random'), rep, :] = \
            calc_metrics(G_CM, metrics)

    # Small-world (Watts-Strogatz) model with standard reconnection prob
    if 'Small-world' in graph_names:
        G_SW = nx.watts_strogatz_graph(n_nodes, int(round(brain_degree_mean)),
                                       SW_REWIRE_PROB)
        met_arr[graph_names.index('Small-world'), rep, :] = \