Python networkx.katz_centrality() Examples

The following are 30 code examples of networkx.katz_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_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_beta_as_scalar(self):
        alpha = 0.1
        beta = 0.1
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        G = nx.path_graph(3)
        b = nx.katz_centrality(G, alpha, beta)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #3
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_bad_beta(self):
        G = nx.Graph([(0, 1)])
        beta = {0: 77}
        e = nx.katz_centrality(G, 0.1, beta=beta) 
Example #4
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_bad_beta_numbe(self):
        G = nx.Graph([(0, 1)])
        e = nx.katz_centrality(G, 0.1, beta='foo') 
Example #5
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #6
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty(self):
        e = nx.katz_centrality(nx.Graph(), 0.1)
        assert_equal(e, {}) 
Example #7
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_K5_unweighted(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha, weight=None)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #8
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_katz_centrality_weighted(self):
        G = self.G
        alpha = self.G.alpha
        p = nx.katz_centrality(G, alpha, weight='weight')
        for (a, b) in zip(list(p.values()), self.G.evc):
            assert_almost_equal(a, b) 
Example #9
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_katz_centrality_unweighted(self):
        H = self.H
        alpha = self.H.alpha
        p = nx.katz_centrality(H, alpha, weight='weight')
        for (a, b) in zip(list(p.values()), self.H.evc):
            assert_almost_equal(a, b) 
Example #10
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.katz_centrality(G, alpha, nstart=nstart)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n]) 
Example #11
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_P3(self):
        """Katz centrality: P3"""
        alpha = 0.1
        G = nx.path_graph(3)
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        b = nx.katz_centrality(G, alpha)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #12
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_maxiter(self):
        alpha = 0.1
        G = nx.path_graph(3)
        max_iter = 0
        try:
            b = nx.katz_centrality(G, alpha, max_iter=max_iter)
        except nx.NetworkXError as e:
            assert str(max_iter) in e.args[0], "max_iter value not in error msg"
            raise  # So that the decorater sees the exception. 
Example #13
Source File: test_katz_centrality.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_empty(self):
        e = nx.katz_centrality(nx.Graph(), 0.1)
        assert_equal(e, {}) 
Example #14
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_beta_as_dict(self):
        alpha = 0.1
        beta = {0: 1.0, 1: 1.0, 2: 1.0}
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        G = nx.path_graph(3)
        b = nx.katz_centrality(G, alpha, beta)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #15
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_multigraph(self):
        e = nx.katz_centrality(nx.MultiGraph(), 0.1) 
Example #16
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_empty(self):
        e = nx.katz_centrality(nx.Graph(), 0.1)
        assert_equal(e, {}) 
Example #17
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_bad_beta(self):
        G = nx.Graph([(0, 1)])
        beta = {0: 77}
        e = nx.katz_centrality(G, 0.1, beta=beta) 
Example #18
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_bad_beta_numbe(self):
        G = nx.Graph([(0, 1)])
        e = nx.katz_centrality(G, 0.1, beta='foo') 
Example #19
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #20
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_empty(self):
        e = nx.katz_centrality(nx.Graph(), 0.1)
        assert_equal(e, {}) 
Example #21
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_K5_unweighted(self):
        """Katz centrality: K5"""
        G = nx.complete_graph(5)
        alpha = 0.1
        b = nx.katz_centrality(G, alpha, weight=None)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = nx.eigenvector_centrality_numpy(G, weight=None)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3) 
Example #22
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_katz_centrality_weighted(self):
        G = self.G
        alpha = self.G.alpha
        p = nx.katz_centrality(G, alpha, weight='weight')
        for (a, b) in zip(list(p.values()), self.G.evc):
            assert_almost_equal(a, b) 
Example #23
Source File: test_katz_centrality.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def test_katz_centrality_unweighted(self):
        H = self.H
        alpha = self.H.alpha
        p = nx.katz_centrality(H, alpha, weight='weight')
        for (a, b) in zip(list(p.values()), self.H.evc):
            assert_almost_equal(a, b) 
Example #24
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_empty(self):
        e = networkx.katz_centrality(networkx.Graph(), 0.1)
        assert_equal(e, {}) 
Example #25
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_K5(self):
        """Katz centrality: K5"""
        G = networkx.complete_graph(5)
        alpha = 0.1
        b = networkx.katz_centrality(G, alpha)
        v = math.sqrt(1 / 5.0)
        b_answer = dict.fromkeys(G, v)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n])
        nstart = dict([(n, 1) for n in G])
        b = networkx.katz_centrality(G, alpha, nstart=nstart)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n]) 
Example #26
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_P3(self):
        """Katz centrality: P3"""
        alpha = 0.1
        G = networkx.path_graph(3)
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        b = networkx.katz_centrality(G, alpha)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #27
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_maxiter(self):
        alpha = 0.1
        G = networkx.path_graph(3)
        max_iter = 0
        try:
            b = networkx.katz_centrality(G, alpha, max_iter=max_iter)
        except networkx.NetworkXError as e:
            assert str(max_iter) in e.args[0], "max_iter value not in error msg"
            raise # So that the decorater sees the exception. 
Example #28
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_beta_as_scalar(self):
        alpha = 0.1
        beta = 0.1
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        G = networkx.path_graph(3)
        b = networkx.katz_centrality(G, alpha, beta)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #29
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_beta_as_dict(self):
        alpha = 0.1
        beta = {0: 1.0, 1: 1.0, 2: 1.0}
        b_answer = {0: 0.5598852584152165, 1: 0.6107839182711449,
                    2: 0.5598852584152162}
        G = networkx.path_graph(3)
        b = networkx.katz_centrality(G, alpha, beta)
        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=4) 
Example #30
Source File: test_katz_centrality.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def test_multigraph(self):
        e = networkx.katz_centrality(networkx.MultiGraph(), 0.1)