Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.38 KB)

1
"""Unit tests for the :mod:`networkx.generators.lattice` module."""
2

    
3
from nose.tools import assert_equal
4
from nose.tools import assert_true
5
from nose.tools import assert_raises
6

    
7
import networkx as nx
8
from networkx.testing import assert_edges_equal
9

    
10

    
11
class TestGrid2DGraph:
12
    """Unit tests for :func:`networkx.generators.lattice.grid_2d_graph`"""
13

    
14
    def test_number_of_vertices(self):
15
        m, n = 5, 6
16
        G = nx.grid_2d_graph(m, n)
17
        assert_equal(len(G), m * n)
18

    
19
    def test_degree_distribution(self):
20
        m, n = 5, 6
21
        G = nx.grid_2d_graph(m, n)
22
        expected_histogram = [0, 0, 4, 2 * (m + n) - 8, (m - 2) * (n - 2)]
23
        assert_equal(nx.degree_histogram(G), expected_histogram)
24

    
25
    def test_directed(self):
26
        m, n = 5, 6
27
        G = nx.grid_2d_graph(m, n)
28
        H = nx.grid_2d_graph(m, n, create_using=nx.DiGraph())
29
        assert_equal(H.succ, G.adj)
30
        assert_equal(H.pred, G.adj)
31

    
32
    def test_multigraph(self):
33
        m, n = 5, 6
34
        G = nx.grid_2d_graph(m, n)
35
        H = nx.grid_2d_graph(m, n, create_using=nx.MultiGraph())
36
        assert_equal(list(H.edges()), list(G.edges()))
37

    
38
    def test_periodic(self):
39
        G = nx.grid_2d_graph(0, 0, periodic=True)
40
        assert_equal(dict(G.degree()), {})
41

    
42
        for m, n, H in [(2, 2, nx.cycle_graph(4)), (1, 7, nx.cycle_graph(7)),
43
                        (7, 1, nx.cycle_graph(7)),
44
                        (2, 5, nx.circular_ladder_graph(5)),
45
                        (5, 2, nx.circular_ladder_graph(5)),
46
                        (2, 4, nx.cubical_graph()),
47
                        (4, 2, nx.cubical_graph())]:
48
            G = nx.grid_2d_graph(m, n, periodic=True)
49
            assert_true(nx.could_be_isomorphic(G, H))
50

    
51
    def test_periodic_directed(self):
52
        G = nx.grid_2d_graph(4, 2, periodic=True)
53
        H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.DiGraph())
54
        assert_equal(H.succ, G.adj)
55
        assert_equal(H.pred, G.adj)
56

    
57
    def test_periodic_multigraph(self):
58
        G = nx.grid_2d_graph(4, 2, periodic=True)
59
        H = nx.grid_2d_graph(4, 2, periodic=True, create_using=nx.MultiGraph())
60
        assert_equal(list(G.edges()), list(H.edges()))
61

    
62
    def test_node_input(self):
63
        G = nx.grid_2d_graph(4, 2, periodic=True)
64
        H = nx.grid_2d_graph(range(4), range(2), periodic=True)
65
        assert_true(nx.is_isomorphic(H, G))
66
        H = nx.grid_2d_graph("abcd", "ef", periodic=True)
67
        assert_true(nx.is_isomorphic(H, G))
68
        G = nx.grid_2d_graph(5, 6)
69
        H = nx.grid_2d_graph(range(5), range(6))
70
        assert_edges_equal(H, G)
71

    
72

    
73
class TestGridGraph:
74
    """Unit tests for :func:`networkx.generators.lattice.grid_graph`"""
75

    
76
    def test_grid_graph(self):
77
        """grid_graph([n,m]) is a connected simple graph with the
78
        following properties:
79
        number_of_nodes = n*m
80
        degree_histogram = [0,0,4,2*(n+m)-8,(n-2)*(m-2)]
81
        """
82
        for n, m in [(3, 5), (5, 3), (4, 5), (5, 4)]:
83
            dim = [n, m]
84
            g = nx.grid_graph(dim)
85
            assert_equal(len(g), n * m)
86
            assert_equal(nx.degree_histogram(g), [0, 0, 4, 2 * (n + m) - 8,
87
                                                  (n - 2) * (m - 2)])
88

    
89
        for n, m in [(1, 5), (5, 1)]:
90
            dim = [n, m]
91
            g = nx.grid_graph(dim)
92
            assert_equal(len(g), n * m)
93
            assert_true(nx.is_isomorphic(g, nx.path_graph(5)))
94

    
95
#        mg = nx.grid_graph([n,m], create_using=MultiGraph())
96
#        assert_equal(mg.edges(), g.edges())
97

    
98
    def test_node_input(self):
99
        G = nx.grid_graph([range(7, 9), range(3, 6)])
100
        assert_equal(len(G), 2 * 3)
101
        assert_true(nx.is_isomorphic(G, nx.grid_graph([2, 3])))
102

    
103

    
104
class TestHypercubeGraph:
105
    """Unit tests for :func:`networkx.generators.lattice.hypercube_graph`"""
106

    
107
    def test_special_cases(self):
108
        for n, H in [(0, nx.null_graph()), (1, nx.path_graph(2)),
109
                     (2, nx.cycle_graph(4)), (3, nx.cubical_graph())]:
110
            G = nx.hypercube_graph(n)
111
            assert_true(nx.could_be_isomorphic(G, H))
112

    
113
    def test_degree_distribution(self):
114
        for n in range(1, 10):
115
            G = nx.hypercube_graph(n)
116
            expected_histogram = [0] * n + [2 ** n]
117
            assert_equal(nx.degree_histogram(G), expected_histogram)
118

    
119

    
120
class TestTriangularLatticeGraph:
121
    "Tests for :func:`networkx.generators.lattice.triangular_lattice_graph`"
122

    
123
    def test_lattice_points(self):
124
        """Tests that the graph is really a triangular lattice."""
125
        for m, n in [(2, 3), (2, 2), (2, 1), (3, 3), (3, 2), (3, 4)]:
126
            G = nx.triangular_lattice_graph(m, n)
127
            N = (n + 1) // 2
128
            assert_equal(len(G), (m + 1) * (1 + N) - (n % 2) * ((m + 1) // 2))
129
        for (i, j) in G.nodes():
130
            nbrs = G[(i, j)]
131
            if i < N:
132
                assert_true((i + 1, j) in nbrs)
133
            if j < m:
134
                assert_true((i, j + 1) in nbrs)
135
            if j < m and (i > 0 or j % 2) and (i < N or (j + 1) % 2):
136
                assert_true((i + 1, j + 1) in nbrs or (i - 1, j + 1) in nbrs)
137

    
138
    def test_directed(self):
139
        """Tests for creating a directed triangular lattice."""
140
        G = nx.triangular_lattice_graph(3, 4, create_using=nx.Graph())
141
        H = nx.triangular_lattice_graph(3, 4, create_using=nx.DiGraph())
142
        assert_true(H.is_directed())
143
        for u, v in H.edges():
144
            assert_true(v[1] >= u[1])
145
            if v[1] == u[1]:
146
                assert_true(v[0] > u[0])
147

    
148
    def test_multigraph(self):
149
        """Tests for creating a triangular lattice multigraph."""
150
        G = nx.triangular_lattice_graph(3, 4, create_using=nx.Graph())
151
        H = nx.triangular_lattice_graph(3, 4, create_using=nx.MultiGraph())
152
        assert_equal(list(H.edges()), list(G.edges()))
153

    
154
    def test_periodic(self):
155
        G = nx.triangular_lattice_graph(4, 6, periodic=True)
156
        assert_equal(len(G), 12)
157
        assert_equal(G.size(), 36)
158
        # all degrees are 6
159
        assert_equal(len([n for n, d in G.degree() if d != 6]), 0)
160
        G = nx.triangular_lattice_graph(5, 7, periodic=True)
161
        TLG = nx.triangular_lattice_graph
162
        assert_raises(nx.NetworkXError, TLG, 2, 4, periodic=True)
163
        assert_raises(nx.NetworkXError, TLG, 4, 4, periodic=True)
164
        assert_raises(nx.NetworkXError, TLG, 2, 6, periodic=True)
165

    
166

    
167
class TestHexagonalLatticeGraph:
168
    "Tests for :func:`networkx.generators.lattice.hexagonal_lattice_graph`"
169

    
170
    def test_lattice_points(self):
171
        """Tests that the graph is really a hexagonal lattice."""
172
        for m, n in [(4, 5), (4, 4), (4, 3), (3, 2), (3, 3), (3, 5)]:
173
            G = nx.hexagonal_lattice_graph(m, n)
174
            assert_equal(len(G), 2 * (m + 1) * (n + 1) - 2)
175
        C_6 = nx.cycle_graph(6)
176
        hexagons = [
177
            [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)],
178
            [(0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4)],
179
            [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3)],
180
            [(2, 0), (2, 1), (2, 2), (3, 0), (3, 1), (3, 2)],
181
            [(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)],
182
        ]
183
        for hexagon in hexagons:
184
            assert_true(nx.is_isomorphic(G.subgraph(hexagon), C_6))
185

    
186
    def test_directed(self):
187
        """Tests for creating a directed hexagonal lattice."""
188
        G = nx.hexagonal_lattice_graph(3, 5, create_using=nx.Graph())
189
        H = nx.hexagonal_lattice_graph(3, 5, create_using=nx.DiGraph())
190
        assert_true(H.is_directed())
191
        pos = nx.get_node_attributes(H, 'pos')
192
        for u, v in H.edges():
193
            assert_true(pos[v][1] >= pos[u][1])
194
            if pos[v][1] == pos[u][1]:
195
                assert_true(pos[v][0] > pos[u][0])
196

    
197
    def test_multigraph(self):
198
        """Tests for creating a hexagonal lattice multigraph."""
199
        G = nx.hexagonal_lattice_graph(3, 5, create_using=nx.Graph())
200
        H = nx.hexagonal_lattice_graph(3, 5, create_using=nx.MultiGraph())
201
        assert_equal(list(H.edges()), list(G.edges()))
202

    
203
    def test_periodic(self):
204
        G = nx.hexagonal_lattice_graph(4, 6, periodic=True)
205
        assert_equal(len(G), 48)
206
        assert_equal(G.size(), 72)
207
        # all degrees are 3
208
        assert_equal(len([n for n, d in G.degree() if d != 3]), 0)
209
        G = nx.hexagonal_lattice_graph(5, 8, periodic=True)
210
        HLG = nx.hexagonal_lattice_graph
211
        assert_raises(nx.NetworkXError, HLG, 2, 7, periodic=True)
212
        assert_raises(nx.NetworkXError, HLG, 1, 4, periodic=True)
213
        assert_raises(nx.NetworkXError, HLG, 2, 1, periodic=True)