Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7.29 KB)

1
"""
2
    Unit tests for edgelists.
3
"""
4
from nose.tools import assert_equal, assert_raises, assert_not_equal
5
import io
6
import tempfile
7
import os
8

    
9
import networkx as nx
10
from networkx.testing import (assert_edges_equal, assert_nodes_equal,
11
                              assert_graphs_equal)
12

    
13

    
14
class TestEdgelist:
15

    
16
    def setUp(self):
17
        self.G = nx.Graph(name="test")
18
        e = [('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f'), ('a', 'f')]
19
        self.G.add_edges_from(e)
20
        self.G.add_node('g')
21
        self.DG = nx.DiGraph(self.G)
22
        self.XG = nx.MultiGraph()
23
        self.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
24
        self. XDG = nx.MultiDiGraph(self.XG)
25

    
26
    def test_read_edgelist_1(self):
27
        s = b"""\
28
# comment line
29
1 2
30
# comment line
31
2 3
32
"""
33
        bytesIO = io.BytesIO(s)
34
        G = nx.read_edgelist(bytesIO, nodetype=int)
35
        assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
36

    
37
    def test_read_edgelist_2(self):
38
        s = b"""\
39
# comment line
40
1 2 2.0
41
# comment line
42
2 3 3.0
43
"""
44
        bytesIO = io.BytesIO(s)
45
        G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
46
        assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
47

    
48
        bytesIO = io.BytesIO(s)
49
        G = nx.read_weighted_edgelist(bytesIO, nodetype=int)
50
        assert_edges_equal(G.edges(data=True),
51
                           [(1, 2, {'weight': 2.0}), (2, 3, {'weight': 3.0})])
52

    
53
    def test_read_edgelist_3(self):
54
        s = b"""\
55
# comment line
56
1 2 {'weight':2.0}
57
# comment line
58
2 3 {'weight':3.0}
59
"""
60
        bytesIO = io.BytesIO(s)
61
        G = nx.read_edgelist(bytesIO, nodetype=int, data=False)
62
        assert_edges_equal(G.edges(), [(1, 2), (2, 3)])
63

    
64
        bytesIO = io.BytesIO(s)
65
        G = nx.read_edgelist(bytesIO, nodetype=int, data=True)
66
        assert_edges_equal(G.edges(data=True),
67
                           [(1, 2, {'weight': 2.0}), (2, 3, {'weight': 3.0})])
68

    
69
    def test_write_edgelist_1(self):
70
        fh = io.BytesIO()
71
        G = nx.OrderedGraph()
72
        G.add_edges_from([(1, 2), (2, 3)])
73
        nx.write_edgelist(G, fh, data=False)
74
        fh.seek(0)
75
        assert_equal(fh.read(), b"1 2\n2 3\n")
76

    
77
    def test_write_edgelist_2(self):
78
        fh = io.BytesIO()
79
        G = nx.OrderedGraph()
80
        G.add_edges_from([(1, 2), (2, 3)])
81
        nx.write_edgelist(G, fh, data=True)
82
        fh.seek(0)
83
        assert_equal(fh.read(), b"1 2 {}\n2 3 {}\n")
84

    
85
    def test_write_edgelist_3(self):
86
        fh = io.BytesIO()
87
        G = nx.OrderedGraph()
88
        G.add_edge(1, 2, weight=2.0)
89
        G.add_edge(2, 3, weight=3.0)
90
        nx.write_edgelist(G, fh, data=True)
91
        fh.seek(0)
92
        assert_equal(fh.read(), b"1 2 {'weight': 2.0}\n2 3 {'weight': 3.0}\n")
93

    
94
    def test_write_edgelist_4(self):
95
        fh = io.BytesIO()
96
        G = nx.OrderedGraph()
97
        G.add_edge(1, 2, weight=2.0)
98
        G.add_edge(2, 3, weight=3.0)
99
        nx.write_edgelist(G, fh, data=[('weight')])
100
        fh.seek(0)
101
        assert_equal(fh.read(), b"1 2 2.0\n2 3 3.0\n")
102

    
103
    def test_unicode(self):
104
        G = nx.Graph()
105
        try:  # Python 3.x
106
            name1 = chr(2344) + chr(123) + chr(6543)
107
            name2 = chr(5543) + chr(1543) + chr(324)
108
        except ValueError:  # Python 2.6+
109
            name1 = unichr(2344) + unichr(123) + unichr(6543)
110
            name2 = unichr(5543) + unichr(1543) + unichr(324)
111
        G.add_edge(name1, 'Radiohead', **{name2: 3})
112
        fd, fname = tempfile.mkstemp()
113
        nx.write_edgelist(G, fname)
114
        H = nx.read_edgelist(fname)
115
        assert_graphs_equal(G, H)
116
        os.close(fd)
117
        os.unlink(fname)
118

    
119
    def test_latin1_issue(self):
120
        G = nx.Graph()
121
        try:  # Python 3.x
122
            name1 = chr(2344) + chr(123) + chr(6543)
123
            name2 = chr(5543) + chr(1543) + chr(324)
124
        except ValueError:  # Python 2.6+
125
            name1 = unichr(2344) + unichr(123) + unichr(6543)
126
            name2 = unichr(5543) + unichr(1543) + unichr(324)
127
        G.add_edge(name1, 'Radiohead', **{name2: 3})
128
        fd, fname = tempfile.mkstemp()
129
        assert_raises(UnicodeEncodeError,
130
                      nx.write_edgelist,
131
                      G, fname, encoding='latin-1')
132
        os.close(fd)
133
        os.unlink(fname)
134

    
135
    def test_latin1(self):
136
        G = nx.Graph()
137
        try:  # Python 3.x
138
            blurb = chr(1245)  # just to trigger the exception
139
            name1 = 'Bj' + chr(246) + 'rk'
140
            name2 = chr(220) + 'ber'
141
        except ValueError:  # Python 2.6+
142
            name1 = 'Bj' + unichr(246) + 'rk'
143
            name2 = unichr(220) + 'ber'
144
        G.add_edge(name1, 'Radiohead', **{name2: 3})
145
        fd, fname = tempfile.mkstemp()
146
        nx.write_edgelist(G, fname, encoding='latin-1')
147
        H = nx.read_edgelist(fname, encoding='latin-1')
148
        assert_graphs_equal(G, H)
149
        os.close(fd)
150
        os.unlink(fname)
151

    
152
    def test_edgelist_graph(self):
153
        G = self.G
154
        (fd, fname) = tempfile.mkstemp()
155
        nx.write_edgelist(G, fname)
156
        H = nx.read_edgelist(fname)
157
        H2 = nx.read_edgelist(fname)
158
        assert_not_equal(H, H2)  # they should be different graphs
159
        G.remove_node('g')  # isolated nodes are not written in edgelist
160
        assert_nodes_equal(list(H), list(G))
161
        assert_edges_equal(list(H.edges()), list(G.edges()))
162
        os.close(fd)
163
        os.unlink(fname)
164

    
165
    def test_edgelist_digraph(self):
166
        G = self.DG
167
        (fd, fname) = tempfile.mkstemp()
168
        nx.write_edgelist(G, fname)
169
        H = nx.read_edgelist(fname, create_using=nx.DiGraph())
170
        H2 = nx.read_edgelist(fname, create_using=nx.DiGraph())
171
        assert_not_equal(H, H2)  # they should be different graphs
172
        G.remove_node('g')  # isolated nodes are not written in edgelist
173
        assert_nodes_equal(list(H), list(G))
174
        assert_edges_equal(list(H.edges()), list(G.edges()))
175
        os.close(fd)
176
        os.unlink(fname)
177

    
178
    def test_edgelist_integers(self):
179
        G = nx.convert_node_labels_to_integers(self.G)
180
        (fd, fname) = tempfile.mkstemp()
181
        nx.write_edgelist(G, fname)
182
        H = nx.read_edgelist(fname, nodetype=int)
183
        # isolated nodes are not written in edgelist
184
        G.remove_nodes_from(list(nx.isolates(G)))
185
        assert_nodes_equal(list(H), list(G))
186
        assert_edges_equal(list(H.edges()), list(G.edges()))
187
        os.close(fd)
188
        os.unlink(fname)
189

    
190
    def test_edgelist_multigraph(self):
191
        G = self.XG
192
        (fd, fname) = tempfile.mkstemp()
193
        nx.write_edgelist(G, fname)
194
        H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
195
        H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiGraph())
196
        assert_not_equal(H, H2)  # they should be different graphs
197
        assert_nodes_equal(list(H), list(G))
198
        assert_edges_equal(list(H.edges()), list(G.edges()))
199
        os.close(fd)
200
        os.unlink(fname)
201

    
202
    def test_edgelist_multidigraph(self):
203
        G = self.XDG
204
        (fd, fname) = tempfile.mkstemp()
205
        nx.write_edgelist(G, fname)
206
        H = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph())
207
        H2 = nx.read_edgelist(fname, nodetype=int, create_using=nx.MultiDiGraph())
208
        assert_not_equal(H, H2)  # they should be different graphs
209
        assert_nodes_equal(list(H), list(G))
210
        assert_edges_equal(list(H.edges()), list(G.edges()))
211
        os.close(fd)
212
        os.unlink(fname)