import extract.brain_graph
import metrics.binary_undirected
import random_graph.binary_undirected as rg

# Load brain
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
n_nodes = len(G_brain.nodes())
n_edges = len(G_brain.edges())

# Calculate length scale
L_brain, r, p = metrics.binary_undirected.cxn_length_scale(A_brain, D_brain,
                                                           bins=BINS)

# Create biophysical model
G, A, D = rg.biophysical(n_nodes, n_edges, L_brain, GAMMA, 
                         brain_size=[10.,10,10])

# Calculate length scale of biophysical model
L, r, p = metrics.binary_undirected.cxn_length_scale(A, D, bins=BINS)

# Plot length histograms plus fitted exponentials 
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)

prob_brain, bins_brain = np.histogram(D_brain[np.triu(A_brain) > 0], bins=BINS, 
                                      normed=True)
bin_centers_brain = .5 * (bins_brain[:-1] + bins_brain[1:])
axs[0].bar(bin_centers_brain, prob_brain)
exp_fit_x = np.linspace(0, D.max(), 1000)
exp_fit_y = (1. / L) * np.exp(-exp_fit_x / L)
axs[0].plot(exp_fit_x, exp_fit_y, color='k', lw=3)
# Get all pairwise distances in brain
pairwise_dists_cortex = D_cortex[np.triu(D_cortex, k=1) > 0]
mean_pairwise_dist_cortex = np.mean(pairwise_dists_cortex)
med_pairwise_dist_cortex = np.median(pairwise_dists_cortex)
std_pairwise_dist_cortex = np.std(pairwise_dists_cortex)

# Get all edge distances in brain
edge_dists_cortex = D_cortex[np.triu(A_cortex, k=1) > 0]
mean_edge_dist_cortex = np.mean(edge_dists_cortex)
med_edge_dist_cortex = np.median(edge_dists_cortex)
std_edge_dist_cortex = np.std(edge_dists_cortex)

# Build model
print 'gen...'
G_model, A_model, D_model = rg.biophysical(N_nodes, N_edges, L, GAMMA,
                                           CORTEX_SIZE)

# Get all pairwise distances in model
pairwise_dists_model = D_model[np.triu(D_model, k=1) > 0]
mean_pairwise_dist_model = np.mean(pairwise_dists_model)
med_pairwise_dist_model = np.median(pairwise_dists_model)
std_pairwise_dist_model = np.std(pairwise_dists_model)

# Get all edge distances in model
edge_dists_model = D_model[np.triu(A_model, k=1) > 0]
mean_edge_dist_model = np.mean(edge_dists_model)
med_edge_dist_model = np.median(edge_dists_model)
std_edge_dist_model = np.std(edge_dists_model)

# Plot histogram of pairwise distances
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
Ejemplo n.º 3
0
model_degree_hist_gammas = [None for gamma in GAMMAS]
model_clustering_bins_gammas = [None for gamma in GAMMAS]
model_clustering_hist_gammas = [None for gamma in GAMMAS]
model_degree_example_gammas = [None for gamma in GAMMAS]
model_clustering_example_gammas = [None for gamma in GAMMAS]

# Loop through choices of gamma
for gamma_idx, gamma in enumerate(GAMMAS):
    # Create several ER graphs and get their degree & clustering coefficients
    print 'Generating model graphs for gamma = %.2f' % gamma
    model_degree_hist = np.zeros((N_MODEL_GRAPHS, DEGREE_BINS))
    model_degree_bins = DEGREE_BINS
    model_clustering_hist = np.zeros((N_MODEL_GRAPHS, CLUSTERING_BINS))
    model_clustering_bins = CLUSTERING_BINS
    for graph_idx in range(N_MODEL_GRAPHS):
        G_model, A_model, D_model = rg.biophysical(n_nodes, n_edges, L, gamma,
                                                   BRAIN_SIZE)
        model_degree = nx.degree(G_model).values()
        model_degree_hist[graph_idx,:], model_degree_bins = \
        np.histogram(model_degree, model_degree_bins, normed=True)
        model_clustering = nx.clustering(G_model).values()
        model_clustering_hist[graph_idx,:], model_clustering_bins = \
        np.histogram(model_clustering, model_clustering_bins, normed=True)

    # Take averages
    model_degree_hist = model_degree_hist.mean(axis=0)
    model_clustering_hist = model_clustering_hist.mean(axis=0)
    model_degree_hist_gammas[gamma_idx] = model_degree_hist
    model_degree_bins_gammas[gamma_idx] = model_degree_bins
    model_clustering_hist_gammas[gamma_idx] = model_clustering_hist
    model_clustering_bins_gammas[gamma_idx] = model_clustering_bins
    # Store examples
Ejemplo n.º 4
0
        largest_component = len(components[0])
    else:
        largest_component = 0.

    # Check if original component size is defined
    if orig_order is not None:
        return largest_component / float(orig_order)
    else:
        return largest_component


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from random_graph import binary_undirected as bu

    biophys, A, D = bu.biophysical()
    ER, A, D = bu.ER_distance()

    prop_removed = np.arange(0.05, 1, 0.1)
    S_bio = percolate_random(biophys, prop_removed, 3)
    S_ER = percolate_random(ER, prop_removed, 3)
    plt.plot(prop_removed, S_bio, prop_removed, S_ER)

    '''
    lesion_list = np.arange(0, 400, 10)
    S_bio = percolate_degree(biophys, lesion_list)
    S_ER = percolate_degree(ER, lesion_list)
    plt.plot(lesion_list, S_bio, lesion_list, S_ER)
    '''

    plt.show()
Ejemplo n.º 5
0
        largest_component = len(components[0])
    else:
        largest_component = 0.

    # Check if original component size is defined
    if orig_order is not None:
        return largest_component / float(orig_order)
    else:
        return largest_component


if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from random_graph import binary_undirected as bu

    biophys, A, D = bu.biophysical()
    ER, A, D = bu.ER_distance()

    prop_removed = np.arange(0.05, 1, 0.1)
    S_bio = percolate_random(biophys, prop_removed, 3)
    S_ER = percolate_random(ER, prop_removed, 3)
    plt.plot(prop_removed, S_bio, prop_removed, S_ER)

    '''
    lesion_list = np.arange(0, 400, 10)
    S_bio = percolate_degree(biophys, lesion_list)
    S_ER = percolate_degree(ER, lesion_list)
    plt.plot(lesion_list, S_bio, lesion_list, S_ER)
    '''

    plt.show()
Ejemplo n.º 6
0
# Get all pairwise distances in brain
pairwise_dists_brain = D_brain[np.triu(D_brain, k=1) > 0]
mean_pairwise_dist_brain = np.mean(pairwise_dists_brain)
med_pairwise_dist_brain = np.median(pairwise_dists_brain)
std_pairwise_dist_brain = np.std(pairwise_dists_brain)

# Get all edge distances in brain
edge_dists_brain = D_brain[np.triu(A_brain, k=1) > 0]
mean_edge_dist_brain = np.mean(edge_dists_brain)
med_edge_dist_brain = np.median(edge_dists_brain)
std_edge_dist_brain = np.std(edge_dists_brain)

# Build model
print 'gen...'
G_model, A_model, D_model = rg.biophysical(N_nodes, N_edges, L, GAMMA,
                                           BRAIN_SIZE)

# Get all pairwise distances in model
pairwise_dists_model = D_model[np.triu(D_model, k=1) > 0]
mean_pairwise_dist_model = np.mean(pairwise_dists_model)
med_pairwise_dist_model = np.median(pairwise_dists_model)
std_pairwise_dist_model = np.std(pairwise_dists_model)

# Get all edge distances in model
edge_dists_model = D_model[np.triu(A_model, k=1) > 0]
mean_edge_dist_model = np.mean(edge_dists_model)
med_edge_dist_model = np.median(edge_dists_model)
std_edge_dist_model = np.std(edge_dists_model)

# Plot histogram of pairwise distances
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
Ejemplo n.º 7
0
import extract.brain_graph
import metrics.binary_undirected
import random_graph.binary_undirected as rg

# Load brain
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
n_nodes = len(G_brain.nodes())
n_edges = len(G_brain.edges())

# Calculate length scale
L_brain, r, p = metrics.binary_undirected.cxn_length_scale(A_brain, D_brain,
                                                           bins=BINS)

# Create biophysical model
G, A, D = rg.biophysical(n_nodes, n_edges, L=L, gamma=GAMMA, 
                         brain_size=BRAIN_SIZE)

# Plot connection distance histograms
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)

prob_brain, bins_brain = np.histogram(D_brain[np.triu(A_brain) > 0], bins=BINS, 
                                      normed=True)
bin_centers_brain = .5 * (bins_brain[:-1] + bins_brain[1:])
bin_width_brain = bins_brain[1] - bins_brain[0]
axs[0].bar(bin_centers_brain, prob_brain, width=bin_width_brain)

prob_model, bins_model = np.histogram(D[np.triu(A) > 0], bins=BINS, normed=True)
bin_centers_model = .5 * (bins_model[:-1] + bins_model[1:])
bin_width_model = bins_model[1] - bins_model[0]
axs[1].bar(bin_centers_model, prob_model, width=bin_width_model)
# Get all pairwise distances in brain
pairwise_dists_cortex = D_cortex[np.triu(D_cortex, k=1) > 0]
mean_pairwise_dist_cortex = np.mean(pairwise_dists_cortex)
med_pairwise_dist_cortex = np.median(pairwise_dists_cortex)
std_pairwise_dist_cortex = np.std(pairwise_dists_cortex)

# Get all edge distances in brain
edge_dists_cortex = D_cortex[np.triu(A_cortex, k=1) > 0]
mean_edge_dist_cortex = np.mean(edge_dists_cortex)
med_edge_dist_cortex = np.median(edge_dists_cortex)
std_edge_dist_cortex = np.std(edge_dists_cortex)

# Build model
print 'gen...'
G_model, A_model, D_model = rg.biophysical(N_nodes, N_edges, L, GAMMA, 
                                           CORTEX_SIZE)

# Get all pairwise distances in model
pairwise_dists_model = D_model[np.triu(D_model, k=1) > 0]
mean_pairwise_dist_model = np.mean(pairwise_dists_model)
med_pairwise_dist_model = np.median(pairwise_dists_model)
std_pairwise_dist_model = np.std(pairwise_dists_model)

# Get all edge distances in model
edge_dists_model = D_model[np.triu(A_model, k=1) > 0]
mean_edge_dist_model = np.mean(edge_dists_model)
med_edge_dist_model = np.median(edge_dists_model)
std_edge_dist_model = np.std(edge_dists_model)

# Plot histogram of pairwise distances
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
MODEL_COLOR = ['k'] * len(GAMMAS)

# Load mouse connectivity graph
G_brain, W_brain, _ = extract.brain_graph.binary_undirected()
n_nodes = len(G_brain.nodes())
n_edges = len(G_brain.edges())
p_edge = float(n_edges) / ((n_nodes * (n_nodes - 1)) / 2.)

# 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, A_model, D_model = rg.biophysical(n_nodes, n_edges, L, gamma,
                                               BRAIN_SIZE)
    # Store examples
    model_degrees[gamma_idx] = nx.degree(G_model).values()
    model_clusterings[gamma_idx] = nx.clustering(G_model).values()

#################################
# Plotting
#################################
plt.ion()
plt.close('all')
plt.rcParams['ps.fonttype'] = 42  # Set for text in Adobe Illustrator
plt.rcParams['pdf.fonttype'] = 42

fig, axs = plt.subplots(1, len(GAMMAS), facecolor=FACE_COLOR, figsize=FIGSIZE,
                        sharey=True, tight_layout=True)
Ejemplo n.º 10
0
import numpy as np
import matplotlib.pyplot as plt

# Load brain network
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
n_nodes = D_brain.shape[0]
n_edges = np.triu(A_brain, k=1).sum()
# Calculate connection probability vs. distance
cxn_prob, dist_bins = metrics.binary_undirected.cxn_probability(
    A_brain, D_brain)
dist_bin_centers = .5 * (dist_bins[:-1] + dist_bins[1:])
bin_width = dist_bins[1] - dist_bins[0]

# Build model
G, A, D = rg.biophysical(n_nodes,
                         n_edges,
                         L=L,
                         gamma=GAMMA,
                         brain_size=BRAIN_SIZE)
# Calculate connection probability vs. distance
cxn_prob_model, dist_bins_model = \
metrics.binary_undirected.cxn_probability(A, D)
dist_bin_centers_model = .5 * (dist_bins_model[:-1] + dist_bins_model[1:])
bin_width_model = dist_bins_model[1] - dist_bins_model[0]

# Plot both
fig, axs = plt.subplots(2, 1, facecolor='white')
axs[0].bar(dist_bin_centers, cxn_prob, width=bin_width)
axs[1].bar(dist_bin_centers_model, cxn_prob_model, width=bin_width_model)
# Get mouse connectivity graph & distance
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
n_nodes = A_brain.shape[0]
n_edges = (np.triu(A_brain)).sum()
p_edge = float(n_edges) / ((n_nodes * (n_nodes - 1)) / 2)
L = 2.2
GAMMA = 1.6
BRAIN_SIZE = [7., 7, 7]

# Calculate swapped-cost distribution for graph
cost_changes = metrics.binary_undirected.swapped_cost_distr(A_brain, D_brain)
positive_cost_changes = float((cost_changes > 0).sum()) / len(cost_changes)

# Create random biophysical graph with properly sampled weights
G_model, A_model, D_model = rg.biophysical(N=n_nodes, N_edges=n_edges, L=L, 
                                           gamma=GAMMA, brain_size=BRAIN_SIZE)
# Calculate swapped-cost distribution for graph
cost_changes_model = metrics.binary_undirected.swapped_cost_distr(A_model, 
                                                                   D_model)
positive_cost_changes_model = float((cost_changes_model > 0).sum()) / \
len(cost_changes_model)

# Create ER graph with distance matrix
G_ERD, A_ERD, D_ERD = rg.ER_distance(n_nodes, p_edge, brain_size=BRAIN_SIZE)
# Calculate swapped-cost distribution for graph
cost_changes_ERD = metrics.binary_undirected.swapped_cost_distr(A_ERD, D_ERD)
positive_cost_changes_ERD = float((cost_changes_ERD > 0).sum()) / \
len(cost_changes_ERD)

fig, axs = plt.subplots(3, 1, facecolor=FACECOLOR)
axs[0].hist(cost_changes, bins=20, normed=True)
Ejemplo n.º 12
0
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
N_brain = A_brain.shape[0]
N_edges_brain = (A_brain > 0).sum() / 2
L = 2.2
GAMMA = 1.6
BRAIN_SIZE = [7., 7, 7]

# Calculate swapped-cost distribution for graph
cost_changes = metrics.binary_undirected.swapped_cost_distr(A_brain, D_brain)
positive_cost_changes = float((cost_changes > 0).sum()) / len(cost_changes)

# Create random biophysical graph with properly sampled weights
G, A, D = rg.biophysical(N=N_brain,
                         N_edges=N_edges_brain,
                         L=L,
                         gamma=GAMMA,
                         brain_size=BRAIN_SIZE)

# Calculate swapped-cost distribution for graph
cost_changes_random = metrics.binary_undirected.swapped_cost_distr(A, D)
positive_cost_changes_random = float((cost_changes_random > 0).sum()) / \
len(cost_changes_random)

fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
axs[0].hist(cost_changes, bins=20, normed=True)
axs[1].hist(cost_changes_random, bins=20, normed=True)

for ax_idx, ax in enumerate(axs):
    ax.set_xlabel('Change in cost (per cent)')
    ax.set_xlim(-1, 2)
Ejemplo n.º 13
0
D_brain, _ = extract.brain_graph.distance_matrix()
n_nodes = A_brain.shape[0]
n_edges = (np.triu(A_brain)).sum()
p_edge = float(n_edges) / ((n_nodes * (n_nodes - 1)) / 2)
L = 2.2
GAMMA = 1.6
BRAIN_SIZE = [7., 7, 7]

# Calculate swapped-cost distribution for graph
cost_changes = metrics.binary_undirected.swapped_cost_distr(A_brain, D_brain)
positive_cost_changes = float((cost_changes > 0).sum()) / len(cost_changes)

# Create random biophysical graph with properly sampled weights
G_model, A_model, D_model = rg.biophysical(N=n_nodes,
                                           N_edges=n_edges,
                                           L=L,
                                           gamma=GAMMA,
                                           brain_size=BRAIN_SIZE)
# Calculate swapped-cost distribution for graph
cost_changes_model = metrics.binary_undirected.swapped_cost_distr(
    A_model, D_model)
positive_cost_changes_model = float((cost_changes_model > 0).sum()) / \
len(cost_changes_model)

# Create ER graph with distance matrix
G_ERD, A_ERD, D_ERD = rg.ER_distance(n_nodes, p_edge, brain_size=BRAIN_SIZE)
# Calculate swapped-cost distribution for graph
cost_changes_ERD = metrics.binary_undirected.swapped_cost_distr(A_ERD, D_ERD)
positive_cost_changes_ERD = float((cost_changes_ERD > 0).sum()) / \
len(cost_changes_ERD)
Ejemplo n.º 14
0
###############################################
# Calculate degree distributions for all graphs
###############################################

G_brain, _, _ = extract.brain_graph.binary_undirected()
n_nodes = len(G_brain.nodes())
n_edges = len(G_brain.edges())
brain_degree = G_brain.degree().values()

# Initialize repetition matrices for standard graphs
gamma_mat = -1 * np.ones((len(GAMMAS), repeats, n_nodes))

for r in np.arange(repeats):
    for gamma_idx, gamma in enumerate(GAMMAS):
        G_model, _, _ = rg.biophysical(n_nodes, n_edges, np.inf, gamma,
                                       BRAIN_SIZE)

        gamma_mat[gamma_idx, r, :] = G_model.degree().values()
    print 'Finished repeat: ' + str(r)

gamma_dists = gamma_mat.reshape((len(GAMMAS), -1))

###################################################
# Plot all panels that will have axis scales changes
###################################################
figsize = (12.5, 6)
fig, axs = plt.subplots(1, 2, figsize=figsize)
lin_bins = np.linspace(0, 150, n_bins)

for ax_i, ax in enumerate(axs):
    ax.hist(brain_degree, lin_bins, color=BRAIN_COLOR, normed=True,
# Get all pairwise distances in brain
pairwise_dists_brain = D_brain[np.triu(D_brain, k=1) > 0]
mean_pairwise_dist_brain = np.mean(pairwise_dists_brain)
med_pairwise_dist_brain = np.median(pairwise_dists_brain)
std_pairwise_dist_brain = np.std(pairwise_dists_brain)

# Get all edge distances in brain
edge_dists_brain = D_brain[np.triu(A_brain, k=1) > 0]
mean_edge_dist_brain = np.mean(edge_dists_brain)
med_edge_dist_brain = np.median(edge_dists_brain)
std_edge_dist_brain = np.std(edge_dists_brain)

# Build model
print 'gen...'
G_model, A_model, D_model = rg.biophysical(N_nodes, N_edges, L, GAMMA, 
                                           BRAIN_SIZE)

# Get all pairwise distances in model
pairwise_dists_model = D_model[np.triu(D_model, k=1) > 0]
mean_pairwise_dist_model = np.mean(pairwise_dists_model)
med_pairwise_dist_model = np.median(pairwise_dists_model)
std_pairwise_dist_model = np.std(pairwise_dists_model)

# Get all edge distances in model
edge_dists_model = D_model[np.triu(A_model, k=1) > 0]
mean_edge_dist_model = np.mean(edge_dists_model)
med_edge_dist_model = np.median(edge_dists_model)
std_edge_dist_model = np.std(edge_dists_model)

# Plot histogram of pairwise distances
fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
Ejemplo n.º 16
0
# Get mouse connectivity graph & distance
G_brain, A_brain, _ = extract.brain_graph.binary_undirected()
D_brain, _ = extract.brain_graph.distance_matrix()
N_brain = A_brain.shape[0]
N_edges_brain = (A_brain > 0).sum()/2
L = 2.2
GAMMA = 1.6
BRAIN_SIZE = [7., 7, 7]

# Calculate swapped-cost distribution for graph
cost_changes = metrics.binary_undirected.swapped_cost_distr(A_brain, D_brain)
positive_cost_changes = float((cost_changes > 0).sum()) / len(cost_changes)

# Create random biophysical graph with properly sampled weights
G, A, D = rg.biophysical(N=N_brain, N_edges=N_edges_brain, L=L, gamma=GAMMA, 
                         brain_size=BRAIN_SIZE)
                                      
# Calculate swapped-cost distribution for graph
cost_changes_random = metrics.binary_undirected.swapped_cost_distr(A, D)
positive_cost_changes_random = float((cost_changes_random > 0).sum()) / \
len(cost_changes_random)

fig, axs = plt.subplots(2, 1, facecolor=FACECOLOR)
axs[0].hist(cost_changes, bins=20, normed=True)
axs[1].hist(cost_changes_random, bins=20, normed=True)

for ax_idx, ax in enumerate(axs):
    ax.set_xlabel('Change in cost (per cent)')
    ax.set_xlim(-1,2)
    ax.set_ylabel('Probability')