Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / tests / test_relabel.py @ 5cef0f13

History | View | Annotate | Download (7.48 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from networkx import *
4
from networkx.convert import *
5
from networkx.algorithms.operators import *
6
from networkx.generators.classic import barbell_graph, cycle_graph
7
from networkx.testing import *
8

    
9

    
10
class TestRelabel():
11
    def test_convert_node_labels_to_integers(self):
12
        # test that empty graph converts fine for all options
13
        G = empty_graph()
14
        H = convert_node_labels_to_integers(G, 100)
15
        assert_equal(list(H.nodes()), [])
16
        assert_equal(list(H.edges()), [])
17

    
18
        for opt in ["default", "sorted", "increasing degree", "decreasing degree"]:
19
            G = empty_graph()
20
            H = convert_node_labels_to_integers(G, 100, ordering=opt)
21
            assert_equal(list(H.nodes()), [])
22
            assert_equal(list(H.edges()), [])
23

    
24
        G = empty_graph()
25
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
26
        H = convert_node_labels_to_integers(G)
27
        degH = (d for n, d in H.degree())
28
        degG = (d for n, d in G.degree())
29
        assert_equal(sorted(degH), sorted(degG))
30

    
31
        H = convert_node_labels_to_integers(G, 1000)
32
        degH = (d for n, d in H.degree())
33
        degG = (d for n, d in G.degree())
34
        assert_equal(sorted(degH), sorted(degG))
35
        assert_nodes_equal(H.nodes(), [1000, 1001, 1002, 1003])
36

    
37
        H = convert_node_labels_to_integers(G, ordering="increasing degree")
38
        degH = (d for n, d in H.degree())
39
        degG = (d for n, d in G.degree())
40
        assert_equal(sorted(degH), sorted(degG))
41
        assert_equal(degree(H, 0), 1)
42
        assert_equal(degree(H, 1), 2)
43
        assert_equal(degree(H, 2), 2)
44
        assert_equal(degree(H, 3), 3)
45

    
46
        H = convert_node_labels_to_integers(G, ordering="decreasing degree")
47
        degH = (d for n, d in H.degree())
48
        degG = (d for n, d in G.degree())
49
        assert_equal(sorted(degH), sorted(degG))
50
        assert_equal(degree(H, 0), 3)
51
        assert_equal(degree(H, 1), 2)
52
        assert_equal(degree(H, 2), 2)
53
        assert_equal(degree(H, 3), 1)
54

    
55
        H = convert_node_labels_to_integers(G, ordering="increasing degree",
56
                                            label_attribute='label')
57
        degH = (d for n, d in H.degree())
58
        degG = (d for n, d in G.degree())
59
        assert_equal(sorted(degH), sorted(degG))
60
        assert_equal(degree(H, 0), 1)
61
        assert_equal(degree(H, 1), 2)
62
        assert_equal(degree(H, 2), 2)
63
        assert_equal(degree(H, 3), 3)
64

    
65
        # check mapping
66
        assert_equal(H.nodes[3]['label'], 'C')
67
        assert_equal(H.nodes[0]['label'], 'D')
68
        assert_true(H.nodes[1]['label'] == 'A' or H.nodes[2]['label'] == 'A')
69
        assert_true(H.nodes[1]['label'] == 'B' or H.nodes[2]['label'] == 'B')
70

    
71
    def test_convert_to_integers2(self):
72
        G = empty_graph()
73
        G.add_edges_from([('C', 'D'), ('A', 'B'), ('A', 'C'), ('B', 'C')])
74
        H = convert_node_labels_to_integers(G, ordering="sorted")
75
        degH = (d for n, d in H.degree())
76
        degG = (d for n, d in G.degree())
77
        assert_equal(sorted(degH), sorted(degG))
78

    
79
        H = convert_node_labels_to_integers(G, ordering="sorted",
80
                                            label_attribute='label')
81
        assert_equal(H.nodes[0]['label'], 'A')
82
        assert_equal(H.nodes[1]['label'], 'B')
83
        assert_equal(H.nodes[2]['label'], 'C')
84
        assert_equal(H.nodes[3]['label'], 'D')
85

    
86
    @raises(nx.NetworkXError)
87
    def test_convert_to_integers_raise(self):
88
        G = nx.Graph()
89
        H = convert_node_labels_to_integers(G, ordering="increasing age")
90

    
91
    def test_relabel_nodes_copy(self):
92
        G = empty_graph()
93
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
94
        mapping = {'A': 'aardvark', 'B': 'bear', 'C': 'cat', 'D': 'dog'}
95
        H = relabel_nodes(G, mapping)
96
        assert_nodes_equal(H.nodes(), ['aardvark', 'bear', 'cat', 'dog'])
97

    
98
    def test_relabel_nodes_function(self):
99
        G = empty_graph()
100
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
101
        # function mapping no longer encouraged but works
102

    
103
        def mapping(n):
104
            return ord(n)
105
        H = relabel_nodes(G, mapping)
106
        assert_nodes_equal(H.nodes(), [65, 66, 67, 68])
107

    
108
    def test_relabel_nodes_graph(self):
109
        G = Graph([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
110
        mapping = {'A': 'aardvark', 'B': 'bear', 'C': 'cat', 'D': 'dog'}
111
        H = relabel_nodes(G, mapping)
112
        assert_nodes_equal(H.nodes(), ['aardvark', 'bear', 'cat', 'dog'])
113

    
114
    def test_relabel_nodes_orderedgraph(self):
115
        G = OrderedGraph()
116
        G.add_nodes_from([1, 2, 3])
117
        G.add_edges_from([(1, 3), (2, 3)])
118
        mapping = {1: 'a', 2: 'b', 3: 'c'}
119
        H = relabel_nodes(G, mapping)
120
        assert list(H.nodes) == ['a', 'b', 'c']
121

    
122
    def test_relabel_nodes_digraph(self):
123
        G = DiGraph([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
124
        mapping = {'A': 'aardvark', 'B': 'bear', 'C': 'cat', 'D': 'dog'}
125
        H = relabel_nodes(G, mapping, copy=False)
126
        assert_nodes_equal(H.nodes(), ['aardvark', 'bear', 'cat', 'dog'])
127

    
128
    def test_relabel_nodes_multigraph(self):
129
        G = MultiGraph([('a', 'b'), ('a', 'b')])
130
        mapping = {'a': 'aardvark', 'b': 'bear'}
131
        G = relabel_nodes(G, mapping, copy=False)
132
        assert_nodes_equal(G.nodes(), ['aardvark', 'bear'])
133
        assert_edges_equal(G.edges(), [('aardvark', 'bear'), ('aardvark', 'bear')])
134

    
135
    def test_relabel_nodes_multidigraph(self):
136
        G = MultiDiGraph([('a', 'b'), ('a', 'b')])
137
        mapping = {'a': 'aardvark', 'b': 'bear'}
138
        G = relabel_nodes(G, mapping, copy=False)
139
        assert_nodes_equal(G.nodes(), ['aardvark', 'bear'])
140
        assert_edges_equal(G.edges(), [('aardvark', 'bear'), ('aardvark', 'bear')])
141

    
142
    def test_relabel_isolated_nodes_to_same(self):
143
        G = Graph()
144
        G.add_nodes_from(range(4))
145
        mapping = {1: 1}
146
        H = relabel_nodes(G, mapping, copy=False)
147
        assert_nodes_equal(H.nodes(), list(range(4)))
148

    
149
    @raises(KeyError)
150
    def test_relabel_nodes_missing(self):
151
        G = Graph([('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'D')])
152
        mapping = {0: 'aardvark'}
153
        G = relabel_nodes(G, mapping, copy=False)
154

    
155
    def test_relabel_copy_name(self):
156
        G = Graph()
157
        H = relabel_nodes(G, {}, copy=True)
158
        assert_equal(H.graph, G.graph)
159
        H = relabel_nodes(G, {}, copy=False)
160
        assert_equal(H.graph, G.graph)
161
        G.name = "first"
162
        H = relabel_nodes(G, {}, copy=True)
163
        assert_equal(H.graph, G.graph)
164
        H = relabel_nodes(G, {}, copy=False)
165
        assert_equal(H.graph, G.graph)
166

    
167
    def test_relabel_toposort(self):
168
        K5 = nx.complete_graph(4)
169
        G = nx.complete_graph(4)
170
        G = nx.relabel_nodes(G, dict([(i, i + 1) for i in range(4)]), copy=False)
171
        nx.is_isomorphic(K5, G)
172
        G = nx.complete_graph(4)
173
        G = nx.relabel_nodes(G, dict([(i, i - 1) for i in range(4)]), copy=False)
174
        nx.is_isomorphic(K5, G)
175

    
176
    def test_relabel_selfloop(self):
177
        G = nx.DiGraph([(1, 1), (1, 2), (2, 3)])
178
        G = nx.relabel_nodes(G, {1: 'One', 2: 'Two', 3: 'Three'}, copy=False)
179
        assert_nodes_equal(G.nodes(), ['One', 'Three', 'Two'])
180
        G = nx.MultiDiGraph([(1, 1), (1, 2), (2, 3)])
181
        G = nx.relabel_nodes(G, {1: 'One', 2: 'Two', 3: 'Three'}, copy=False)
182
        assert_nodes_equal(G.nodes(), ['One', 'Three', 'Two'])
183
        G = nx.MultiDiGraph([(1, 1)])
184
        G = nx.relabel_nodes(G, {1: 0}, copy=False)
185
        assert_nodes_equal(G.nodes(), [0])