def test_number_of_nonzero_connections_increases_as_p_value_increases(self): p_ths = (0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1) n_edges = brain_graph.binary_directed(p_th=p_ths[0])[1].sum() for p_th in p_ths[1:]: new_n_edges = brain_graph.binary_directed(p_th=p_th)[1].sum() self.assertGreater(new_n_edges, n_edges) n_edges = new_n_edges
def test_number_of_nonzero_connections_increases_as_p_value_increases( self): p_ths = (0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1) n_edges = brain_graph.binary_directed(p_th=p_ths[0])[1].sum() for p_th in p_ths[1:]: new_n_edges = brain_graph.binary_directed(p_th=p_th)[1].sum() self.assertGreater(new_n_edges, n_edges) n_edges = new_n_edges
from random_graph.binary_directed import biophysical_indegree as pa from network_plot import change_settings import brain_constants as bc from config.graph_parameters import LENGTH_SCALE from config import COLORS, FACE_COLOR, AX_COLOR, FONT_SIZE # parameters for this particular plot FACE_COLOR = 'w' ALPHA = .6 FIG_SIZE = (12, 6) # load brain graph print('loading brain graph...') G_brain, _, _ = brain_graph.binary_directed() # create pgpa graphs print('generating model...') G_pgpa_L_inf, _, _ = pgpa(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf) print('generating model...') G_pgpa, _, _ = pgpa(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=LENGTH_SCALE) print('generating model...') G_pa, _, _ = pa(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf) # cast both models to undirected graphs # plot preferential growth (L = np.inf) and pref attachment fig, ax = plt.subplots(1, 1, facecolor=FACE_COLOR, figsize=FIG_SIZE,
LS = np.linspace(0, 2, 21) BRAIN_SIZE = [7., 7., 7.] N_REPEATS = 100 ALPHA = 0.3 FONT_SIZE = 20 D_BINS = np.linspace(0, 12, 30) L = 0.725 TEMP_FILE_NAME = 'reciprocity_temp.npy' 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,
import os import numpy as np from extract import brain_graph SAVE_PATH_PREFIX = os.path.join(os.getenv('DBW_DATA_DIRECTORY'), 'nodes_sorted') G, A, labels = brain_graph.binary_directed() idxs_sorted = {} labels_sorted = {} values_sorted = {} # sort by outdegree out_deg = A.sum(axis=1) idxs_sorted['out_deg'] = out_deg.argsort() labels_sorted['out_deg'] = list(np.array(labels)[idxs_sorted['out_deg']]) values_sorted['out_deg'] = out_deg[idxs_sorted['out_deg']] # sort by indegree in_deg = A.sum(axis=0) idxs_sorted['in_deg'] = in_deg.argsort() labels_sorted['in_deg'] = list(np.array(labels)[idxs_sorted['in_deg']]) values_sorted['in_deg'] = in_deg[idxs_sorted['in_deg']] # write a new file for each metric for metric in ['out_deg', 'in_deg']: file_path = '{}_{}.txt'.format(SAVE_PATH_PREFIX, metric) with open(file_path, 'wb') as f: f.write('rank, node idx, node label, {}\n\n'.format(metric)) for ctr, (idx, label, value) in \ enumerate(zip(idxs_sorted[metric], labels_sorted[metric], values_sorted[metric])):
from config import COLORS from config.graph_parameters import LENGTH_SCALE FACE_COLOR = 'w' AX_COLOR = 'k' FONT_SIZE = 20 # parameters for this particular plot FIG_SIZE = (12, 5) BINS = np.linspace(0, 150, 40) LOAD_FILE_NAME = 'model_graphs_with_efficiency.npy' # load brain graph G_brain, _, _ = brain_graph.binary_directed() # look for file containing graphs print('Attempting to open file "{}"...'.format(LOAD_FILE_NAME)) try: with open(LOAD_FILE_NAME, 'rb') as f: graphs = np.load(LOAD_FILE_NAME)[0]['sg'] except Exception, e: raise IOError('Error loading data from file "{}" (please run "fig6.py" first'.format(LOAD_FILE_NAME)) else: print('File loaded successfully!') degrees_sgpa = [nx.degree(g.to_undirected()).values() for g in graphs] degree_brain = nx.degree(G_brain).values()
def setUp(self): self.W, self.P, self.labels = auxiliary.load_W_and_P() self.G, self.A, self.labels = brain_graph.binary_directed()
rho = distance_clustering.correlation p = distance_clustering.pvalue return rho,p if __name__ == "__main__": run_graphs = True n_runs = 10 if n_runs%2 == 0: # this must be odd, otherwise the median will be an average n_runs += 1 G, _, labels = binary_directed() nodes = G.nodes() new_labels = {nodes[i]:labels[i] for i in range(len(nodes))} centroids = G.centroids G = nx.relabel_nodes(G,new_labels) G.centroids = centroids # Page 2: In the connectome, clustering coefficient can be expressed as a power of degree: # C ~ k^gamma, where gamma = -0.44 # Also give corresponding values for SG and SGPA networks print '=== Clustering coefficients as a power of degree (C ~ k^gamma) ===' gamma,R2,p = get_gamma(G.to_undirected()) print 'Mouse connectome: C ~ k^%.2f, R^2=%.3f, p=%.3e'%(gamma,R2,p)
import config labelsize = 11 ticksize = 10 legendsize = 8 def dist(x, y): return np.sqrt(np.sum((x - y)**2)) if __name__ == "__main__": # this isn't right... the third return object is a distance matrix # Get the connectome G, _, labels = binary_directed() nodes = G.nodes() new_labels = {nodes[i]: labels[i] for i in range(len(nodes))} distance_matrix = aux.load_brain_dist_matrix(new_labels) mds = manifold.MDS(n_components=3, max_iter=1000, eps=1e-10, dissimilarity='precomputed') centroids = mds.fit_transform(distance_matrix) inter_node_distances = [ dist(edge1, edge2) for edge1 in centroids for edge2 in centroids if not all(edge1 == edge2) ]
top_margin_ax = plt.subplot2grid(cf.subplot_divisions,cf.top_margin_location,rowspan=cf.top_margin_rowspan,\ colspan=cf.top_margin_colspan,sharex=left_main_ax) right_margin_ax = plt.subplot2grid(cf.subplot_divisions,cf.right_margin_location,rowspan=cf.right_margin_rowspan,\ colspan=cf.right_margin_colspan,sharey=left_main_ax) # To get the log axes we need to create another axis on top of our existing ones top_dummy_ax = top_margin_ax.twinx() right_dummy_ax = right_margin_ax.twiny() #right_dummy_ax = plt.subplot2grid(subplot_divisions,right_margin_location,rowspan=right_margin_rowspan,\ # colspan=right_margin_colspan,sharey=left_main_ax,frameon=False) # create attachment and growht models G, _, _ = binary_directed() # Get in- & out-degree indeg = np.array([G.in_degree()[node] for node in G]) outdeg = np.array([G.out_degree()[node] for node in G]) deg = indeg + outdeg # Calculate proportion in degree percent_indeg = indeg / deg.astype(float) a1 = 1.0
import color_scheme import in_out_plot_config as cf from network_compute import reciprocity from extract.brain_graph import binary_directed Gattachment, _, _ = biophysical_reverse_outdegree(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=0.75, gamma=0.) Ggrowth, _, _ = biophysical_reverse_outdegree(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=np.inf, gamma=1.) Gpgpa, _, _ = biophysical_reverse_outdegree(N=bc.num_brain_nodes, N_edges=bc.num_brain_edges_directed, L=0.75, gamma=1.) Gbrain, _, _ = binary_directed() graphs = [Ggrowth,Gattachment,Gpgpa,Gbrain] graph_names = ['Pref growth only','Proximal attachment only','PGPA','Connectome'] metrics = ['Reciprocity','Clustering','Degree','CC-deg rank','Prop in-total rank'] table = pd.DataFrame(index=metrics,col for G in graphs: A = np.matrix(G.adjacency_list())
rs = [] i=0 j=0 rs = [] slopes = [] alpha=0.25 markersize=30. leg = [] cyan = (0.5,0.5,1.0) cols = ['cyan',(0.2,0.6,1.0),(0.,0.2,0.9)] deg_bins = np.linspace(0,150,50) Gbrain,_,_ = binary_directed() deg_brain = Gbrain.to_undirected().degree().values() N = 100 axs[0].hist(deg_brain,deg_bins,facecolor=config.COLORS['brain'], alpha=0.75,normed=True) leg.append(mlines.Line2D([],[],color=config.COLORS['brain'], linestyle='-',markersize=13,\ label='Connectome',lw=3,alpha=0.75)) for i,L in enumerate(Ls): print 'L = {}'.format(L) degs=[] for k in range(N): print 'repeat {}'.format(k) G,_,_ = pure_geometric(N=bc.num_brain_nodes,N_edges=bc.num_brain_edges_undirected, L=L)
BRAIN_SIZE = [7., 7., 7.] N_REPEATS = 100 ALPHA = 0.3 FONT_SIZE = 20 D_BINS = np.linspace(0, 12, 30) L = 0.725 TEMP_FILE_NAME = 'reciprocity_temp.npy' 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,
from mpl_toolkits.axes_grid1 import make_axes_locatable from network_plot.network_viz import plot_scatterAndMarginal # IMPORT PLOT PARAMETERS import in_out_plot_config as cf plt.close('all') plt.ion() # PLOT PARAMETERS FONTSIZE = 24 NBINS = 15 MARKERCOLOR = 'm' # load brain graph, adjacency matrix, and labels G, A, labels = binary_directed() # Get in & out degree indeg = G.in_degree().values() outdeg = np.array([G.out_degree()[node] for node in G]) deg = indeg + outdeg # Calculate proportion in degree percent_indeg = indeg / deg.astype(float) # Create figure #fig = plt.figure(figsize=cf.FIGSIZE, facecolor=cf.FACECOLOR, tight_layout=True) fig = plt.figure(figsize=cf.FIGSIZE, tight_layout=True) ax0 = plt.subplot2grid((1, 1), (0, 0)) #ax0 = plt.subplot2grid(cf.SUBPLOT_DIVISIONS, cf.AX0_LOCATION, # colspan=cf.AX0_COLSPAN)