From 4165e4acba4af9c53f928bd350d1cbe8137a6e78 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Mon, 4 Mar 2024 11:37:23 -0600 Subject: [PATCH] added recipe for maximal indices --- docs/source/api/recipes/recipes.ipynb | 42 +++++++++++++++++++++++---- tests/drawing/test_draw.py | 4 ++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/docs/source/api/recipes/recipes.ipynb b/docs/source/api/recipes/recipes.ipynb index 027e32424..82518e68a 100644 --- a/docs/source/api/recipes/recipes.ipynb +++ b/docs/source/api/recipes/recipes.ipynb @@ -732,8 +732,8 @@ "m, n = 10, 20\n", "p = 0.5\n", "\n", - "G = nx.triangular_lattice_graph(m,n, with_positions=True)\n", - "pos = nx.get_node_attributes(G, 'pos')\n", + "G = nx.triangular_lattice_graph(m, n, with_positions=True)\n", + "pos = nx.get_node_attributes(G, \"pos\")\n", "\n", "mapping = {i: list(G.nodes)[i] for i in range(0, len(list(G.nodes)))}\n", "inv_mapping = {v: k for k, v in mapping.items()}\n", @@ -783,14 +783,44 @@ " lens += tup[1].values()\n", "\n", "# remove lengths 0 for self-loops\n", - "lens = [i for i in lens if i!=0] \n", + "lens = [i for i in lens if i != 0]\n", "\n", "# replace inf by 0 for disconnected nodes\n", - "lens = [0 if i==np.inf else i for i in lens] \n", + "lens = [0 if i == np.inf else i for i in lens]\n", "\n", - "avg_shortest_path = np.sum(lens) / (N * (N-1))\n", + "avg_shortest_path = np.sum(lens) / (N * (N - 1))\n", "print(\"The average shortest path length is\", avg_shortest_path)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 16. Get all of the node IDs that have maximum degree\n", + "This recipe demonstrates how to get all of the indices corresponding to the maximum degree, because `argmax` only returns the first index corresponding to the maximal value." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "H = xgi.Hypergraph([[1, 2, 3, 4], [1, 2, 3], [1, 2]])\n", + "\n", + "[k for k, v in H.degree().items() if v == H.nodes.degree.max()]" + ] } ], "metadata": { @@ -809,7 +839,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.11.4" }, "toc": { "base_numbering": 1, diff --git a/tests/drawing/test_draw.py b/tests/drawing/test_draw.py index 8900b0093..ee340ed79 100644 --- a/tests/drawing/test_draw.py +++ b/tests/drawing/test_draw.py @@ -650,6 +650,8 @@ def test_issue_515(edgelist8): with warnings.catch_warnings(): warnings.simplefilter("error") - ax, (node_coll, edge_coll) = xgi.draw_multilayer(H, node_fc=["black"] * H.num_nodes) + ax, (node_coll, edge_coll) = xgi.draw_multilayer( + H, node_fc=["black"] * H.num_nodes + ) plt.close("all")