def test_ra_clustering_zero(): G = nx.Graph() assert_equal(bipartite.robins_alexander_clustering(G), 0) G.add_nodes_from(range(4)) assert_equal(bipartite.robins_alexander_clustering(G), 0) G.add_edges_from([(0, 1), (2, 3), (3, 4)]) assert_equal(bipartite.robins_alexander_clustering(G), 0) G.add_edge(1, 2) assert_equal(bipartite.robins_alexander_clustering(G), 0)
def compute_bipartite_small_world_index(G): Gc = max(nx.connected_component_subgraphs(G), key=len) actual_cc = bp.robins_alexander_clustering(G) actual_apl = nx.average_shortest_path_length(Gc) random_cc, random_apl = compute_cc_apl_random_networks(G) result = {} result['n'] = G.order() result['m'] = G.size() if 'python' in G.graph['name'].lower(): devs = len({n for n, d in G.nodes(data=True) if d['bipartite'] == 1}) result['developers'] = devs result['files'] = G.order() - devs else: devs = len({n for n, d in G.nodes(data=True) if d['bipartite'] == 0}) result['developers'] = devs result['packages'] = G.order() - devs result['actual_cc'] = actual_cc result['actual_apl'] = actual_apl result['random_cc'] = random_cc result['random_apl'] = random_apl try: result['swi'] = (actual_cc / random_cc) / (actual_apl / random_apl) except ZeroDivisionError: result['swi'] = float('nan') return result
def compute_cc_apl_random_networks(G): ccs = [] apls = [] for Gr in get_random_networks_2mode(G, n=10, r=10): ccs.append(bp.robins_alexander_clustering(Gr)) if not nx.is_connected(Gr): Gr = max(nx.connected_component_subgraphs(Gr), key=len) apls.append(nx.average_shortest_path_length(Gr)) return mean(ccs), mean(apls)
import networkx from networkx.algorithms import bipartite structures_dir = '/home/ucbtepi/code/network/data' tissue_c = [] random_c = [] gd_range = range(1,21) for gd in gd_range: print ' ' print gd tissue_model = networkx.read_graphml(structures_dir+'/gd{0}/cr0/gd{0}_cr0.graphml'.format(gd)) random_bipartite_model = networkx.read_graphml(structures_dir+'/gd{0}/cr1/gd{0}_cr1.graphml'.format(gd)) for k,g in enumerate([tissue_model, random_bipartite_model]): mf_nodes = list(set(n for n,d in g.nodes(data=True) if d['bipartite']==0)) grc_nodes = list(set(n for n,d in g.nodes(data=True) if d['bipartite']==1)) print(['tissue', 'random'][k]) c = bipartite.robins_alexander_clustering(g) print("robins-alexander clustering: {}".format(c)) if k==0: tissue_c.append(c) else: random_c.append(c) fig, ax = plt.subplots() ax.plot(gd_range, tissue_c, linewidth=1.5, color='k', label='tissue model') ax.plot(gd_range, random_c, linewidth=1.5, color='r', label='random bipartite graph') ax.set_xlabel('dendrites') ax.set_ylabel('Robins-Alexander clustering') ax.legend(loc='best') plt.show()
def robins_alexander_clustering(self): self.robins_alexander_clustering_dict = bi.robins_alexander_clustering(self.G)
def test_ra_clustering_square(): G = nx.path_graph(4) G.add_edge(0, 3) assert_equal(bipartite.robins_alexander_clustering(G), 1.0)
def test_ra_clustering_davis(): G = nx.davis_southern_women_graph() cc4 = round(bipartite.robins_alexander_clustering(G), 3) assert_equal(cc4, 0.468)
import networkx as nx import matplotlib.pyplot as plt from networkx.algorithms import bipartite from networkx.algorithms import centrality B = nx.Graph() B.add_nodes_from([1, 2, 3, 4], bipartite=0) B.add_nodes_from(['a', 'b', 'c', 'd'], bipartite=1) edges_list = [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (2, 'c'), (3, 'c'), (3, 'd'), (4, 'c'), (4, 'd')] B.add_edges_from(edges_list) pos = nx.spring_layout(B) sets = bipartite.sets(B) colors = ['r', 'b'] c = bipartite.robins_alexander_clustering(B) print centrality.degree_centrality(B) for i in range(len(sets)): nx.draw_networkx_nodes(B, pos, nodelist=list(sets[i]), node_color=colors[i]) nx.draw_networkx_edges(B, pos) plt.show()