Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (8.01 KB)

1
from nose import SkipTest
2

    
3
import networkx as nx
4
from networkx.generators.degree_seq import havel_hakimi_graph
5

    
6

    
7
class TestGraphMatrix(object):
8
    numpy = 1  # nosetests attribute, use nosetests -a 'not numpy' to skip test
9

    
10
    @classmethod
11
    def setupClass(cls):
12
        global numpy
13
        global assert_equal
14
        global assert_almost_equal
15
        try:
16
            import numpy
17
            import scipy
18
            from numpy.testing import assert_equal, assert_almost_equal
19
        except ImportError:
20
            raise SkipTest('SciPy not available.')
21

    
22
    def setUp(self):
23
        deg = [3, 2, 2, 1, 0]
24
        self.G = havel_hakimi_graph(deg)
25
        self.OI = numpy.array([[-1, -1, -1, 0],
26
                               [1, 0, 0, -1],
27
                               [0, 1, 0, 1],
28
                               [0, 0, 1, 0],
29
                               [0, 0, 0, 0]])
30
        self.A = numpy.array([[0, 1, 1, 1, 0],
31
                              [1, 0, 1, 0, 0],
32
                              [1, 1, 0, 0, 0],
33
                              [1, 0, 0, 0, 0],
34
                              [0, 0, 0, 0, 0]])
35
        self.WG = havel_hakimi_graph(deg)
36
        self.WG.add_edges_from((u, v, {'weight': 0.5, 'other': 0.3})
37
                               for (u, v) in self.G.edges())
38
        self.WA = numpy.array([[0, 0.5, 0.5, 0.5, 0],
39
                               [0.5, 0, 0.5, 0, 0],
40
                               [0.5, 0.5, 0, 0, 0],
41
                               [0.5, 0, 0, 0, 0],
42
                               [0, 0, 0, 0, 0]])
43
        self.MG = nx.MultiGraph(self.G)
44
        self.MG2 = self.MG.copy()
45
        self.MG2.add_edge(0, 1)
46
        self.MG2A = numpy.array([[0, 2, 1, 1, 0],
47
                                 [2, 0, 1, 0, 0],
48
                                 [1, 1, 0, 0, 0],
49
                                 [1, 0, 0, 0, 0],
50
                                 [0, 0, 0, 0, 0]])
51
        self.MGOI = numpy.array([[-1, -1, -1, -1, 0],
52
                                 [1, 1, 0, 0, -1],
53
                                 [0, 0, 1, 0, 1],
54
                                 [0, 0, 0, 1, 0],
55
                                 [0, 0, 0, 0, 0]])
56
        self.no_edges_G = nx.Graph([(1, 2), (3, 2, {'weight': 8})])
57
        self.no_edges_A = numpy.array([[0, 0], [0, 0]])
58

    
59
    def test_incidence_matrix(self):
60
        "Conversion to incidence matrix"
61
        I = nx.incidence_matrix(self.G,
62
                                nodelist=sorted(self.G),
63
                                edgelist=sorted(self.G.edges()),
64
                                oriented=True).todense().astype(int)
65
        assert_equal(I, self.OI)
66
        I = nx.incidence_matrix(self.G,
67
                                nodelist=sorted(self.G),
68
                                edgelist=sorted(self.G.edges()),
69
                                oriented=False).todense().astype(int)
70
        assert_equal(I, numpy.abs(self.OI))
71

    
72
        I = nx.incidence_matrix(self.MG,
73
                                nodelist=sorted(self.MG),
74
                                edgelist=sorted(self.MG.edges()),
75
                                oriented=True).todense().astype(int)
76
        assert_equal(I, self.OI)
77
        I = nx.incidence_matrix(self.MG,
78
                                nodelist=sorted(self.MG),
79
                                edgelist=sorted(self.MG.edges()),
80
                                oriented=False).todense().astype(int)
81
        assert_equal(I, numpy.abs(self.OI))
82

    
83
        I = nx.incidence_matrix(self.MG2,
84
                                nodelist=sorted(self.MG2),
85
                                edgelist=sorted(self.MG2.edges()),
86
                                oriented=True).todense().astype(int)
87
        assert_equal(I, self.MGOI)
88
        I = nx.incidence_matrix(self.MG2,
89
                                nodelist=sorted(self.MG),
90
                                edgelist=sorted(self.MG2.edges()),
91
                                oriented=False).todense().astype(int)
92
        assert_equal(I, numpy.abs(self.MGOI))
93

    
94
    def test_weighted_incidence_matrix(self):
95
        I = nx.incidence_matrix(self.WG,
96
                                nodelist=sorted(self.WG),
97
                                edgelist=sorted(self.WG.edges()),
98
                                oriented=True).todense().astype(int)
99
        assert_equal(I, self.OI)
100
        I = nx.incidence_matrix(self.WG,
101
                                nodelist=sorted(self.WG),
102
                                edgelist=sorted(self.WG.edges()),
103
                                oriented=False).todense().astype(int)
104
        assert_equal(I, numpy.abs(self.OI))
105

    
106
        # assert_equal(nx.incidence_matrix(self.WG,oriented=True,
107
        #                                  weight='weight').todense(),0.5*self.OI)
108
        # assert_equal(nx.incidence_matrix(self.WG,weight='weight').todense(),
109
        #              numpy.abs(0.5*self.OI))
110
        # assert_equal(nx.incidence_matrix(self.WG,oriented=True,weight='other').todense(),
111
        #              0.3*self.OI)
112

    
113
        I = nx.incidence_matrix(self.WG,
114
                                nodelist=sorted(self.WG),
115
                                edgelist=sorted(self.WG.edges()),
116
                                oriented=True,
117
                                weight='weight').todense()
118
        assert_equal(I, 0.5 * self.OI)
119
        I = nx.incidence_matrix(self.WG,
120
                                nodelist=sorted(self.WG),
121
                                edgelist=sorted(self.WG.edges()),
122
                                oriented=False,
123
                                weight='weight').todense()
124
        assert_equal(I, numpy.abs(0.5 * self.OI))
125
        I = nx.incidence_matrix(self.WG,
126
                                nodelist=sorted(self.WG),
127
                                edgelist=sorted(self.WG.edges()),
128
                                oriented=True,
129
                                weight='other').todense()
130
        assert_equal(I, 0.3 * self.OI)
131

    
132
        # WMG=nx.MultiGraph(self.WG)
133
        # WMG.add_edge(0,1,weight=0.5,other=0.3)
134
        # assert_equal(nx.incidence_matrix(WMG,weight='weight').todense(),
135
        #              numpy.abs(0.5*self.MGOI))
136
        # assert_equal(nx.incidence_matrix(WMG,weight='weight',oriented=True).todense(),
137
        #              0.5*self.MGOI)
138
        # assert_equal(nx.incidence_matrix(WMG,weight='other',oriented=True).todense(),
139
        #              0.3*self.MGOI)
140

    
141
        WMG = nx.MultiGraph(self.WG)
142
        WMG.add_edge(0, 1, weight=0.5, other=0.3)
143
        I = nx.incidence_matrix(WMG,
144
                                nodelist=sorted(WMG),
145
                                edgelist=sorted(WMG.edges(keys=True)),
146
                                oriented=True,
147
                                weight='weight').todense()
148
        assert_equal(I, 0.5 * self.MGOI)
149
        I = nx.incidence_matrix(WMG,
150
                                nodelist=sorted(WMG),
151
                                edgelist=sorted(WMG.edges(keys=True)),
152
                                oriented=False,
153
                                weight='weight').todense()
154
        assert_equal(I, numpy.abs(0.5 * self.MGOI))
155
        I = nx.incidence_matrix(WMG,
156
                                nodelist=sorted(WMG),
157
                                edgelist=sorted(WMG.edges(keys=True)),
158
                                oriented=True,
159
                                weight='other').todense()
160
        assert_equal(I, 0.3 * self.MGOI)
161

    
162
    def test_adjacency_matrix(self):
163
        "Conversion to adjacency matrix"
164
        assert_equal(nx.adj_matrix(self.G).todense(), self.A)
165
        assert_equal(nx.adj_matrix(self.MG).todense(), self.A)
166
        assert_equal(nx.adj_matrix(self.MG2).todense(), self.MG2A)
167
        assert_equal(nx.adj_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2])
168
        assert_equal(nx.adj_matrix(self.WG).todense(), self.WA)
169
        assert_equal(nx.adj_matrix(self.WG, weight=None).todense(), self.A)
170
        assert_equal(nx.adj_matrix(self.MG2, weight=None).todense(), self.MG2A)
171
        assert_equal(nx.adj_matrix(self.WG, weight='other').todense(), 0.6 * self.WA)
172
        assert_equal(nx.adj_matrix(self.no_edges_G, nodelist=[1, 3]).todense(), self.no_edges_A)