summaryrefslogtreecommitdiff
path: root/networkx
diff options
context:
space:
mode:
authorAric Hagberg <aric.hagberg@gmail.com>2011-10-02 10:05:58 -0600
committerAric Hagberg <aric.hagberg@gmail.com>2011-10-02 10:05:58 -0600
commit4274d6966b89917efd983ad88c105447e2efacb1 (patch)
treea2952399f7f62b8442fc763a1f2bd17584824c50 /networkx
parentd2e03fca6b9e8e06cefb0c6925da8946d30c98cb (diff)
downloadnetworkx-4274d6966b89917efd983ad88c105447e2efacb1.tar.gz
Refactor tests for assortativity. Addresses #640 and #639
--HG-- rename : networkx/algorithms/assortativity/tests/test_mixing_attributes.py => networkx/algorithms/assortativity/tests/test_pairs.py
Diffstat (limited to 'networkx')
-rw-r--r--networkx/algorithms/assortativity/correlation.py1
-rw-r--r--networkx/algorithms/assortativity/pairs.py73
-rw-r--r--networkx/algorithms/assortativity/tests/base_test.py50
-rw-r--r--networkx/algorithms/assortativity/tests/test_correlation.py100
-rw-r--r--networkx/algorithms/assortativity/tests/test_mixing.py179
-rw-r--r--networkx/algorithms/assortativity/tests/test_mixing_attributes.py206
-rw-r--r--networkx/algorithms/assortativity/tests/test_mixing_degree.py216
-rw-r--r--networkx/algorithms/assortativity/tests/test_pairs.py98
8 files changed, 483 insertions, 440 deletions
diff --git a/networkx/algorithms/assortativity/correlation.py b/networkx/algorithms/assortativity/correlation.py
index 743327ae..5290d014 100644
--- a/networkx/algorithms/assortativity/correlation.py
+++ b/networkx/algorithms/assortativity/correlation.py
@@ -11,6 +11,7 @@ __author__ = ' '.join(['Aric Hagberg <aric.hagberg@gmail.com>',
__all__ = ['degree_pearsonr',
'degree_assortativity',
'attribute_assortativity',
+ 'attribute_assortativity_coefficient',
'numeric_assortativity']
def degree_assortativity(G, x='out', y='in', weight=None, nodes=None):
diff --git a/networkx/algorithms/assortativity/pairs.py b/networkx/algorithms/assortativity/pairs.py
index 4fcb0707..0ed0fa9c 100644
--- a/networkx/algorithms/assortativity/pairs.py
+++ b/networkx/algorithms/assortativity/pairs.py
@@ -6,35 +6,62 @@ __author__ = ' '.join(['Aric Hagberg <aric.hagberg@gmail.com>'])
__all__ = ['node_attribute_xy',
'node_degree_xy']
-def node_attribute_xy(G,attribute,nodes=None):
- """Return iterator of node attribute pairs for all edges in G.
+def node_attribute_xy(G, attribute, nodes=None):
+ """Return iterator of node-attribute pairs for all edges in G.
- For undirected graphs each edge is produced twice, once for each
- representation u-v and v-u, with the exception of self loop edges
- that only appear once.
+ Parameters
+ ----------
+ G: NetworkX graph
+
+ attribute: key
+ The node attribute key.
+
+ nodes: list or iterable (optional)
+ Use only edges that are adjacency to specified nodes.
+ The default is all nodes.
+
+ Returns
+ -------
+ (x,y): 2-tuple
+ Generates 2-tuple of (attribute,attribute) values.
+
+ Examples
+ --------
+ >>> G = nx.DiGraph()
+ >>> G.add_node(1,color='red')
+ >>> G.add_node(2,color='blue')
+ >>> G.add_edge(1,2)
+ >>> list(nx.node_attribute_xy(G,'color'))
+ [('red', 'blue')]
+
+ Notes
+ -----
+ For undirected graphs each edge is produced twice, once for each edge
+ representation (u,v) and (v,u), with the exception of self-loop edges
+ which only appear once.
"""
if nodes is None:
- node_set = G
+ nodes = set(G)
else:
- node_set = G.subgraph(nodes)
- node=G.node
+ nodes = set(nodes)
+ node = G.node
for u,nbrsdict in G.adjacency_iter():
- if u not in node_set:
+ if u not in nodes:
continue
- uattr=node[u].get(attribute,None)
+ uattr = node[u].get(attribute,None)
if G.is_multigraph():
for v,keys in nbrsdict.items():
- vattr=node[v].get(attribute,None)
+ vattr = node[v].get(attribute,None)
for k,d in keys.items():
yield (uattr,vattr)
else:
for v,eattr in nbrsdict.items():
- vattr=node[v].get(attribute,None)
+ vattr = node[v].get(attribute,None)
yield (uattr,vattr)
def node_degree_xy(G, x='out', y='in', weight=None, nodes=None):
- """Generate degree-degree pairs for edges in G.
+ """Generate node degree-degree pairs for edges in G.
Parameters
----------
@@ -52,19 +79,29 @@ def node_degree_xy(G, x='out', y='in', weight=None, nodes=None):
The degree is the sum of the edge weights adjacent to the node.
nodes: list or iterable (optional)
- Use only edges that start or end in nodes in this container.
+ Use only edges that are adjacency to specified nodes.
The default is all nodes.
Returns
-------
(x,y): 2-tuple
- Generates 2-tuple of degree-degree values.
+ Generates 2-tuple of (degree,degree) values.
+
+
+ Examples
+ --------
+ >>> G = nx.DiGraph()
+ >>> G.add_edge(1,2)
+ >>> list(nx.node_degree_xy(G,x='out',y='in'))
+ [(1, 1)]
+ >>> list(nx.node_degree_xy(G,x='in',y='out'))
+ [(0, 0)]
Notes
-----
- For undirected graphs each edge is produced twice, once for each
- representation u-v and v-u, with the exception of self loop edges
- that only appear once.
+ For undirected graphs each edge is produced twice, once for each edge
+ representation (u,v) and (v,u), with the exception of self-loop edges
+ which only appear once.
"""
if nodes is None:
nodes = set(G)
diff --git a/networkx/algorithms/assortativity/tests/base_test.py b/networkx/algorithms/assortativity/tests/base_test.py
new file mode 100644
index 00000000..2e165441
--- /dev/null
+++ b/networkx/algorithms/assortativity/tests/base_test.py
@@ -0,0 +1,50 @@
+import networkx as nx
+
+class BaseTestAttributeMixing(object):
+
+ def setUp(self):
+ G=nx.Graph()
+ G.add_nodes_from([0,1],fish='one')
+ G.add_nodes_from([2,3],fish='two')
+ G.add_nodes_from([4],fish='red')
+ G.add_nodes_from([5],fish='blue')
+ G.add_edges_from([(0,1),(2,3),(0,4),(2,5)])
+ self.G=G
+
+ D=nx.DiGraph()
+ D.add_nodes_from([0,1],fish='one')
+ D.add_nodes_from([2,3],fish='two')
+ D.add_nodes_from([4],fish='red')
+ D.add_nodes_from([5],fish='blue')
+ D.add_edges_from([(0,1),(2,3),(0,4),(2,5)])
+ self.D=D
+
+ M=nx.MultiGraph()
+ M.add_nodes_from([0,1],fish='one')
+ M.add_nodes_from([2,3],fish='two')
+ M.add_nodes_from([4],fish='red')
+ M.add_nodes_from([5],fish='blue')
+ M.add_edges_from([(0,1),(0,1),(2,3)])
+ self.M=M
+
+ S=nx.Graph()
+ S.add_nodes_from([0,1],fish='one')
+ S.add_nodes_from([2,3],fish='two')
+ S.add_nodes_from([4],fish='red')
+ S.add_nodes_from([5],fish='blue')
+ S.add_edge(0,0)
+ S.add_edge(2,2)
+ self.S=S
+
+class BaseTestDegreeMixing(object):
+
+ def setUp(self):
+ self.P4=nx.path_graph(4)
+ self.D=nx.DiGraph()
+ self.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
+ self.M=nx.MultiGraph()
+ self.M.add_path(list(range(4)))
+ self.M.add_edge(0,1)
+ self.S=nx.Graph()
+ self.S.add_edges_from([(0,0),(1,1)])
+
diff --git a/networkx/algorithms/assortativity/tests/test_correlation.py b/networkx/algorithms/assortativity/tests/test_correlation.py
new file mode 100644
index 00000000..fc61cac9
--- /dev/null
+++ b/networkx/algorithms/assortativity/tests/test_correlation.py
@@ -0,0 +1,100 @@
+#!/usr/bin/env python
+from nose.tools import *
+from nose import SkipTest
+import networkx as nx
+from base_test import BaseTestAttributeMixing,BaseTestDegreeMixing
+
+
+class TestDegreeMixingCorrelation(BaseTestDegreeMixing):
+ @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
+ import scipy.stats
+ except ImportError:
+ raise SkipTest('SciPy not available.')
+
+
+
+ def test_degree_assortativity_undirected(self):
+ r=nx.degree_assortativity(self.P4)
+ npt.assert_almost_equal(r,-1.0/2,decimal=4)
+
+ def test_degree_assortativity_directed(self):
+ r=nx.degree_assortativity(self.D)
+ npt.assert_almost_equal(r,-0.57735,decimal=4)
+
+ def test_degree_assortativity_multigraph(self):
+ r=nx.degree_assortativity(self.M)
+ npt.assert_almost_equal(r,-1.0/7.0,decimal=4)
+
+
+ def test_degree_assortativity_undirected(self):
+ r=nx.degree_pearsonr(self.P4)
+ npt.assert_almost_equal(r,-1.0/2,decimal=4)
+
+ def test_degree_assortativity_directed(self):
+ r=nx.degree_pearsonr(self.D)
+ npt.assert_almost_equal(r,-0.57735,decimal=4)
+
+ def test_degree_assortativity_multigraph(self):
+ r=nx.degree_pearsonr(self.M)
+ npt.assert_almost_equal(r,-1.0/7.0,decimal=4)
+
+
+
+class TestAttributeMixingCorrelation(BaseTestAttributeMixing):
+ @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 test_attribute_assortativity_undirected(self):
+ r=nx.attribute_assortativity(self.G,'fish')
+ assert_equal(r,6.0/22.0)
+
+ def test_attribute_assortativity_directed(self):
+ r=nx.attribute_assortativity(self.D,'fish')
+ assert_equal(r,1.0/3.0)
+
+ def test_attribute_assortativity_multigraph(self):
+ r=nx.attribute_assortativity(self.M,'fish')
+ assert_equal(r,1.0)
+
+ def test_attribute_assortativity_coefficient(self):
+ # from "Mixing patterns in networks"
+ a=np.array([[0.258,0.016,0.035,0.013],
+ [0.012,0.157,0.058,0.019],
+ [0.013,0.023,0.306,0.035],
+ [0.005,0.007,0.024,0.016]])
+ r=nx.attribute_assortativity_coefficient(a)
+ npt.assert_almost_equal(r,0.623,decimal=3)
+
+ def test_attribute_assortativity_coefficient(self):
+ a=np.array([[0.18,0.02,0.01,0.03],
+ [0.02,0.20,0.03,0.02],
+ [0.01,0.03,0.16,0.01],
+ [0.03,0.02,0.01,0.22]])
+
+ r=nx.attribute_assortativity_coefficient(a)
+ npt.assert_almost_equal(r,0.68,decmial=2)
+
+ def test_attribute_assortativity_coefficient(self):
+ a=np.array([[50,50,0],[50,50,0],[0,0,2]])
+ r=nx.attribute_assortativity_coefficient(a)
+ npt.assert_almost_equal(r,0.029,decimal=3)
+
diff --git a/networkx/algorithms/assortativity/tests/test_mixing.py b/networkx/algorithms/assortativity/tests/test_mixing.py
new file mode 100644
index 00000000..131969df
--- /dev/null
+++ b/networkx/algorithms/assortativity/tests/test_mixing.py
@@ -0,0 +1,179 @@
+#!/usr/bin/env python
+from nose.tools import *
+from nose import SkipTest
+import networkx as nx
+from base_test import BaseTestAttributeMixing,BaseTestDegreeMixing
+
+
+class TestDegreeMixingDict(BaseTestDegreeMixing):
+
+
+ def test_degree_mixing_dict_undirected(self):
+ d=nx.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=nx.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=nx.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(BaseTestDegreeMixing):
+
+ @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 test_degree_mixing_matrix_undirected(self):
+ a_result=np.array([[0,0,0],
+ [0,0,2],
+ [0,2,2]]
+ )
+ a=nx.degree_mixing_matrix(self.P4,normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.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=nx.degree_mixing_matrix(self.D,normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.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=nx.degree_mixing_matrix(self.M,normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.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=nx.degree_mixing_matrix(self.S,normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.degree_mixing_matrix(self.S)
+ npt.assert_equal(a,a_result/float(a_result.sum()))
+
+
+class TestAttributeMixingDict(BaseTestAttributeMixing):
+
+ def test_attribute_mixing_dict_undirected(self):
+ d=nx.attribute_mixing_dict(self.G,'fish')
+ d_result={'one':{'one':2,'red':1},
+ 'two':{'two':2,'blue':1},
+ 'red':{'one':1},
+ 'blue':{'two':1}
+ }
+ assert_equal(d,d_result)
+
+ def test_attribute_mixing_dict_directed(self):
+ d=nx.attribute_mixing_dict(self.D,'fish')
+ d_result={'one':{'one':1,'red':1},
+ 'two':{'two':1,'blue':1},
+ 'red':{},
+ 'blue':{}
+ }
+ assert_equal(d,d_result)
+
+
+ def test_attribute_mixing_dict_multigraph(self):
+ d=nx.attribute_mixing_dict(self.M,'fish')
+ d_result={'one':{'one':4},
+ 'two':{'two':2},
+ }
+ assert_equal(d,d_result)
+
+
+
+class TestAttributeMixingMatrix(BaseTestAttributeMixing):
+ @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 test_attribute_mixing_matrix_undirected(self):
+ mapping={'one':0,'two':1,'red':2,'blue':3}
+ a_result=np.array([[2,0,1,0],
+ [0,2,0,1],
+ [1,0,0,0],
+ [0,1,0,0]]
+ )
+ a=nx.attribute_mixing_matrix(self.G,'fish',
+ mapping=mapping,
+ normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.attribute_mixing_matrix(self.G,'fish',
+ mapping=mapping)
+ npt.assert_equal(a,a_result/float(a_result.sum()))
+
+ def test_attribute_mixing_matrix_directed(self):
+ mapping={'one':0,'two':1,'red':2,'blue':3}
+ a_result=np.array([[1,0,1,0],
+ [0,1,0,1],
+ [0,0,0,0],
+ [0,0,0,0]]
+ )
+ a=nx.attribute_mixing_matrix(self.D,'fish',
+ mapping=mapping,
+ normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.attribute_mixing_matrix(self.D,'fish',
+ mapping=mapping)
+ npt.assert_equal(a,a_result/float(a_result.sum()))
+
+ def test_attribute_mixing_matrix_multigraph(self):
+ mapping={'one':0,'two':1,'red':2,'blue':3}
+ a_result=np.array([[4,0,0,0],
+ [0,2,0,0],
+ [0,0,0,0],
+ [0,0,0,0]]
+ )
+ a=nx.attribute_mixing_matrix(self.M,'fish',
+ mapping=mapping,
+ normalized=False)
+ npt.assert_equal(a,a_result)
+ a=nx.attribute_mixing_matrix(self.M,'fish',
+ mapping=mapping)
+ npt.assert_equal(a,a_result/float(a_result.sum()))
+
diff --git a/networkx/algorithms/assortativity/tests/test_mixing_attributes.py b/networkx/algorithms/assortativity/tests/test_mixing_attributes.py
deleted file mode 100644
index 7f7c3051..00000000
--- a/networkx/algorithms/assortativity/tests/test_mixing_attributes.py
+++ /dev/null
@@ -1,206 +0,0 @@
-#!/usr/bin/env python
-from nose.tools import *
-from nose import SkipTest
-
-import networkx
-import networkx.algorithms.mixing as mixing
-
-
-class TestAttributeMixing(object):
-
- def setUp(self):
- G=networkx.Graph()
- G.add_nodes_from([0,1],fish='one')
- G.add_nodes_from([2,3],fish='two')
- G.add_nodes_from([4],fish='red')
- G.add_nodes_from([5],fish='blue')
- G.add_edges_from([(0,1),(2,3),(0,4),(2,5)])
- self.G=G
-
- D=networkx.DiGraph()
- D.add_nodes_from([0,1],fish='one')
- D.add_nodes_from([2,3],fish='two')
- D.add_nodes_from([4],fish='red')
- D.add_nodes_from([5],fish='blue')
- D.add_edges_from([(0,1),(2,3),(0,4),(2,5)])
- self.D=D
-
- M=networkx.MultiGraph()
- M.add_nodes_from([0,1],fish='one')
- M.add_nodes_from([2,3],fish='two')
- M.add_nodes_from([4],fish='red')
- M.add_nodes_from([5],fish='blue')
- M.add_edges_from([(0,1),(0,1),(2,3)])
- self.M=M
-
- S=networkx.Graph()
- S.add_nodes_from([0,1],fish='one')
- S.add_nodes_from([2,3],fish='two')
- S.add_nodes_from([4],fish='red')
- S.add_nodes_from([5],fish='blue')
- S.add_edge(0,0)
- S.add_edge(2,2)
- self.S=S
-
- def test_node_attribute_xy_undirected(self):
- attrxy=sorted(mixing.node_attribute_xy(self.G,'fish'))
- attrxy_result=sorted([('one','one'),
- ('one','one'),
- ('two','two'),
- ('two','two'),
- ('one','red'),
- ('red','one'),
- ('blue','two'),
- ('two','blue')
- ])
- assert_equal(attrxy,attrxy_result)
-
- def test_node_attribute_xy_directed(self):
- attrxy=sorted(mixing.node_attribute_xy(self.D,'fish'))
- attrxy_result=sorted([('one','one'),
- ('two','two'),
- ('one','red'),
- ('two','blue')
- ])
- assert_equal(attrxy,attrxy_result)
-
- def test_node_attribute_xy_multigraph(self):
- attrxy=sorted(mixing.node_attribute_xy(self.M,'fish'))
- attrxy_result=[('one','one'),
- ('one','one'),
- ('one','one'),
- ('one','one'),
- ('two','two'),
- ('two','two')
- ]
- assert_equal(attrxy,attrxy_result)
-
- def test_node_attribute_xy_selfloop(self):
- attrxy=sorted(mixing.node_attribute_xy(self.S,'fish'))
- attrxy_result=[('one','one'),
- ('two','two')
- ]
- assert_equal(attrxy,attrxy_result)
-
- def test_attribute_mixing_dict_undirected(self):
- d=mixing.attribute_mixing_dict(self.G,'fish')
- d_result={'one':{'one':2,'red':1},
- 'two':{'two':2,'blue':1},
- 'red':{'one':1},
- 'blue':{'two':1}
- }
- assert_equal(d,d_result)
-
- def test_attribute_mixing_dict_directed(self):
- d=mixing.attribute_mixing_dict(self.D,'fish')
- d_result={'one':{'one':1,'red':1},
- 'two':{'two':1,'blue':1},
- 'red':{},
- 'blue':{}
- }
- assert_equal(d,d_result)
-
-
- def test_attribute_mixing_dict_multigraph(self):
- d=mixing.attribute_mixing_dict(self.M,'fish')
- d_result={'one':{'one':4},
- 'two':{'two':2},
- }
- assert_equal(d,d_result)
-
-
-
-class TestAttributeMixingMatrix(TestAttributeMixing):
- @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 test_attribute_mixing_matrix_undirected(self):
- mapping={'one':0,'two':1,'red':2,'blue':3}
- a_result=np.array([[2,0,1,0],
- [0,2,0,1],
- [1,0,0,0],
- [0,1,0,0]]
- )
- a=mixing.attribute_mixing_matrix(self.G,'fish',
- mapping=mapping,
- normalized=False)
- npt.assert_equal(a,a_result)
- a=mixing.attribute_mixing_matrix(self.G,'fish',
- mapping=mapping)
- npt.assert_equal(a,a_result/float(a_result.sum()))
-
- def test_attribute_mixing_matrix_directed(self):
- mapping={'one':0,'two':1,'red':2,'blue':3}
- a_result=np.array([[1,0,1,0],
- [0,1,0,1],
- [0,0,0,0],
- [0,0,0,0]]
- )
- a=mixing.attribute_mixing_matrix(self.D,'fish',
- mapping=mapping,
- normalized=False)
- npt.assert_equal(a,a_result)
- a=mixing.attribute_mixing_matrix(self.D,'fish',
- mapping=mapping)
- npt.assert_equal(a,a_result/float(a_result.sum()))
-
- def test_attribute_mixing_matrix_multigraph(self):
- mapping={'one':0,'two':1,'red':2,'blue':3}
- a_result=np.array([[4,0,0,0],
- [0,2,0,0],
- [0,0,0,0],
- [0,0,0,0]]
- )
- a=mixing.attribute_mixing_matrix(self.M,'fish',
- mapping=mapping,
- normalized=False)
- npt.assert_equal(a,a_result)
- a=mixing.attribute_mixing_matrix(self.M,'fish',
- mapping=mapping)
- npt.assert_equal(a,a_result/float(a_result.sum()))
-
-
- def test_attribute_assortativity_undirected(self):
- r=mixing.attribute_assortativity(self.G,'fish')
- assert_equal(r,6.0/22.0)
-
- def test_attribute_assortativity_directed(self):
- r=mixing.attribute_assortativity(self.D,'fish')
- assert_equal(r,1.0/3.0)
-
- def test_attribute_assortativity_multigraph(self):
- r=mixing.attribute_assortativity(self.M,'fish')
- assert_equal(r,1.0)
-
- def test_attribute_assortativity_coefficient(self):
- # from "Mixing patterns in networks"
- a=np.array([[0.258,0.016,0.035,0.013],
- [0.012,0.157,0.058,0.019],
- [0.013,0.023,0.306,0.035],
- [0.005,0.007,0.024,0.016]])
- r=mixing.attribute_assortativity_coefficient(a)
- npt.assert_almost_equal(r,0.623,decimal=3)
-
- def test_attribute_assortativity_coefficient(self):
- a=np.array([[0.18,0.02,0.01,0.03],
- [0.02,0.20,0.03,0.02],
- [0.01,0.03,0.16,0.01],
- [0.03,0.02,0.01,0.22]])
-
- r=mixing.attribute_assortativity_coefficient(a)
- npt.assert_almost_equal(r,0.68,decmial=2)
-
- def test_attribute_assortativity_coefficient(self):
- a=np.array([[50,50,0],[50,50,0],[0,0,2]])
- r=mixing.attribute_assortativity_coefficient(a)
- npt.assert_almost_equal(r,0.029,decimal=3)
-
-
diff --git a/networkx/algorithms/assortativity/tests/test_mixing_degree.py b/networkx/algorithms/assortativity/tests/test_mixing_degree.py
deleted file mode 100644
index e37e3c88..00000000
--- a/networkx/algorithms/assortativity/tests/test_mixing_degree.py
+++ /dev/null
@@ -1,216 +0,0 @@
-#!/usr/bin/env python
-from nose.tools import *
-from nose import SkipTest
-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(list(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_node_degree_xy_weighted(self):
- G = networkx.Graph()
- G.add_edge(1,2,weight=7)
- G.add_edge(2,3,weight=10)
- xy=sorted(mixing.node_degree_xy(G,weight='weight'))
- xy_result=sorted([(7,17),
- (17,10),
- (17,7),
- (10,17)])
- 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(list(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
- import scipy.stats
- 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(list(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)
-
-
diff --git a/networkx/algorithms/assortativity/tests/test_pairs.py b/networkx/algorithms/assortativity/tests/test_pairs.py
new file mode 100644
index 00000000..53bba587
--- /dev/null
+++ b/networkx/algorithms/assortativity/tests/test_pairs.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+from nose.tools import *
+import networkx as nx
+from base_test import BaseTestAttributeMixing,BaseTestDegreeMixing
+
+class TestAttributeMixingXY(BaseTestAttributeMixing):
+
+ def test_node_attribute_xy_undirected(self):
+ attrxy=sorted(nx.node_attribute_xy(self.G,'fish'))
+ attrxy_result=sorted([('one','one'),
+ ('one','one'),
+ ('two','two'),
+ ('two','two'),
+ ('one','red'),
+ ('red','one'),
+ ('blue','two'),
+ ('two','blue')
+ ])
+ assert_equal(attrxy,attrxy_result)
+
+ def test_node_attribute_xy_directed(self):
+ attrxy=sorted(nx.node_attribute_xy(self.D,'fish'))
+ attrxy_result=sorted([('one','one'),
+ ('two','two'),
+ ('one','red'),
+ ('two','blue')
+ ])
+ assert_equal(attrxy,attrxy_result)
+
+ def test_node_attribute_xy_multigraph(self):
+ attrxy=sorted(nx.node_attribute_xy(self.M,'fish'))
+ attrxy_result=[('one','one'),
+ ('one','one'),
+ ('one','one'),
+ ('one','one'),
+ ('two','two'),
+ ('two','two')
+ ]
+ assert_equal(attrxy,attrxy_result)
+
+ def test_node_attribute_xy_selfloop(self):
+ attrxy=sorted(nx.node_attribute_xy(self.S,'fish'))
+ attrxy_result=[('one','one'),
+ ('two','two')
+ ]
+ assert_equal(attrxy,attrxy_result)
+
+
+class TestDegreeMixingXY(BaseTestDegreeMixing):
+
+ def test_node_degree_xy_undirected(self):
+ xy=sorted(nx.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(nx.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(nx.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(nx.node_degree_xy(self.S))
+ xy_result=sorted([(2,2),
+ (2,2)])
+ assert_equal(xy,xy_result)
+
+ def test_node_degree_xy_weighted(self):
+ G = nx.Graph()
+ G.add_edge(1,2,weight=7)
+ G.add_edge(2,3,weight=10)
+ xy=sorted(nx.node_degree_xy(G,weight='weight'))
+ xy_result=sorted([(7,17),
+ (17,10),
+ (17,7),
+ (10,17)])
+ assert_equal(xy,xy_result)
+