Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / assortativity / tests / test_neighbor_degree.py @ 5cef0f13

History | View | Annotate | Download (2.46 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
import networkx as nx
4

    
5

    
6
class TestAverageNeighbor(object):
7

    
8
    def test_degree_p4(self):
9
        G = nx.path_graph(4)
10
        answer = {0: 2, 1: 1.5, 2: 1.5, 3: 2}
11
        nd = nx.average_neighbor_degree(G)
12
        assert_equal(nd, answer)
13

    
14
        D = G.to_directed()
15
        nd = nx.average_neighbor_degree(D)
16
        assert_equal(nd, answer)
17

    
18
        D = G.to_directed()
19
        nd = nx.average_neighbor_degree(D)
20
        assert_equal(nd, answer)
21

    
22
        D = G.to_directed()
23
        nd = nx.average_neighbor_degree(D, source='in', target='in')
24
        assert_equal(nd, answer)
25

    
26
    def test_degree_p4_weighted(self):
27
        G = nx.path_graph(4)
28
        G[1][2]['weight'] = 4
29
        answer = {0: 2, 1: 1.8, 2: 1.8, 3: 2}
30
        nd = nx.average_neighbor_degree(G, weight='weight')
31
        assert_equal(nd, answer)
32

    
33
        D = G.to_directed()
34
        nd = nx.average_neighbor_degree(D, weight='weight')
35
        assert_equal(nd, answer)
36

    
37
        D = G.to_directed()
38
        nd = nx.average_neighbor_degree(D, weight='weight')
39
        assert_equal(nd, answer)
40
        nd = nx.average_neighbor_degree(D, source='out', target='out',
41
                                        weight='weight')
42
        assert_equal(nd, answer)
43

    
44
        D = G.to_directed()
45
        nd = nx.average_neighbor_degree(D, source='in', target='in',
46
                                        weight='weight')
47
        assert_equal(nd, answer)
48

    
49
    def test_degree_k4(self):
50
        G = nx.complete_graph(4)
51
        answer = {0: 3, 1: 3, 2: 3, 3: 3}
52
        nd = nx.average_neighbor_degree(G)
53
        assert_equal(nd, answer)
54

    
55
        D = G.to_directed()
56
        nd = nx.average_neighbor_degree(D)
57
        assert_equal(nd, answer)
58

    
59
        D = G.to_directed()
60
        nd = nx.average_neighbor_degree(D)
61
        assert_equal(nd, answer)
62

    
63
        D = G.to_directed()
64
        nd = nx.average_neighbor_degree(D, source='in', target='in')
65
        assert_equal(nd, answer)
66

    
67
    def test_degree_k4_nodes(self):
68
        G = nx.complete_graph(4)
69
        answer = {1: 3.0, 2: 3.0}
70
        nd = nx.average_neighbor_degree(G, nodes=[1, 2])
71
        assert_equal(nd, answer)
72

    
73
    def test_degree_barrat(self):
74
        G = nx.star_graph(5)
75
        G.add_edges_from([(5, 6), (5, 7), (5, 8), (5, 9)])
76
        G[0][5]['weight'] = 5
77
        nd = nx.average_neighbor_degree(G)[5]
78
        assert_equal(nd, 1.8)
79
        nd = nx.average_neighbor_degree(G, weight='weight')[5]
80
        assert_almost_equal(nd, 3.222222, places=5)