Skip to content

Commit

Permalink
Get rid of KeyErrors in to_line_graph (#558)
Browse files Browse the repository at this point in the history
* Use edge_label_dict.values() as nodes and edge_label_dict.keys() for edge1, edge2 to avoid KeyError

* fix broken test

* Test added and keep duplicates

* test_557 independant

---------

Co-authored-by: Pierre Robiglio <83019028+thomasrobiglio@users.noreply.github.com>
  • Loading branch information
pgberlureau and thomasrobiglio authored Jul 4, 2024
1 parent 04ec9e8 commit bc90a38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 7 additions & 0 deletions tests/convert/test_line_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ def test_to_line_graph(edgelist1, hypergraph1):

assert L.number_of_nodes() == 0
assert L.number_of_edges() == 0

def test_557():
H = xgi.Hypergraph({0: [(6.019592999999995, 47.2350647), (6.019592999999853, 47.2350647), (6.01959299999999, 47.2350647), (6.019592999999993, 47.2350647), (6.0195929999999755, 47.2350647)],
1: [(6.019592999999995, 47.2350647), (6.019592999999853, 47.2350647), (6.01959299999999, 47.2350647), (6.019592999999993, 47.2350647), (6.0195929999999755, 47.2350647)]})
L = xgi.to_line_graph(H)

assert L.number_of_nodes() == 2
assert L.number_of_edges() == 1

def test_abs_weighted_line_graph(edgelist1, hypergraph1, hypergraph2):
H = xgi.Hypergraph(edgelist1)
Expand Down
15 changes: 6 additions & 9 deletions xgi/convert/line_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,25 @@ def to_line_graph(H, s=1, weights=None):
)
LG = nx.Graph()

edge_label_dict = {tuple(edge): index for index, edge in H._edge.items()}
LG.add_nodes_from([(k, {'original_hyperedge':v}) for k,v in H._edge.items()])

LG.add_nodes_from(H.edges)

for edge1, edge2 in combinations(H.edges.members(), 2):
for e1, e2 in combinations(H._edge, 2):
# Check that the intersection size is larger than s
intersection_size = len(edge1.intersection(edge2))
intersection_size = len(H._edge[e1].intersection(H._edge[e2]))
if intersection_size >= s:
if not weights:
# Add unweighted edge
LG.add_edge(
edge_label_dict[tuple(edge1)], edge_label_dict[tuple(edge2)]
e1, e2
)
else:
# Compute the (normalized) weight
weight = intersection_size
if weights == "normalized":
weight /= min([len(edge1), len(edge2)])
weight /= min([len(H._edge[e1]), len(H._edge[e2])])
# Add edge with weight
LG.add_edge(
edge_label_dict[tuple(edge1)],
edge_label_dict[tuple(edge2)],
e1, e2,
weight=weight,
)

Expand Down

0 comments on commit bc90a38

Please sign in to comment.