Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / algorithms / assortativity / tests / test_correlation.py @ 5cef0f13

History | View | Annotate | Download (3.32 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
from nose import SkipTest
4
import networkx as nx
5
from base_test import BaseTestAttributeMixing, BaseTestDegreeMixing
6
from networkx.algorithms.assortativity.correlation import attribute_ac
7

    
8

    
9
class TestDegreeMixingCorrelation(BaseTestDegreeMixing):
10
    @classmethod
11
    def setupClass(cls):
12
        global np
13
        global npt
14
        try:
15
            import numpy as np
16
            import numpy.testing as npt
17
        except ImportError:
18
            raise SkipTest('NumPy not available.')
19
        try:
20
            import scipy
21
            import scipy.stats
22
        except ImportError:
23
            raise SkipTest('SciPy not available.')
24

    
25
    def test_degree_assortativity_undirected(self):
26
        r = nx.degree_assortativity_coefficient(self.P4)
27
        npt.assert_almost_equal(r, -1.0 / 2, decimal=4)
28

    
29
    def test_degree_assortativity_directed(self):
30
        r = nx.degree_assortativity_coefficient(self.D)
31
        npt.assert_almost_equal(r, -0.57735, decimal=4)
32

    
33
    def test_degree_assortativity_multigraph(self):
34
        r = nx.degree_assortativity_coefficient(self.M)
35
        npt.assert_almost_equal(r, -1.0 / 7.0, decimal=4)
36

    
37
    def test_degree_pearson_assortativity_undirected(self):
38
        r = nx.degree_pearson_correlation_coefficient(self.P4)
39
        npt.assert_almost_equal(r, -1.0 / 2, decimal=4)
40

    
41
    def test_degree_pearson_assortativity_directed(self):
42
        r = nx.degree_pearson_correlation_coefficient(self.D)
43
        npt.assert_almost_equal(r, -0.57735, decimal=4)
44

    
45
    def test_degree_pearson_assortativity_multigraph(self):
46
        r = nx.degree_pearson_correlation_coefficient(self.M)
47
        npt.assert_almost_equal(r, -1.0 / 7.0, decimal=4)
48

    
49

    
50
class TestAttributeMixingCorrelation(BaseTestAttributeMixing):
51
    @classmethod
52
    def setupClass(cls):
53
        global np
54
        global npt
55
        try:
56
            import numpy as np
57
            import numpy.testing as npt
58

    
59
        except ImportError:
60
            raise SkipTest('NumPy not available.')
61

    
62
    def test_attribute_assortativity_undirected(self):
63
        r = nx.attribute_assortativity_coefficient(self.G, 'fish')
64
        assert_equal(r, 6.0 / 22.0)
65

    
66
    def test_attribute_assortativity_directed(self):
67
        r = nx.attribute_assortativity_coefficient(self.D, 'fish')
68
        assert_equal(r, 1.0 / 3.0)
69

    
70
    def test_attribute_assortativity_multigraph(self):
71
        r = nx.attribute_assortativity_coefficient(self.M, 'fish')
72
        assert_equal(r, 1.0)
73

    
74
    def test_attribute_assortativity_coefficient(self):
75
        # from "Mixing patterns in networks"
76
        a = np.array([[0.258, 0.016, 0.035, 0.013],
77
                      [0.012, 0.157, 0.058, 0.019],
78
                      [0.013, 0.023, 0.306, 0.035],
79
                      [0.005, 0.007, 0.024, 0.016]])
80
        r = attribute_ac(a)
81
        npt.assert_almost_equal(r, 0.623, decimal=3)
82

    
83
    def test_attribute_assortativity_coefficient2(self):
84
        a = np.array([[0.18, 0.02, 0.01, 0.03],
85
                      [0.02, 0.20, 0.03, 0.02],
86
                      [0.01, 0.03, 0.16, 0.01],
87
                      [0.03, 0.02, 0.01, 0.22]])
88

    
89
        r = attribute_ac(a)
90
        npt.assert_almost_equal(r, 0.68, decimal=2)
91

    
92
    def test_attribute_assortativity(self):
93
        a = np.array([[50, 50, 0], [50, 50, 0], [0, 0, 2]])
94
        r = attribute_ac(a)
95
        npt.assert_almost_equal(r, 0.029, decimal=3)