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

Support DiHypergraph in from_bipartite_graph #633

Merged
merged 7 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
18 changes: 18 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,24 @@ def bipartite_graph4():
return G


@pytest.fixture
def bipartite_dihypergraph1():
G = nx.DiGraph()
G.add_nodes_from([1, 2, 3, 4], bipartite=0)
G.add_nodes_from(["a", "b", "c"], bipartite=1)
G.add_edges_from(
[
("a", 1),
("b", 1, {"direction": "tail"}),
("b", 2),
("c", 2, {"direction": "tail"}),
("c", 3),
("a", 4, {"direction": "tail"}),
]
)
return G


@pytest.fixture
def attr0():
return {"color": "brown", "name": "camel"}
Expand Down
13 changes: 13 additions & 0 deletions tests/convert/test_bipartite_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,16 @@ def test_from_bipartite_graph(
# not bipartite
with pytest.raises(XGIError):
H = xgi.from_bipartite_graph(bipartite_graph4, dual=True)


def test_from_bipartite_graph_to_dihypergraph(bipartite_dihypergraph1):
H = xgi.from_bipartite_graph(bipartite_dihypergraph1)

assert set(H.nodes) == {1, 2, 3, 4}
assert set(H.edges) == {"a", "b", "c"}
assert H.edges.head("a") == {1}
assert H.edges.tail("a") == {4}
assert H.edges.head("b") == {2}
assert H.edges.tail("b") == {1}
assert H.edges.head("c") == {3}
assert H.edges.tail("c") == {2}
33 changes: 27 additions & 6 deletions xgi/convert/bipartite_graph.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Methods for converting to and from bipartite graphs."""

import networkx as nx
from networkx import bipartite

import xgi

from ..exception import XGIError
from ..generators import empty_hypergraph
from ..generators import empty_dihypergraph, empty_hypergraph

__all__ = ["from_bipartite_graph", "to_bipartite_graph"]

Expand Down Expand Up @@ -73,17 +74,37 @@ def from_bipartite_graph(G, create_using=None, dual=False):
else:
raise XGIError("Invalid type specifier")

if not bipartite.is_bipartite_node_set(G, nodes):
if not _is_bipartite(G, nodes, edges):
raise XGIError("The network is not bipartite")

H = empty_hypergraph(create_using)
if G.is_directed():
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
H = empty_dihypergraph(create_using)
else:
H = empty_hypergraph(create_using)

H.add_nodes_from(nodes)
for edge in edges:
nodes_in_edge = list(G.neighbors(edge))
H.add_edge(nodes_in_edge, idx=edge)
for u, v, d in G.edges(edge, data="direction"):
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(H, xgi.DiHypergraph):
if d == "tail":
H.add_node_to_edge(u, v, direction="in")
else:
colltoaction marked this conversation as resolved.
Show resolved Hide resolved
H.add_node_to_edge(u, v, direction="out")
else:
H.add_node_to_edge(u, v)
return H.dual() if dual else H


def _is_bipartite(G, nodes1, nodes2):
"""Assumption is that nodes1.union(nodes2) == G.nodes"""
for i, j in G.edges:
cond1 = i in nodes1
cond2 = j in nodes2
if not cond1 == cond2: # if not both true or both false
return False
return True


def to_bipartite_graph(H, index=False):
"""Create a NetworkX bipartite network from a hypergraph.

Expand Down
Loading