Python networkx.degree_centrality() Examples

The following are 16 code examples of networkx.degree_centrality(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module networkx , or try the search function .
Example #1
Source File: analyse_meg_epilepsy_clips.py    From mmvt with GNU General Public License v3.0 8 votes vote down vote up
def _calc_graph_func(p):
    con, times_chunk, graph_func = p
    vals = []
    now = time.time()
    for run, t in enumerate(times_chunk):
        utils.time_to_go(now, run, len(times_chunk), 10)
        con_t = con[:, :, t]
        g = nx.from_numpy_matrix(con_t)
        if graph_func == 'closeness_centrality':
            x = nx.closeness_centrality(g)
        elif graph_func == 'degree_centrality':
            x = nx.degree_centrality(g)
        elif graph_func == 'eigenvector_centrality':
            x = nx.eigenvector_centrality(g, max_iter=10000)
        elif graph_func == 'katz_centrality':
            x = nx.katz_centrality(g, max_iter=100000)
        else:
            raise Exception('Wrong graph func!')
        vals.append([x[k] for k in range(len(x))])
    vals = np.array(vals)
    return vals, times_chunk 
Example #2
Source File: test_degree_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_degree_centrality_1(self):
        d = nx.degree_centrality(self.K5)
        exact = dict(zip(range(5), [1]*5))
        for n,dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #3
Source File: test_degree_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_degree_centrality_2(self):
        d = nx.degree_centrality(self.P3)
        exact = {0:0.5, 1:1, 2:0.5}
        for n,dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #4
Source File: test_degree_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_degree_centrality_3(self):
        d = nx.degree_centrality(self.K)
        exact = {0:.444, 1:.444, 2:.333, 3:.667, 4:.333,
                 5:.556, 6:.556, 7:.333, 8:.222, 9:.111}
        for n,dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc)) 
Example #5
Source File: test_degree_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_degree_centrality_4(self):
        d = nx.degree_centrality(self.F)
        names = sorted(self.F.nodes())
        dcs = [0.071, 0.214, 0.143, 0.214, 0.214, 0.071, 0.286, 
               0.071, 0.429, 0.071, 0.214, 0.214, 0.143, 0.286, 0.214]
        exact = dict(zip(names, dcs))
        for n,dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc)) 
Example #6
Source File: graph_test.py    From mmvt with GNU General Public License v3.0 5 votes vote down vote up
def calc_closeness_centrality(p):
    con, times_chunk = p
    vals = []
    now = time.time()
    for run, t in enumerate(times_chunk):
        utils.time_to_go(now, run, len(times_chunk), 10)
        con_t = con[:, :, t]
        g = nx.from_numpy_matrix(con_t)
        # x = nx.closeness_centrality(g)
        x = nx.degree_centrality(g)
        vals.append([x[k] for k in range(len(x))])
    vals = np.array(vals)
    return vals, times_chunk 
Example #7
Source File: contact_network_analysis.py    From getcontacts with Apache License 2.0 5 votes vote down vote up
def degree_centrality_dist(graph, plot=False):
    """
    Calculate and plot distribution for degree centrality for all nodes in protein network

    Parameters
    ----------
    graph: networkx object
        Residue interaction graph object
    plot: bool
        Default = False. Display distribution
    Returns
    -------
    dc: dictionary
        Mapping between node to degree centrality values
    """

    dc = nx.degree_centrality(graph)
    centrality_values = []
    for key, value in reversed(sorted(dc.items(), key=lambda item: (item[1], item[0]))):
        print("%s: %s" % (key, value))
        centrality_values += [value]
    if plot:
        sns.distplot(centrality_values)
        plt.xlabel("Degree Centrality")
        plt.ylabel("Frequency")
        plt.title("Degree Centrality Distribution")
        plt.show()
    return dc 
Example #8
Source File: test_degree_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_degree_centrality_1(self):
        d = nx.degree_centrality(self.K5)
        exact = dict(zip(range(5), [1] * 5))
        for n, dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #9
Source File: test_degree_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_degree_centrality_2(self):
        d = nx.degree_centrality(self.P3)
        exact = {0: 0.5, 1: 1, 2: 0.5}
        for n, dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #10
Source File: test_degree_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_degree_centrality_3(self):
        d = nx.degree_centrality(self.K)
        exact = {0: .444, 1: .444, 2: .333, 3: .667, 4: .333,
                 5: .556, 6: .556, 7: .333, 8: .222, 9: .111}
        for n, dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc)) 
Example #11
Source File: test_degree_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_degree_centrality_4(self):
        d = nx.degree_centrality(self.F)
        names = sorted(self.F.nodes())
        dcs = [0.071, 0.214, 0.143, 0.214, 0.214, 0.071, 0.286,
               0.071, 0.429, 0.071, 0.214, 0.214, 0.143, 0.286, 0.214]
        exact = dict(zip(names, dcs))
        for n, dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc)) 
Example #12
Source File: test_degree_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_small_graph_centrality(self):
        G = nx.empty_graph(create_using=nx.DiGraph)
        assert_equal({}, nx.degree_centrality(G))
        assert_equal({}, nx.out_degree_centrality(G))
        assert_equal({}, nx.in_degree_centrality(G))

        G = nx.empty_graph(1, create_using=nx.DiGraph)
        assert_equal({0: 1}, nx.degree_centrality(G))
        assert_equal({0: 1}, nx.out_degree_centrality(G))
        assert_equal({0: 1}, nx.in_degree_centrality(G)) 
Example #13
Source File: test_degree_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_centrality_1(self):
        d = nx.degree_centrality(self.K5)
        exact = dict(zip(range(5), [1]*5))
        for n,dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #14
Source File: test_degree_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_centrality_2(self):
        d = nx.degree_centrality(self.P3)
        exact = {0:0.5, 1:1, 2:0.5}
        for n,dc in d.items():
            assert_almost_equal(exact[n], dc) 
Example #15
Source File: test_degree_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_centrality_3(self):
        d = nx.degree_centrality(self.K)
        exact = {0:.444, 1:.444, 2:.333, 3:.667, 4:.333,
                 5:.556, 6:.556, 7:.333, 8:.222, 9:.111}
        for n,dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc)) 
Example #16
Source File: test_degree_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_degree_centrality_4(self):
        d = nx.degree_centrality(self.F)
        names = sorted(self.F.nodes())
        dcs = [0.071, 0.214, 0.143, 0.214, 0.214, 0.071, 0.286, 
               0.071, 0.429, 0.071, 0.214, 0.214, 0.143, 0.286, 0.214]
        exact = dict(zip(names, dcs))
        for n,dc in d.items():
            assert_almost_equal(exact[n], float("%5.3f" % dc))