test_mixing_degree.py :  » Network » NetworkX » networkx-1.1 » networkx » algorithms » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » NetworkX 
NetworkX » networkx 1.1 » networkx » algorithms » tests » test_mixing_degree.py
#!/usr/bin/env python
from nose.tools import *
import nose.tools
import networkx
import networkx.algorithms.mixing as mixing


class TestDegreeMixing(object):
    
    def setUp(self):
        self.P4=networkx.path_graph(4)
        self.D=networkx.DiGraph() 
        self.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
        self.M=networkx.MultiGraph() 
        self.M.add_path(range(4))
        self.M.add_edge(0,1)
        self.S=networkx.Graph()
        self.S.add_edges_from([(0,0),(1,1)])


    def test_node_degree_xy_undirected(self):
        xy=sorted(mixing.node_degree_xy(self.P4))
        xy_result=sorted([(1,2),
                          (2,1),
                          (2,2),
                          (2,2),
                          (1,2),
                          (2,1)])
        assert_equal(xy,xy_result)

    def test_node_degree_xy_directed(self):
        xy=sorted(mixing.node_degree_xy(self.D))
        xy_result=sorted([(2,1),
                          (2,3),
                          (1,3),
                          (1,3)])
        assert_equal(xy,xy_result)

    def test_node_degree_xy_multigraph(self):
        xy=sorted(mixing.node_degree_xy(self.M))
        xy_result=sorted([(2,3),
                          (2,3),
                          (3,2),
                          (3,2),
                          (2,3),
                          (3,2),
                          (1,2),
                          (2,1)])
        assert_equal(xy,xy_result)


    def test_node_degree_xy_selfloop(self):
        xy=sorted(mixing.node_degree_xy(self.S))
        xy_result=sorted([(2,2),
                          (2,2)])
        assert_equal(xy,xy_result)


    def test_degree_mixing_dict_undirected(self):
        d=mixing.degree_mixing_dict(self.P4)
        d_result={1:{2:2},
                  2:{1:2,2:2},
                  }
        assert_equal(d,d_result)

    def test_degree_mixing_dict_directed(self):
        d=mixing.degree_mixing_dict(self.D)
        print d
        d_result={1:{3:2},
                  2:{1:1,3:1},
                  3:{}
                  }
        assert_equal(d,d_result)

    def test_degree_mixing_dict_multigraph(self):
        d=mixing.degree_mixing_dict(self.M)
        d_result={1:{2:1},
                  2:{1:1,3:3},
                  3:{2:3}
                  }
        assert_equal(d,d_result)


class TestDegreeMixingMatrix(object):

    @classmethod
    def setupClass(cls):
        global np
        global npt
        try:
            import numpy as np
            import numpy.testing as npt

        except ImportError:
             raise SkipTest('NumPy not available.')
    
    def setUp(self):
        self.P4=networkx.path_graph(4)
        self.D=networkx.DiGraph() 
        self.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
        self.M=networkx.MultiGraph() 
        self.M.add_path(range(4))
        self.M.add_edge(0,1)
        self.S=networkx.Graph()
        self.S.add_edges_from([(0,0),(1,1)])



    def test_degree_mixing_matrix_undirected(self):
        a_result=np.array([[0,0,0],
                           [0,0,2],
                           [0,2,2]]
                          )
        a=mixing.degree_mixing_matrix(self.P4,normalized=False)
        npt.assert_equal(a,a_result)
        a=mixing.degree_mixing_matrix(self.P4)
        npt.assert_equal(a,a_result/float(a_result.sum()))

    def test_degree_mixing_matrix_directed(self):
        a_result=np.array([[0,0,0,0],
                           [0,0,0,2],
                           [0,1,0,1],
                           [0,0,0,0]]
                          )
        a=mixing.degree_mixing_matrix(self.D,normalized=False)
        npt.assert_equal(a,a_result)
        a=mixing.degree_mixing_matrix(self.D)
        npt.assert_equal(a,a_result/float(a_result.sum()))

    def test_degree_mixing_matrix_multigraph(self):
        a_result=np.array([[0,0,0,0],
                           [0,0,1,0],
                           [0,1,0,3],
                           [0,0,3,0]]
                          )
        a=mixing.degree_mixing_matrix(self.M,normalized=False)
        npt.assert_equal(a,a_result)
        a=mixing.degree_mixing_matrix(self.M)
        npt.assert_equal(a,a_result/float(a_result.sum()))


    def test_degree_mixing_matrix_selfloop(self):
        a_result=np.array([[0,0,0],
                           [0,0,0],
                           [0,0,2]]
                          )
        a=mixing.degree_mixing_matrix(self.S,normalized=False)
        npt.assert_equal(a,a_result)
        a=mixing.degree_mixing_matrix(self.S)
        npt.assert_equal(a,a_result/float(a_result.sum()))


    def test_degree_assortativity_undirected(self):
        r=mixing.degree_assortativity(self.P4)
        npt.assert_almost_equal(r,-1.0/2,decimal=4)

    def test_degree_assortativity_directed(self):
        r=mixing.degree_assortativity(self.D)
        npt.assert_almost_equal(r,-0.57735,decimal=4)

    def test_degree_assortativity_multigraph(self):
        r=mixing.degree_assortativity(self.M)
        npt.assert_almost_equal(r,-1.0/7.0,decimal=4)



class TestDegreeMixingMatrixPearsonr(object):
    @classmethod
    def setupClass(cls):
        global np
        global npt
        try:
            import numpy as np
            import numpy.testing as npt
        except ImportError:
             raise SkipTest('NumPy not available.')
        try:
            import scipy
        except ImportError:
             raise SkipTest('SciPy not available.')

    def setUp(self):
        self.P4=networkx.path_graph(4)
        self.D=networkx.DiGraph() 
        self.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
        self.M=networkx.MultiGraph() 
        self.M.add_path(range(4))
        self.M.add_edge(0,1)
        self.S=networkx.Graph()
        self.S.add_edges_from([(0,0),(1,1)])

    def test_degree_assortativity_undirected(self):
        r=mixing.degree_pearsonr(self.P4)
        npt.assert_almost_equal(r,-1.0/2,decimal=4)

    def test_degree_assortativity_directed(self):
        r=mixing.degree_pearsonr(self.D)
        npt.assert_almost_equal(r,-0.57735,decimal=4)

    def test_degree_assortativity_multigraph(self):
        r=mixing.degree_pearsonr(self.M)
        npt.assert_almost_equal(r,-1.0/7.0,decimal=4)


class TestNeighborhoodConnectivity():
    def setUp(self):
        self.P4=networkx.path_graph(4)
        self.D=networkx.DiGraph() 
        self.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
        self.M=networkx.MultiGraph() 
        self.M.add_path(range(4))
        self.M.add_edge(0,1)
        self.S=networkx.Graph()
        self.S.add_edges_from([(0,0),(1,1)])

    def test_neighbor_connectivity(self):
        d=networkx.neighbor_connectivity(self.P4)
        assert_equal(d,{1:2.0/3,2:1.0})
        d=networkx.neighbor_connectivity(networkx.complete_graph(4))
        assert_equal(d,{3:3.0})
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.