summaryrefslogtreecommitdiff
path: root/networkx
diff options
context:
space:
mode:
authortfardet <79037344+tfardet@users.noreply.github.com>2021-08-04 13:50:31 +0200
committerGitHub <noreply@github.com>2021-08-04 07:50:31 -0400
commit16ca69b1bdb50ff854922a9ed38265d75fce74cd (patch)
tree62a53fb05ffeee4dbe0aba03a8d095bb2bec4c7e /networkx
parentd475ffe7b24633934b5e566974fe7e72a9400cf4 (diff)
downloadnetworkx-16ca69b1bdb50ff854922a9ed38265d75fce74cd.tar.gz
Fix degree_assortativity_coefficient for directed graphs (#4999)
* Fix degree_assortativity_coefficient for directed graphs * added test * addressed review comments
Diffstat (limited to 'networkx')
-rw-r--r--networkx/algorithms/assortativity/correlation.py20
-rw-r--r--networkx/algorithms/assortativity/tests/base_test.py2
-rw-r--r--networkx/algorithms/assortativity/tests/test_correlation.py12
3 files changed, 33 insertions, 1 deletions
diff --git a/networkx/algorithms/assortativity/correlation.py b/networkx/algorithms/assortativity/correlation.py
index 8a244fc7..529311f7 100644
--- a/networkx/algorithms/assortativity/correlation.py
+++ b/networkx/algorithms/assortativity/correlation.py
@@ -75,9 +75,27 @@ def degree_assortativity_coefficient(G, x="out", y="in", weight=None, nodes=None
"""
if nodes is None:
nodes = G.nodes
- degrees = set([d for n, d in G.degree(nodes, weight=weight)])
+
+ degrees = None
+
+ if G.is_directed():
+ indeg = (
+ set([d for _, d in G.in_degree(nodes, weight=weight)])
+ if "in" in (x, y)
+ else set()
+ )
+ outdeg = (
+ set([d for _, d in G.out_degree(nodes, weight=weight)])
+ if "out" in (x, y)
+ else set()
+ )
+ degrees = set.union(indeg, outdeg)
+ else:
+ degrees = set([d for _, d in G.degree(nodes, weight=weight)])
+
mapping = {d: i for i, d, in enumerate(degrees)}
M = degree_mixing_matrix(G, x=x, y=y, nodes=nodes, weight=weight, mapping=mapping)
+
return numeric_ac(M, mapping=mapping)
diff --git a/networkx/algorithms/assortativity/tests/base_test.py b/networkx/algorithms/assortativity/tests/base_test.py
index ee5b126c..73bb32d2 100644
--- a/networkx/algorithms/assortativity/tests/base_test.py
+++ b/networkx/algorithms/assortativity/tests/base_test.py
@@ -44,6 +44,8 @@ class BaseTestDegreeMixing:
cls.P4 = nx.path_graph(4)
cls.D = nx.DiGraph()
cls.D.add_edges_from([(0, 2), (0, 3), (1, 3), (2, 3)])
+ cls.D2 = nx.DiGraph()
+ cls.D2.add_edges_from([(0, 3), (1, 0), (1, 2), (2, 4), (4, 1), (4, 3), (4, 2)])
cls.M = nx.MultiGraph()
nx.add_path(cls.M, range(4))
cls.M.add_edge(0, 1)
diff --git a/networkx/algorithms/assortativity/tests/test_correlation.py b/networkx/algorithms/assortativity/tests/test_correlation.py
index 8da72652..f1917524 100644
--- a/networkx/algorithms/assortativity/tests/test_correlation.py
+++ b/networkx/algorithms/assortativity/tests/test_correlation.py
@@ -22,6 +22,12 @@ class TestDegreeMixingCorrelation(BaseTestDegreeMixing):
r = nx.degree_assortativity_coefficient(self.D)
np.testing.assert_almost_equal(r, -0.57735, decimal=4)
+ def test_degree_assortativity_directed2(self):
+ """Test degree assortativity for a directed graph where the set of
+ in/out degree does not equal the total degree."""
+ r = nx.degree_assortativity_coefficient(self.D2)
+ np.testing.assert_almost_equal(r, 0.14852, decimal=4)
+
def test_degree_assortativity_multigraph(self):
r = nx.degree_assortativity_coefficient(self.M)
np.testing.assert_almost_equal(r, -1.0 / 7.0, decimal=4)
@@ -34,6 +40,12 @@ class TestDegreeMixingCorrelation(BaseTestDegreeMixing):
r = nx.degree_pearson_correlation_coefficient(self.D)
np.testing.assert_almost_equal(r, -0.57735, decimal=4)
+ def test_degree_pearson_assortativity_directed2(self):
+ """Test degree assortativity with Pearson for a directed graph where
+ the set of in/out degree does not equal the total degree."""
+ r = nx.degree_pearson_correlation_coefficient(self.D2)
+ np.testing.assert_almost_equal(r, 0.14852, decimal=4)
+
def test_degree_pearson_assortativity_multigraph(self):
r = nx.degree_pearson_correlation_coefficient(self.M)
np.testing.assert_almost_equal(r, -1.0 / 7.0, decimal=4)