Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.63 KB)

1
#!/usr/bin/env python
2
"""
3
Pajek tests
4
"""
5
from nose.tools import assert_equal
6
from networkx import *
7
import os
8
import tempfile
9
from io import open
10
from networkx.testing import *
11

    
12

    
13
class TestPajek(object):
14
    def setUp(self):
15
        self.data = """*network Tralala\n*vertices 4\n   1 "A1"         0.0938 0.0896   ellipse x_fact 1 y_fact 1\n   2 "Bb"         0.8188 0.2458   ellipse x_fact 1 y_fact 1\n   3 "C"          0.3688 0.7792   ellipse x_fact 1\n   4 "D2"         0.9583 0.8563   ellipse x_fact 1\n*arcs\n1 1 1  h2 0 w 3 c Blue s 3 a1 -130 k1 0.6 a2 -130 k2 0.6 ap 0.5 l "Bezier loop" lc BlueViolet fos 20 lr 58 lp 0.3 la 360\n2 1 1  h2 0 a1 120 k1 1.3 a2 -120 k2 0.3 ap 25 l "Bezier arc" lphi 270 la 180 lr 19 lp 0.5\n1 2 1  h2 0 a1 40 k1 2.8 a2 30 k2 0.8 ap 25 l "Bezier arc" lphi 90 la 0 lp 0.65\n4 2 -1  h2 0 w 1 k1 -2 k2 250 ap 25 l "Circular arc" c Red lc OrangeRed\n3 4 1  p Dashed h2 0 w 2 c OliveGreen ap 25 l "Straight arc" lc PineGreen\n1 3 1  p Dashed h2 0 w 5 k1 -1 k2 -20 ap 25 l "Oval arc" c Brown lc Black\n3 3 -1  h1 6 w 1 h2 12 k1 -2 k2 -15 ap 0.5 l "Circular loop" c Red lc OrangeRed lphi 270 la 180"""
16
        self.G = nx.MultiDiGraph()
17
        self.G.add_nodes_from(['A1', 'Bb', 'C', 'D2'])
18
        self.G.add_edges_from([('A1', 'A1'), ('A1', 'Bb'), ('A1', 'C'),
19
                               ('Bb', 'A1'), ('C', 'C'), ('C', 'D2'),
20
                               ('D2', 'Bb')])
21

    
22
        self.G.graph['name'] = 'Tralala'
23
        (fd, self.fname) = tempfile.mkstemp()
24
        with os.fdopen(fd, 'wb') as fh:
25
            fh.write(self.data.encode('UTF-8'))
26

    
27
    def tearDown(self):
28
        os.unlink(self.fname)
29

    
30
    def test_parse_pajek_simple(self):
31
        # Example without node positions or shape
32
        data = """*Vertices 2\n1 "1"\n2 "2"\n*Edges\n1 2\n2 1"""
33
        G = parse_pajek(data)
34
        assert_equal(sorted(G.nodes()), ['1', '2'])
35
        assert_edges_equal(G.edges(), [('1', '2'), ('1', '2')])
36

    
37
    def test_parse_pajek(self):
38
        G = parse_pajek(self.data)
39
        assert_equal(sorted(G.nodes()), ['A1', 'Bb', 'C', 'D2'])
40
        assert_edges_equal(G.edges(), [('A1', 'A1'), ('A1', 'Bb'),
41
                                       ('A1', 'C'), ('Bb', 'A1'),
42
                                       ('C', 'C'), ('C', 'D2'), ('D2', 'Bb')])
43

    
44
    def test_parse_pajet_mat(self):
45
        data = """*Vertices 3\n1 "one"\n2 "two"\n3 "three"\n*Matrix\n1 1 0\n0 1 0\n0 1 0\n"""
46
        G = parse_pajek(data)
47
        assert_equal(set(G.nodes()), {'one', 'two', 'three'})
48
        assert_equal(G.nodes['two'], {'id': '2'})
49
        assert_edges_equal(set(G.edges()), {('one', 'one'), ('two', 'one'), ('two', 'two'), ('two', 'three')})
50

    
51
    def test_read_pajek(self):
52
        G = parse_pajek(self.data)
53
        Gin = read_pajek(self.fname)
54
        assert_equal(sorted(G.nodes()), sorted(Gin.nodes()))
55
        assert_edges_equal(G.edges(), Gin.edges())
56
        assert_equal(self.G.graph, Gin.graph)
57
        for n in G:
58
            assert_equal(G.nodes[n], Gin.nodes[n])
59

    
60
    def test_write_pajek(self):
61
        import io
62
        G = parse_pajek(self.data)
63
        fh = io.BytesIO()
64
        nx.write_pajek(G,fh)
65
        fh.seek(0)
66
        H = nx.read_pajek(fh)
67
        assert_nodes_equal(list(G), list(H))
68
        assert_edges_equal(list(G.edges()), list(H.edges()))
69
        # Graph name is left out for now, therefore it is not tested.
70
        # assert_equal(G.graph, H.graph)
71

    
72
    def test_ignored_attribute(self):
73
        import io
74
        G = nx.Graph()
75
        fh = io.BytesIO()
76
        G.add_node(1, int_attr=1)
77
        G.add_node(2, empty_attr='  ')
78
        G.add_edge(1, 2, int_attr=2)
79
        G.add_edge(2, 3, empty_attr='  ')
80

    
81
        import warnings
82
        with warnings.catch_warnings(record=True) as w:
83
            nx.write_pajek(G,fh)
84
            assert_equal(len(w), 4)
85

    
86
    def test_noname(self):
87
        # Make sure we can parse a line such as:  *network
88
        # Issue #952
89
        line = "*network\n"
90
        other_lines = self.data.split('\n')[1:]
91
        data = line + '\n'.join(other_lines)
92
        G = parse_pajek(data)
93

    
94
    def test_unicode(self):
95
        import io
96
        G = nx.Graph()
97
        try:  # Python 3.x
98
            name1 = chr(2344) + chr(123) + chr(6543)
99
            name2 = chr(5543) + chr(1543) + chr(324)
100
        except ValueError:  # Python 2.6+
101
            name1 = unichr(2344) + unichr(123) + unichr(6543)
102
            name2 = unichr(5543) + unichr(1543) + unichr(324)
103
        G.add_edge(name1, 'Radiohead', foo=name2)
104
        fh = io.BytesIO()
105
        nx.write_pajek(G, fh)
106
        fh.seek(0)
107
        H = nx.read_pajek(fh)
108
        assert_nodes_equal(list(G), list(H))
109
        assert_edges_equal(list(G.edges()), list(H.edges()))
110
        assert_equal(G.graph, H.graph)