Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.04 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 TestSpectrum(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.P = nx.path_graph(3)
26
        self.WG = nx.Graph((u, v, {'weight': 0.5, 'other': 0.3})
27
                           for (u, v) in self.G.edges())
28
        self.WG.add_node(4)
29
        self.DG = nx.DiGraph()
30
        nx.add_path(self.DG, [0, 1, 2])
31

    
32
    def test_laplacian_spectrum(self):
33
        "Laplacian eigenvalues"
34
        evals = numpy.array([0, 0, 1, 3, 4])
35
        e = sorted(nx.laplacian_spectrum(self.G))
36
        assert_almost_equal(e, evals)
37
        e = sorted(nx.laplacian_spectrum(self.WG, weight=None))
38
        assert_almost_equal(e, evals)
39
        e = sorted(nx.laplacian_spectrum(self.WG))
40
        assert_almost_equal(e, 0.5 * evals)
41
        e = sorted(nx.laplacian_spectrum(self.WG, weight='other'))
42
        assert_almost_equal(e, 0.3 * evals)
43

    
44
    def test_normalized_laplacian_spectrum(self):
45
        "Normalized Laplacian eigenvalues"
46
        evals = numpy.array([0, 0, 0.7712864461218, 1.5, 1.7287135538781])
47
        e = sorted(nx.normalized_laplacian_spectrum(self.G))
48
        assert_almost_equal(e, evals)
49
        e = sorted(nx.normalized_laplacian_spectrum(self.WG, weight=None))
50
        assert_almost_equal(e, evals)
51
        e = sorted(nx.normalized_laplacian_spectrum(self.WG))
52
        assert_almost_equal(e, evals)
53
        e = sorted(nx.normalized_laplacian_spectrum(self.WG, weight='other'))
54
        assert_almost_equal(e, evals)
55

    
56

    
57
    def test_adjacency_spectrum(self):
58
        "Adjacency eigenvalues"
59
        evals = numpy.array([-numpy.sqrt(2), 0, numpy.sqrt(2)])
60
        e = sorted(nx.adjacency_spectrum(self.P))
61
        assert_almost_equal(e, evals)
62

    
63
    def test_modularity_spectrum(self):
64
        "Modularity eigenvalues"
65
        evals = numpy.array([-1.5, 0., 0.])
66
        e = sorted(nx.modularity_spectrum(self.P))
67
        assert_almost_equal(e, evals)
68
        # Directed modularity eigenvalues
69
        evals = numpy.array([-0.5, 0., 0.])
70
        e = sorted(nx.modularity_spectrum(self.DG))
71
        assert_almost_equal(e, evals)
72

    
73
    def test_bethe_hessian_spectrum(self):
74
        "Bethe Hessian eigenvalues"
75
        evals = numpy.array([0.5 * (9 - numpy.sqrt(33)), 4,
76
                             0.5 * (9 + numpy.sqrt(33))])
77
        e = sorted(nx.bethe_hessian_spectrum(self.P, r=2))
78
        assert_almost_equal(e, evals)
79
        # Collapses back to Laplacian:
80
        e1 = sorted(nx.bethe_hessian_spectrum(self.P, r=1))
81
        e2 = sorted(nx.laplacian_spectrum(self.P))
82
        assert_almost_equal(e1, e2)