Skip to content

Commit

Permalink
added recipe for maximal indices
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Mar 4, 2024
1 parent faacbfe commit 4165e4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
42 changes: 36 additions & 6 deletions docs/source/api/recipes/recipes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand All @@ -809,7 +839,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.4"
},
"toc": {
"base_numbering": 1,
Expand Down
4 changes: 3 additions & 1 deletion tests/drawing/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 4165e4a

Please sign in to comment.