Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.15 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 TestModularity(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 scipy
14
        global assert_equal
15
        global assert_almost_equal
16
        try:
17
            import numpy
18
            import scipy
19
            from numpy.testing import assert_equal, assert_almost_equal
20
        except ImportError:
21
            raise SkipTest('SciPy not available.')
22

    
23
    def setUp(self):
24
        deg = [3, 2, 2, 1, 0]
25
        self.G = havel_hakimi_graph(deg)
26
        # Graph used as an example in Sec. 4.1 of Langville and Meyer,
27
        # "Google's PageRank and Beyond". (Used for test_directed_laplacian)
28
        self.DG = nx.DiGraph()
29
        self.DG.add_edges_from(((1, 2), (1, 3), (3, 1), (3, 2), (3, 5), (4, 5), (4, 6),
30
                                (5, 4), (5, 6), (6, 4)))
31

    
32
    def test_modularity(self):
33
        "Modularity matrix"
34
        B = numpy.matrix([[-1.125,  0.25,  0.25,  0.625,  0.],
35
                          [0.25, -0.5,  0.5, -0.25,  0.],
36
                          [0.25,  0.5, -0.5, -0.25,  0.],
37
                          [0.625, -0.25, -0.25, -0.125,  0.],
38
                          [0.,  0.,  0.,  0.,  0.]])
39

    
40
        permutation = [4, 0, 1, 2, 3]
41
        assert_equal(nx.modularity_matrix(self.G), B)
42
        assert_equal(nx.modularity_matrix(self.G, nodelist=permutation),
43
                     B[numpy.ix_(permutation, permutation)])
44

    
45
    def test_modularity_weight(self):
46
        "Modularity matrix with weights"
47
        B = numpy.matrix([[-1.125,  0.25,  0.25,  0.625,  0.],
48
                          [0.25, -0.5,  0.5, -0.25,  0.],
49
                          [0.25,  0.5, -0.5, -0.25,  0.],
50
                          [0.625, -0.25, -0.25, -0.125,  0.],
51
                          [0.,  0.,  0.,  0.,  0.]])
52

    
53
        G_weighted = self.G.copy()
54
        for n1, n2 in G_weighted.edges():
55
            G_weighted.edges[n1, n2]["weight"] = 0.5
56
        # The following test would fail in networkx 1.1
57
        assert_equal(nx.modularity_matrix(G_weighted), B)
58
        # The following test that the modularity matrix get rescaled accordingly
59
        assert_equal(nx.modularity_matrix(G_weighted, weight="weight"), 0.5 * B)
60

    
61
    def test_directed_modularity(self):
62
        "Directed Modularity matrix"
63
        B = numpy.matrix([[-0.2,  0.6,  0.8, -0.4, -0.4, -0.4],
64
                          [0.,  0.,  0.,  0.,  0.,  0.],
65
                          [0.7,  0.4, -0.3, -0.6,  0.4, -0.6],
66
                          [-0.2, -0.4, -0.2, -0.4,  0.6,  0.6],
67
                          [-0.2, -0.4, -0.2,  0.6, -0.4,  0.6],
68
                          [-0.1, -0.2, -0.1,  0.8, -0.2, -0.2]])
69
        node_permutation = [5, 1, 2, 3, 4, 6]
70
        idx_permutation = [4, 0, 1, 2, 3, 5]
71
        mm = nx.directed_modularity_matrix(self.DG,  nodelist=sorted(self.DG))
72
        assert_equal(mm, B)
73
        assert_equal(nx.directed_modularity_matrix(self.DG,
74
                                                   nodelist=node_permutation),
75
                     B[numpy.ix_(idx_permutation, idx_permutation)])