Repository: networkx/networkx

Issue:
Wrong degree_assortativity_coefficient for directed graphs
### Current Behavior
``degree_assortativity_coefficient`` will fail for most directed graphs except if the set of in- or out-degrees is the same as the set of total-degrees.
This issue was introduced in 2.6 by #4928 ([L78](https://github.com/networkx/networkx/pull/4928/files#diff-76675aa4f0d3a79d394219c8e15ec346b3f5af9f4a733d5ef9e7026421d43bd9R78)).

### Expected Behavior
The mapping should include all relevant in- and out-degrees for directed graphs.

### Steps to Reproduce
```python
G = nx.DiGraph()
G.add_edges_from([(0, 3), (1, 0), (1, 2), (2, 4), (4, 1), (4, 3), (4, 2)])

nx.degree_assortativity_coefficient(G)  # returns NaN
nx.degree_pearson_correlation_coefficient(G)  # returns the correct value 0.14852
```

### Environment
Python version: 3.9
NetworkX version: 2.6+



Discussion:

Resolving patch:
diff --git a/networkx/algorithms/assortativity/correlation.py b/networkx/algorithms/assortativity/correlation.py
--- 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)
 
 


Test patch:
diff --git a/networkx/algorithms/assortativity/tests/base_test.py b/networkx/algorithms/assortativity/tests/base_test.py
--- a/networkx/algorithms/assortativity/tests/base_test.py
+++ b/networkx/algorithms/assortativity/tests/base_test.py
@@ -44,6 +44,8 @@ def setup_class(cls):
         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
--- a/networkx/algorithms/assortativity/tests/test_correlation.py
+++ b/networkx/algorithms/assortativity/tests/test_correlation.py
@@ -22,6 +22,12 @@ def test_degree_assortativity_directed(self):
         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 @@ def test_degree_pearson_assortativity_directed(self):
         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)