Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of KeyErrors in to_line_graph #558

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion xgi/algorithms/centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def clique_eigenvector_centrality(H, tol=1e-6):
if not is_connected(H):
return {n: np.nan for n in H.nodes}
W, node_dict = clique_motif_matrix(H, index=True)
_, v = eigsh(W.asfptype(), k=1, which="LM", tol=tol)
_, v = eigsh(W.astype(float), k=1, which="LM", tol=tol)

# multiply by the sign to try and enforce positivity
v = np.sign(v[0]) * v / norm(v, 1)
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
Loading