From 16e39f6b773ed35f2c922b3d962f35a5fd277f08 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Wed, 25 Oct 2023 11:47:49 -0400 Subject: [PATCH] Fix #480 (#481) --- docs/source/api/recipes/recipes.ipynb | 9 ++++++--- .../tutorials/In Depth 2 - Drawing hyperedges.ipynb | 10 +++------- tests/core/test_hypergraph.py | 12 ++++++++++++ xgi/core/hypergraph.py | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/source/api/recipes/recipes.ipynb b/docs/source/api/recipes/recipes.ipynb index c6cdebb71..07c3ed512 100644 --- a/docs/source/api/recipes/recipes.ipynb +++ b/docs/source/api/recipes/recipes.ipynb @@ -631,7 +631,7 @@ } ], "source": [ - "edge_color = [colors[i-2] for i in H.edges.filterby(\"order\", 1, \"gt\").size.aslist()]\n", + "edge_color = [colors[i - 2] for i in H.edges.filterby(\"order\", 1, \"gt\").size.aslist()]\n", "\n", "xgi.draw(H, pos=pos, dyad_color=link_color, edge_fc=edge_color);" ] @@ -661,7 +661,7 @@ } ], "source": [ - "color_dict = {idx : colors[i-2] for idx, i in H.edges.size.asdict().items()}\n", + "color_dict = {idx: colors[i - 2] for idx, i in H.edges.size.asdict().items()}\n", "\n", "xgi.draw(H, pos=pos, dyad_color=color_dict, edge_fc=color_dict);" ] @@ -692,9 +692,12 @@ ], "source": [ "from matplotlib.colors import ListedColormap\n", + "\n", "cmap = ListedColormap(colors[1:])\n", "\n", - "_, (node_collection, dyad_collection, edge_collection) = xgi.draw(H, pos=pos, dyad_color=link_color, edge_fc_cmap=cmap)\n", + "_, (node_collection, dyad_collection, edge_collection) = xgi.draw(\n", + " H, pos=pos, dyad_color=link_color, edge_fc_cmap=cmap\n", + ")\n", "\n", "plt.colorbar(edge_collection, label=\"edges\")\n", "\n", diff --git a/docs/source/api/tutorials/In Depth 2 - Drawing hyperedges.ipynb b/docs/source/api/tutorials/In Depth 2 - Drawing hyperedges.ipynb index 1f1ffa59e..c2d143975 100644 --- a/docs/source/api/tutorials/In Depth 2 - Drawing hyperedges.ipynb +++ b/docs/source/api/tutorials/In Depth 2 - Drawing hyperedges.ipynb @@ -447,10 +447,8 @@ ], "source": [ "ax, (dyad_collection, edge_collection) = xgi.draw_hyperedges(\n", - " H,\n", - " pos=pos,\n", - " dyad_color=[3, 1, 4],\n", - " edge_fc=H.edges.size)\n", + " H, pos=pos, dyad_color=[3, 1, 4], edge_fc=H.edges.size\n", + ")\n", "\n", "ax, node_collection = xgi.draw_nodes(H, pos=pos, node_fc=H.nodes.degree, zorder=3)\n", "\n", @@ -458,9 +456,7 @@ "plt.colorbar(dyad_collection, label=\"dyads\")\n", "plt.colorbar(edge_collection, label=\"edges\")\n", "\n", - "plt.tight_layout()\n", - "\n", - " " + "plt.tight_layout()" ] } ], diff --git a/tests/core/test_hypergraph.py b/tests/core/test_hypergraph.py index dcc7f1709..74fbe74ee 100644 --- a/tests/core/test_hypergraph.py +++ b/tests/core/test_hypergraph.py @@ -742,6 +742,18 @@ def test_set_edge_attributes(edgelist1): H3.set_edge_attributes({"test": {2: "weight"}}) +def test_480(edgelist1): + H1 = xgi.Hypergraph(edgelist1) + attr_dict1 = { + 0: 1, + 1: 2, + 2: 3, + 3: -1, + } + H1.set_edge_attributes(attr_dict1, "weight") + assert H1.edges.attrs("weight").asdict() == attr_dict1 + + def test_cleanup(): H = xgi.Hypergraph() H.add_edges_from([["a", "b", "c"], ["a", "b", "c"], ["e", "f"]]) diff --git a/xgi/core/hypergraph.py b/xgi/core/hypergraph.py index 3d1539ba4..d43ed5a68 100644 --- a/xgi/core/hypergraph.py +++ b/xgi/core/hypergraph.py @@ -862,7 +862,7 @@ def set_edge_attributes(self, values, name=None): try: for e, value in values.items(): try: - self._edge_attr[id][name] = value + self._edge_attr[e][name] = value except IDNotFound: warn(f"Edge {e} does not exist!") except AttributeError: