summaryrefslogtreecommitdiff
path: root/networkx/algorithms/assortativity/tests/test_neighbor_degree.py
blob: 27abbb0e61ad509230582c8b57c84e3a9edc2025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pytest

import networkx as nx


class TestAverageNeighbor:
    def test_degree_p4(self):
        G = nx.path_graph(4)
        answer = {0: 2, 1: 1.5, 2: 1.5, 3: 2}
        nd = nx.average_neighbor_degree(G)
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D)
        assert nd == answer

        D = nx.DiGraph(G.edges(data=True))
        nd = nx.average_neighbor_degree(D)
        assert nd == {0: 1, 1: 1, 2: 0, 3: 0}
        nd = nx.average_neighbor_degree(D, "in", "out")
        assert nd == {0: 0, 1: 1, 2: 1, 3: 1}
        nd = nx.average_neighbor_degree(D, "out", "in")
        assert nd == {0: 1, 1: 1, 2: 1, 3: 0}
        nd = nx.average_neighbor_degree(D, "in", "in")
        assert nd == {0: 0, 1: 0, 2: 1, 3: 1}

    def test_degree_p4_weighted(self):
        G = nx.path_graph(4)
        G[1][2]["weight"] = 4
        answer = {0: 2, 1: 1.8, 2: 1.8, 3: 2}
        nd = nx.average_neighbor_degree(G, weight="weight")
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D, weight="weight")
        assert nd == answer

        D = nx.DiGraph(G.edges(data=True))
        print(D.edges(data=True))
        nd = nx.average_neighbor_degree(D, weight="weight")
        assert nd == {0: 1, 1: 1, 2: 0, 3: 0}
        nd = nx.average_neighbor_degree(D, "out", "out", weight="weight")
        assert nd == {0: 1, 1: 1, 2: 0, 3: 0}
        nd = nx.average_neighbor_degree(D, "in", "in", weight="weight")
        assert nd == {0: 0, 1: 0, 2: 1, 3: 1}
        nd = nx.average_neighbor_degree(D, "in", "out", weight="weight")
        assert nd == {0: 0, 1: 1, 2: 1, 3: 1}
        nd = nx.average_neighbor_degree(D, "out", "in", weight="weight")
        assert nd == {0: 1, 1: 1, 2: 1, 3: 0}

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D, weight="weight")
        assert nd == answer
        nd = nx.average_neighbor_degree(D, source="out", target="out", weight="weight")
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D, source="in", target="in", weight="weight")
        assert nd == answer

    def test_degree_k4(self):
        G = nx.complete_graph(4)
        answer = {0: 3, 1: 3, 2: 3, 3: 3}
        nd = nx.average_neighbor_degree(G)
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D)
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D)
        assert nd == answer

        D = G.to_directed()
        nd = nx.average_neighbor_degree(D, source="in", target="in")
        assert nd == answer

    def test_degree_k4_nodes(self):
        G = nx.complete_graph(4)
        answer = {1: 3.0, 2: 3.0}
        nd = nx.average_neighbor_degree(G, nodes=[1, 2])
        assert nd == answer

    def test_degree_barrat(self):
        G = nx.star_graph(5)
        G.add_edges_from([(5, 6), (5, 7), (5, 8), (5, 9)])
        G[0][5]["weight"] = 5
        nd = nx.average_neighbor_degree(G)[5]
        assert nd == 1.8
        nd = nx.average_neighbor_degree(G, weight="weight")[5]
        assert nd == pytest.approx(3.222222, abs=1e-5)

    def test_error_invalid_source_target(self):
        G = nx.path_graph(4)
        with pytest.raises(nx.NetworkXError):
            nx.average_neighbor_degree(G, "error")
        with pytest.raises(nx.NetworkXError):
            nx.average_neighbor_degree(G, "in", "error")
        G = G.to_directed()
        with pytest.raises(nx.NetworkXError):
            nx.average_neighbor_degree(G, "error")
        with pytest.raises(nx.NetworkXError):
            nx.average_neighbor_degree(G, "in", "error")