Пример #1
0
 def assign_cores_to_communities(graph):
     """"""
     num_comm = max([graph.nodes[n]['community'] for n in graph.nodes])
     community_coreness_be = {i: 0 for i in range(num_comm)}
     community_coreness_rb = {i: 0 for i in range(num_comm)}
     for i in range(num_comm+1):
         community = [n for n in graph.nodes if graph.nodes[n]['community']==i]
         subgraph = graph.subgraph(community).copy()
         matrix = nx.convert_matrix.to_numpy_array(subgraph)
         if (matrix.size>1) & (np.sum(matrix)>0):
             # borgatti-everett
             be = bct.core_periphery_dir(matrix)
             # rombach
             rb = cpa.Rombach()
             rb.detect(subgraph)
             # assign
             community_coreness_be[i] = be[1]
             community_coreness_rb[i] = rb.score()[0]
             cp_rb = rb.get_coreness()
             for j, node in enumerate(subgraph.nodes):
                 graph.nodes[node]['community_core_be'] = be[0][j]
                 graph.nodes[node]['community_core_rb'] = cp_rb[node]
         else:
             community_coreness_be[i] = 0
             community_coreness_rb[i] = 0
             for j, node in enumerate(subgraph.nodes):
                 graph.nodes[node]['community_core_be'] = 1
                 graph.nodes[node]['community_core_rb'] = 1
     graph.graph['community_coreness_be'] = community_coreness_be
     graph.graph['community_coreness_rb'] = community_coreness_rb
Пример #2
0
 def assign_core_periphery(graph):
     """ Compute core-periphery of ``graph`` (``nx.DiGraph``;
     converted to symmetric ``nx.Graph``).
     Assign ``core`` as ``1`` or ``0`` to each node.
     Assign ``coreness`` to ``graph``.
     See ``core_periphery_dir()`` in ``bctpy``.
     """
     # borgatti-everett
     be = bct.core_periphery_dir(nx.convert_matrix.to_numpy_array(graph))
     for i, node in enumerate(graph.nodes):
         graph.nodes[node]['core_be'] = be[0][i]
     graph.graph['coreness_be'] = be[1]
Пример #3
0
 def assign_core_periphery(graph):
     """ Compute core-periphery of ``graph`` (``nx.DiGraph``;
     converted to symmetric ``nx.Graph``).
     Assign ``core`` as ``1`` or ``0`` to each node.
     Assign ``coreness`` to ``graph``.
     See ``core_periphery_dir()`` in ``bctpy``.
     """
     # borgatti-everett
     be = bct.core_periphery_dir(nx.convert_matrix.to_numpy_array(graph))
     for i, node in enumerate(graph.nodes):
         graph.nodes[node]['core_be'] = be[0][i]
     graph.graph['coreness_be'] = be[1]
     # rombach
     rb = cpa.Rombach()
     rb.detect(graph)
     if rb.get_coreness() != 0:
         for node, coreness in rb.get_coreness().items():
             graph.nodes[node]['core_rb'] = coreness
         graph.graph['coreness_rb'] = rb.score()[0]
     else:
         for node in graph.nodes:
             graph.nodes[node]['core_rb'] = 0
         graph.graph['coreness_rb'] = 0
Пример #4
0
def core_periphery_analysis(network0):
    network0 /= np.sum(network0)
    C, Q_core = bct.core_periphery_dir(network0)
    per_nodes = []
    for i in range(len(C)):
        if C[i] == 0:
            per_nodes.append(i)
    G = nx.from_numpy_matrix(network0)
    G_per = G.subgraph(per_nodes)
    per_network = np.array(nx.to_numpy_matrix(G_per))
    M_per, Q_comm_per = bct.community_louvain(per_network)
    print(Q_comm_per, "Q")
    # print(M_per, Q_comm_per)
    per_comm_assignments = {}
    for i in range(len(per_nodes)):
        per_comm_assignments[per_nodes[i]] = M_per[i]
    classifications = [
        [], [], []
    ]  # index 0 means periphery-periphery edge, 1 means periphery-core, 2 means core-core
    for i in range(len(network0) - 1):
        for j in range(i + 1, len(network0)):
            if network0[i][j] > 0:
                classifications[C[i] + C[j]].append((i, j))
    return classifications, per_comm_assignments, G_per, M_per
Пример #5
0
def test_core_periphery_dir():
    x = load_sample(thres=.1)
    c, q = bct.core_periphery_dir(x)
    assert np.sum(c) == 57
    assert np.sum(np.cumsum(c)) == 4170
    assert np.allclose(q, .3086, atol=.0001)
Пример #6
0
def test_core_periphery_dir():
    x = load_sample(thres=.1)
    c, q = bct.core_periphery_dir(x)
    assert np.sum(c) == 57
    assert np.sum(np.cumsum(c)) == 4170
    assert np.allclose(q, .3086, atol=.0001)