Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (1.57 KB)

1

    
2
from nose import SkipTest
3

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

    
7

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

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

    
24
    def setUp(self):
25
        deg = [3, 2, 2, 1, 0]
26
        self.G = havel_hakimi_graph(deg)
27
        self.P = nx.path_graph(3)
28

    
29
    def test_bethe_hessian(self):
30
        "Bethe Hessian matrix"
31
        H = numpy.array([[ 4, -2,  0],
32
                          [-2,  5, -2],
33
                          [ 0, -2,  4]])
34
        permutation = [2, 0, 1]
35
        # Bethe Hessian gives expected form
36
        assert_equal(nx.bethe_hessian_matrix(self.P, r=2).todense(), H)
37
        # nodelist is correctly implemented
38
        assert_equal(nx.bethe_hessian_matrix(self.P, r=2, nodelist=permutation).todense(),
39
                     H[numpy.ix_(permutation, permutation)])
40
        # Equal to Laplacian matrix when r=1
41
        assert_equal(nx.bethe_hessian_matrix(self.G, r=1).todense(),
42
                     nx.laplacian_matrix(self.G).todense())
43
        # Correct default for the regularizer r
44
        assert_equal(nx.bethe_hessian_matrix(self.G).todense(),
45
                     nx.bethe_hessian_matrix(self.G, r=1.25).todense())