Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (14.3 KB)

1
#!/usr/bin/env python
2
import io
3
import time
4
from nose import SkipTest
5
from nose.tools import *
6

    
7
import networkx as nx
8

    
9

    
10
class TestGEXF(object):
11
    @classmethod
12
    def setupClass(cls):
13
        try:
14
            import xml.etree.ElementTree
15
        except ImportError:
16
            raise SkipTest('xml.etree.ElementTree not available.')
17

    
18
    def setUp(self):
19
        self.simple_directed_data = """<?xml version="1.0" encoding="UTF-8"?>
20
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">
21
    <graph mode="static" defaultedgetype="directed">
22
        <nodes>
23
            <node id="0" label="Hello" />
24
            <node id="1" label="Word" />
25
        </nodes>
26
        <edges>
27
            <edge id="0" source="0" target="1" />
28
        </edges>
29
    </graph>
30
</gexf>
31
"""
32
        self.simple_directed_graph = nx.DiGraph()
33
        self.simple_directed_graph.add_node('0', label='Hello')
34
        self.simple_directed_graph.add_node('1', label='World')
35
        self.simple_directed_graph.add_edge('0', '1', id='0')
36

    
37
        self.simple_directed_fh = \
38
            io.BytesIO(self.simple_directed_data.encode('UTF-8'))
39

    
40
        self.attribute_data = """<?xml version="1.0" encoding="UTF-8"?>
41
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2">
42
  <meta lastmodifieddate="2009-03-20">
43
    <creator>Gephi.org</creator>
44
    <description>A Web network</description>
45
  </meta>
46
  <graph defaultedgetype="directed">
47
    <attributes class="node">
48
      <attribute id="0" title="url" type="string"/>
49
      <attribute id="1" title="indegree" type="integer"/>
50
      <attribute id="2" title="frog" type="boolean">
51
        <default>true</default>
52
      </attribute>
53
    </attributes>
54
    <nodes>
55
      <node id="0" label="Gephi">
56
        <attvalues>
57
          <attvalue for="0" value="https://gephi.org"/>
58
          <attvalue for="1" value="1"/>
59
          <attvalue for="2" value="false"/>
60
        </attvalues>
61
      </node>
62
      <node id="1" label="Webatlas">
63
        <attvalues>
64
          <attvalue for="0" value="http://webatlas.fr"/>
65
          <attvalue for="1" value="2"/>
66
          <attvalue for="2" value="false"/>
67
        </attvalues>
68
      </node>
69
      <node id="2" label="RTGI">
70
        <attvalues>
71
          <attvalue for="0" value="http://rtgi.fr"/>
72
          <attvalue for="1" value="1"/>
73
          <attvalue for="2" value="true"/>
74
        </attvalues>
75
      </node>
76
      <node id="3" label="BarabasiLab">
77
        <attvalues>
78
          <attvalue for="0" value="http://barabasilab.com"/>
79
          <attvalue for="1" value="1"/>
80
          <attvalue for="2" value="true"/>
81
        </attvalues>
82
      </node>
83
    </nodes>
84
    <edges>
85
      <edge id="0" source="0" target="1"/>
86
      <edge id="1" source="0" target="2"/>
87
      <edge id="2" source="1" target="0"/>
88
      <edge id="3" source="2" target="1"/>
89
      <edge id="4" source="0" target="3"/>
90
    </edges>
91
  </graph>
92
</gexf>
93
"""
94
        self.attribute_graph = nx.DiGraph()
95
        self.attribute_graph.graph['node_default'] = {'frog': True}
96
        self.attribute_graph.add_node('0',
97
                                      label='Gephi',
98
                                      url='https://gephi.org',
99
                                      indegree=1, frog=False)
100
        self.attribute_graph.add_node('1',
101
                                      label='Webatlas',
102
                                      url='http://webatlas.fr',
103
                                      indegree=2, frog=False)
104
        self.attribute_graph.add_node('2',
105
                                      label='RTGI',
106
                                      url='http://rtgi.fr',
107
                                      indegree=1, frog=True)
108
        self.attribute_graph.add_node('3',
109
                                      label='BarabasiLab',
110
                                      url='http://barabasilab.com',
111
                                      indegree=1, frog=True)
112
        self.attribute_graph.add_edge('0', '1', id='0')
113
        self.attribute_graph.add_edge('0', '2', id='1')
114
        self.attribute_graph.add_edge('1', '0', id='2')
115
        self.attribute_graph.add_edge('2', '1', id='3')
116
        self.attribute_graph.add_edge('0', '3', id='4')
117
        self.attribute_fh = io.BytesIO(self.attribute_data.encode('UTF-8'))
118

    
119
        self.simple_undirected_data = """<?xml version="1.0" encoding="UTF-8"?>
120
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">
121
    <graph mode="static" defaultedgetype="undirected">
122
        <nodes>
123
            <node id="0" label="Hello" />
124
            <node id="1" label="Word" />
125
        </nodes>
126
        <edges>
127
            <edge id="0" source="0" target="1" />
128
        </edges>
129
    </graph>
130
</gexf>
131
"""
132
        self.simple_undirected_graph = nx.Graph()
133
        self.simple_undirected_graph.add_node('0', label='Hello')
134
        self.simple_undirected_graph.add_node('1', label='World')
135
        self.simple_undirected_graph.add_edge('0', '1', id='0')
136

    
137
        self.simple_undirected_fh = io.BytesIO(self.simple_undirected_data.encode('UTF-8'))
138

    
139
    def test_read_simple_directed_graphml(self):
140
        G = self.simple_directed_graph
141
        H = nx.read_gexf(self.simple_directed_fh)
142
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
143
        assert_equal(sorted(G.edges()), sorted(H.edges()))
144
        assert_equal(sorted(G.edges(data=True)),
145
                     sorted(H.edges(data=True)))
146
        self.simple_directed_fh.seek(0)
147

    
148
    def test_write_read_simple_directed_graphml(self):
149
        G = self.simple_directed_graph
150
        fh = io.BytesIO()
151
        nx.write_gexf(G, fh)
152
        fh.seek(0)
153
        H = nx.read_gexf(fh)
154
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
155
        assert_equal(sorted(G.edges()), sorted(H.edges()))
156
        assert_equal(sorted(G.edges(data=True)),
157
                     sorted(H.edges(data=True)))
158
        self.simple_directed_fh.seek(0)
159

    
160
    def test_read_simple_undirected_graphml(self):
161
        G = self.simple_undirected_graph
162
        H = nx.read_gexf(self.simple_undirected_fh)
163
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
164
        assert_equal(
165
            sorted(sorted(e) for e in G.edges()),
166
            sorted(sorted(e) for e in H.edges()))
167
        self.simple_undirected_fh.seek(0)
168

    
169
    def test_read_attribute_graphml(self):
170
        G = self.attribute_graph
171
        H = nx.read_gexf(self.attribute_fh)
172
        assert_equal(sorted(G.nodes(True)), sorted(H.nodes(data=True)))
173
        ge = sorted(G.edges(data=True))
174
        he = sorted(H.edges(data=True))
175
        for a, b in zip(ge, he):
176
            assert_equal(a, b)
177
        self.attribute_fh.seek(0)
178

    
179
    def test_directed_edge_in_undirected(self):
180
        s = """<?xml version="1.0" encoding="UTF-8"?>
181
<gexf xmlns="http://www.gexf.net/1.2draft" version='1.2'>
182
    <graph mode="static" defaultedgetype="undirected" name="">
183
        <nodes>
184
            <node id="0" label="Hello" />
185
            <node id="1" label="Word" />
186
        </nodes>
187
        <edges>
188
            <edge id="0" source="0" target="1" type="directed"/>
189
        </edges>
190
    </graph>
191
</gexf>
192
"""
193
        fh = io.BytesIO(s.encode('UTF-8'))
194
        assert_raises(nx.NetworkXError, nx.read_gexf, fh)
195

    
196
    def test_undirected_edge_in_directed(self):
197
        s = """<?xml version="1.0" encoding="UTF-8"?>
198
<gexf xmlns="http://www.gexf.net/1.2draft" version='1.2'>
199
    <graph mode="static" defaultedgetype="directed" name="">
200
        <nodes>
201
            <node id="0" label="Hello" />
202
            <node id="1" label="Word" />
203
        </nodes>
204
        <edges>
205
            <edge id="0" source="0" target="1" type="undirected"/>
206
        </edges>
207
    </graph>
208
</gexf>
209
"""
210
        fh = io.BytesIO(s.encode('UTF-8'))
211
        assert_raises(nx.NetworkXError, nx.read_gexf, fh)
212

    
213
    def test_key_raises(self):
214
        s = """<?xml version="1.0" encoding="UTF-8"?>
215
<gexf xmlns="http://www.gexf.net/1.2draft" version='1.2'>
216
    <graph mode="static" defaultedgetype="directed" name="">
217
        <nodes>
218
            <node id="0" label="Hello">
219
              <attvalues>
220
                <attvalue for='0' value='1'/>
221
              </attvalues>
222
            </node>
223
            <node id="1" label="Word" />
224
        </nodes>
225
        <edges>
226
            <edge id="0" source="0" target="1" type="undirected"/>
227
        </edges>
228
    </graph>
229
</gexf>
230
"""
231
        fh = io.BytesIO(s.encode('UTF-8'))
232
        assert_raises(nx.NetworkXError, nx.read_gexf, fh)
233

    
234
    def test_relabel(self):
235
        s = """<?xml version="1.0" encoding="UTF-8"?>
236
<gexf xmlns="http://www.gexf.net/1.2draft" version='1.2'>
237
    <graph mode="static" defaultedgetype="directed" name="">
238
        <nodes>
239
            <node id="0" label="Hello" />
240
            <node id="1" label="Word" />
241
        </nodes>
242
        <edges>
243
            <edge id="0" source="0" target="1"/>
244
        </edges>
245
    </graph>
246
</gexf>
247
"""
248
        fh = io.BytesIO(s.encode('UTF-8'))
249
        G = nx.read_gexf(fh, relabel=True)
250
        assert_equal(sorted(G.nodes()), ["Hello", "Word"])
251

    
252
    def test_default_attribute(self):
253
        G = nx.Graph()
254
        G.add_node(1, label='1', color='green')
255
        nx.add_path(G, [0, 1, 2, 3])
256
        G.add_edge(1, 2, foo=3)
257
        G.graph['node_default'] = {'color': 'yellow'}
258
        G.graph['edge_default'] = {'foo': 7}
259
        fh = io.BytesIO()
260
        nx.write_gexf(G, fh)
261
        fh.seek(0)
262
        H = nx.read_gexf(fh, node_type=int)
263
        assert_equal(sorted(G.nodes()), sorted(H.nodes()))
264
        assert_equal(
265
            sorted(sorted(e) for e in G.edges()),
266
            sorted(sorted(e) for e in H.edges()))
267
        # Reading a gexf graph always sets mode attribute to either
268
        # 'static' or 'dynamic'. Remove the mode attribute from the
269
        # read graph for the sake of comparing remaining attributes.
270
        del H.graph['mode']
271
        assert_equal(G.graph, H.graph)
272

    
273
    def test_serialize_ints_to_strings(self):
274
        G = nx.Graph()
275
        G.add_node(1, id=7, label=77)
276
        fh = io.BytesIO()
277
        nx.write_gexf(G, fh)
278
        fh.seek(0)
279
        H = nx.read_gexf(fh, node_type=int)
280
        assert_equal(list(H), [7])
281
        assert_equal(H.nodes[7]['label'], '77')
282

    
283
    def test_write_with_node_attributes(self):
284
        # Addresses #673.
285
        G = nx.OrderedGraph()
286
        G.add_edges_from([(0, 1), (1, 2), (2, 3)])
287
        for i in range(4):
288
            G.nodes[i]['id'] = i
289
            G.nodes[i]['label'] = i
290
            G.nodes[i]['pid'] = i
291
            G.nodes[i]['start'] = i
292
            G.nodes[i]['end'] = i + 1
293

    
294
        expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
295
  <graph defaultedgetype="undirected" mode="dynamic" name="" timeformat="long">
296
    <meta>
297
      <creator>NetworkX {}</creator>
298
      <lastmodified>{}</lastmodified>
299
    </meta>
300
    <nodes>
301
      <node end="1" id="0" label="0" pid="0" start="0" />
302
      <node end="2" id="1" label="1" pid="1" start="1" />
303
      <node end="3" id="2" label="2" pid="2" start="2" />
304
      <node end="4" id="3" label="3" pid="3" start="3" />
305
    </nodes>
306
    <edges>
307
      <edge id="0" source="0" target="1" />
308
      <edge id="1" source="1" target="2" />
309
      <edge id="2" source="2" target="3" />
310
    </edges>
311
  </graph>
312
</gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y'))
313
        obtained = '\n'.join(nx.generate_gexf(G))
314
        assert_equal(expected, obtained)
315

    
316
    def test_edge_id_construct(self):
317
        G = nx.Graph()
318
        G.add_edges_from([(0, 1, {'id': 0}), (1, 2, {'id': 2}), (2, 3)])
319
        expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
320
  <graph defaultedgetype="undirected" mode="static" name="">
321
    <meta>
322
      <creator>NetworkX {}</creator>
323
      <lastmodified>{}</lastmodified>
324
    </meta>
325
    <nodes>
326
      <node id="0" label="0" />
327
      <node id="1" label="1" />
328
      <node id="2" label="2" />
329
      <node id="3" label="3" />
330
    </nodes>
331
    <edges>
332
      <edge id="0" source="0" target="1" />
333
      <edge id="2" source="1" target="2" />
334
      <edge id="1" source="2" target="3" />
335
    </edges>
336
  </graph>
337
</gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y'))
338
        obtained = '\n'.join(nx.generate_gexf(G))
339
        assert_equal(expected, obtained)
340

    
341
    def test_numpy_type(self):
342
        G = nx.path_graph(4)
343
        try:
344
            import numpy
345
        except ImportError:
346
            return
347
        nx.set_node_attributes(G, {n:n for n in numpy.arange(4)}, 'number')
348
        G[0][1]['edge-number'] = numpy.float64(1.1)
349

    
350
        expected = """<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
351
  <graph defaultedgetype="undirected" mode="static" name="">
352
    <attributes class="edge" mode="static">
353
      <attribute id="1" title="edge-number" type="float" />
354
    </attributes>
355
    <attributes class="node" mode="static">
356
      <attribute id="0" title="number" type="int" />
357
    </attributes>
358
    <meta>
359
      <creator>NetworkX {}</creator>
360
      <lastmodified>{}</lastmodified>
361
    </meta>
362
    <nodes>
363
      <node id="0" label="0">
364
        <attvalues>
365
          <attvalue for="0" value="0" />
366
        </attvalues>
367
      </node>
368
      <node id="1" label="1">
369
        <attvalues>
370
          <attvalue for="0" value="1" />
371
        </attvalues>
372
      </node>
373
      <node id="2" label="2">
374
        <attvalues>
375
          <attvalue for="0" value="2" />
376
        </attvalues>
377
      </node>
378
      <node id="3" label="3">
379
        <attvalues>
380
          <attvalue for="0" value="3" />
381
        </attvalues>
382
      </node>
383
    </nodes>
384
    <edges>
385
      <edge id="0" source="0" target="1">
386
        <attvalues>
387
          <attvalue for="1" value="1.1" />
388
        </attvalues>
389
      </edge>
390
      <edge id="1" source="1" target="2" />
391
      <edge id="2" source="2" target="3" />
392
    </edges>
393
  </graph>
394
</gexf>""".format(nx.__version__, time.strftime('%d/%m/%Y'))
395
        obtained = '\n'.join(nx.generate_gexf(G))
396
        assert_equal(expected, obtained)
397

    
398
    def test_bool(self):
399
        G = nx.Graph()
400
        G.add_node(1, testattr=True)
401
        fh = io.BytesIO()
402
        nx.write_gexf(G, fh)
403
        fh.seek(0)
404
        H = nx.read_gexf(fh, node_type=int)
405
        assert_equal(H.nodes[1]['testattr'], True)