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...') # get reciprocal/nonreciprocal distance histograms for real brain r_dists_brain, non_r_dists_brain = metrics.dists_by_reciprocity( a_brain, d_brain) # do the same for each of the four graphs
graphss = {key: [] for key in KEYS} in_degree_brain = G_brain.in_degree().values() out_degree_brain = G_brain.out_degree().values() for g_ctr in range(N_GRAPH_SAMPLES): Gs = {} # create directed ER graph Gs['er'] = nx.erdos_renyi_graph( bc.num_brain_nodes, bc.p_brain_edge_directed, directed=True) # create directed degree-controlled random graph Gs['rand'] = random_directed_deg_seq(in_sequence=in_degree_brain, out_sequence=out_degree_brain, simplify=True)[0] # create target attraction graph Gs['ta'] = ta(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L, brain_size=BRAIN_SIZE,)[0] # create source growth graph Gs['sg'] = sg(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L, brain_size=BRAIN_SIZE,)[0] # create source growth graph with only topo rule Gs['sg_topo'] = sg(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf,
@author: rkp degree_controlled_random_spurious_edges.py Calculate how many self/multi-edges are removed on average when making a directed degree-controlled random graph with degree-distribution fixed to the brain's. """ from __future__ import division, print_function import numpy as np import networkx as nx from extract import brain_graph from random_graph import binary_directed N_GRAPHS = 30 G_brain_dir, _, _ = brain_graph.binary_directed() in_degree_seq = G_brain_dir.in_degree().values() out_degree_seq = G_brain_dir.out_degree().values() print('Making directed degree-controlled random graphs') n_edges_lost_dir = [] for ctr in range(N_GRAPHS): print(ctr) G_dir, _, _ = binary_directed.random_directed_deg_seq(in_degree_seq, out_degree_seq, simplify=True) n_edges_lost_dir.append(len(G_brain_dir.edges()) - len(G_dir.edges())) print('Directed: mean edges lost +- std = {} +- {}'.format( np.mean(n_edges_lost_dir), np.std(n_edges_lost_dir)))
@author: rkp degree_controlled_random_spurious_edges.py Calculate how many self/multi-edges are removed on average when making a directed degree-controlled random graph with degree-distribution fixed to the brain's. """ from __future__ import division, print_function import numpy as np import networkx as nx from extract import brain_graph from random_graph import binary_directed N_GRAPHS = 30 G_brain_dir, _, _ = brain_graph.binary_directed() in_degree_seq = G_brain_dir.in_degree().values() out_degree_seq = G_brain_dir.out_degree().values() print("Making directed degree-controlled random graphs") n_edges_lost_dir = [] for ctr in range(N_GRAPHS): print(ctr) G_dir, _, _ = binary_directed.random_directed_deg_seq(in_degree_seq, out_degree_seq, simplify=True) n_edges_lost_dir.append(len(G_brain_dir.edges()) - len(G_dir.edges())) print("Directed: mean edges lost +- std = {} +- {}".format(np.mean(n_edges_lost_dir), np.std(n_edges_lost_dir)))
from extract import brain_graph import networkx as nx G_brain, _, _ = brain_graph.binary_directed() p = G_brain.number_of_nodes() / G_brain.number_of_edges() in_sequence = G_brain.in_degree().values() out_sequence = G_brain.out_degree().values() labels = ['Deg. Cont. Rand.', 'ER Rand.', 'Brain'] n_edges_deg_controlled_random = [] n_edges_ER_random = [] for ri in range(N_TRIALS): G_RAND, _, _ = binary_directed.random_directed_deg_seq(in_sequence, out_sequence, simplify=True) G_ER, _, _ = binary_directed.ER_distance(p=p) G_ER = nx.DiGraph(G_ER) # Remove parallel G_ER.remove_edges_from(G_ER.selfloop_edges()) # Remove self-loops n_edges_deg_controlled_random.append(G_RAND.number_of_edges()) n_edges_ER_random.append(G_ER.number_of_edges()) print('Finished repeat: ' + str(ri + 1) + ' of ' + str(N_TRIALS)) _, ax = plt.subplots(1, 1) ax.hist(n_edges_deg_controlled_random, color='b', alpha=0.5, label=labels[0]) ax.hist(n_edges_ER_random, color='r', alpha=0.5, label=labels[1]) ax.axvline(len(G_brain.edges()), color='k', label=labels[2]) #ax.legend(['Deg. Cont. Rand', 'ER Rand.', 'Brain']) ax.legend(loc='best')
G_er = nx.erdos_renyi_graph(bc.num_brain_nodes, bc.p_brain_edge_directed, directed=True) # create sgpa graph G_sgpa, _, _ = sgpa(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=LENGTH_SCALE) # create source growth only graph G_sg, _, _ = sgpa(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf) # create target attraction graph G_ta, _, _ = ta(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf) # create directed degree-controlled random graph G_rand, _, _ = random_directed_deg_seq(in_sequence=in_degree_brain, out_sequence=out_degree_brain, simplify=True) # calculate things for both types of graph labels = ['er', 'sgpa', 'source-growth', 'target-attraction', 'random'] Gs = [G_er, G_sgpa, G_sg, G_ta, G_rand] for label, G in zip(labels, Gs): G.efficiency_matrix = metrics_bd.efficiency_matrix(G) # calculate nodal efficiency nodal_efficiency = np.sum(G.efficiency_matrix, axis=1) / (len(G.nodes()) - 1) G.counts_nodal_efficiency = np.histogram(nodal_efficiency, bins=BINS_NODAL_EFFICIENCY)[0] if label == 'er': graphs_er += [G] elif label == 'sgpa':
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...') # get reciprocal/nonreciprocal distance histograms for real brain r_dists_brain, non_r_dists_brain = metrics.dists_by_reciprocity(a_brain, d_brain) # do the same for each of the four graphs
NBINS = 15 ############################### # Create graph/ compute metrics ############################### # Extract brain graph G_brain, _, _ = extract.brain_graph.binary_directed() # Error check that keys all match (in_seq is in the same order as out_seq) assert G_brain.in_degree().keys() == G_brain.out_degree().keys() # Get in and out degree for each node and construct constrained random graph brain_indeg = G_brain.in_degree().values() brain_outdeg = G_brain.out_degree().values() G, _, _ = binary_directed.random_directed_deg_seq(brain_indeg, brain_outdeg, simplify=True) # Get in & out degree of random directed graph (both should match brain) indeg = np.array(G.in_degree().values()) outdeg = np.array(G.out_degree().values()) deg = indeg + outdeg # Calculate proportion in degree proportion_indeg = indeg / deg.astype(float) ########### # Plotting ########### # Create figure fig = plt.figure(figsize=cf.FIGSIZE, facecolor=cf.FACECOLOR, tight_layout=True)
graphss = {key: [] for key in KEYS} in_degree_brain = G_brain.in_degree().values() out_degree_brain = G_brain.out_degree().values() for g_ctr in range(N_GRAPH_SAMPLES): Gs = {} # create directed ER graph Gs['er'] = nx.erdos_renyi_graph(bc.num_brain_nodes, bc.p_brain_edge_directed, directed=True) # create directed degree-controlled random graph Gs['rand'] = random_directed_deg_seq(in_sequence=in_degree_brain, out_sequence=out_degree_brain, simplify=True)[0] # create target attraction graph Gs['ta'] = ta( N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L, brain_size=BRAIN_SIZE, )[0] # create source growth graph Gs['sg'] = sg( N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=L,
met_arr[graph_names.index('Mouse Connectome'), :, met_i] = bm(G_brain) for rep in np.arange(repeats): #TODO: recheck for correct brain model # Pref. Growth, Prox. Attachment model if 'Pref. Growth, Prox. Attachment' in graph_names: G_Bio, _, _ = bio_dir.biophysical_reverse_outdegree(n_nodes, n_edges, L=np.inf, brain_size=brain_size) met_arr[graph_names.index('Pref. Growth, Prox. Attachment'), rep, :] =\ calc_metrics(G_Bio, metrics) # Random Configuration model (random with fixed degree sequence) if 'Random' in graph_names: G_CM, _, _ = bio_dir.random_directed_deg_seq(brain_indegree, brain_outdegree, simplify=True, brain_size=brain_size) met_arr[graph_names.index('Random'), rep, :] = calc_metrics(G_CM, metrics) # Scale-Free Price's model if 'Scale-Free' in graph_names: pass ########################## # Save metrics ########################## # Save original array and version averaged across repeats fname = op.join(save_dir, 'directed_metrics') np.save(fname + '_orig.npy', met_arr)