Setup


In [1]:
import seaborn
import pandas as pd
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import clusterrewire as cr
from clusterrewire import cluster_rewire_graph
import networkx as nx

Create the initial graph


In [6]:
n_nodes = 500
p = 1.5*log(n_nodes)/n_nodes
g = nx.erdos_renyi_graph(n=n_nodes, p=p)

try_count = 1
max_tries = 1000
while not nx.is_connected(g):
    g = nx.erdos_renyi_graph(n=n_nodes, p=p)
    try_count += 1
    if try_count>max_tries:
        print("Can't make a connected graph. Tried %i times."%max_tries)
        break

original_graph = g.copy()

print("Average degree: %.2f"%mean(list(g.degree().values())))


Average degree: 9.32

Cluster up


In [7]:
%%time
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best)
find_best_clustering = array(triangles_completed)/array(triangles_possible)


Attempting 2331 edge rewires, out of 2331 edges
Rewiring 0 out of 2331
Rewiring 10 out of 2331
Rewiring 20 out of 2331
Rewiring 30 out of 2331
Rewiring 40 out of 2331
Rewiring 50 out of 2331
Rewiring 60 out of 2331
Rewiring 70 out of 2331
Rewiring 80 out of 2331
Rewiring 90 out of 2331
Rewiring 100 out of 2331
Rewiring 110 out of 2331
Rewiring 120 out of 2331
Rewiring 130 out of 2331
Rewiring 140 out of 2331
Rewiring 150 out of 2331
Rewiring 160 out of 2331
Rewiring 170 out of 2331
Rewiring 180 out of 2331
Rewiring 190 out of 2331
Rewiring 200 out of 2331
Rewiring 210 out of 2331
Rewiring 220 out of 2331
Rewiring 230 out of 2331
Rewiring 240 out of 2331
Rewiring 250 out of 2331
Rewiring 260 out of 2331
Rewiring 270 out of 2331
Rewiring 280 out of 2331
Rewiring 290 out of 2331
Rewiring 300 out of 2331
Rewiring 310 out of 2331
Rewiring 320 out of 2331
Rewiring 330 out of 2331
Rewiring 340 out of 2331
Rewiring 350 out of 2331
Rewiring 360 out of 2331
Rewiring 370 out of 2331
Rewiring 380 out of 2331
Rewiring 390 out of 2331
Rewiring 400 out of 2331
Rewiring 410 out of 2331
Rewiring 420 out of 2331
Rewiring 430 out of 2331
Rewiring 440 out of 2331
Rewiring 450 out of 2331
Rewiring 460 out of 2331
Rewiring 470 out of 2331
Rewiring 480 out of 2331
Rewiring 490 out of 2331
Rewiring 500 out of 2331
Rewiring 510 out of 2331
Rewiring 520 out of 2331
Rewiring 530 out of 2331
Rewiring 540 out of 2331
Rewiring 550 out of 2331
Rewiring 560 out of 2331
Rewiring 570 out of 2331
Rewiring 580 out of 2331
Rewiring 590 out of 2331
Rewiring 600 out of 2331
Rewiring 610 out of 2331
Rewiring 620 out of 2331
Rewiring 630 out of 2331
Rewiring 640 out of 2331
Rewiring 650 out of 2331
Rewiring 660 out of 2331
Rewiring 670 out of 2331
Rewiring 680 out of 2331
Rewiring 690 out of 2331
Rewiring 700 out of 2331
Rewiring 710 out of 2331
Rewiring 720 out of 2331
Rewiring 730 out of 2331
Rewiring 740 out of 2331
Rewiring 750 out of 2331
Rewiring 760 out of 2331
Rewiring 770 out of 2331
Rewiring 780 out of 2331
Rewiring 790 out of 2331
Rewiring 800 out of 2331
Rewiring 810 out of 2331
Rewiring 820 out of 2331
Rewiring 830 out of 2331
Rewiring 840 out of 2331
Rewiring 850 out of 2331
Rewiring 860 out of 2331
Rewiring 870 out of 2331
Rewiring 880 out of 2331
Rewiring 890 out of 2331
Rewiring 900 out of 2331
Rewiring 910 out of 2331
Rewiring 920 out of 2331
Rewiring 930 out of 2331
Rewiring 940 out of 2331
Rewiring 950 out of 2331
Rewiring 960 out of 2331
Rewiring 970 out of 2331
Rewiring 980 out of 2331
Rewiring 990 out of 2331
Rewiring 1000 out of 2331
Rewiring 1010 out of 2331
Rewiring 1020 out of 2331
Rewiring 1030 out of 2331
Rewiring 1040 out of 2331
Rewiring 1050 out of 2331
Rewiring 1060 out of 2331
Rewiring 1070 out of 2331
Rewiring 1080 out of 2331
Rewiring 1090 out of 2331
Rewiring 1100 out of 2331
Rewiring 1110 out of 2331
Rewiring 1120 out of 2331
Rewiring 1130 out of 2331
Rewiring 1140 out of 2331
Rewiring 1150 out of 2331
Rewiring 1160 out of 2331
Rewiring 1170 out of 2331
Rewiring 1180 out of 2331
Rewiring 1190 out of 2331
Rewiring 1200 out of 2331
Rewiring 1210 out of 2331
Rewiring 1220 out of 2331
Rewiring 1230 out of 2331
Rewiring 1240 out of 2331
Rewiring 1250 out of 2331
Rewiring 1260 out of 2331
Rewiring 1270 out of 2331
Rewiring 1280 out of 2331
Rewiring 1290 out of 2331
Rewiring 1300 out of 2331
Rewiring 1310 out of 2331
Rewiring 1320 out of 2331
Rewiring 1330 out of 2331
Rewiring 1340 out of 2331
Rewiring 1350 out of 2331
Rewiring 1360 out of 2331
Rewiring 1370 out of 2331
Rewiring 1380 out of 2331
Rewiring 1390 out of 2331
Rewiring 1400 out of 2331
Rewiring 1410 out of 2331
Rewiring 1420 out of 2331
Rewiring 1430 out of 2331
Rewiring 1440 out of 2331
Rewiring 1450 out of 2331
Rewiring 1460 out of 2331
Rewiring 1470 out of 2331
Rewiring 1480 out of 2331
Rewiring 1490 out of 2331
Rewiring 1500 out of 2331
Rewiring 1510 out of 2331
Rewiring 1520 out of 2331
Rewiring 1530 out of 2331
Rewiring 1540 out of 2331
Rewiring 1550 out of 2331
Rewiring 1560 out of 2331
Rewiring 1570 out of 2331
Rewiring 1580 out of 2331
Rewiring 1590 out of 2331
Rewiring 1600 out of 2331
Rewiring 1610 out of 2331
Rewiring 1620 out of 2331
Rewiring 1630 out of 2331
Rewiring 1640 out of 2331
Rewiring 1650 out of 2331
Rewiring 1660 out of 2331
Rewiring 1670 out of 2331
Rewiring 1680 out of 2331
Rewiring 1690 out of 2331
Rewiring 1700 out of 2331
Rewiring 1710 out of 2331
Rewiring 1720 out of 2331
Rewiring 1730 out of 2331
Rewiring 1740 out of 2331
Rewiring 1750 out of 2331
Rewiring 1760 out of 2331
Rewiring 1770 out of 2331
Rewiring 1780 out of 2331
Rewiring 1790 out of 2331
Rewiring 1800 out of 2331
Rewiring 1810 out of 2331
Rewiring 1820 out of 2331
Rewiring 1830 out of 2331
Rewiring 1840 out of 2331
Rewiring 1850 out of 2331
Rewiring 1860 out of 2331
Rewiring 1870 out of 2331
Rewiring 1880 out of 2331
Rewiring 1890 out of 2331
Rewiring 1900 out of 2331
Rewiring 1910 out of 2331
Rewiring 1920 out of 2331
Rewiring 1930 out of 2331
Rewiring 1940 out of 2331
Rewiring 1950 out of 2331
Rewiring 1960 out of 2331
Rewiring 1970 out of 2331
Couldn't make a move!
Rewired 84.6 percent of edges
CPU times: user 13min 52s, sys: 20.9 s, total: 14min 13s
Wall time: 15min 23s

In [5]:
plot(find_best_clustering)
min(diff(find_best_clustering))


Out[5]:
0.0

In [5]:
plot(find_best_clustering)
min(diff(find_best_clustering))


Out[5]:
0.0

In [6]:
%%time
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
                                                                             property_functions=None)


Attempting 361 edge rewires, out of 361 edges
Rewiring 0 out of 361
Rewiring 10 out of 361
Rewiring 20 out of 361
Rewiring 30 out of 361
Rewiring 40 out of 361
Rewiring 50 out of 361
Rewiring 60 out of 361
Rewiring 70 out of 361
Rewiring 80 out of 361
Rewiring 90 out of 361
Rewiring 100 out of 361
Rewiring 110 out of 361
Rewiring 120 out of 361
Rewiring 130 out of 361
Rewiring 140 out of 361
Rewiring 150 out of 361
Rewiring 160 out of 361
Rewiring 170 out of 361
Rewiring 180 out of 361
Rewiring 190 out of 361
Rewiring 200 out of 361
Rewiring 210 out of 361
Rewiring 220 out of 361
Rewiring 230 out of 361
Rewiring 240 out of 361
Rewiring 250 out of 361
Rewiring 260 out of 361
Rewiring 270 out of 361
Couldn't make a move!
Rewired 76.7 percent of edges
CPU times: user 4.41 s, sys: 51.8 ms, total: 4.46 s
Wall time: 4.56 s

In [5]:
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
improve_worst_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_improve_worst)
improve_worst_clustering = array(triangles_completed)/array(triangles_possible)


Attempting 333 edge rewires, out of 333 edges
Rewiring 0 out of 333
Rewiring 10 out of 333
Rewiring 20 out of 333
Rewiring 30 out of 333
Rewiring 40 out of 333
Rewiring 50 out of 333
Rewiring 60 out of 333
Rewiring 70 out of 333
Rewiring 80 out of 333
Rewiring 90 out of 333
Rewiring 100 out of 333
Rewiring 110 out of 333
Rewiring 120 out of 333
Rewiring 130 out of 333
Rewiring 140 out of 333
Rewiring 150 out of 333
Rewiring 160 out of 333
Rewiring 170 out of 333
Rewiring 180 out of 333
Rewiring 190 out of 333
Rewiring 200 out of 333
Rewiring 210 out of 333
Rewiring 220 out of 333
Rewiring 230 out of 333
Rewiring 240 out of 333
Rewiring 250 out of 333
Couldn't make a move!
Rewired 77.5 percent of edges

In [6]:
plot(improve_worst_clustering/improve_worst_clustering[0], color='b', label="Improve Worst")
plot(find_best_clustering/find_best_clustering[0], color='r', label="Find Best")
ylabel("Clustering Increase From Initial")
title("Clustering Goes Up with Both Algorithms")
xlabel("Number of Rewires")

legend(loc=4)

figure()
hist(sum(find_best_A,axis=1), alpha=.7, label='Rewired (Find Best)', normed=True)
hist(sum(improve_worst_A,axis=1), alpha=.7, label='Rewired (Improve Worst)', normed=True)
hist(sum(nx.adjacency_matrix(g).todense(),axis=1), alpha=.7, label='Original Network', normed=True)
ylabel("P(Degree)")
xlabel("Node Degree")
legend(loc=1)


Out[6]:
<matplotlib.legend.Legend at 0x10a308da0>

In [7]:
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
                                                                             preserve_degrees=True)
find_best_clustering = array(triangles_completed)/array(triangles_possible)

g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
improve_worst_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_improve_worst,
                                                                                 preserve_degrees=True)
improve_worst_clustering = array(triangles_completed)/array(triangles_possible)

plot(improve_worst_clustering/improve_worst_clustering[0], color='b', label="Improve Worst")
plot(find_best_clustering/find_best_clustering[0], color='r', label="Find Best")
ylabel("Clustering Increase From Initial")
title("Clustering Goes Up with Both Algorithms")
xlabel("Number of Rewires")

legend(loc=4)

figure()
hist(sum(find_best_A,axis=1), alpha=.7, label='Rewired (Find Best)', normed=True)
hist(sum(improve_worst_A,axis=1), alpha=.7, label='Rewired (Improve Worst)', normed=True)
hist(sum(nx.adjacency_matrix(g).todense(),axis=1), alpha=.7, label='Original Network', normed=True)
ylabel("P(Degree)")
xlabel("Node Degree")
legend(loc=1)


Attempting 333 edge rewires, out of 333 edges
Rewiring 0 out of 333
Rewiring 10 out of 333
Rewiring 20 out of 333
Rewiring 30 out of 333
Rewiring 40 out of 333
Rewiring 50 out of 333
Rewiring 60 out of 333
Rewiring 70 out of 333
Rewiring 80 out of 333
Rewiring 90 out of 333
Rewiring 100 out of 333
Rewiring 110 out of 333
Rewiring 120 out of 333
Rewiring 130 out of 333
Rewiring 140 out of 333
Rewiring 150 out of 333
Rewiring 160 out of 333
Rewiring 170 out of 333
Rewiring 180 out of 333
Rewiring 190 out of 333
Rewiring 200 out of 333
Rewiring 210 out of 333
Rewiring 220 out of 333
Rewiring 230 out of 333
Rewiring 240 out of 333
Rewiring 250 out of 333
Rewiring 260 out of 333
Couldn't make a move!
Rewired 78.1 percent of edges
Attempting 333 edge rewires, out of 333 edges
Rewiring 0 out of 333
Rewiring 10 out of 333
Rewiring 20 out of 333
Rewiring 30 out of 333
Rewiring 40 out of 333
Rewiring 50 out of 333
Rewiring 60 out of 333
Rewiring 70 out of 333
Rewiring 80 out of 333
Rewiring 90 out of 333
Rewiring 100 out of 333
Rewiring 110 out of 333
Rewiring 120 out of 333
Rewiring 130 out of 333
Rewiring 140 out of 333
Rewiring 150 out of 333
Rewiring 160 out of 333
Rewiring 170 out of 333
Rewiring 180 out of 333
Rewiring 190 out of 333
Rewiring 200 out of 333
Rewiring 210 out of 333
Rewiring 220 out of 333
Rewiring 230 out of 333
Rewiring 240 out of 333
Couldn't make a move!
Rewired 73.3 percent of edges
Out[7]:
<matplotlib.legend.Legend at 0x10a43c9e8>

In [36]:
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
                                                                             preserve_degrees=True,
                                                                             property_functions=None)

g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
improve_worst_A = cluster_rewire_graph(A, rewire_function=cr.one_move_improve_worst,
                                                                                 preserve_degrees=True,
                                                                                 property_functions=None)
figure()
hist(sum(find_best_A,axis=1), alpha=.7, label='Rewired (Find Best)', normed=True)
hist(sum(improve_worst_A,axis=1), alpha=.7, label='Rewired (Improve Worst)', normed=True)
hist(sum(nx.adjacency_matrix(g).todense(),axis=1), alpha=.7, label='Original Network', normed=True)
ylabel("P(Degree)")
xlabel("Node Degree")
legend(loc=1)


Attempting 5231 edge rewires, out of 5231 edges
Rewiring 0 out of 5231
Rewiring 10 out of 5231
Rewiring 20 out of 5231
Rewiring 30 out of 5231
Rewiring 40 out of 5231
Rewiring 50 out of 5231
Rewiring 60 out of 5231
Rewiring 70 out of 5231
Rewiring 80 out of 5231
Rewiring 90 out of 5231
Rewiring 100 out of 5231
Rewiring 110 out of 5231
Rewiring 120 out of 5231
Rewiring 130 out of 5231
Rewiring 140 out of 5231
Rewiring 150 out of 5231
Rewiring 160 out of 5231
Rewiring 170 out of 5231
Rewiring 180 out of 5231
Rewiring 190 out of 5231
Rewiring 200 out of 5231
Rewiring 210 out of 5231
Rewiring 220 out of 5231
Rewiring 230 out of 5231
Rewiring 240 out of 5231
Rewiring 250 out of 5231
Rewiring 260 out of 5231
Rewiring 270 out of 5231
Rewiring 280 out of 5231
Rewiring 290 out of 5231
Rewiring 300 out of 5231
Rewiring 310 out of 5231
Rewiring 320 out of 5231
Rewiring 330 out of 5231
Rewiring 340 out of 5231
Rewiring 350 out of 5231
Rewiring 360 out of 5231
Rewiring 370 out of 5231
Rewiring 380 out of 5231
Rewiring 390 out of 5231
Rewiring 400 out of 5231
Rewiring 410 out of 5231
Rewiring 420 out of 5231
Rewiring 430 out of 5231
Rewiring 440 out of 5231
Rewiring 450 out of 5231
Rewiring 460 out of 5231
Rewiring 470 out of 5231
Rewiring 480 out of 5231
Rewiring 490 out of 5231
Rewiring 500 out of 5231
Rewiring 510 out of 5231
Rewiring 520 out of 5231
Rewiring 530 out of 5231
Rewiring 540 out of 5231
Rewiring 550 out of 5231
Rewiring 560 out of 5231
Rewiring 570 out of 5231
Rewiring 580 out of 5231
Rewiring 590 out of 5231
Rewiring 600 out of 5231
Rewiring 610 out of 5231
Rewiring 620 out of 5231
Rewiring 630 out of 5231
Rewiring 640 out of 5231
Rewiring 650 out of 5231
Rewiring 660 out of 5231
Rewiring 670 out of 5231
Rewiring 680 out of 5231
Rewiring 690 out of 5231
Rewiring 700 out of 5231
Rewiring 710 out of 5231
Rewiring 720 out of 5231
Rewiring 730 out of 5231
Rewiring 740 out of 5231
Rewiring 750 out of 5231
Rewiring 760 out of 5231
Rewiring 770 out of 5231
Rewiring 780 out of 5231
Rewiring 790 out of 5231
Rewiring 800 out of 5231
Rewiring 810 out of 5231
Rewiring 820 out of 5231
Rewiring 830 out of 5231
Rewiring 840 out of 5231
Rewiring 850 out of 5231
Rewiring 860 out of 5231
Rewiring 870 out of 5231
Rewiring 880 out of 5231
Rewiring 890 out of 5231
Rewiring 900 out of 5231
Rewiring 910 out of 5231
Rewiring 920 out of 5231
Rewiring 930 out of 5231
Rewiring 940 out of 5231
Rewiring 950 out of 5231
Rewiring 960 out of 5231
Rewiring 970 out of 5231
Rewiring 980 out of 5231
Rewiring 990 out of 5231
Rewiring 1000 out of 5231
Rewiring 1010 out of 5231
Rewiring 1020 out of 5231
Rewiring 1030 out of 5231
Rewiring 1040 out of 5231
Rewiring 1050 out of 5231
Rewiring 1060 out of 5231
Rewiring 1070 out of 5231
Rewiring 1080 out of 5231
Rewiring 1090 out of 5231
Rewiring 1100 out of 5231
Rewiring 1110 out of 5231
Rewiring 1120 out of 5231
Rewiring 1130 out of 5231
Rewiring 1140 out of 5231
Rewiring 1150 out of 5231
Rewiring 1160 out of 5231
Rewiring 1170 out of 5231
Rewiring 1180 out of 5231
Rewiring 1190 out of 5231
Rewiring 1200 out of 5231
Rewiring 1210 out of 5231
Rewiring 1220 out of 5231
Rewiring 1230 out of 5231
Rewiring 1240 out of 5231
Rewiring 1250 out of 5231
Rewiring 1260 out of 5231
Rewiring 1270 out of 5231
Rewiring 1280 out of 5231
Rewiring 1290 out of 5231
Rewiring 1300 out of 5231
Rewiring 1310 out of 5231
Rewiring 1320 out of 5231
Rewiring 1330 out of 5231
Rewiring 1340 out of 5231
Rewiring 1350 out of 5231
Rewiring 1360 out of 5231
Rewiring 1370 out of 5231
Rewiring 1380 out of 5231
Rewiring 1390 out of 5231
Rewiring 1400 out of 5231
Rewiring 1410 out of 5231
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-36-13b6fa01c6e8> in <module>()
      3 find_best_A = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
      4                                                                              preserve_degrees=True,
----> 5                                                                              property_functions=None)
      6 
      7 g = original_graph.copy()

/Users/jeff/Dropbox/MRC_NetworkScience_clustering/Code/MRC_Notebook/clusterrewire.py in cluster_rewire_graph(A, percent_of_edges_to_rewire, n_trials, rewire_function, verbose, verbose_count, property_functions, preserve_degrees)
    221 
    222         outputs = rewire_function(A, A2, fixed, preserve_degrees)
--> 223         if not outputs:
    224             if verbose:
    225                 print("Couldn't make a move!")

/Users/jeff/Dropbox/MRC_NetworkScience_clustering/Code/MRC_Notebook/clusterrewire.py in one_move_find_best(A, A2, fixed, preserve_degrees)
     15         if doorway[0]==doorway[1]: #The proposed target is a self link
     16             continue
---> 17         door = find_door(A, doorway, A2, fixed, preserve_degrees)
     18         if door:
     19             hinge, target, latch = door

/Users/jeff/Dropbox/MRC_NetworkScience_clustering/Code/MRC_Notebook/clusterrewire.py in find_door(A, sides_of_doorway, A2, fixed, preserve_degrees)
     49 
     50     best_doors = [find_door_helper(sides_of_doorway[0], sides_of_doorway[1], A, A2, 
---> 51                                    fixed, wedges_across_doorway, preserve_degrees),
     52                   find_door_helper(sides_of_doorway[1], sides_of_doorway[0], A, A2, 
     53                                    fixed, wedges_across_doorway, preserve_degrees)]

/Users/jeff/Dropbox/MRC_NetworkScience_clustering/Code/MRC_Notebook/clusterrewire.py in find_door_helper(hinge, latch, A, A2, fixed, wedges_across_doorway, preserve_degrees)
     29     def find_door_helper(hinge, latch, A, A2, 
     30                          fixed, wedges_across_doorway, preserve_degrees):
---> 31         latch_degree = A[latch].sum()
     32         neighbors_of_hinge = A[hinge].astype('bool')
     33         neighbors_degrees = A.sum(axis=1)

/Users/jeff/anaconda3/lib/python3.4/site-packages/numpy/core/_methods.py in _sum(a, axis, dtype, out, keepdims)
     30 
     31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):
---> 32     return umr_sum(a, axis, dtype, out, keepdims)
     33 
     34 def _prod(a, axis=None, dtype=None, out=None, keepdims=False):

KeyboardInterrupt: 

In [12]:
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best)
find_best_clustering = array(triangles_completed)/array(triangles_possible)


Attempting 326 edge rewires, out of 326 edges
Rewiring 0 out of 326
Rewiring 10 out of 326
Rewiring 20 out of 326
Rewiring 30 out of 326
Rewiring 40 out of 326
Rewiring 50 out of 326
Rewiring 60 out of 326
Rewiring 70 out of 326
Rewiring 80 out of 326
Rewiring 90 out of 326
Rewiring 100 out of 326
Rewiring 110 out of 326
Rewiring 120 out of 326
Rewiring 130 out of 326
Rewiring 140 out of 326
Rewiring 150 out of 326
Rewiring 160 out of 326
Rewiring 170 out of 326
Rewiring 180 out of 326
Rewiring 190 out of 326
Rewiring 200 out of 326
Rewiring 210 out of 326
Rewiring 220 out of 326
Rewiring 230 out of 326
Rewiring 240 out of 326
Rewiring 250 out of 326
Rewiring 260 out of 326
Couldn't make a move!
Rewired 80.7 percent of edges

In [13]:
plot(array(triangles_completed)/array(triangles_possible))
min(diff(array(triangles_completed)/array(triangles_possible)))


Out[13]:
0.0

In [28]:
def number_of_triangles_update(nt, A, A2, hinge, doorstop, latch):
    #return nt + A2[hinge, latch] - A2[hinge,doorstop] #This isn't working for some reason and I don't know why
#     return nt + A2[hinge, latch] - A2[hinge,doorstop] + A[latch, doorstop]
    return nt + A2[hinge, latch] - A2[hinge,doorstop] + A[latch, doorstop]

def number_of_possible_triangles_update(np, A, A2, hinge, doorstop, latch):
#     return np + sum(A[latch]) - sum(A[doorstop]) -1 #This isn't working for some reason and I don't know why
    return np + (sum(A[latch])-1) - sum(A[doorstop])#This isn't working for some reason and I don't know why

g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed_test, triangles_possible_test) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
                                                                                        property_functions = [(cr.number_of_triangles, 
                                                                                                               number_of_triangles_update),
                                                                                                               (cr.number_of_possible_triangles,
                                                                                                                number_of_possible_triangles_update)])


Attempting 326 edge rewires, out of 326 edges
Rewiring 0 out of 326
Rewiring 10 out of 326
Rewiring 20 out of 326
Rewiring 30 out of 326
Rewiring 40 out of 326
Rewiring 50 out of 326
Rewiring 60 out of 326
Rewiring 70 out of 326
Rewiring 80 out of 326
Rewiring 90 out of 326
Rewiring 100 out of 326
Rewiring 110 out of 326
Rewiring 120 out of 326
Rewiring 130 out of 326
Rewiring 140 out of 326
Rewiring 150 out of 326
Rewiring 160 out of 326
Rewiring 170 out of 326
Rewiring 180 out of 326
Rewiring 190 out of 326
Rewiring 200 out of 326
Rewiring 210 out of 326
Rewiring 220 out of 326
Rewiring 230 out of 326
Rewiring 240 out of 326
Rewiring 250 out of 326
Rewiring 260 out of 326
Couldn't make a move!
Rewired 80.7 percent of edges

In [29]:
plot(array(triangles_completed_test)/array(triangles_possible_test))
min(diff(array(triangles_completed_test)/array(triangles_possible_test)))


Out[29]:
0.0

In [30]:
scatter(diff(triangles_completed),diff(triangles_completed_test))
plot((0,7),(0,7))
ylabel("True")
xlabel("My Code")
figure()
scatter(diff(triangles_possible),diff(triangles_possible_test))
ylabel("True")
xlabel("My Code")
plot((-2,4),(-2,4))
#plot(triangles_possible,triangles_possible_test)


Out[30]:
[<matplotlib.lines.Line2D at 0x10ab9a080>]

In [20]:
%%time
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed_test, triangles_possible_test) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best,
                                                                                        property_functions = [(cr.number_of_triangles, 
                                                                                                               number_of_triangles_update),
                                                                                                               (cr.number_of_possible_triangles,
                                                                                                                number_of_possible_triangles_update)])


Attempting 326 edge rewires, out of 326 edges
Rewiring 0 out of 326
Rewiring 10 out of 326
Rewiring 20 out of 326
Rewiring 30 out of 326
Rewiring 40 out of 326
Rewiring 50 out of 326
Rewiring 60 out of 326
Rewiring 70 out of 326
Rewiring 80 out of 326
Rewiring 90 out of 326
Rewiring 100 out of 326
Rewiring 110 out of 326
Rewiring 120 out of 326
Rewiring 130 out of 326
Rewiring 140 out of 326
Rewiring 150 out of 326
Rewiring 160 out of 326
Rewiring 170 out of 326
Rewiring 180 out of 326
Rewiring 190 out of 326
Rewiring 200 out of 326
Rewiring 210 out of 326
Rewiring 220 out of 326
Rewiring 230 out of 326
Rewiring 240 out of 326
Rewiring 250 out of 326
Rewiring 260 out of 326
Couldn't make a move!
Rewired 80.7 percent of edges
CPU times: user 4.22 s, sys: 84.6 ms, total: 4.3 s
Wall time: 4.83 s

In [21]:
%%time
g = original_graph.copy()
A = nx.adjacency_matrix(g).todense()
find_best_A, (triangles_completed, triangles_possible) = cluster_rewire_graph(A, rewire_function=cr.one_move_find_best)


Attempting 326 edge rewires, out of 326 edges
Rewiring 0 out of 326
Rewiring 10 out of 326
Rewiring 20 out of 326
Rewiring 30 out of 326
Rewiring 40 out of 326
Rewiring 50 out of 326
Rewiring 60 out of 326
Rewiring 70 out of 326
Rewiring 80 out of 326
Rewiring 90 out of 326
Rewiring 100 out of 326
Rewiring 110 out of 326
Rewiring 120 out of 326
Rewiring 130 out of 326
Rewiring 140 out of 326
Rewiring 150 out of 326
Rewiring 160 out of 326
Rewiring 170 out of 326
Rewiring 180 out of 326
Rewiring 190 out of 326
Rewiring 200 out of 326
Rewiring 210 out of 326
Rewiring 220 out of 326
Rewiring 230 out of 326
Rewiring 240 out of 326
Rewiring 250 out of 326
Rewiring 260 out of 326
Couldn't make a move!
Rewired 80.7 percent of edges
CPU times: user 7.64 s, sys: 108 ms, total: 7.75 s
Wall time: 7.95 s